LibJS: Align ShadowRealmConstructor closer with latest spec

The current shadow realm constructor implementation was based off a
merge request to the shadow realm proposal for integration into the
web platform. However, this merge request had a bug which we had
applied a workaround for by popping off the execution stack.

Since then, the spec has had an update of:

https://github.com/tc39/proposal-shadowrealm/commit/28b0cc

Which closer aligned the mainline spec to the proposed merge request.

Now, the original shadow realm proposal merge request has been closed
and a new one has been reopened with a much more minimal set of changes
that merely adds extra arguments to HostInitializeShadowRealm, which
this commit aligns with.
This commit is contained in:
Shannon Booth 2024-11-03 21:29:30 +13:00 committed by Andrew Kaster
commit 424a0cda93
Notes: github-actions[bot] 2024-11-05 00:16:16 +00:00
3 changed files with 29 additions and 31 deletions

View file

@ -39,47 +39,33 @@ ThrowCompletionOr<Value> ShadowRealmConstructor::call()
return vm.throw_completion<TypeError>(ErrorType::ConstructorWithoutNew, vm.names.ShadowRealm); return vm.throw_completion<TypeError>(ErrorType::ConstructorWithoutNew, vm.names.ShadowRealm);
} }
// https://github.com/tc39/proposal-shadowrealm/pull/392
static ThrowCompletionOr<NonnullGCPtr<Object>> initialize_shadow_realm(ShadowRealm& object)
{
auto& vm = object.vm();
// 1. Let context be the running Javascript execution context.
auto& context = vm.running_execution_context();
// 2. Let realm be the Realm of context.
auto& realm = *context.realm;
// 3. Return ? HostInitializeShadowRealm(realm, context, O).
if (vm.host_initialize_shadow_realm)
return TRY(vm.host_initialize_shadow_realm(realm, context.copy(), object));
// AD-HOC: Fallback for when there is no host defined implementation.
vm.pop_execution_context();
object.set_shadow_realm(*vm.running_execution_context().realm);
return Object::create(realm, realm.intrinsics().object_prototype());
}
// 3.2.1 ShadowRealm ( ), https://tc39.es/proposal-shadowrealm/#sec-shadowrealm // 3.2.1 ShadowRealm ( ), https://tc39.es/proposal-shadowrealm/#sec-shadowrealm
// https://github.com/tc39/proposal-shadowrealm/pull/392 // https://github.com/tc39/proposal-shadowrealm/pull/410
ThrowCompletionOr<NonnullGCPtr<Object>> ShadowRealmConstructor::construct(FunctionObject& new_target) ThrowCompletionOr<NonnullGCPtr<Object>> ShadowRealmConstructor::construct(FunctionObject& new_target)
{ {
auto& vm = this->vm(); auto& vm = this->vm();
// 2. Let O be ? OrdinaryCreateFromConstructor(NewTarget, "%ShadowRealm.prototype%", « [[ShadowRealm]], [[ExecutionContext]] »). // 2. Let O be ? OrdinaryCreateFromConstructor(NewTarget, "%ShadowRealm.prototype%", « [[ShadowRealm]] »).
auto object = TRY(ordinary_create_from_constructor<ShadowRealm>(vm, new_target, &Intrinsics::shadow_realm_prototype)); auto object = TRY(ordinary_create_from_constructor<ShadowRealm>(vm, new_target, &Intrinsics::shadow_realm_prototype));
// 3. Perform ? InitializeHostDefinedRealm(). The customizations for creating the global object are to return ? InitializeShadowRealm(). // 3. Let callerContext be the running execution context.
// 4. Let context be the running Javascript execution context. // 4. Perform ? InitializeHostDefinedRealm().
auto context = TRY(Realm::initialize_host_defined_realm(vm, [&object](JS::Realm&) -> JS::Object* { return MUST(initialize_shadow_realm(object)); }, nullptr)); // 5. Let innerContext be the running execution context.
auto inner_context = TRY(Realm::initialize_host_defined_realm(vm, nullptr, nullptr));
// 5. Let realmRec be the Realm of context. // 6. Remove innerContext from the execution context stack and restore callerContext as the running execution context.
auto& realm_record = *context->realm; vm.pop_execution_context();
// 6. Set O.[[ShadowRealm]] to realmRec. // 7. Let realmRec be the Realm of innerContext.
auto& realm_record = *inner_context->realm;
// 8. Set O.[[ShadowRealm]] to realmRec.
object->set_shadow_realm(realm_record); object->set_shadow_realm(realm_record);
// 7. Return O. // 9. Perform ? HostInitializeShadowRealm(realmRec).
TRY(vm.host_initialize_shadow_realm(realm_record, move(inner_context), object));
// 10. Return O.
return object; return object;
} }

View file

@ -167,6 +167,18 @@ VM::VM(OwnPtr<CustomData> custom_data, ErrorMessages error_messages)
return HandledByHost::Handled; return HandledByHost::Handled;
}; };
// 3.6.1 HostInitializeShadowRealm ( realm ), https://tc39.es/proposal-shadowrealm/#sec-hostinitializeshadowrealm
// https://github.com/tc39/proposal-shadowrealm/pull/410
host_initialize_shadow_realm = [](Realm&, NonnullOwnPtr<ExecutionContext>, ShadowRealm&) -> ThrowCompletionOr<void> {
// The host-defined abstract operation HostInitializeShadowRealm takes argument realm (a Realm Record) and returns
// either a normal completion containing unused or a throw completion. It is used to inform the host of any newly
// created realms from the ShadowRealm constructor. The idea of this hook is to initialize host data structures
// related to the ShadowRealm, e.g., for module loading.
//
// The host may use this hook to add properties to the ShadowRealm's global object. Those properties must be configurable.
return {};
};
// AD-HOC: Inform the host that we received a date string we were unable to parse. // AD-HOC: Inform the host that we received a date string we were unable to parse.
host_unrecognized_date_string = [](StringView) { host_unrecognized_date_string = [](StringView) {
}; };

View file

@ -285,7 +285,7 @@ public:
Function<ThrowCompletionOr<void>(Object&)> host_ensure_can_add_private_element; Function<ThrowCompletionOr<void>(Object&)> host_ensure_can_add_private_element;
Function<ThrowCompletionOr<HandledByHost>(ArrayBuffer&, size_t)> host_resize_array_buffer; Function<ThrowCompletionOr<HandledByHost>(ArrayBuffer&, size_t)> host_resize_array_buffer;
Function<void(StringView)> host_unrecognized_date_string; Function<void(StringView)> host_unrecognized_date_string;
Function<ThrowCompletionOr<NonnullGCPtr<ShadowRealm>>(Realm&, NonnullOwnPtr<ExecutionContext>, ShadowRealm&)> host_initialize_shadow_realm; Function<ThrowCompletionOr<void>(Realm&, NonnullOwnPtr<ExecutionContext>, ShadowRealm&)> host_initialize_shadow_realm;
Vector<StackTraceElement> stack_trace() const; Vector<StackTraceElement> stack_trace() const;