From df1e51b80b13719be5b21b6c8f8148674924020f Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Tue, 15 Apr 2025 17:10:02 -0400 Subject: [PATCH] LibWeb: Use the correct realm when initializing streams There is an open issue to clarify exactly what realm is to be used when creating promises. There are surely many more places we will need to update to use the correct realm (which will be the realm of `this`'s relevant global object). --- Libraries/LibWeb/Streams/AbstractOperations.cpp | 8 ++++++-- .../wpt-import/streams/readable-streams/global.txt | 5 ++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/Libraries/LibWeb/Streams/AbstractOperations.cpp b/Libraries/LibWeb/Streams/AbstractOperations.cpp index 6de2c390564..f872f25e6db 100644 --- a/Libraries/LibWeb/Streams/AbstractOperations.cpp +++ b/Libraries/LibWeb/Streams/AbstractOperations.cpp @@ -2017,7 +2017,9 @@ GC::Ref readable_stream_reader_generic_cancel(ReadableStreamGen // https://streams.spec.whatwg.org/#readable-stream-reader-generic-initialize void readable_stream_reader_generic_initialize(ReadableStreamReader reader, ReadableStream& stream) { - auto& realm = stream.realm(); + // FIXME: Exactly when we should effectively be using the relevant realm of `this` is to be clarified by the spec. + // For now, we do so as needed by WPT tests. See: https://github.com/whatwg/streams/issues/1213 + auto& realm = HTML::relevant_realm(reader.visit([](auto& reader) -> JS::Object& { return reader; })); // 1. Set reader.[[stream]] to stream. reader.visit([&](auto& reader) { reader->set_stream(stream); }); @@ -3574,7 +3576,9 @@ bool is_writable_stream_locked(WritableStream const& stream) // https://streams.spec.whatwg.org/#set-up-writable-stream-default-writer WebIDL::ExceptionOr set_up_writable_stream_default_writer(WritableStreamDefaultWriter& writer, WritableStream& stream) { - auto& realm = writer.realm(); + // FIXME: Exactly when we should effectively be using the relevant realm of `this` is to be clarified by the spec. + // For now, we do so as needed by WPT tests. See: https://github.com/whatwg/streams/issues/1213 + auto& realm = HTML::relevant_realm(writer); // 1. If ! IsWritableStreamLocked(stream) is true, throw a TypeError exception. if (is_writable_stream_locked(stream)) diff --git a/Tests/LibWeb/Text/expected/wpt-import/streams/readable-streams/global.txt b/Tests/LibWeb/Text/expected/wpt-import/streams/readable-streams/global.txt index ba9f729ecac..b5dde1c6d8c 100644 --- a/Tests/LibWeb/Text/expected/wpt-import/streams/readable-streams/global.txt +++ b/Tests/LibWeb/Text/expected/wpt-import/streams/readable-streams/global.txt @@ -2,11 +2,10 @@ Harness status: OK Found 9 tests -8 Pass -1 Fail +9 Pass Pass Stream objects created in expected globals Pass Cancel promise is created in same global as stream -Fail Closed Promise in correct global +Pass Closed Promise in correct global Pass Reader objects in correct global Pass Desired size can be grafted from one prototype to another Pass Closing errored stream throws object in appropriate global