LibJS: Inline somes AO's into InitializeHostDefinedRealm

Aligning with the editorial change in the spec of:

https://github.com/tc39/ecma262/commit/977a6c
This commit is contained in:
Shannon Booth 2024-11-03 16:34:27 +13:00 committed by Tim Flynn
commit 0a1c9e4038
Notes: github-actions[bot] 2024-11-03 12:50:28 +00:00
3 changed files with 58 additions and 81 deletions

View file

@ -62,13 +62,16 @@ JS_DEFINE_NATIVE_FUNCTION($262Object::clear_kept_objects)
JS_DEFINE_NATIVE_FUNCTION($262Object::create_realm) JS_DEFINE_NATIVE_FUNCTION($262Object::create_realm)
{ {
auto realm = MUST_OR_THROW_OOM(Realm::create(vm)); JS::GCPtr<JS::Test262::GlobalObject> global_object;
auto realm_global_object = vm.heap().allocate_without_realm<GlobalObject>(*realm); auto root_execution_context = MUST(JS::Realm::initialize_host_defined_realm(
VERIFY(realm_global_object); vm,
realm->set_global_object(realm_global_object, nullptr); [&](JS::Realm& realm) -> JS::GlobalObject* {
set_default_global_bindings(*realm); global_object = vm.heap().allocate_without_realm<JS::Test262::GlobalObject>(realm);
realm_global_object->initialize(*realm); return global_object;
return Value(realm_global_object->$262()); },
nullptr));
vm.pop_execution_context();
return Value(global_object->$262());
} }
JS_DEFINE_NATIVE_FUNCTION($262Object::detach_array_buffer) JS_DEFINE_NATIVE_FUNCTION($262Object::detach_array_buffer)

View file

