summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorIdan Horowitz <idan.horowitz@gmail.com>2021-06-30 17:54:47 +0300
committerLinus Groh <mail@linusgroh.de>2021-06-30 18:41:32 +0100
commit5606332ed72e60fa720cf17ad055cca8d2d36b05 (patch)
treeadcc4254d415b34ea43b983df38a37821dd590f8 /Userland
parent38b8fa8f3e2f0ff0174b9cb9bd7345b74b155931 (diff)
downloadserenity-5606332ed72e60fa720cf17ad055cca8d2d36b05.zip
LibJS: Add a [[Realm]] getter to FunctionObject and use it where needed
Defined by https://tc39.es/ecma262/#sec-ordinaryfunctioncreate step #17 and by https://tc39.es/ecma262/#sec-createbuiltinfunction step #6.
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp5
-rw-r--r--Userland/Libraries/LibJS/Runtime/FunctionObject.h3
-rw-r--r--Userland/Libraries/LibJS/Runtime/NativeFunction.h2
-rw-r--r--Userland/Libraries/LibJS/Runtime/OrdinaryFunctionObject.cpp1
-rw-r--r--Userland/Libraries/LibJS/Runtime/OrdinaryFunctionObject.h3
5 files changed, 11 insertions, 3 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp b/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp
index b9441a6e4a..9f045b211f 100644
--- a/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp
+++ b/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp
@@ -110,9 +110,8 @@ GlobalObject* get_function_realm(GlobalObject& global_object, FunctionObject con
{
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 (function.realm())
+ return function.realm();
if (is<BoundFunction>(function)) {
auto& bound_function = static_cast<BoundFunction const&>(function);
auto& target = bound_function.target_function();
diff --git a/Userland/Libraries/LibJS/Runtime/FunctionObject.h b/Userland/Libraries/LibJS/Runtime/FunctionObject.h
index 2db3894dde..91f5b35cd7 100644
--- a/Userland/Libraries/LibJS/Runtime/FunctionObject.h
+++ b/Userland/Libraries/LibJS/Runtime/FunctionObject.h
@@ -47,6 +47,9 @@ public:
// Used as the outer environment when evaluating the code of the function.
virtual EnvironmentRecord* environment() { return nullptr; }
+ // [[Realm]]
+ virtual GlobalObject* realm() const { return nullptr; }
+
enum class ThisMode : u8 {
Lexical,
Strict,
diff --git a/Userland/Libraries/LibJS/Runtime/NativeFunction.h b/Userland/Libraries/LibJS/Runtime/NativeFunction.h
index aa7899f456..39d66edb76 100644
--- a/Userland/Libraries/LibJS/Runtime/NativeFunction.h
+++ b/Userland/Libraries/LibJS/Runtime/NativeFunction.h
@@ -29,6 +29,8 @@ public:
virtual bool is_strict_mode() const override;
+ GlobalObject* realm() const override { return &global_object(); }
+
protected:
NativeFunction(FlyString name, Object& prototype);
explicit NativeFunction(Object& prototype);
diff --git a/Userland/Libraries/LibJS/Runtime/OrdinaryFunctionObject.cpp b/Userland/Libraries/LibJS/Runtime/OrdinaryFunctionObject.cpp
index ac2d9c0763..0c01c16fcc 100644
--- a/Userland/Libraries/LibJS/Runtime/OrdinaryFunctionObject.cpp
+++ b/Userland/Libraries/LibJS/Runtime/OrdinaryFunctionObject.cpp
@@ -56,6 +56,7 @@ OrdinaryFunctionObject::OrdinaryFunctionObject(GlobalObject& global_object, cons
, m_body(body)
, m_parameters(move(parameters))
, m_environment(parent_scope)
+ , m_realm(&global_object)
, m_function_length(function_length)
, m_kind(kind)
, m_is_strict(is_strict)
diff --git a/Userland/Libraries/LibJS/Runtime/OrdinaryFunctionObject.h b/Userland/Libraries/LibJS/Runtime/OrdinaryFunctionObject.h
index dfdf96cb42..5720c87845 100644
--- a/Userland/Libraries/LibJS/Runtime/OrdinaryFunctionObject.h
+++ b/Userland/Libraries/LibJS/Runtime/OrdinaryFunctionObject.h
@@ -37,6 +37,8 @@ public:
virtual EnvironmentRecord* environment() override { return m_environment; }
+ GlobalObject* realm() const override { return m_realm; }
+
protected:
virtual bool is_strict_mode() const final { return m_is_strict; }
@@ -55,6 +57,7 @@ private:
const Vector<FunctionNode::Parameter> m_parameters;
Optional<Bytecode::Executable> m_bytecode_executable;
EnvironmentRecord* m_environment { nullptr };
+ GlobalObject* m_realm { nullptr };
i32 m_function_length { 0 };
FunctionKind m_kind { FunctionKind::Regular };
bool m_is_strict { false };