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
parent f4e2476284
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)
{
auto realm = MUST_OR_THROW_OOM(Realm::create(vm));
auto realm_global_object = vm.heap().allocate_without_realm<GlobalObject>(*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<JS::Test262::GlobalObject> 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<JS::Test262::GlobalObject>(realm);
return global_object;
},
nullptr));
vm.pop_execution_context();
return Value(global_object->$262());
}
JS_DEFINE_NATIVE_FUNCTION($262Object::detach_array_buffer)

View file

@ -16,103 +16,80 @@ namespace JS {
JS_DEFINE_ALLOCATOR(Realm);
// 9.3.1 CreateRealm ( ), https://tc39.es/ecma262/#sec-createrealm
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
// 9.3.1 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)
{
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<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<GlobalObject>(*this);
global = vm.heap().allocate_without_realm<GlobalObject>(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<GlobalEnvironment>(*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<GlobalEnvironment>(*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)

View file

@ -31,11 +31,8 @@ public:
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);
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; }