@ -16,103 +16,80 @@ namespace JS {
JS_DEFINE_ALLOCATOR(Realm); JS_DEFINE_ALLOCATOR(Realm);
// 9.3.1 CreateRealm ( ), https://tc39.es/ecma262/#sec-createrealm // 9.3.1 InitializeHostDefinedRealm ( ), https://tc39.es/ecma262/#sec-initializehostdefinedrealm
ThrowCompletionOr<NonnullGCPtr<Realm>> Realm::create(VM& vm)
{
// 1. Let realmRec be a new Realm Record.
auto realm = vm.heap().allocate_without_realm<Realm>();
// 2. Perform CreateIntrinsics(realmRec).
MUST_OR_THROW_OOM(Intrinsics::create(*realm));
// 3. Set realmRec.[[GlobalObject]] to undefined.
// 4. Set realmRec.[[GlobalEnv]] to undefined.
// 5. Set realmRec.[[TemplateMap]] to a new empty List.
// 6. Return realmRec.
return realm;
}
// 9.6 InitializeHostDefinedRealm ( ), https://tc39.es/ecma262/#sec-initializehostdefinedrealm
ThrowCompletionOr<NonnullOwnPtr<ExecutionContext>> Realm::initialize_host_defined_realm(VM& vm, Function<Object*(Realm&)> create_global_object, Function<Object*(Realm&)> create_global_this_value) ThrowCompletionOr<NonnullOwnPtr<ExecutionContext>> Realm::initialize_host_defined_realm(VM& vm, Function<Object*(Realm&)> create_global_object, Function<Object*(Realm&)> create_global_this_value)
{ {
DeferGC defer_gc(vm.heap()); DeferGC defer_gc(vm.heap());
// 1. Let realm be CreateRealm(). // 1. Let realm be a new Realm Record
auto realm = MUST_OR_THROW_OOM(Realm::create(vm)); auto realm = vm.heap().allocate_without_realm<Realm>();
// 2. Let newContext be a new execution context. // 2. Perform CreateIntrinsics(realm).
MUST(Intrinsics::create(*realm));
// FIXME: 3. Set realm.[[AgentSignifier]] to AgentSignifier().
// NOTE: Done on step 1.
// 4. Set realm.[[GlobalObject]] to undefined.
// 5. Set realm.[[GlobalEnv]] to undefined.
// FIXME: 6. Set realm.[[TemplateMap]] to a new empty List.
// 7. Let newContext be a new execution context.
auto new_context = ExecutionContext::create(); auto new_context = ExecutionContext::create();
// 3. Set the Function of newContext to null. // 8. Set the Function of newContext to null.
new_context->function = nullptr; new_context->function = nullptr;
// 4. Set the Realm of newContext to realm. // 9. Set the Realm of newContext to realm.
new_context->realm = realm; new_context->realm = realm;
// 5. Set the ScriptOrModule of newContext to null. // 10. Set the ScriptOrModule of newContext to null.
new_context->script_or_module = {}; new_context->script_or_module = {};
// 6. Push newContext onto the execution context stack; newContext is now the running execution context. // 11. Push newContext onto the execution context stack; newContext is now the running execution context.
vm.push_execution_context(*new_context); vm.push_execution_context(*new_context);
// 7. If the host requires use of an exotic object to serve as realm's global object, // 12. If the host requires use of an exotic object to serve as realm's global object, then
// let global be such an object created in a host-defined manner.
// Otherwise, let global be undefined, indicating that an ordinary object should be created as the global object.
Object* global = nullptr; Object* global = nullptr;
if (create_global_object) if (create_global_object) {
// a. Let global be such an object created in a host-defined manner.
global = create_global_object(*realm); global = create_global_object(*realm);
}
// 8. If the host requires that the this binding in realm's global scope return an object other than the global object, // 13. Else,
// let thisValue be such an object created in a host-defined manner. else {
// Otherwise, let thisValue be undefined, indicating that realm's global this binding should be the global object. // a. Let global be OrdinaryObjectCreate(realm.[[Intrinsics]].[[%Object.prototype%]]).
Object* this_value = nullptr;
if (create_global_this_value)
this_value = create_global_this_value(*realm);
// 9. Perform SetRealmGlobalObject(realm, global, thisValue).
realm->set_global_object(global, this_value);
// 10. Let globalObj be ? SetDefaultGlobalBindings(realm).
auto& global_object = set_default_global_bindings(*realm);
// 11. Create any host-defined global object properties on globalObj.
global_object.initialize(*realm);
// 12. Return unused.
return new_context;
}
// 9.3.3 SetRealmGlobalObject ( realmRec, globalObj, thisValue ), https://tc39.es/ecma262/#sec-setrealmglobalobject
void Realm::set_global_object(Object* global_object, Object* this_value)
{
// 1. If globalObj is undefined, then
if (global_object == nullptr) {
// a. Let intrinsics be realmRec.[[Intrinsics]].
// b. Set globalObj to OrdinaryObjectCreate(intrinsics.[[%Object.prototype%]]).
// NOTE: We allocate a proper GlobalObject directly as this plain object is // NOTE: We allocate a proper GlobalObject directly as this plain object is
// turned into one via SetDefaultGlobalBindings in the spec. // turned into one via SetDefaultGlobalBindings in the spec.
global_object = heap().allocate_without_realm<GlobalObject>(*this); global = vm.heap().allocate_without_realm<GlobalObject>(realm);
} }
// 2. Assert: Type(globalObj) is Object. // 14. If the host requires that the this binding in realm's global scope return an object other than the global object, then
VERIFY(global_object); Object* this_value = nullptr;
if (create_global_this_value) {
// a. Let thisValue be such an object created in a host-defined manner.
this_value = create_global_this_value(*realm);
}
// 15. Else,
else {
// a. Let thisValue be global.
this_value = global;
}
// 3. If thisValue is undefined, set thisValue to globalObj. // 16. Set realm.[[GlobalObject]] to global.
if (this_value == nullptr) realm->m_global_object = global;
this_value = global_object;
// Non-standard // 17. Set realm.[[GlobalEnv]] to NewGlobalEnvironment(global, thisValue).
VERIFY(this_value); realm->m_global_environment = vm.heap().allocate_without_realm<GlobalEnvironment>(*global, *this_value);
// 4. Set realmRec.[[GlobalObject]] to globalObj. // 18. Perform ? SetDefaultGlobalBindings(realm).
m_global_object = global_object; set_default_global_bindings(*realm);
// 5. Let newGlobalEnv be NewGlobalEnvironment(globalObj, thisValue). // 19. Create any host-defined global object properties on global.
// 6. Set realmRec.[[GlobalEnv]] to newGlobalEnv. global->initialize(*realm);
m_global_environment = m_global_object->heap().allocate_without_realm<GlobalEnvironment>(*global_object, *this_value);
// 7. Return unused. // 20. Return unused.
return new_context;
} }
void Realm::visit_edges(Visitor& visitor) void Realm::visit_edges(Visitor& visitor)

View file

@ -31,11 +31,8 @@ public:
virtual void visit_edges(Cell::Visitor&) { } virtual void visit_edges(Cell::Visitor&) { }
}; };
static ThrowCompletionOr<NonnullGCPtr<Realm>> create(VM&);
static ThrowCompletionOr<NonnullOwnPtr<ExecutionContext>> initialize_host_defined_realm(VM&, Function<Object*(Realm&)> create_global_object, Function<Object*(Realm&)> create_global_this_value); static ThrowCompletionOr<NonnullOwnPtr<ExecutionContext>> initialize_host_defined_realm(VM&, Function<Object*(Realm&)> create_global_object, Function<Object*(Realm&)> create_global_this_value);
void set_global_object(Object* global_object, Object* this_value);
[[nodiscard]] Object& global_object() const { return *m_global_object; } [[nodiscard]] Object& 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; }