From 2b8d5696abd595f64927bd756c76a0366f15d4b7 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Sat, 11 Sep 2021 19:36:25 +0100 Subject: LibJS: Allocate a Realm next to GlobalObject in Interpreter::create() Also pass a Realm reference to the Bytecode::Interpreter constructor, just like we pass the GlobalObject. --- Userland/Libraries/LibJS/Bytecode/Interpreter.cpp | 3 ++- Userland/Libraries/LibJS/Bytecode/Interpreter.h | 4 +++- Userland/Libraries/LibJS/Interpreter.cpp | 13 +++++++++++-- Userland/Libraries/LibJS/Interpreter.h | 16 +++++++++++++--- Userland/Libraries/LibJS/Runtime/Realm.cpp | 20 ++++++++++++++++++++ Userland/Libraries/LibJS/Runtime/Realm.h | 2 ++ 6 files changed, 51 insertions(+), 7 deletions(-) (limited to 'Userland/Libraries/LibJS') 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; 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 m_register_windows; Optional 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 #include #include -#include #include #include #include @@ -23,7 +22,7 @@ NonnullOwnPtr 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(&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(*m_global_object.cell()); } +Realm& Interpreter::realm() +{ + return static_cast(*m_realm.cell()); +} + +const Realm& Interpreter::realm() const +{ + return static_cast(*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 #include #include +#include #include +#include #include #include @@ -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(interpreter->heap().allocate_without_global_object(forward(args)...))); - static_cast(interpreter->m_global_object.cell())->initialize_global_object(); + auto* global_object = static_cast(interpreter->heap().allocate_without_global_object(forward(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(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 m_vm; - Handle m_global_object; + Handle m_global_object; + Handle 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(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(); } + 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; } -- cgit v1.2.3