Commit graph

61 commits

Author SHA1 Message Date
Timothy Flynn
629cd3c42a LibWeb: Sort remaining stream AOs (mostly) in spec order
The remaining AOs can stay where they are. This patch just sorts them in
spec order to match the other AO files. Section 8.1 is last, however, as
these are all templates and need the declarations of other AOs above.
2025-04-18 06:55:40 -04:00
Timothy Flynn
3e8c6dbaff LibWeb: Move TransformStream AOs into their own file
The main streams AO file has gotten very large, and is a bit difficult
to navigate. In an effort to improve DX, this migrates TransformStream
AOs to their own file.
2025-04-18 06:55:40 -04:00
Timothy Flynn
26c01f0957 LibWeb: Move WritableStream AOs into their own file
The main streams AO file has gotten very large, and is a bit difficult
to navigate. In an effort to improve DX, this migrates WritableStream
AOs to their own file.
2025-04-18 06:55:40 -04:00
Timothy Flynn
a9ddd427cb LibWeb: Move ReadableStream AOs into their own file
The main streams AO file has gotten very large, and is a bit difficult
to navigate. In an effort to improve DX, this migrates ReadableStream
AOs to their own file. And the helper classes used for the tee and pipe-
to operations are also in their own files.
2025-04-18 06:55:40 -04:00
Timothy Flynn
c35ef36293 LibWeb: Fix typos in ReadableStream AO names 2025-04-18 06:55:40 -04:00
Timothy Flynn
955f6fc9cb LibWeb: Fulfill TransformStream promises with the correct "reason"
Some checks are pending
CI / Lagom (arm64, Sanitizer_CI, false, macos-15, macOS, Clang) (push) Waiting to run
CI / Lagom (x86_64, Fuzzers_CI, false, ubuntu-24.04, Linux, Clang) (push) Waiting to run
CI / Lagom (x86_64, Sanitizer_CI, false, ubuntu-24.04, Linux, GNU) (push) Waiting to run
CI / Lagom (x86_64, Sanitizer_CI, true, ubuntu-24.04, Linux, Clang) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (arm64, macos-15, macOS, macOS-universal2) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (x86_64, ubuntu-24.04, Linux, Linux-x86_64) (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Label PRs with merge conflicts / auto-labeler (push) Waiting to run
Push notes / build (push) Waiting to run
We need to use the reason provided to the abort algorithm, not the one
from the resolved cancel promise (which will be undefined).
2025-04-16 13:15:33 -04:00
Timothy Flynn
3fdad8fe22 LibWeb: Update spec steps for closing TransformStreams
This was actually an older change to the Streams spec that we missed
when we implemented TransformStreams. This fixes a crash in the imported
WPT tests.

See: https://github.com/whatwg/streams/commit/007d729
2025-04-16 11:39:25 -04:00
Timothy Flynn
6dd2a4c945 LibWeb: Do not create a RootVector to invoke IDL callbacks
These callbacks are evaluated synchronously via JS::Call. We do not need
to construct an expensive RootVector container just to immediately
invoke the callbacks.

Stylistically, this also helps indicate where the actual arguments start
at the call sites, by wrapping the arguments in braces.
2025-04-16 07:32:02 -04:00
Timothy Flynn
b1a189acfa LibWeb: Use WebIDL::invoke_promise_callback in Streams where appropriate
Some checks are pending
CI / Lagom (x86_64, Fuzzers_CI, false, ubuntu-24.04, Linux, Clang) (push) Waiting to run
CI / Lagom (arm64, Sanitizer_CI, false, macos-15, macOS, Clang) (push) Waiting to run
CI / Lagom (x86_64, Sanitizer_CI, false, ubuntu-24.04, Linux, GNU) (push) Waiting to run
CI / Lagom (x86_64, Sanitizer_CI, true, ubuntu-24.04, Linux, Clang) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (arm64, macos-15, macOS, macOS-universal2) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (x86_64, ubuntu-24.04, Linux, Linux-x86_64) (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Label PRs with merge conflicts / auto-labeler (push) Waiting to run
Push notes / build (push) Waiting to run
This avoids an issue where rejected JS::Promise types were converted to
a resolved WebIDL::Promise type.
2025-04-15 20:33:30 -04:00
Timothy Flynn
df1e51b80b 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).
2025-04-15 20:33:11 -04:00
Timothy Flynn
cef7821f53 LibWeb: Unindent looping over a BYOB reader's pull-into requests
This was causing us to hang on some BYOB tests.
2025-04-14 19:56:58 -04:00
Timothy Flynn
f22fa827ec LibWeb: Align some BYOB stream AOs with spec text/reality
A couple of minor things I came across while debugging BYOB streams.
Adjust some spec text to match the latest spec, and use GC::Ref instead
of a raw pointer where applicable.
2025-04-14 19:56:58 -04:00
Timothy Flynn
d5d9d999ae LibWeb: Implement ReadableStream's async iterator 2025-04-14 17:43:11 -04:00
Timothy Flynn
988df9cc8c LibWeb: Perform writes during ReadableStreamPipeTo asynchronously
I don't quite see what spec text requires this, but it is explicitly
checked by WPT. We used to pass this test, but that regressed after
commit 3c6010c663.
2025-04-14 16:56:15 -04:00
Kenneth Myhra
b285202951 LibWeb: Widen assertion to avoid accidentally writing to a closed stream
This widens the assertion from only checking if the WritableStream's
state is Errored or Erroring to asserting that the WritableStream is not
in a Writable state.
2025-04-14 14:50:54 -04:00
Timothy Flynn
3c6010c663 LibWeb: Implement ReadableStreamPipeTo reads with less observability
The spec states:

    Public API must not be used: while reading or writing, or performing
    any of the operations below, the JavaScript-modifiable reader,
    writer, and stream APIs (i.e. methods on the appropriate prototypes)
    must not be used. Instead, the streams must be manipulated directly.

This migrates the default request request we were using to a custom read
request which does not involve extra promises.

I'm not sure about an analogous change with the way we write chunks to
the receiving end. There isn't a "WriteRequest" utility to be used here,
and no matter what AO we use, promises will be involved. Our current
implementation at least does not seem to affect any tests.
2025-04-14 07:38:11 -04:00
Timothy Flynn
eb0a51faf0 LibWeb: Implement ReadableStreamPipeTo according to spec
Our existing implementation of stream piping was extremely ad-hoc. It
did nothing to handle closed/errored streams, and did not read from or
write to streams in a way required by the spec.

This new implementation uses a custom JS::Cell to drive the read/write
loop.
2025-04-11 12:10:46 -04:00
Timothy Flynn
f268f24dd5 LibWeb: Explicitly rethrow exceptions from writable stream start
This is an editorial change in the Streams spec. See:
https://github.com/whatwg/streams/commit/95a5adf
2025-04-11 12:10:46 -04:00
Timothy Flynn
e9a7694cdb LibWeb: Prefer react_to_promise over upon_fulfillment + upon_rejection
While debugging a spec-compliant implementation of ReadableStreamPipeTo,
I spent a lot of time inspecting promise internals. This is much less
noisy if we halve the number of temporary promises.
2025-04-11 12:10:46 -04:00
Timothy Flynn
3033929bb6 LibWeb: Pass abort signal as its concrete type to ReadableStreamPipeTo
There's no real need to wrap it in a JS::Value just to unrwap it again.
2025-04-11 12:10:46 -04:00
Timothy Flynn
ab43c3be23 LibWeb: Store WritableStream's strategy high water mark as a double
It is received from user JS as a double and is only used as a double in
all subsequent calculations. This bug would cause UBSAN errors in an
upcoming imported WPT test, which passes Infinity as the HWM.

Note there is an equivalent HWM for ReadableStream, which already stores
the value as a double.
2025-04-11 12:10:46 -04:00
Timothy Flynn
1d6e1637cc LibWeb: Implement an AO to close writable streams with error propagation 2025-04-11 12:10:46 -04:00
Andreas Kling
3cf50539ec LibJS: Make Value() default-construct the undefined value
The special empty value (that we use for array holes, Optional<Value>
when empty and a few other other placeholder/sentinel tasks) still
exists, but you now create one via JS::js_special_empty_value() and
check for it with Value::is_special_empty_value().

The main idea here is to make it very unlikely to accidentally create an
unexpected special empty value.
2025-04-05 11:20:26 +02:00
Andreas Kling
de424d6879 LibJS: Make Completion.[[Value]] non-optional
Instead, just use js_undefined() whenever the [[Value]] field is unused.
This avoids a whole bunch of presence checks.
2025-04-05 11:20:26 +02:00
Andreas Kling
53da8893ac LibJS: Replace PropertyKey(char[]) with PropertyKey(FlyString)
...and deal with the fallout.
2025-03-24 22:27:17 +00:00
Kenneth Myhra
21989ef543 LibWeb: Align ReadableByteStreamControllerEnqueue
Align ReadableByteStreamControllerEnqueue with current spec steps.
2025-02-10 13:25:51 +00:00
Kenneth Myhra
4699bf24dc LibWeb: Align ReadableByteStreamControllerRespondInClosedState
Align ReadableByteStreamControllerRespondInClosedState with current spec
steps.
2025-02-10 13:25:51 +00:00
Kenneth Myhra
4b917d366c LibWeb: Align ReadableByteStream(...)FillPullIntoDescriptorFromQueue
Align ReadableByteStreamControllerFillPullIntoDescriptorFromQueue with
current spec steps.
2025-02-10 13:25:51 +00:00
Luke Wilde
c14d5f27f9 LibWeb: Use DefaultReader request in read_all_chunks instead of ReadLoop
ReadLoop requests require the chunks to be Uint8Array objects, however,
TextEncoderStream requires a String (Convertible) value. This is fixed
by implementing read_all_chunks as a loop of DefaultReader requests
instead, which is an identity transformation. This should be okay to
do, as stream chunk steps expect a JS::Value, and convert it to the
type they want.
2025-02-07 11:04:46 -05:00
Timothy Flynn
85b424464a AK+Everywhere: Rename verify_cast to as
Follow-up to fc20e61e72.
2025-01-21 11:34:06 -05:00
Shannon Booth
add8bf4790 LibWeb/Streams: Ensure pending pull into's objects are visited by GC
While PendingPullIntos are typically visted by their controller there
were some cases that we were removing those references from the
controller and storing them in a SinglyLinkedList on the stack which
is not safe.

Instead, make PendingPullInto a GC::Cell type, which also allows us
to remove an awkward copy of the struct where the underlying reference
was previously being destroyed.
2025-01-18 10:26:40 +01:00
Shannon Booth
ffda698d3a LibWeb/Streams: Actually implement the piped through steps
This mistakenly implemented the 'piped to' operation on ReadableStream.
No functional difference as the caller was doing the extra work already
of 'piped through' vs 'piped to'.
2024-12-27 06:56:38 -08:00
Shannon Booth
a28961a92d LibWeb/Streams: Fully implement BYOB operations for ReadableStream
As far as I can tell there is no change in WPT from this implementation.
But these steps should be more effecient than using the non BYOB steps
as we can make direct use of the provided buffer to the byte stream.
2024-12-27 06:56:38 -08:00
Shannon Booth
5d8d5375a0 LibWeb/Streams: Fix spec link for RBS controller release steps 2024-12-27 06:56:38 -08:00
Shannon Booth
5f2b75852f LibWeb: Add an 'enqueue' helper method on TransformStream 2024-12-25 12:00:54 +01:00
Shannon Booth
9ce0c5914b LibWeb: Add a 'get a reader' helper method on ReadableStream 2024-12-25 12:00:54 +01:00
Shannon Booth
da408cb09a LibWeb: Add a 'piped through' helper method on ReadableStream
This reads a bit nicer, and follows the streams spec pattern on
performing operations on a stream outside of the streams spec.
2024-12-25 12:00:54 +01:00
Shannon Booth
99073c0561 LibWeb: Move ad hoc CallbackType helper method to CallbackType header
Abstract operations of a stream does not seem like the correct home for
this function.
2024-12-11 15:11:21 +01:00
Shannon Booth
93f258deb7 LibWeb/Streams: Do not expose some non-standard functions in header
These are non-standard and only needed internally as implementation
details in the implementation of AbstractOperations, so let's keep them
at a file-local level.
2024-12-11 15:11:21 +01:00
Shannon Booth
19bbfb023a LibWeb/Streams: Move "set up transform stream" to TransformStream
This is not marked as an AO in the spec, and is a publically exported
API exposed on TransformStream.
2024-12-11 15:11:21 +01:00
Shannon Booth
3f572d9ab7 LibWeb/Streams: Move ReadableStream functions out of AbstractOperations
These are not defined in the abstract operations section of the spec and
are the publically exported Stream APIs exposed on ReadableStream.
2024-12-11 15:11:21 +01:00
Shannon Booth
c6d0f87bb7 LibWeb/Streams: Put algorithm definitions in a separate header file
To solve a future cyclic dependency problem.
2024-12-11 15:11:21 +01:00
Jelle Raaijmakers
1514197e36 LibWeb: Remove dom_ from dom_exception_to_throw_completion
We're not converting `WebIDL::DOMException`, but `WebIDL::Exception`
instead.
2024-12-09 20:02:51 -07:00
Timothy Flynn
4b4b12165e LibWeb: Release acquired readers after piping through a stream
This very partially implements the spec's "finalize" steps for piping
streams.
2024-12-09 20:02:51 -07:00
Timothy Flynn
383d303b79 LibWeb: Enable callbacks in execution contexts when teeing streams
This will be needed once fetched response bodies are read using streams.
2024-12-09 20:02:51 -07:00
Kenneth Myhra
861f6e3965 LibWeb: Commit pull-into descriptors after filling from queue
These changes make sure that we postpone calls to
ReadableByteStreamControllerCommitPullIntoDescriptor until after all
pull-into descriptors have been filled up by
ReadableByteStreamControllerProcessPullIntoDescriptorsUsingQueue.

Also pulls in the WPT test which was created in relation to this spec
change. The test verifies that a patched then() will see a null
byobRequest.
2024-12-05 13:03:12 -05:00
Kenneth Myhra
5a9f602fef LibWeb: Add AO CanCopyDataBlockBytes() 2024-12-05 13:03:12 -05:00
Shannon Booth
1647893fc8 LibWeb/Streams: Update comments for resolved spec bug
Our bug report has been resolved in the same way we had already
applied a fix (just with some asserts added) with:

https://github.com/whatwg/streams/commit/86d07e
2024-11-27 10:58:45 +01:00
Kenneth Myhra
4569b997a6 LibWeb: Add missing spec links for pipe_through() and pipe_to() 2024-11-24 11:11:44 +01:00
Shannon Booth
9724c67be2 LibWeb: Default initialize StructuredDeserialize memory argument
This is optional in the spec, so let's make it actually optional at the
call site.
2024-11-23 16:43:55 +01:00