Skip to content

Commit fb1900e

Browse files
committed
LibJS: Make Interpreter::create() call InitializeHostDefinedRealm()
Instead of having two implementations of this AO, let's just have Interpreter::create() call the new full version of the AO in Realm.
1 parent 8a03b17 commit fb1900e

File tree

2 files changed

+15
-52
lines changed

2 files changed

+15
-52
lines changed

Userland/Libraries/LibJS/Interpreter.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ NonnullOwnPtr<Interpreter> Interpreter::create_with_existing_realm(Realm& realm)
3232

3333
Interpreter::Interpreter(VM& vm)
3434
: m_vm(vm)
35-
, m_global_execution_context(vm.heap())
3635
{
3736
}
3837

Userland/Libraries/LibJS/Interpreter.h

Lines changed: 15 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -36,71 +36,35 @@ struct ExecutingASTNodeChain {
3636

3737
class Interpreter : public Weakable<Interpreter> {
3838
public:
39-
// 9.6 InitializeHostDefinedRealm ( ), https://tc39.es/ecma262/#sec-initializehostdefinedrealm
40-
template<typename GlobalObjectType, typename GlobalThisObjectType, typename... Args>
41-
static NonnullOwnPtr<Interpreter> create(VM& vm, Args&&... args) requires(IsBaseOf<GlobalObject, GlobalObjectType>&& IsBaseOf<Object, GlobalThisObjectType>)
39+
template<typename GlobalObjectType, typename... Args>
40+
static NonnullOwnPtr<Interpreter> create(VM& vm, Args&&... args) requires(IsBaseOf<GlobalObject, GlobalObjectType>)
4241
{
4342
DeferGC defer_gc(vm.heap());
4443
auto interpreter = adopt_own(*new Interpreter(vm));
4544
VM::InterpreterExecutionScope scope(*interpreter);
4645

47-
// 1. Let realm be CreateRealm().
48-
auto* realm = Realm::create(vm);
49-
50-
// 2. Let newContext be a new execution context.
51-
auto& new_context = interpreter->m_global_execution_context;
52-
53-
// 3. Set the Function of newContext to null.
54-
// NOTE: This was done during execution context construction.
55-
56-
// 4. Set the Realm of newContext to realm.
57-
new_context.realm = realm;
58-
59-
// 5. Set the ScriptOrModule of newContext to null.
60-
// NOTE: This was done during execution context construction.
61-
62-
// 6. Push newContext onto the execution context stack; newContext is now the running execution context.
63-
vm.push_execution_context(new_context);
64-
65-
// 7. If the host requires use of an exotic object to serve as realm's global object, let global be such an object created in a host-defined manner.
66-
// Otherwise, let global be undefined, indicating that an ordinary object should be created as the global object.
67-
auto* global_object = static_cast<GlobalObject*>(interpreter->heap().allocate_without_global_object<GlobalObjectType>(*realm, forward<Args>(args)...));
46+
GlobalObject* global_object { nullptr };
6847

69-
// 8. If the host requires that the this binding in realm's global scope return an object other than the global object, let thisValue be such an object created
70-
// in a host-defined manner. Otherwise, let thisValue be undefined, indicating that realm's global this binding should be the global object.
71-
Object* this_value;
72-
if constexpr (IsSame<GlobalObjectType, GlobalThisObjectType>) {
73-
this_value = global_object;
74-
} else {
75-
// FIXME: Should we pass args in here? Let's er on the side of caution and say yes.
76-
this_value = static_cast<Object*>(interpreter->heap().allocate_without_global_object<GlobalThisObjectType>(*realm, forward<Args>(args)...));
77-
}
78-
79-
// 9. Perform SetRealmGlobalObject(realm, global, thisValue).
80-
realm->set_global_object(*global_object, this_value);
48+
interpreter->m_global_execution_context = MUST(Realm::initialize_host_defined_realm(
49+
vm,
50+
[&](Realm& realm) -> Value {
51+
global_object = interpreter->heap().allocate_without_global_object<GlobalObjectType>(realm, forward<Args>(args)...);
52+
return global_object;
53+
},
54+
[](Realm&) -> Value {
55+
return js_undefined();
56+
}));
8157

8258
// NOTE: These are not in the spec.
8359
static FlyString global_execution_context_name = "(global execution context)";
84-
interpreter->m_global_execution_context.function_name = global_execution_context_name;
60+
interpreter->m_global_execution_context->function_name = global_execution_context_name;
8561

8662
interpreter->m_global_object = make_handle(global_object);
87-
interpreter->m_realm = make_handle(realm);
88-
89-
// 10. Let globalObj be ? SetDefaultGlobalBindings(realm).
90-
// 11. Create any host-defined global object properties on globalObj.
91-
static_cast<GlobalObjectType*>(global_object)->initialize_global_object();
63+
interpreter->m_realm = make_handle(global_object->associated_realm());
9264

93-
// 12. Return unused.
9465
return interpreter;
9566
}
9667

97-
template<typename GlobalObjectType, typename... Args>
98-
static NonnullOwnPtr<Interpreter> create(VM& vm, Args&&... args) requires IsBaseOf<GlobalObject, GlobalObjectType>
99-
{
100-
// NOTE: This function is here to facilitate step 8 of InitializeHostDefinedRealm. (Callers don't have to specify the same type twice if not necessary)
101-
return create<GlobalObjectType, GlobalObjectType>(vm, args...);
102-
}
103-
10468
static NonnullOwnPtr<Interpreter> create_with_existing_realm(Realm&);
10569

10670
~Interpreter() = default;
@@ -145,7 +109,7 @@ class Interpreter : public Weakable<Interpreter> {
145109
Handle<Realm> m_realm;
146110

147111
// This is here to keep the global execution context alive for the entire lifespan of the Interpreter.
148-
ExecutionContext m_global_execution_context;
112+
OwnPtr<ExecutionContext> m_global_execution_context;
149113
};
150114

151115
}

0 commit comments

Comments
 (0)