Explorar o código

LibJS: Implement the GetFunctionRealm() abstract operation

Linus Groh %!s(int64=4) %!d(string=hai) anos
pai
achega
6312627218

+ 28 - 0
Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp

@@ -13,6 +13,7 @@
 #include <LibJS/Runtime/GlobalObject.h>
 #include <LibJS/Runtime/Object.h>
 #include <LibJS/Runtime/PropertyName.h>
+#include <LibJS/Runtime/ProxyObject.h>
 
 namespace JS {
 
@@ -112,4 +113,31 @@ Function* species_constructor(GlobalObject& global_object, Object const& object,
     return nullptr;
 }
 
+// 7.3.24 GetFunctionRealm ( obj ), https://tc39.es/ecma262/#sec-getfunctionrealm
+GlobalObject* get_function_realm(GlobalObject& global_object, Function const& function)
+{
+    auto& vm = global_object.vm();
+
+    // FIXME: not sure how to do this currently.
+    // 2. If obj has a [[Realm]] internal slot, then
+    //     a. Return obj.[[Realm]].
+    if (is<BoundFunction>(function)) {
+        auto& bound_function = static_cast<BoundFunction const&>(function);
+        auto& target = bound_function.target_function();
+        return get_function_realm(global_object, target);
+    }
+    if (is<ProxyObject>(function)) {
+        auto& proxy = static_cast<ProxyObject const&>(function);
+        if (proxy.is_revoked()) {
+            vm.throw_exception<TypeError>(global_object, ErrorType::ProxyRevoked);
+            return nullptr;
+        }
+        auto& proxy_target = proxy.target();
+        VERIFY(proxy_target.is_function());
+        return get_function_realm(global_object, static_cast<Function const&>(proxy_target));
+    }
+    // 5. Return the current Realm Record.
+    return &global_object;
+}
+
 }

+ 1 - 0
Userland/Libraries/LibJS/Runtime/AbstractOperations.h

@@ -17,5 +17,6 @@ Function* get_method(GlobalObject& global_object, Value, PropertyName const&);
 size_t length_of_array_like(GlobalObject&, Object const&);
 MarkedValueList create_list_from_array_like(GlobalObject&, Value, AK::Function<Result<void, ErrorType>(Value)> = {});
 Function* species_constructor(GlobalObject&, Object const&, Function& default_constructor);
+GlobalObject* get_function_realm(GlobalObject&, Function const&);
 
 }