summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Userland/Libraries/LibJS/Bytecode/Interpreter.cpp3
-rw-r--r--Userland/Libraries/LibJS/Bytecode/Interpreter.h4
-rw-r--r--Userland/Libraries/LibJS/Interpreter.cpp13
-rw-r--r--Userland/Libraries/LibJS/Interpreter.h16
-rw-r--r--Userland/Libraries/LibJS/Runtime/Realm.cpp20
-rw-r--r--Userland/Libraries/LibJS/Runtime/Realm.h2
-rw-r--r--Userland/Libraries/LibTest/JavaScriptTestRunner.h4
-rw-r--r--Userland/Utilities/js.cpp2
8 files changed, 54 insertions, 10 deletions
diff --git a/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp b/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp
index 50d9f2a49b..9368f39af4 100644
--- a/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp
+++ b/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp
@@ -22,9 +22,10 @@ Interpreter* Interpreter::current()
return s_current;
}
-Interpreter::Interpreter(GlobalObject& global_object)
+Interpreter::Interpreter(GlobalObject& global_object, Realm& realm)
: m_vm(global_object.vm())
, m_global_object(global_object)
+ , m_realm(realm)
{
VERIFY(!s_current);
s_current = this;
diff --git a/Userland/Libraries/LibJS/Bytecode/Interpreter.h b/Userland/Libraries/LibJS/Bytecode/Interpreter.h
index 0dda59a99b..f8faea9029 100644
--- a/Userland/Libraries/LibJS/Bytecode/Interpreter.h
+++ b/Userland/Libraries/LibJS/Bytecode/Interpreter.h
@@ -22,13 +22,14 @@ using RegisterWindow = Vector<Value>;
class Interpreter {
public:
- explicit Interpreter(GlobalObject&);
+ Interpreter(GlobalObject&, Realm&);
~Interpreter();
// FIXME: Remove this thing once we don't need it anymore!
static Interpreter* current();
GlobalObject& global_object() { return m_global_object; }
+ Realm& realm() { return m_realm; }
VM& vm() { return m_vm; }
Value run(Bytecode::Executable const&, Bytecode::BasicBlock const* entry_point = nullptr);
@@ -74,6 +75,7 @@ private:
VM& m_vm;
GlobalObject& m_global_object;
+ Realm& m_realm;
NonnullOwnPtrVector<RegisterWindow> m_register_windows;
Optional<BasicBlock const*> m_pending_jump;
Value m_return_value;
diff --git a/Userland/Libraries/LibJS/Interpreter.cpp b/Userland/Libraries/LibJS/Interpreter.cpp
index 41db16c75d..3436be652e 100644
--- a/Userland/Libraries/LibJS/Interpreter.cpp
+++ b/Userland/Libraries/LibJS/Interpreter.cpp
@@ -11,7 +11,6 @@
#include <LibJS/Runtime/FunctionEnvironment.h>
#include <LibJS/Runtime/GlobalEnvironment.h>
#include <LibJS/Runtime/GlobalObject.h>
-#include <LibJS/Runtime/Object.h>
#include <LibJS/Runtime/OrdinaryFunctionObject.h>
#include <LibJS/Runtime/Reference.h>
#include <LibJS/Runtime/Shape.h>
@@ -23,7 +22,7 @@ NonnullOwnPtr<Interpreter> Interpreter::create_with_existing_global_object(Globa
{
DeferGC defer_gc(global_object.heap());
auto interpreter = adopt_own(*new Interpreter(global_object.vm()));
- interpreter->m_global_object = make_handle(static_cast<Object*>(&global_object));
+ interpreter->m_global_object = make_handle(&global_object);
return interpreter;
}
@@ -80,6 +79,16 @@ const GlobalObject& Interpreter::global_object() const
return static_cast<const GlobalObject&>(*m_global_object.cell());
}
+Realm& Interpreter::realm()
+{
+ return static_cast<Realm&>(*m_realm.cell());
+}
+
+const Realm& Interpreter::realm() const
+{
+ return static_cast<const Realm&>(*m_realm.cell());
+}
+
void Interpreter::enter_scope(const ScopeNode& scope_node, ScopeType scope_type, GlobalObject& global_object)
{
ScopeGuard guard([&] {
diff --git a/Userland/Libraries/LibJS/Interpreter.h b/Userland/Libraries/LibJS/Interpreter.h
index 204cf041d5..cb75ab14ce 100644
--- a/Userland/Libraries/LibJS/Interpreter.h
+++ b/Userland/Libraries/LibJS/Interpreter.h
@@ -18,7 +18,9 @@
#include <LibJS/Runtime/DeclarativeEnvironment.h>
#include <LibJS/Runtime/ErrorTypes.h>
#include <LibJS/Runtime/Exception.h>
+#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/MarkedValueList.h>
+#include <LibJS/Runtime/Realm.h>
#include <LibJS/Runtime/VM.h>
#include <LibJS/Runtime/Value.h>
@@ -37,8 +39,12 @@ public:
DeferGC defer_gc(vm.heap());
auto interpreter = adopt_own(*new Interpreter(vm));
VM::InterpreterExecutionScope scope(*interpreter);
- interpreter->m_global_object = make_handle(static_cast<Object*>(interpreter->heap().allocate_without_global_object<GlobalObjectType>(forward<Args>(args)...)));
- static_cast<GlobalObjectType*>(interpreter->m_global_object.cell())->initialize_global_object();
+ auto* global_object = static_cast<GlobalObject*>(interpreter->heap().allocate_without_global_object<GlobalObjectType>(forward<Args>(args)...));
+ auto* realm = Realm::create(vm);
+ realm->set_global_object(*global_object, global_object);
+ interpreter->m_global_object = make_handle(global_object);
+ interpreter->m_realm = make_handle(realm);
+ static_cast<GlobalObjectType*>(global_object)->initialize_global_object();
return interpreter;
}
@@ -51,6 +57,9 @@ public:
GlobalObject& global_object();
const GlobalObject& global_object() const;
+ Realm& realm();
+ Realm const& realm() const;
+
ALWAYS_INLINE VM& vm() { return *m_vm; }
ALWAYS_INLINE const VM& vm() const { return *m_vm; }
ALWAYS_INLINE Heap& heap() { return vm().heap(); }
@@ -91,7 +100,8 @@ private:
NonnullRefPtr<VM> m_vm;
- Handle<Object> m_global_object;
+ Handle<GlobalObject> m_global_object;
+ Handle<Realm> m_realm;
};
}
diff --git a/Userland/Libraries/LibJS/Runtime/Realm.cpp b/Userland/Libraries/LibJS/Runtime/Realm.cpp
index 7d832905f5..1082f0804f 100644
--- a/Userland/Libraries/LibJS/Runtime/Realm.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Realm.cpp
@@ -8,6 +8,26 @@
namespace JS {
+// 9.3.3 SetRealmGlobalObject ( realmRec, globalObj, thisValue ), https://tc39.es/ecma262/#sec-setrealmglobalobject
+void Realm::set_global_object(GlobalObject& global_object, Object* this_value)
+{
+ // NOTE: Step 1 is not supported, the global object must be allocated elsewhere.
+ // 2. Assert: Type(globalObj) is Object.
+
+ // 3. If thisValue is undefined, set thisValue to globalObj.
+ if (!this_value)
+ this_value = &global_object;
+
+ // 4. Set realmRec.[[GlobalObject]] to globalObj.
+ m_global_object = &global_object;
+
+ // 5. Let newGlobalEnv be NewGlobalEnvironment(globalObj, thisValue).
+ // 6. Set realmRec.[[GlobalEnv]] to newGlobalEnv.
+ m_global_environment = global_object.heap().allocate<GlobalEnvironment>(global_object, global_object, *this_value);
+
+ // 7. Return realmRec.
+}
+
void Realm::visit_edges(Visitor& visitor)
{
visitor.visit(m_global_object);
diff --git a/Userland/Libraries/LibJS/Runtime/Realm.h b/Userland/Libraries/LibJS/Runtime/Realm.h
index 56571729cf..bd4d25d5e2 100644
--- a/Userland/Libraries/LibJS/Runtime/Realm.h
+++ b/Userland/Libraries/LibJS/Runtime/Realm.h
@@ -23,6 +23,8 @@ public:
return vm.heap().allocate_without_global_object<Realm>();
}
+ void set_global_object(GlobalObject&, Object* this_value = nullptr);
+
[[nodiscard]] GlobalObject& global_object() const { return *m_global_object; }
[[nodiscard]] GlobalEnvironment& global_environment() const { return *m_global_environment; }
diff --git a/Userland/Libraries/LibTest/JavaScriptTestRunner.h b/Userland/Libraries/LibTest/JavaScriptTestRunner.h
index 801aa847f1..0389acecd5 100644
--- a/Userland/Libraries/LibTest/JavaScriptTestRunner.h
+++ b/Userland/Libraries/LibTest/JavaScriptTestRunner.h
@@ -326,7 +326,7 @@ inline JSFileResult TestRunner::run_file_test(const String& test_path)
}
}
- JS::Bytecode::Interpreter bytecode_interpreter(interpreter->global_object());
+ JS::Bytecode::Interpreter bytecode_interpreter(interpreter->global_object(), interpreter->realm());
bytecode_interpreter.run(unit);
} else {
interpreter->run(interpreter->global_object(), *m_test_program);
@@ -348,7 +348,7 @@ inline JSFileResult TestRunner::run_file_test(const String& test_path)
}
}
- JS::Bytecode::Interpreter bytecode_interpreter(interpreter->global_object());
+ JS::Bytecode::Interpreter bytecode_interpreter(interpreter->global_object(), interpreter->realm());
bytecode_interpreter.run(unit);
} else {
interpreter->run(interpreter->global_object(), *file_program.value());
diff --git a/Userland/Utilities/js.cpp b/Userland/Utilities/js.cpp
index 25dd94ef7b..75b2d4cb0e 100644
--- a/Userland/Utilities/js.cpp
+++ b/Userland/Utilities/js.cpp
@@ -830,7 +830,7 @@ static bool parse_and_run(JS::Interpreter& interpreter, StringView const& source
}
if (s_run_bytecode) {
- JS::Bytecode::Interpreter bytecode_interpreter(interpreter.global_object());
+ JS::Bytecode::Interpreter bytecode_interpreter(interpreter.global_object(), interpreter.realm());
bytecode_interpreter.run(unit);
} else {
return true;