1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 18:28:12 +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

@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/VM.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Fetch/Headers.h>
@ -46,10 +47,12 @@ void Headers::visit_edges(JS::Cell::Visitor& visitor)
// https://fetch.spec.whatwg.org/#dom-headers-append
WebIDL::ExceptionOr<void> Headers::append(DeprecatedString const& name_string, DeprecatedString const& value_string)
{
auto& vm = this->vm();
// The append(name, value) method steps are to append (name, value) to this.
auto header = Infrastructure::Header {
.name = TRY_OR_RETURN_OOM(realm(), ByteBuffer::copy(name_string.bytes())),
.value = TRY_OR_RETURN_OOM(realm(), ByteBuffer::copy(value_string.bytes())),
.name = TRY_OR_THROW_OOM(vm, ByteBuffer::copy(name_string.bytes())),
.value = TRY_OR_THROW_OOM(vm, ByteBuffer::copy(value_string.bytes())),
};
TRY(append(move(header)));
return {};
@ -64,7 +67,7 @@ WebIDL::ExceptionOr<void> Headers::delete_(DeprecatedString const& name_string)
// 1. If validating (name, ``) for headers returns false, then return.
// NOTE: Passing a dummy header value ought not to have any negative repercussions.
auto header = TRY_OR_RETURN_OOM(realm, Infrastructure::Header::from_string_pair(name, ""sv));
auto header = TRY_OR_THROW_OOM(realm.vm(), Infrastructure::Header::from_string_pair(name, ""sv));
if (!TRY(validate(header)))
return {};
@ -97,7 +100,7 @@ WebIDL::ExceptionOr<DeprecatedString> Headers::get(DeprecatedString const& name_
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Invalid header name"sv };
// 2. Return the result of getting name from thiss header list.
auto byte_buffer = TRY_OR_RETURN_OOM(realm(), m_header_list->get(name));
auto byte_buffer = TRY_OR_THROW_OOM(vm(), m_header_list->get(name));
// FIXME: Teach BindingsGenerator about Optional<DeprecatedString>
return byte_buffer.has_value() ? DeprecatedString { byte_buffer->span() } : DeprecatedString {};
}
@ -119,16 +122,18 @@ WebIDL::ExceptionOr<bool> Headers::has(DeprecatedString const& name_string)
// https://fetch.spec.whatwg.org/#dom-headers-set
WebIDL::ExceptionOr<void> Headers::set(DeprecatedString const& name_string, DeprecatedString const& value_string)
{
// The set(name, value) method steps are:
auto& realm = this->realm();
auto& vm = realm.vm();
// The set(name, value) method steps are:
auto name = name_string.bytes();
auto value = value_string.bytes();
// 1. Normalize value.
auto normalized_value = TRY_OR_RETURN_OOM(realm, Infrastructure::normalize_header_value(value));
auto normalized_value = TRY_OR_THROW_OOM(vm, Infrastructure::normalize_header_value(value));
auto header = Infrastructure::Header {
.name = TRY_OR_RETURN_OOM(realm, ByteBuffer::copy(name)),
.name = TRY_OR_THROW_OOM(vm, ByteBuffer::copy(name)),
.value = move(normalized_value),
};
@ -141,7 +146,7 @@ WebIDL::ExceptionOr<void> Headers::set(DeprecatedString const& name_string, Depr
return {};
// 4. Set (name, value) in thiss header list.
TRY_OR_RETURN_OOM(realm, m_header_list->set(move(header)));
TRY_OR_THROW_OOM(vm, m_header_list->set(move(header)));
// 5. If thiss guard is "request-no-cors", then remove privileged no-CORS request-headers from this.
if (m_guard == Guard::RequestNoCORS)
@ -190,8 +195,9 @@ JS::ThrowCompletionOr<void> Headers::for_each(ForEachCallback callback)
// https://fetch.spec.whatwg.org/#headers-validate
WebIDL::ExceptionOr<bool> Headers::validate(Infrastructure::Header const& header) const
{
// To validate a header (name, value) for a Headers object headers:
auto& realm = this->realm();
// To validate a header (name, value) for a Headers object headers:
auto const& [name, value] = header;
// 1. If name is not a header name or value is not a header value, then throw a TypeError.
@ -205,7 +211,7 @@ WebIDL::ExceptionOr<bool> Headers::validate(Infrastructure::Header const& header
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Headers object is immutable"sv };
// 3. If headerss guard is "request" and (name, value) is a forbidden request-header, then return false.
if (m_guard == Guard::Request && TRY_OR_RETURN_OOM(realm, Infrastructure::is_forbidden_request_header(header)))
if (m_guard == Guard::Request && TRY_OR_THROW_OOM(realm.vm(), Infrastructure::is_forbidden_request_header(header)))
return false;
// 4. If headerss guard is "response" and name is a forbidden response-header name, then return false.
@ -219,12 +225,14 @@ WebIDL::ExceptionOr<bool> Headers::validate(Infrastructure::Header const& header
// https://fetch.spec.whatwg.org/#concept-headers-append
WebIDL::ExceptionOr<void> Headers::append(Infrastructure::Header header)
{
// To append a header (name, value) to a Headers object headers, run these steps:
auto& realm = this->realm();
auto& vm = realm.vm();
// To append a header (name, value) to a Headers object headers, run these steps:
auto& [name, value] = header;
// 1. Normalize value.
value = TRY_OR_RETURN_OOM(realm, Infrastructure::normalize_header_value(value));
value = TRY_OR_THROW_OOM(vm, Infrastructure::normalize_header_value(value));
// 2. If validating (name, value) for headers returns false, then return.
if (!TRY(validate(header)))
@ -233,21 +241,21 @@ WebIDL::ExceptionOr<void> Headers::append(Infrastructure::Header header)
// 3. If headerss guard is "request-no-cors":
if (m_guard == Guard::RequestNoCORS) {
// 1. Let temporaryValue be the result of getting name from headerss header list.
auto temporary_value = TRY_OR_RETURN_OOM(realm, m_header_list->get(name));
auto temporary_value = TRY_OR_THROW_OOM(vm, m_header_list->get(name));
// 2. If temporaryValue is null, then set temporaryValue to value.
if (!temporary_value.has_value()) {
temporary_value = TRY_OR_RETURN_OOM(realm, ByteBuffer::copy(value));
temporary_value = TRY_OR_THROW_OOM(vm, ByteBuffer::copy(value));
}
// 3. Otherwise, set temporaryValue to temporaryValue, followed by 0x2C 0x20, followed by value.
else {
TRY_OR_RETURN_OOM(realm, temporary_value->try_append(0x2c));
TRY_OR_RETURN_OOM(realm, temporary_value->try_append(0x20));
TRY_OR_RETURN_OOM(realm, temporary_value->try_append(value));
TRY_OR_THROW_OOM(vm, temporary_value->try_append(0x2c));
TRY_OR_THROW_OOM(vm, temporary_value->try_append(0x20));
TRY_OR_THROW_OOM(vm, temporary_value->try_append(value));
}
auto temporary_header = Infrastructure::Header {
.name = TRY_OR_RETURN_OOM(realm, ByteBuffer::copy(name)),
.name = TRY_OR_THROW_OOM(vm, ByteBuffer::copy(name)),
.value = temporary_value.release_value(),
};
@ -257,7 +265,7 @@ WebIDL::ExceptionOr<void> Headers::append(Infrastructure::Header header)
}
// 4. Append (name, value) to headerss header list.
TRY_OR_RETURN_OOM(realm, m_header_list->append(move(header)));
TRY_OR_THROW_OOM(vm, m_header_list->append(move(header)));
// 5. If headerss guard is "request-no-cors", then remove privileged no-CORS request-headers from headers.
if (m_guard == Guard::RequestNoCORS)
@ -269,25 +277,27 @@ WebIDL::ExceptionOr<void> Headers::append(Infrastructure::Header header)
// https://fetch.spec.whatwg.org/#concept-headers-fill
WebIDL::ExceptionOr<void> Headers::fill(HeadersInit const& object)
{
auto& vm = realm().vm();
// To fill a Headers object headers with a given object object, run these steps:
return object.visit(
// 1. If object is a sequence, then for each header of object:
[this](Vector<Vector<DeprecatedString>> const& object) -> WebIDL::ExceptionOr<void> {
[&](Vector<Vector<DeprecatedString>> const& object) -> WebIDL::ExceptionOr<void> {
for (auto const& entry : object) {
// 1. If header's size is not 2, then throw a TypeError.
if (entry.size() != 2)
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Array must contain header key/value pair"sv };
// 2. Append (header[0], header[1]) to headers.
auto header = TRY_OR_RETURN_OOM(realm(), Infrastructure::Header::from_string_pair(entry[0], entry[1].bytes()));
auto header = TRY_OR_THROW_OOM(vm, Infrastructure::Header::from_string_pair(entry[0], entry[1].bytes()));
TRY(append(move(header)));
}
return {};
},
// 2. Otherwise, object is a record, then for each key → value of object, append (key, value) to headers.
[this](OrderedHashMap<DeprecatedString, DeprecatedString> const& object) -> WebIDL::ExceptionOr<void> {
[&](OrderedHashMap<DeprecatedString, DeprecatedString> const& object) -> WebIDL::ExceptionOr<void> {
for (auto const& entry : object) {
auto header = TRY_OR_RETURN_OOM(realm(), Infrastructure::Header::from_string_pair(entry.key, entry.value));
auto header = TRY_OR_THROW_OOM(vm, Infrastructure::Header::from_string_pair(entry.key, entry.value));
TRY(append(move(header)));
}
return {};