1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:47:36 +00:00

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()
This commit is contained in:
Matthew Olsson 2023-04-09 15:21:54 -07:00 committed by Linus Groh
parent 1f7f63ae5a
commit 48b67e41f0
2 changed files with 28 additions and 5 deletions

View file

@ -453,7 +453,7 @@ WebIDL::ExceptionOr<void> readable_stream_default_controller_enqueue(ReadableStr
} }
// 3. Let chunkSize be result.[[Value]]. // 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). // 4. Let enqueueResult be EnqueueValueWithSize(controller, chunk, chunkSize).
auto enqueue_result = enqueue_value_with_size(controller, chunk, chunk_size); 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) WebIDL::ExceptionOr<void> writable_stream_default_controller_close(WritableStreamDefaultController& controller)
{ {
// 1. Perform ! EnqueueValueWithSize(controller, close sentinel, 0). // 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). // 2. Perform ! WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller).
TRY(writable_stream_default_controller_advance_queue_if_needed(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 {}; 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 // https://streams.spec.whatwg.org/#close-sentinel
// Non-standard function that implements the "close sentinel" value. // Non-standard function that implements the "close sentinel" value.
JS::Value create_close_sentinel() JS::Value create_close_sentinel()

View file

@ -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_close(WritableStreamDefaultController&);
WebIDL::ExceptionOr<void> writable_stream_default_controller_process_write(WritableStreamDefaultController&, JS::Value chunk); 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(); JS::Value create_close_sentinel();
bool is_close_sentinel(JS::Value); 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); 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 // https://streams.spec.whatwg.org/#enqueue-value-with-size
template<typename T> 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. // 1. Assert: container has [[queue]] and [[queueTotalSize]] internal slots.
// 2. If ! IsNonNegativeNumber(size) is false, throw a RangeError exception. // 2. If ! IsNonNegativeNumber(size) is false, throw a RangeError exception.
if (size < 0.0) if (!is_non_negative_number(size_value))
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::RangeError, "Chunk has negative size"sv }; 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. // 3. If size is +∞, throw a RangeError exception.
if (size == HUGE_VAL) if (size == HUGE_VAL)