@@ -36,71 +36,35 @@ struct ExecutingASTNodeChain {
36
36
37
37
class Interpreter : public Weakable <Interpreter> {
38
38
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>)
42
41
{
43
42
DeferGC defer_gc (vm.heap ());
44
43
auto interpreter = adopt_own (*new Interpreter (vm));
45
44
VM::InterpreterExecutionScope scope (*interpreter);
46
45
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 };
68
47
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
+ }));
81
57
82
58
// NOTE: These are not in the spec.
83
59
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;
85
61
86
62
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 ());
92
64
93
- // 12. Return unused.
94
65
return interpreter;
95
66
}
96
67
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
-
104
68
static NonnullOwnPtr<Interpreter> create_with_existing_realm (Realm&);
105
69
106
70
~Interpreter () = default ;
@@ -145,7 +109,7 @@ class Interpreter : public Weakable<Interpreter> {
145
109
Handle<Realm> m_realm;
146
110
147
111
// 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;
149
113
};
150
114
151
115
}
0 commit comments