mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 00:42:44 +00:00 
			
		
		
		
	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.
This commit is contained in:
		
							parent
							
								
									d9c3bafcd9
								
							
						
					
					
						commit
						2b8d5696ab
					
				
					 8 changed files with 54 additions and 10 deletions
				
			
		|  | @ -22,9 +22,10 @@ Interpreter* Interpreter::current() | ||||||
|     return s_current; |     return s_current; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| Interpreter::Interpreter(GlobalObject& global_object) | Interpreter::Interpreter(GlobalObject& global_object, Realm& realm) | ||||||
|     : m_vm(global_object.vm()) |     : m_vm(global_object.vm()) | ||||||
|     , m_global_object(global_object) |     , m_global_object(global_object) | ||||||
|  |     , m_realm(realm) | ||||||
| { | { | ||||||
|     VERIFY(!s_current); |     VERIFY(!s_current); | ||||||
|     s_current = this; |     s_current = this; | ||||||
|  |  | ||||||
|  | @ -22,13 +22,14 @@ using RegisterWindow = Vector<Value>; | ||||||
| 
 | 
 | ||||||
| class Interpreter { | class Interpreter { | ||||||
| public: | public: | ||||||
|     explicit Interpreter(GlobalObject&); |     Interpreter(GlobalObject&, Realm&); | ||||||
|     ~Interpreter(); |     ~Interpreter(); | ||||||
| 
 | 
 | ||||||
|     // FIXME: Remove this thing once we don't need it anymore!
 |     // FIXME: Remove this thing once we don't need it anymore!
 | ||||||
|     static Interpreter* current(); |     static Interpreter* current(); | ||||||
| 
 | 
 | ||||||
|     GlobalObject& global_object() { return m_global_object; } |     GlobalObject& global_object() { return m_global_object; } | ||||||
|  |     Realm& realm() { return m_realm; } | ||||||
|     VM& vm() { return m_vm; } |     VM& vm() { return m_vm; } | ||||||
| 
 | 
 | ||||||
|     Value run(Bytecode::Executable const&, Bytecode::BasicBlock const* entry_point = nullptr); |     Value run(Bytecode::Executable const&, Bytecode::BasicBlock const* entry_point = nullptr); | ||||||
|  | @ -74,6 +75,7 @@ private: | ||||||
| 
 | 
 | ||||||
|     VM& m_vm; |     VM& m_vm; | ||||||
|     GlobalObject& m_global_object; |     GlobalObject& m_global_object; | ||||||
|  |     Realm& m_realm; | ||||||
|     NonnullOwnPtrVector<RegisterWindow> m_register_windows; |     NonnullOwnPtrVector<RegisterWindow> m_register_windows; | ||||||
|     Optional<BasicBlock const*> m_pending_jump; |     Optional<BasicBlock const*> m_pending_jump; | ||||||
|     Value m_return_value; |     Value m_return_value; | ||||||
|  |  | ||||||
|  | @ -11,7 +11,6 @@ | ||||||
| #include <LibJS/Runtime/FunctionEnvironment.h> | #include <LibJS/Runtime/FunctionEnvironment.h> | ||||||
| #include <LibJS/Runtime/GlobalEnvironment.h> | #include <LibJS/Runtime/GlobalEnvironment.h> | ||||||
| #include <LibJS/Runtime/GlobalObject.h> | #include <LibJS/Runtime/GlobalObject.h> | ||||||
| #include <LibJS/Runtime/Object.h> |  | ||||||
| #include <LibJS/Runtime/OrdinaryFunctionObject.h> | #include <LibJS/Runtime/OrdinaryFunctionObject.h> | ||||||
| #include <LibJS/Runtime/Reference.h> | #include <LibJS/Runtime/Reference.h> | ||||||
| #include <LibJS/Runtime/Shape.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()); |     DeferGC defer_gc(global_object.heap()); | ||||||
|     auto interpreter = adopt_own(*new Interpreter(global_object.vm())); |     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; |     return interpreter; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -80,6 +79,16 @@ const GlobalObject& Interpreter::global_object() const | ||||||
|     return static_cast<const GlobalObject&>(*m_global_object.cell()); |     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) | void Interpreter::enter_scope(const ScopeNode& scope_node, ScopeType scope_type, GlobalObject& global_object) | ||||||
| { | { | ||||||
|     ScopeGuard guard([&] { |     ScopeGuard guard([&] { | ||||||
|  |  | ||||||
|  | @ -18,7 +18,9 @@ | ||||||
| #include <LibJS/Runtime/DeclarativeEnvironment.h> | #include <LibJS/Runtime/DeclarativeEnvironment.h> | ||||||
| #include <LibJS/Runtime/ErrorTypes.h> | #include <LibJS/Runtime/ErrorTypes.h> | ||||||
| #include <LibJS/Runtime/Exception.h> | #include <LibJS/Runtime/Exception.h> | ||||||
|  | #include <LibJS/Runtime/GlobalObject.h> | ||||||
| #include <LibJS/Runtime/MarkedValueList.h> | #include <LibJS/Runtime/MarkedValueList.h> | ||||||
|  | #include <LibJS/Runtime/Realm.h> | ||||||
| #include <LibJS/Runtime/VM.h> | #include <LibJS/Runtime/VM.h> | ||||||
| #include <LibJS/Runtime/Value.h> | #include <LibJS/Runtime/Value.h> | ||||||
| 
 | 
 | ||||||
|  | @ -37,8 +39,12 @@ public: | ||||||
|         DeferGC defer_gc(vm.heap()); |         DeferGC defer_gc(vm.heap()); | ||||||
|         auto interpreter = adopt_own(*new Interpreter(vm)); |         auto interpreter = adopt_own(*new Interpreter(vm)); | ||||||
|         VM::InterpreterExecutionScope scope(*interpreter); |         VM::InterpreterExecutionScope scope(*interpreter); | ||||||
|         interpreter->m_global_object = make_handle(static_cast<Object*>(interpreter->heap().allocate_without_global_object<GlobalObjectType>(forward<Args>(args)...))); |         auto* global_object = static_cast<GlobalObject*>(interpreter->heap().allocate_without_global_object<GlobalObjectType>(forward<Args>(args)...)); | ||||||
|         static_cast<GlobalObjectType*>(interpreter->m_global_object.cell())->initialize_global_object(); |         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; |         return interpreter; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  | @ -51,6 +57,9 @@ public: | ||||||
|     GlobalObject& global_object(); |     GlobalObject& global_object(); | ||||||
|     const GlobalObject& global_object() const; |     const GlobalObject& global_object() const; | ||||||
| 
 | 
 | ||||||
|  |     Realm& realm(); | ||||||
|  |     Realm const& realm() const; | ||||||
|  | 
 | ||||||
|     ALWAYS_INLINE VM& vm() { return *m_vm; } |     ALWAYS_INLINE VM& vm() { return *m_vm; } | ||||||
|     ALWAYS_INLINE const VM& vm() const { return *m_vm; } |     ALWAYS_INLINE const VM& vm() const { return *m_vm; } | ||||||
|     ALWAYS_INLINE Heap& heap() { return vm().heap(); } |     ALWAYS_INLINE Heap& heap() { return vm().heap(); } | ||||||
|  | @ -91,7 +100,8 @@ private: | ||||||
| 
 | 
 | ||||||
|     NonnullRefPtr<VM> m_vm; |     NonnullRefPtr<VM> m_vm; | ||||||
| 
 | 
 | ||||||
|     Handle<Object> m_global_object; |     Handle<GlobalObject> m_global_object; | ||||||
|  |     Handle<Realm> m_realm; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -8,6 +8,26 @@ | ||||||
| 
 | 
 | ||||||
| namespace JS { | 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) | void Realm::visit_edges(Visitor& visitor) | ||||||
| { | { | ||||||
|     visitor.visit(m_global_object); |     visitor.visit(m_global_object); | ||||||
|  |  | ||||||
|  | @ -23,6 +23,8 @@ public: | ||||||
|         return vm.heap().allocate_without_global_object<Realm>(); |         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]] GlobalObject& global_object() const { return *m_global_object; } | ||||||
|     [[nodiscard]] GlobalEnvironment& global_environment() const { return *m_global_environment; } |     [[nodiscard]] GlobalEnvironment& global_environment() const { return *m_global_environment; } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -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); |         bytecode_interpreter.run(unit); | ||||||
|     } else { |     } else { | ||||||
|         interpreter->run(interpreter->global_object(), *m_test_program); |         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); |         bytecode_interpreter.run(unit); | ||||||
|     } else { |     } else { | ||||||
|         interpreter->run(interpreter->global_object(), *file_program.value()); |         interpreter->run(interpreter->global_object(), *file_program.value()); | ||||||
|  |  | ||||||
|  | @ -830,7 +830,7 @@ static bool parse_and_run(JS::Interpreter& interpreter, StringView const& source | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|             if (s_run_bytecode) { |             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); |                 bytecode_interpreter.run(unit); | ||||||
|             } else { |             } else { | ||||||
|                 return true; |                 return true; | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Linus Groh
						Linus Groh