LibWeb: Add & use TRY_OR_RETURN_OOM macro

This is a convenient way to return a DOM exception for operations that
return ErrorOr and only have an OOM failure path.
This commit is contained in:
Linus Groh 2022-07-17 18:23:27 +01:00
parent 452dc544bc
commit bc68539e26
Notes: sideshowbarker 2024-07-17 08:51:33 +09:00
4 changed files with 19 additions and 15 deletions

View File

@ -13,6 +13,16 @@
namespace Web::DOM {
#define TRY_OR_RETURN_OOM(expression) \
({ \
auto _temporary_result = (expression); \
if (_temporary_result.is_error()) { \
VERIFY(_temporary_result.error().code() == ENOMEM); \
return DOM::UnknownError::create("Out of memory."sv); \
} \
_temporary_result.release_value(); \
})
// The following have a legacy code value but *don't* produce it as
// DOMException.code value when used as name (and are therefore omitted here):
// - DOMStringSizeError (DOMSTRING_SIZE_ERR = 2)

View File

@ -28,7 +28,7 @@ DOM::ExceptionOr<NonnullRefPtr<Blob>> Blob::create(Optional<Vector<BlobPart>> co
ByteBuffer byte_buffer {};
// 2. Let bytes be the result of processing blob parts given blobParts and options.
if (blob_parts.has_value()) {
byte_buffer = TRY(process_blob_parts(blob_parts.value()));
byte_buffer = TRY_OR_RETURN_OOM(process_blob_parts(blob_parts.value()));
}
String type = String::empty();
@ -50,14 +50,14 @@ DOM::ExceptionOr<NonnullRefPtr<Blob>> Blob::create_with_global_object(Bindings::
}
// https://w3c.github.io/FileAPI/#process-blob-parts
DOM::ExceptionOr<ByteBuffer> Blob::process_blob_parts(Vector<BlobPart> const& blob_parts)
ErrorOr<ByteBuffer> Blob::process_blob_parts(Vector<BlobPart> const& blob_parts)
{
// 1. Let bytes be an empty sequence of bytes.
ByteBuffer bytes {};
// 2. For each element in parts:
for (auto const& blob_part : blob_parts) {
auto error = blob_part.visit(
TRY(blob_part.visit(
// 1. If element is a USVString, run the following sub-steps:
[&](String const& string) -> ErrorOr<void> {
// NOTE: This step is handled by the lambda expression.
@ -79,9 +79,7 @@ DOM::ExceptionOr<ByteBuffer> Blob::process_blob_parts(Vector<BlobPart> const& bl
// 3. If element is a Blob, append the bytes it represents to bytes.
[&](NonnullRefPtr<Blob> const& blob) -> ErrorOr<void> {
return bytes.try_append(blob->m_byte_buffer.bytes());
});
if (error.is_error())
return DOM::UnknownError::create("Out of memory. Failed to process blob parts."sv);
}));
}
return bytes;
}
@ -146,10 +144,8 @@ DOM::ExceptionOr<NonnullRefPtr<Blob>> Blob::slice(Optional<i64> start, Optional<
// a. S refers to span consecutive bytes from this, beginning with the byte at byte-order position relativeStart.
// b. S.size = span.
// c. S.type = relativeContentType.
auto byte_buffer_or_error = m_byte_buffer.slice(relative_start, span);
if (byte_buffer_or_error.is_error())
return DOM::UnknownError::create("Out of memory."sv);
return adopt_ref(*new Blob(byte_buffer_or_error.release_value(), move(relative_content_type)));
auto byte_buffer = TRY_OR_RETURN_OOM(m_byte_buffer.slice(relative_start, span));
return adopt_ref(*new Blob(move(byte_buffer), move(relative_content_type)));
}
// https://w3c.github.io/FileAPI/#dom-blob-text

View File

@ -50,7 +50,7 @@ public:
private:
Blob() = default;
static DOM::ExceptionOr<ByteBuffer> process_blob_parts(Vector<BlobPart> const& blob_parts);
static ErrorOr<ByteBuffer> process_blob_parts(Vector<BlobPart> const& blob_parts);
ByteBuffer m_byte_buffer {};
String m_type {};

View File

@ -118,10 +118,8 @@ DOM::ExceptionOr<JS::Value> XMLHttpRequest::response()
}
// 6. Otherwise, if thiss response type is "blob", set thiss response object to a new Blob object representing thiss received bytes with type set to the result of get a final MIME type for this.
else if (m_response_type == Bindings::XMLHttpRequestResponseType::Blob) {
auto blob_part_or_error = try_make_ref_counted<FileAPI::Blob>(m_received_bytes, get_final_mime_type().type());
if (blob_part_or_error.is_error())
return DOM::UnknownError::create("Out of memory."sv);
auto blob = TRY(FileAPI::Blob::create(Vector<FileAPI::BlobPart> { blob_part_or_error.release_value() }));
auto blob_part = TRY_OR_RETURN_OOM(try_make_ref_counted<FileAPI::Blob>(m_received_bytes, get_final_mime_type().type()));
auto blob = TRY(FileAPI::Blob::create(Vector<FileAPI::BlobPart> { move(blob_part) }));
m_response_object = JS::make_handle(JS::Value(blob->create_wrapper(global_object)));
}
// 7. Otherwise, if thiss response type is "document", set a document response for this.