mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 17:17:44 +00:00
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:
parent
452dc544bc
commit
bc68539e26
4 changed files with 19 additions and 15 deletions
|
@ -13,6 +13,16 @@
|
||||||
|
|
||||||
namespace Web::DOM {
|
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
|
// 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):
|
// DOMException.code value when used as name (and are therefore omitted here):
|
||||||
// - DOMStringSizeError (DOMSTRING_SIZE_ERR = 2)
|
// - DOMStringSizeError (DOMSTRING_SIZE_ERR = 2)
|
||||||
|
|
|
@ -28,7 +28,7 @@ DOM::ExceptionOr<NonnullRefPtr<Blob>> Blob::create(Optional<Vector<BlobPart>> co
|
||||||
ByteBuffer byte_buffer {};
|
ByteBuffer byte_buffer {};
|
||||||
// 2. Let bytes be the result of processing blob parts given blobParts and options.
|
// 2. Let bytes be the result of processing blob parts given blobParts and options.
|
||||||
if (blob_parts.has_value()) {
|
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();
|
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
|
// 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.
|
// 1. Let bytes be an empty sequence of bytes.
|
||||||
ByteBuffer bytes {};
|
ByteBuffer bytes {};
|
||||||
|
|
||||||
// 2. For each element in parts:
|
// 2. For each element in parts:
|
||||||
for (auto const& blob_part : blob_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:
|
// 1. If element is a USVString, run the following sub-steps:
|
||||||
[&](String const& string) -> ErrorOr<void> {
|
[&](String const& string) -> ErrorOr<void> {
|
||||||
// NOTE: This step is handled by the lambda expression.
|
// 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.
|
// 3. If element is a Blob, append the bytes it represents to bytes.
|
||||||
[&](NonnullRefPtr<Blob> const& blob) -> ErrorOr<void> {
|
[&](NonnullRefPtr<Blob> const& blob) -> ErrorOr<void> {
|
||||||
return bytes.try_append(blob->m_byte_buffer.bytes());
|
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;
|
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.
|
// a. S refers to span consecutive bytes from this, beginning with the byte at byte-order position relativeStart.
|
||||||
// b. S.size = span.
|
// b. S.size = span.
|
||||||
// c. S.type = relativeContentType.
|
// c. S.type = relativeContentType.
|
||||||
auto byte_buffer_or_error = m_byte_buffer.slice(relative_start, span);
|
auto byte_buffer = TRY_OR_RETURN_OOM(m_byte_buffer.slice(relative_start, span));
|
||||||
if (byte_buffer_or_error.is_error())
|
return adopt_ref(*new Blob(move(byte_buffer), move(relative_content_type)));
|
||||||
return DOM::UnknownError::create("Out of memory."sv);
|
|
||||||
return adopt_ref(*new Blob(byte_buffer_or_error.release_value(), move(relative_content_type)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://w3c.github.io/FileAPI/#dom-blob-text
|
// https://w3c.github.io/FileAPI/#dom-blob-text
|
||||||
|
|
|
@ -50,7 +50,7 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Blob() = default;
|
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 {};
|
ByteBuffer m_byte_buffer {};
|
||||||
String m_type {};
|
String m_type {};
|
||||||
|
|
|
@ -118,10 +118,8 @@ DOM::ExceptionOr<JS::Value> XMLHttpRequest::response()
|
||||||
}
|
}
|
||||||
// 6. Otherwise, if this’s response type is "blob", set this’s response object to a new Blob object representing this’s received bytes with type set to the result of get a final MIME type for this.
|
// 6. Otherwise, if this’s response type is "blob", set this’s response object to a new Blob object representing this’s received bytes with type set to the result of get a final MIME type for this.
|
||||||
else if (m_response_type == Bindings::XMLHttpRequestResponseType::Blob) {
|
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());
|
auto blob_part = TRY_OR_RETURN_OOM(try_make_ref_counted<FileAPI::Blob>(m_received_bytes, get_final_mime_type().type()));
|
||||||
if (blob_part_or_error.is_error())
|
auto blob = TRY(FileAPI::Blob::create(Vector<FileAPI::BlobPart> { move(blob_part) }));
|
||||||
return DOM::UnknownError::create("Out of memory."sv);
|
|
||||||
auto blob = TRY(FileAPI::Blob::create(Vector<FileAPI::BlobPart> { blob_part_or_error.release_value() }));
|
|
||||||
m_response_object = JS::make_handle(JS::Value(blob->create_wrapper(global_object)));
|
m_response_object = JS::make_handle(JS::Value(blob->create_wrapper(global_object)));
|
||||||
}
|
}
|
||||||
// 7. Otherwise, if this’s response type is "document", set a document response for this.
|
// 7. Otherwise, if this’s response type is "document", set a document response for this.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue