1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 07:27:45 +00:00

LibJS: Make Heap::allocate<T>() infallible

Stop worrying about tiny OOMs. Work towards #20449.

While going through these, I also changed the function signature in many
places where returning ThrowCompletionOr<T> is no longer necessary.
This commit is contained in:
Andreas Kling 2023-08-13 13:05:26 +02:00
parent 980e7164fe
commit 72c9f56c66
337 changed files with 1229 additions and 1251 deletions

View file

@ -41,7 +41,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<ReadableStreamDefaultReader>> acquire_reada
auto& realm = stream.realm();
// 1. Let reader be a new ReadableStreamDefaultReader.
auto reader = TRY(realm.heap().allocate<ReadableStreamDefaultReader>(realm, realm));
auto reader = realm.heap().allocate<ReadableStreamDefaultReader>(realm, realm);
// 2. Perform ? SetUpReadableStreamDefaultReader(reader, stream).
TRY(set_up_readable_stream_default_reader(reader, stream));
@ -56,7 +56,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<ReadableStreamBYOBReader>> acquire_readable
auto& realm = stream.realm();
// 1. Let reader be a new ReadableStreamBYOBReader.
auto reader = TRY(realm.heap().allocate<ReadableStreamBYOBReader>(realm, realm));
auto reader = realm.heap().allocate<ReadableStreamBYOBReader>(realm, realm);
// 2. Perform ? SetUpReadableStreamBYOBReader(reader, stream).
TRY(set_up_readable_stream_byob_reader(reader, stream));
@ -834,7 +834,7 @@ WebIDL::ExceptionOr<void> set_up_readable_stream_default_controller_from_underly
auto& realm = stream.realm();
// 1. Let controller be a new ReadableStreamDefaultController.
auto controller = MUST_OR_THROW_OOM(stream.heap().allocate<ReadableStreamDefaultController>(realm, realm));
auto controller = stream.heap().allocate<ReadableStreamDefaultController>(realm, realm);
// 2. Let startAlgorithm be an algorithm that returns undefined.
StartAlgorithm start_algorithm = [] { return JS::js_undefined(); };
@ -1172,13 +1172,13 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<ReadableStream>> create_readable_stream(JS:
VERIFY(is_non_negative_number(JS::Value { *high_water_mark }));
// 4. Let stream be a new ReadableStream.
auto stream = MUST_OR_THROW_OOM(realm.heap().allocate<ReadableStream>(realm, realm));
auto stream = realm.heap().allocate<ReadableStream>(realm, realm);
// 5. Perform ! InitializeReadableStream(stream).
initialize_readable_stream(*stream);
// 6. Let controller be a new ReadableStreamDefaultController.
auto controller = MUST_OR_THROW_OOM(realm.heap().allocate<ReadableStreamDefaultController>(realm, realm));
auto controller = realm.heap().allocate<ReadableStreamDefaultController>(realm, realm);
// 7. Perform ? SetUpReadableStreamDefaultController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, sizeAlgorithm).
TRY(set_up_readable_stream_default_controller(*stream, *controller, move(start_algorithm), move(pull_algorithm), move(cancel_algorithm), *high_water_mark, move(*size_algorithm)));
@ -1194,13 +1194,13 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<WritableStream>> create_writable_stream(JS:
VERIFY(is_non_negative_number(JS::Value { high_water_mark }));
// 2. Let stream be a new WritableStream.
auto stream = MUST_OR_THROW_OOM(realm.heap().allocate<WritableStream>(realm, realm));
auto stream = realm.heap().allocate<WritableStream>(realm, realm);
// 3. Perform ! InitializeWritableStream(stream).
initialize_writable_stream(*stream);
// 4. Let controller be a new WritableStreamDefaultController.
auto controller = MUST_OR_THROW_OOM(realm.heap().allocate<WritableStreamDefaultController>(realm, realm));
auto controller = realm.heap().allocate<WritableStreamDefaultController>(realm, realm);
// 5. Perform ? SetUpWritableStreamDefaultController(stream, controller, startAlgorithm, writeAlgorithm, closeAlgorithm, abortAlgorithm, highWaterMark, sizeAlgorithm).
TRY(set_up_writable_stream_default_controller(*stream, *controller, move(start_algorithm), move(write_algorithm), move(close_algorithm), move(abort_algorithm), high_water_mark, move(size_algorithm)));
@ -1252,7 +1252,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<WritableStreamDefaultWriter>> acquire_writa
auto& realm = stream.realm();
// 1. Let writer be a new WritableStreamDefaultWriter.
auto writer = MUST_OR_THROW_OOM(stream.heap().allocate<WritableStreamDefaultWriter>(realm, realm));
auto writer = stream.heap().allocate<WritableStreamDefaultWriter>(realm, realm);
// 2. Perform ? SetUpWritableStreamDefaultWriter(writer, stream).
TRY(set_up_writable_stream_default_writer(*writer, stream));
@ -1730,7 +1730,7 @@ WebIDL::ExceptionOr<void> set_up_readable_stream_controller_with_byte_reading_su
// 4. Perform ! InitializeReadableStream(stream).
// 5. Let controller be a new ReadableByteStreamController.
auto controller = MUST_OR_THROW_OOM(stream.heap().allocate<ReadableByteStreamController>(realm, realm));
auto controller = stream.heap().allocate<ReadableByteStreamController>(realm, realm);
// 6. Perform ! SetUpReadableByteStreamController(stream, controller, startAlgorithm, pullAlgorithmWrapper, cancelAlgorithmWrapper, highWaterMark, undefined).
TRY(set_up_readable_byte_stream_controller(stream, controller, move(start_algorithm), move(pull_algorithm_wrapper), move(cancel_algorithm_wrapper), high_water_mark, JS::js_undefined()));
@ -2418,7 +2418,7 @@ WebIDL::ExceptionOr<void> set_up_writable_stream_default_controller(WritableStre
reset_queue(controller);
// 6. Set controller.[[signal]] to a new AbortSignal.
controller.set_signal(MUST_OR_THROW_OOM(realm.heap().allocate<DOM::AbortSignal>(realm, realm)));
controller.set_signal(realm.heap().allocate<DOM::AbortSignal>(realm, realm));
// 7. Set controller.[[started]] to false.
controller.set_started(false);
@ -2489,7 +2489,7 @@ WebIDL::ExceptionOr<void> set_up_writable_stream_default_controller_from_underly
auto& realm = stream.realm();
// 1. Let controller be a new WritableStreamDefaultController.
auto controller = MUST_OR_THROW_OOM(realm.heap().allocate<WritableStreamDefaultController>(realm, realm));
auto controller = realm.heap().allocate<WritableStreamDefaultController>(realm, realm);
// 2. Let startAlgorithm be an algorithm that returns undefined.
StartAlgorithm start_algorithm = [] { return JS::js_undefined(); };
@ -2916,7 +2916,7 @@ WebIDL::ExceptionOr<void> set_up_transform_stream_default_controller_from_transf
auto& vm = realm.vm();
// 1. Let controller be a new TransformStreamDefaultController.
auto controller = MUST_OR_THROW_OOM(realm.heap().allocate<TransformStreamDefaultController>(realm, realm));
auto controller = realm.heap().allocate<TransformStreamDefaultController>(realm, realm);
// 2. Let transformAlgorithm be the following steps, taking a chunk argument:
TransformAlgorithm transform_algorithm = [controller, &realm, &vm](JS::Value chunk) {
@ -3308,7 +3308,7 @@ WebIDL::ExceptionOr<void> set_up_readable_byte_stream_controller_from_underlying
auto& realm = stream.realm();
// 1. Let controller be a new ReadableByteStreamController.
auto controller = MUST_OR_THROW_OOM(stream.heap().allocate<ReadableByteStreamController>(realm, realm));
auto controller = stream.heap().allocate<ReadableByteStreamController>(realm, realm);
// 2. Let startAlgorithm be an algorithm that returns undefined.
StartAlgorithm start_algorithm = [] { return JS::js_undefined(); };

View file

@ -18,7 +18,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<ByteLengthQueuingStrategy>> ByteLengthQueui
{
// The new ByteLengthQueuingStrategy(init) constructor steps are:
// 1. Set this.[[highWaterMark]] to init["highWaterMark"].
return MUST_OR_THROW_OOM(realm.heap().allocate<ByteLengthQueuingStrategy>(realm, realm, init.high_water_mark));
return realm.heap().allocate<ByteLengthQueuingStrategy>(realm, realm, init.high_water_mark);
}
ByteLengthQueuingStrategy::ByteLengthQueuingStrategy(JS::Realm& realm, double high_water_mark)

View file

@ -18,7 +18,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<CountQueuingStrategy>> CountQueuingStrategy
{
// The new CountQueuingStrategy(init) constructor steps are:
// 1. Set this.[[highWaterMark]] to init["highWaterMark"].
return MUST_OR_THROW_OOM(realm.heap().allocate<CountQueuingStrategy>(realm, realm, init.high_water_mark));
return realm.heap().allocate<CountQueuingStrategy>(realm, realm, init.high_water_mark);
}
CountQueuingStrategy::CountQueuingStrategy(JS::Realm& realm, double high_water_mark)

View file

@ -23,7 +23,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<ReadableStream>> ReadableStream::construct_
{
auto& vm = realm.vm();
auto readable_stream = MUST_OR_THROW_OOM(realm.heap().allocate<ReadableStream>(realm, realm));
auto readable_stream = realm.heap().allocate<ReadableStream>(realm, realm);
// 1. If underlyingSource is missing, set it to null.
auto underlying_source = underlying_source_object.has_value() ? JS::Value(underlying_source_object.value().ptr()) : JS::js_null();

View file

@ -33,7 +33,7 @@ void ReadLoopReadRequest::visit_edges(Visitor& visitor)
// https://streams.spec.whatwg.org/#default-reader-constructor
WebIDL::ExceptionOr<JS::NonnullGCPtr<ReadableStreamDefaultReader>> ReadableStreamDefaultReader::construct_impl(JS::Realm& realm, JS::NonnullGCPtr<ReadableStream> stream)
{
auto reader = TRY(realm.heap().allocate<ReadableStreamDefaultReader>(realm, realm));
auto reader = realm.heap().allocate<ReadableStreamDefaultReader>(realm, realm);
// 1. Perform ? SetUpReadableStreamDefaultReader(this, stream);
TRY(set_up_readable_stream_default_reader(reader, *stream));

View file

@ -20,7 +20,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<TransformStream>> TransformStream::construc
{
auto& vm = realm.vm();
auto stream = MUST_OR_THROW_OOM(realm.heap().allocate<TransformStream>(realm, realm));
auto stream = realm.heap().allocate<TransformStream>(realm, realm);
// 1. If transformer is missing, set it to null.
auto transformer = transformer_object.has_value() ? JS::Value { transformer_object.value().ptr() } : JS::js_null();

View file

@ -21,7 +21,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<WritableStream>> WritableStream::construct_
{
auto& vm = realm.vm();
auto writable_stream = MUST_OR_THROW_OOM(realm.heap().allocate<WritableStream>(realm, realm));
auto writable_stream = realm.heap().allocate<WritableStream>(realm, realm);
// 1. If underlyingSink is missing, set it to null.
auto underlying_sink = underlying_sink_object.has_value() ? JS::Value(underlying_sink_object.value().ptr()) : JS::js_null();

View file

@ -16,7 +16,7 @@ namespace Web::Streams {
WebIDL::ExceptionOr<JS::NonnullGCPtr<WritableStreamDefaultWriter>> WritableStreamDefaultWriter::construct_impl(JS::Realm& realm, JS::NonnullGCPtr<WritableStream> stream)
{
auto writer = MUST_OR_THROW_OOM(realm.heap().allocate<WritableStreamDefaultWriter>(realm, realm));
auto writer = realm.heap().allocate<WritableStreamDefaultWriter>(realm, realm);
// 1. Perform ? SetUpWritableStreamDefaultWriter(this, stream).
TRY(set_up_writable_stream_default_writer(*writer, stream));