From 0a1c9e4038dddbe0078e7c0ef82d8374b928bde1 Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Sun, 3 Nov 2024 16:34:27 +1300 Subject: [PATCH] LibJS: Inline somes AO's into InitializeHostDefinedRealm Aligning with the editorial change in the spec of: https://github.com/tc39/ecma262/commit/977a6c --- .../LibJS/Contrib/Test262/262Object.cpp | 17 +-- Userland/Libraries/LibJS/Runtime/Realm.cpp | 119 +++++++----------- Userland/Libraries/LibJS/Runtime/Realm.h | 3 - 3 files changed, 58 insertions(+), 81 deletions(-) diff --git a/Userland/Libraries/LibJS/Contrib/Test262/262Object.cpp b/Userland/Libraries/LibJS/Contrib/Test262/262Object.cpp index 5fc7a3f17cb..c8d01eeba76 100644 --- a/Userland/Libraries/LibJS/Contrib/Test262/262Object.cpp +++ b/Userland/Libraries/LibJS/Contrib/Test262/262Object.cpp @@ -62,13 +62,16 @@ JS_DEFINE_NATIVE_FUNCTION($262Object::clear_kept_objects) JS_DEFINE_NATIVE_FUNCTION($262Object::create_realm) { - auto realm = MUST_OR_THROW_OOM(Realm::create(vm)); - auto realm_global_object = vm.heap().allocate_without_realm(*realm); - VERIFY(realm_global_object); - realm->set_global_object(realm_global_object, nullptr); - set_default_global_bindings(*realm); - realm_global_object->initialize(*realm); - return Value(realm_global_object->$262()); + JS::GCPtr global_object; + auto root_execution_context = MUST(JS::Realm::initialize_host_defined_realm( + vm, + [&](JS::Realm& realm) -> JS::GlobalObject* { + global_object = vm.heap().allocate_without_realm(realm); + return global_object; + }, + nullptr)); + vm.pop_execution_context(); + return Value(global_object->$262()); } JS_DEFINE_NATIVE_FUNCTION($262Object::detach_array_buffer) diff --git a/Userland/Libraries/LibJS/Runtime/Realm.cpp b/Userland/Libraries/LibJS/Runtime/Realm.cpp index 1369074fc2c..4c9d7a44142 100644 --- a/Userland/Libraries/LibJS/Runtime/Realm.cpp +++ b/Userland/Libraries/LibJS/Runtime/Realm.cpp @@ -16,103 +16,80 @@ namespace JS { JS_DEFINE_ALLOCATOR(Realm); -// 9.3.1 CreateRealm ( ), https://tc39.es/ecma262/#sec-createrealm -ThrowCompletionOr> Realm::create(VM& vm) -{ - // 1. Let realmRec be a new Realm Record. - auto realm = vm.heap().allocate_without_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 +// 9.3.1 InitializeHostDefinedRealm ( ), https://tc39.es/ecma262/#sec-initializehostdefinedrealm ThrowCompletionOr> Realm::initialize_host_defined_realm(VM& vm, Function create_global_object, Function create_global_this_value) { DeferGC defer_gc(vm.heap()); - // 1. Let realm be CreateRealm(). - auto realm = MUST_OR_THROW_OOM(Realm::create(vm)); + // 1. Let realm be a new Realm Record + auto realm = vm.heap().allocate_without_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(); - // 3. Set the Function of newContext to null. + // 8. Set the Function of newContext to null. new_context->function = nullptr; - // 4. Set the Realm of newContext to realm. + // 9. Set the Realm of newContext to 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 = {}; - // 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); - // 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. - // Otherwise, let global be undefined, indicating that an ordinary object should be created as the global object. + // 12. If the host requires use of an exotic object to serve as realm's global object, then 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); - - // 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 in a host-defined manner. - // Otherwise, let thisValue be undefined, indicating that realm's global this binding should be the global object. - 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%]]). + } + // 13. Else, + else { + // a. Let global be OrdinaryObjectCreate(realm.[[Intrinsics]].[[%Object.prototype%]]). // NOTE: We allocate a proper GlobalObject directly as this plain object is // turned into one via SetDefaultGlobalBindings in the spec. - global_object = heap().allocate_without_realm(*this); + global = vm.heap().allocate_without_realm(realm); } - // 2. Assert: Type(globalObj) is Object. - VERIFY(global_object); + // 14. If the host requires that the this binding in realm's global scope return an object other than the global object, then + 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. - if (this_value == nullptr) - this_value = global_object; + // 16. Set realm.[[GlobalObject]] to global. + realm->m_global_object = global; - // Non-standard - VERIFY(this_value); + // 17. Set realm.[[GlobalEnv]] to NewGlobalEnvironment(global, thisValue). + realm->m_global_environment = vm.heap().allocate_without_realm(*global, *this_value); - // 4. Set realmRec.[[GlobalObject]] to globalObj. - m_global_object = global_object; + // 18. Perform ? SetDefaultGlobalBindings(realm). + set_default_global_bindings(*realm); - // 5. Let newGlobalEnv be NewGlobalEnvironment(globalObj, thisValue). - // 6. Set realmRec.[[GlobalEnv]] to newGlobalEnv. - m_global_environment = m_global_object->heap().allocate_without_realm(*global_object, *this_value); + // 19. Create any host-defined global object properties on global. + global->initialize(*realm); - // 7. Return unused. + // 20. Return unused. + return new_context; } void Realm::visit_edges(Visitor& visitor) diff --git a/Userland/Libraries/LibJS/Runtime/Realm.h b/Userland/Libraries/LibJS/Runtime/Realm.h index 2ea3728e30d..a47eff87519 100644 --- a/Userland/Libraries/LibJS/Runtime/Realm.h +++ b/Userland/Libraries/LibJS/Runtime/Realm.h @@ -31,11 +31,8 @@ public: virtual void visit_edges(Cell::Visitor&) { } }; - static ThrowCompletionOr> create(VM&); static ThrowCompletionOr> initialize_host_defined_realm(VM&, Function create_global_object, Function create_global_this_value); - void set_global_object(Object* global_object, Object* this_value); - [[nodiscard]] Object& global_object() const { return *m_global_object; } [[nodiscard]] GlobalEnvironment& global_environment() const { return *m_global_environment; }