diff options
author | Matthew Olsson <matthewcolsson@gmail.com> | 2023-04-09 15:21:54 -0700 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2023-04-10 00:45:03 +0200 |
commit | 48b67e41f068262fbe387bfbc7f5f52b398c8008 (patch) | |
tree | bca7fb3c33b7441b0e08799b786bb549007c8760 /Userland | |
parent | 1f7f63ae5a3586877d92300d1e3291c3f9c64aad (diff) | |
download | serenity-48b67e41f068262fbe387bfbc7f5f52b398c8008.zip |
LibWeb: Accept a JS::Value for size in enqueue_value instead of a double
This matches what the spec does, and consolidates all of the size-
related errors in one spot instead of distributing them throughout the
various uses of enqueue_value_with_size()
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Libraries/LibWeb/Streams/AbstractOperations.cpp | 23 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Streams/AbstractOperations.h | 10 |
2 files changed, 28 insertions, 5 deletions
diff --git a/Userland/Libraries/LibWeb/Streams/AbstractOperations.cpp b/Userland/Libraries/LibWeb/Streams/AbstractOperations.cpp index 3d93baa7ae..5e7f424f90 100644 --- a/Userland/Libraries/LibWeb/Streams/AbstractOperations.cpp +++ b/Userland/Libraries/LibWeb/Streams/AbstractOperations.cpp @@ -453,7 +453,7 @@ WebIDL::ExceptionOr<void> readable_stream_default_controller_enqueue(ReadableStr } // 3. Let chunkSize be result.[[Value]]. - auto chunk_size = TRY(result.release_value().release_value().to_double(vm)); + auto chunk_size = result.release_value().release_value(); // 4. Let enqueueResult be EnqueueValueWithSize(controller, chunk, chunkSize). auto enqueue_result = enqueue_value_with_size(controller, chunk, chunk_size); @@ -1489,7 +1489,7 @@ void writable_stream_default_controller_clear_algorithms(WritableStreamDefaultCo WebIDL::ExceptionOr<void> writable_stream_default_controller_close(WritableStreamDefaultController& controller) { // 1. Perform ! EnqueueValueWithSize(controller, close sentinel, 0). - TRY(enqueue_value_with_size(controller, create_close_sentinel(), 0.0)); + TRY(enqueue_value_with_size(controller, create_close_sentinel(), JS::Value(0.0))); // 2. Perform ! WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller). TRY(writable_stream_default_controller_advance_queue_if_needed(controller)); @@ -1626,6 +1626,25 @@ WebIDL::ExceptionOr<void> writable_stream_default_controller_process_write(Writa return {}; } +// https://streams.spec.whatwg.org/#is-non-negative-number +bool is_non_negative_number(JS::Value value) +{ + // 1. If Type(v) is not Number, return false. + if (!value.is_number()) + return false; + + // 2. If v is NaN, return false. + if (value.is_nan()) + return false; + + // 3. If v < 0, return false. + if (value.as_double() < 0.0) + return false; + + // 4. Return true. + return true; +} + // https://streams.spec.whatwg.org/#close-sentinel // Non-standard function that implements the "close sentinel" value. JS::Value create_close_sentinel() diff --git a/Userland/Libraries/LibWeb/Streams/AbstractOperations.h b/Userland/Libraries/LibWeb/Streams/AbstractOperations.h index f6f0466620..c10fdca560 100644 --- a/Userland/Libraries/LibWeb/Streams/AbstractOperations.h +++ b/Userland/Libraries/LibWeb/Streams/AbstractOperations.h @@ -89,6 +89,8 @@ double writable_stream_default_controller_get_desired_size(WritableStreamDefault WebIDL::ExceptionOr<void> writable_stream_default_controller_process_close(WritableStreamDefaultController&); WebIDL::ExceptionOr<void> writable_stream_default_controller_process_write(WritableStreamDefaultController&, JS::Value chunk); +bool is_non_negative_number(JS::Value); + JS::Value create_close_sentinel(); bool is_close_sentinel(JS::Value); JS::ThrowCompletionOr<JS::Handle<WebIDL::CallbackType>> property_to_callback(JS::VM& vm, JS::Value value, JS::PropertyKey const& property_key); @@ -125,13 +127,15 @@ JS::Value dequeue_value(T& container) // https://streams.spec.whatwg.org/#enqueue-value-with-size template<typename T> -WebIDL::ExceptionOr<void> enqueue_value_with_size(T& container, JS::Value value, double size) +WebIDL::ExceptionOr<void> enqueue_value_with_size(T& container, JS::Value value, JS::Value size_value) { // 1. Assert: container has [[queue]] and [[queueTotalSize]] internal slots. // 2. If ! IsNonNegativeNumber(size) is false, throw a RangeError exception. - if (size < 0.0) - return WebIDL::SimpleException { WebIDL::SimpleExceptionType::RangeError, "Chunk has negative size"sv }; + if (!is_non_negative_number(size_value)) + return WebIDL::SimpleException { WebIDL::SimpleExceptionType::RangeError, "Chunk has non-positive size"sv }; + + auto size = size_value.as_double(); // 3. If size is +∞, throw a RangeError exception. if (size == HUGE_VAL) |