1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 14:48:14 +00:00

LibJS+LibWeb: Move the macro to convert ENOMEM to an exception to LibJS

Move the macro to LibJS and change it to return a throw completion
instead of a WebIDL exception. This will let us use this macro within
LibJS to handle OOM conditions.
This commit is contained in:
Timothy Flynn 2023-01-07 12:14:54 -05:00 committed by Linus Groh
parent ba97f6a0d3
commit d8044c5358
15 changed files with 118 additions and 93 deletions

View file

@ -5,6 +5,7 @@
*/
#include <AK/URLParser.h>
#include <LibJS/Runtime/Completion.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Bindings/MainThreadVM.h>
#include <LibWeb/Fetch/Enums.h>
@ -82,6 +83,8 @@ JS::NonnullGCPtr<Response> Response::create(JS::Realm& realm, JS::NonnullGCPtr<I
// https://fetch.spec.whatwg.org/#initialize-a-response
WebIDL::ExceptionOr<void> Response::initialize_response(ResponseInit const& init, Optional<Infrastructure::BodyWithType> const& body)
{
auto& vm = this->vm();
// 1. If init["status"] is not in the range 200 to 599, inclusive, then throw a RangeError.
if (init.status < 200 || init.status > 599)
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::RangeError, "Status must be in range 200-599"sv };
@ -92,7 +95,7 @@ WebIDL::ExceptionOr<void> Response::initialize_response(ResponseInit const& init
m_response->set_status(init.status);
// 4. Set responses responses status message to init["statusText"].
m_response->set_status_message(TRY_OR_RETURN_OOM(realm(), ByteBuffer::copy(init.status_text.bytes())));
m_response->set_status_message(TRY_OR_THROW_OOM(vm, ByteBuffer::copy(init.status_text.bytes())));
// 5. If init["headers"] exists, then fill responses headers with init["headers"].
if (init.headers.has_value())
@ -111,9 +114,9 @@ WebIDL::ExceptionOr<void> Response::initialize_response(ResponseInit const& init
if (body->type.has_value() && m_response->header_list()->contains("Content-Type"sv.bytes())) {
auto header = Infrastructure::Header {
.name = MUST(ByteBuffer::copy("Content-Type"sv.bytes())),
.value = TRY_OR_RETURN_OOM(realm(), ByteBuffer::copy(body->type->span())),
.value = TRY_OR_THROW_OOM(vm, ByteBuffer::copy(body->type->span())),
};
TRY_OR_RETURN_OOM(realm(), m_response->header_list()->append(move(header)));
TRY_OR_THROW_OOM(vm, m_response->header_list()->append(move(header)));
}
}
@ -185,8 +188,8 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> Response::redirect(JS::VM& vm, D
auto value = parsed_url.serialize();
// 7. Append (`Location`, value) to responseObjects responses header list.
auto header = TRY_OR_RETURN_OOM(realm, Infrastructure::Header::from_string_pair("Location"sv, value));
TRY_OR_RETURN_OOM(realm, response_object->response()->header_list()->append(move(header)));
auto header = TRY_OR_THROW_OOM(vm, Infrastructure::Header::from_string_pair("Location"sv, value));
TRY_OR_THROW_OOM(vm, response_object->response()->header_list()->append(move(header)));
// 8. Return responseObject.
return response_object;
@ -210,7 +213,7 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Response>> Response::json(JS::VM& vm, JS::V
// 4. Perform initialize a response given responseObject, init, and (body, "application/json").
auto body_with_type = Infrastructure::BodyWithType {
.body = move(body),
.type = TRY_OR_RETURN_OOM(realm, ByteBuffer::copy("application/json"sv.bytes()))
.type = TRY_OR_THROW_OOM(vm, ByteBuffer::copy("application/json"sv.bytes()))
};
TRY(response_object->initialize_response(init, move(body_with_type)));