Truly independent web browser
LibWeb: Perform microtask checkpoints between message listeners
Andreas Klinge8d3ac6558b6
diff --git a/Libraries/LibWeb/HTML/MessagePort.cpp b/Libraries/LibWeb/HTML/MessagePort.cppindex 4c726aff547..7e6ce5bc99e 100644--- a/Libraries/LibWeb/HTML/MessagePort.cpp+++ b/Libraries/LibWeb/HTML/MessagePort.cpp@@ -462,10 +462,13 @@ void MessagePort::post_message_task_steps(SerializedTransferRecord& serialize_wi// 2. Let targetRealm be finalTargetPort's relevant realm.auto& target_realm = HTML::relevant_realm(final_target_port->relevant_global_object());- TemporaryExecutionContext context { target_realm };-// 3. Let deserializeRecord be StructuredDeserializeWithTransfer(serializeWithTransferResult, targetRealm).- auto deserialize_record_or_error = structured_deserialize_with_transfer(serialize_with_transfer_result, target_realm);+ // NB: The execution context must not stay on the stack while the event is dispatched, so that a microtask+ // checkpoint can be performed after each listener.+ auto deserialize_record_or_error = [&] {+ TemporaryExecutionContext context { target_realm };+ return structured_deserialize_with_transfer(serialize_with_transfer_result, target_realm);+ }();if (deserialize_record_or_error.is_error()) {// If this throws an exception, catch it, fire an event named messageerror at finalTargetPort, using MessageEvent, and then return.auto exception = deserialize_record_or_error.release_error();diff --git a/Libraries/LibWeb/HTML/Window.cpp b/Libraries/LibWeb/HTML/Window.cppindex 4e0311ca623..f61c45b7386 100644--- a/Libraries/LibWeb/HTML/Window.cpp+++ b/Libraries/LibWeb/HTML/Window.cpp@@ -1682,10 +1682,13 @@ void Window::deliver_posted_message(SerializedTransferRecord serialize_with_tranif (source)source_for_event = GC::Ref { *source };- TemporaryExecutionContext temporary_execution_context { target_realm, TemporaryExecutionContext::CallbacksEnabled::Yes };-// 4. Let deserializeRecord be StructuredDeserializeWithTransfer(serializeWithTransferResult, targetRealm).- auto deserialize_record_or_error = structured_deserialize_with_transfer(serialize_with_transfer_result, target_realm);+ // NB: The execution context must not stay on the stack while the event is dispatched, so that a microtask+ // checkpoint can be performed after each listener.+ auto deserialize_record_or_error = [&] {+ TemporaryExecutionContext temporary_execution_context { target_realm, TemporaryExecutionContext::CallbacksEnabled::Yes };+ return structured_deserialize_with_transfer(serialize_with_transfer_result, target_realm);+ }();// If this throws an exception, catch it, fire an event named messageerror at targetWindow, using MessageEvent,// with its origin initialized to origin and the source attribute initialized to source, and then return.diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/checkpoint-after-window-onerror-module.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/checkpoint-after-window-onerror-module.txtindex f60f3a3aa45..c8521c75be6 100644--- a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/checkpoint-after-window-onerror-module.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/checkpoint-after-window-onerror-module.txt@@ -2,7 +2,6 @@ Harness status: OKFound 2 tests-1 Pass-1 Fail+2 PassPass Promise resolved during #report-the-error-Fail Promise resolved during event handlers other than error\ No newline at end of file+Pass Promise resolved during event handlers other than error\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/checkpoint-after-window-onerror.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/checkpoint-after-window-onerror.txtindex f60f3a3aa45..c8521c75be6 100644--- a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/checkpoint-after-window-onerror.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/checkpoint-after-window-onerror.txt@@ -2,7 +2,6 @@ Harness status: OKFound 2 tests-1 Pass-1 Fail+2 PassPass Promise resolved during #report-the-error-Fail Promise resolved during event handlers other than error\ No newline at end of file+Pass Promise resolved during event handlers other than error\ No newline at end of file