From 6312627218b9eb48d5dc0c719b860cc9862aa7fd Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Sat, 19 Jun 2021 22:15:00 +0100 Subject: [PATCH] LibJS: Implement the GetFunctionRealm() abstract operation --- .../LibJS/Runtime/AbstractOperations.cpp | 28 +++++++++++++++++++ .../LibJS/Runtime/AbstractOperations.h | 1 + 2 files changed, 29 insertions(+) diff --git a/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp b/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp index 9e58f28a17..f9a483f6e6 100644 --- a/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp +++ b/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp @@ -13,6 +13,7 @@ #include #include #include +#include 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(function)) { + auto& bound_function = static_cast(function); + auto& target = bound_function.target_function(); + return get_function_realm(global_object, target); + } + if (is(function)) { + auto& proxy = static_cast(function); + if (proxy.is_revoked()) { + vm.throw_exception(global_object, ErrorType::ProxyRevoked); + return nullptr; + } + auto& proxy_target = proxy.target(); + VERIFY(proxy_target.is_function()); + return get_function_realm(global_object, static_cast(proxy_target)); + } + // 5. Return the current Realm Record. + return &global_object; +} + } diff --git a/Userland/Libraries/LibJS/Runtime/AbstractOperations.h b/Userland/Libraries/LibJS/Runtime/AbstractOperations.h index df9ecf40df..ede34dea1f 100644 --- a/Userland/Libraries/LibJS/Runtime/AbstractOperations.h +++ b/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(Value)> = {}); Function* species_constructor(GlobalObject&, Object const&, Function& default_constructor); +GlobalObject* get_function_realm(GlobalObject&, Function const&); }