From 028e3f0f7662307f907cfdacf0077f90976754ff Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Sat, 2 Nov 2024 14:08:51 +1300 Subject: [PATCH] LibJS: Also apply AD-HOC execution context fixup on host defined path As described in the comments, this was a bug in the shadow realm spec. For now, just apply a fixup to the execution context so that it works as intended. Once https://github.com/tc39/proposal-shadowrealm/pull/392 is updated to match these changes to the spec we should be able to implement this in a better way :^) --- .../LibJS/Runtime/ShadowRealmConstructor.cpp | 26 +++++++++++++++---- Userland/Libraries/LibJS/Runtime/VM.h | 2 +- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/ShadowRealmConstructor.cpp b/Userland/Libraries/LibJS/Runtime/ShadowRealmConstructor.cpp index 12ddc563d6e2a..45271e3a9c477 100644 --- a/Userland/Libraries/LibJS/Runtime/ShadowRealmConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/ShadowRealmConstructor.cpp @@ -39,6 +39,14 @@ ThrowCompletionOr ShadowRealmConstructor::call() return vm.throw_completion(ErrorType::ConstructorWithoutNew, vm.names.ShadowRealm); } +// https://github.com/tc39/proposal-shadowrealm/pull/392 +// The default implementation of HostInitializeShadowRealm performs the following steps when called: +static NonnullGCPtr default_create_global_object(JS::Realm& realm) +{ + // Return NormalCompletion(OrdinaryObjectCreate(realm.[[Intrinsics]].[[%Object.prototype%]])). + return Object::create(realm, realm.intrinsics().object_prototype()); +} + // https://github.com/tc39/proposal-shadowrealm/pull/392 static ThrowCompletionOr> initialize_shadow_realm(ShadowRealm& object) { @@ -50,15 +58,23 @@ static ThrowCompletionOr> initialize_shadow_realm(ShadowRea // 2. Let realm be the Realm of context. auto& realm = *context.realm; + // FIXME: Spec bug. InitializeHostDefinedRealm is pushing on an execution context and active realm that we need to undo here. + // + // This relates to spec changes: + // - https://github.com/tc39/proposal-shadowrealm/commit/76bb92682c448b8caf5cb7012c5f826be121880a + // - https://github.com/tc39/proposal-shadowrealm/commit/28b0cc3f749ddcbb05290f4481bddf5b8253a4c7 + // + // The proposed change to integrate with the Web Platform: https://github.com/tc39/proposal-shadowrealm/pull/392 + // + // Needs to be adjusted to align with these updates. + vm.pop_execution_context(); + object.set_execution_context(vm.running_execution_context().copy()); + // 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_execution_context(vm.running_execution_context().copy()); - object.set_shadow_realm(*vm.running_execution_context().realm); - return Object::create(realm, realm.intrinsics().object_prototype()); + return default_create_global_object(realm); } // 3.2.1 ShadowRealm ( ), https://tc39.es/proposal-shadowrealm/#sec-shadowrealm diff --git a/Userland/Libraries/LibJS/Runtime/VM.h b/Userland/Libraries/LibJS/Runtime/VM.h index 373b67e644939..3a66ee9de4d4a 100644 --- a/Userland/Libraries/LibJS/Runtime/VM.h +++ b/Userland/Libraries/LibJS/Runtime/VM.h @@ -280,7 +280,7 @@ class VM : public RefCounted { Function(Object&)> host_ensure_can_add_private_element; Function(ArrayBuffer&, size_t)> host_resize_array_buffer; Function host_unrecognized_date_string; - Function>(Realm&, NonnullOwnPtr, ShadowRealm&)> host_initialize_shadow_realm; + Function>(Realm&, NonnullOwnPtr, ShadowRealm&)> host_initialize_shadow_realm; Vector stack_trace() const;