Jelajahi Sumber

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.
Linus Groh 3 tahun lalu
induk
melakukan
2b8d5696ab

+ 2 - 1
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;

+ 3 - 1
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;

+ 11 - 2
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([&] {

+ 13 - 3
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;
 };
 
 }

+ 20 - 0
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);

+ 2 - 0
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; }
 

+ 2 - 2
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());

+ 1 - 1
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;