mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-10 13:00:29 +03:00
LibWeb: Let get_buffer_source_copy() return ErrorOr instead of Optional
This is a minor refactor of IDL::get_buffer_source_copy() letting it return ErrorOr<ByteBuffer> instead of Optional<ByteBuffer>. This also updates all places that use IDL::get_buffer_source_copy().
This commit is contained in:
parent
7a2bef7fe1
commit
9fe12c1851
Notes:
sideshowbarker
2024-07-17 08:38:47 +09:00
Author: https://github.com/kennethmyhra Commit: https://github.com/SerenityOS/serenity/commit/9fe12c1851 Pull-request: https://github.com/SerenityOS/serenity/pull/14648 Reviewed-by: https://github.com/linusg ✅
@ -17,7 +17,7 @@
|
||||
namespace Web::Bindings::IDL {
|
||||
|
||||
// https://webidl.spec.whatwg.org/#dfn-get-buffer-source-copy
|
||||
Optional<ByteBuffer> get_buffer_source_copy(JS::Object const& buffer_source)
|
||||
ErrorOr<ByteBuffer> get_buffer_source_copy(JS::Object const& buffer_source)
|
||||
{
|
||||
// 1. Let esBufferSource be the result of converting bufferSource to an ECMAScript value.
|
||||
|
||||
@ -69,18 +69,16 @@ Optional<ByteBuffer> get_buffer_source_copy(JS::Object const& buffer_source)
|
||||
return ByteBuffer {};
|
||||
|
||||
// 8. Let bytes be a new byte sequence of length equal to length.
|
||||
auto bytes = ByteBuffer::create_zeroed(length);
|
||||
if (bytes.is_error())
|
||||
return {};
|
||||
auto bytes = TRY(ByteBuffer::create_zeroed(length));
|
||||
|
||||
// 9. For i in the range offset to offset + length − 1, inclusive, set bytes[i − offset] to ! GetValueFromBuffer(esArrayBuffer, i, Uint8, true, Unordered).
|
||||
for (u64 i = offset; i <= offset + length - 1; ++i) {
|
||||
auto value = es_array_buffer->get_value<u8>(i, true, JS::ArrayBuffer::Unordered);
|
||||
bytes.value()[i - offset] = (u8)value.as_u32();
|
||||
bytes[i - offset] = (u8)value.as_u32();
|
||||
}
|
||||
|
||||
// 10. Return bytes.
|
||||
return bytes.release_value();
|
||||
return bytes;
|
||||
}
|
||||
|
||||
// https://webidl.spec.whatwg.org/#invoke-a-callback-function
|
||||
|
@ -16,7 +16,7 @@
|
||||
|
||||
namespace Web::Bindings::IDL {
|
||||
|
||||
Optional<ByteBuffer> get_buffer_source_copy(JS::Object const& buffer_source);
|
||||
ErrorOr<ByteBuffer> get_buffer_source_copy(JS::Object const& buffer_source);
|
||||
|
||||
// https://webidl.spec.whatwg.org/#call-user-object-operation-return
|
||||
inline JS::Completion clean_up_on_return(HTML::EnvironmentSettingsObject& stored_settings, HTML::EnvironmentSettingsObject& relevant_settings, JS::Completion& completion)
|
||||
|
@ -22,13 +22,14 @@ JS::Promise* SubtleCrypto::digest(String const& algorithm, JS::Handle<JS::Object
|
||||
// 1. Let algorithm be the algorithm parameter passed to the digest() method.
|
||||
|
||||
// 2. Let data be the result of getting a copy of the bytes held by the data parameter passed to the digest() method.
|
||||
auto data_buffer = Bindings::IDL::get_buffer_source_copy(*data.cell());
|
||||
if (!data_buffer.has_value()) {
|
||||
auto data_buffer_or_error = Bindings::IDL::get_buffer_source_copy(*data.cell());
|
||||
if (data_buffer_or_error.is_error()) {
|
||||
auto* error = wrap(wrapper()->global_object(), DOM::OperationError::create("Failed to copy bytes from ArrayBuffer"));
|
||||
auto* promise = JS::Promise::create(global_object);
|
||||
promise->reject(error);
|
||||
return promise;
|
||||
}
|
||||
auto& data_buffer = data_buffer_or_error.value();
|
||||
|
||||
// 3. Let normalizedAlgorithm be the result of normalizing an algorithm, with alg set to algorithm and op set to "digest".
|
||||
// FIXME: This is way more generic than it needs to be right now, so we simplify it.
|
||||
@ -60,7 +61,7 @@ JS::Promise* SubtleCrypto::digest(String const& algorithm, JS::Handle<JS::Object
|
||||
|
||||
// 8. Let result be the result of performing the digest operation specified by normalizedAlgorithm using algorithm, with data as message.
|
||||
::Crypto::Hash::Manager hash { hash_kind };
|
||||
hash.update(*data_buffer);
|
||||
hash.update(data_buffer);
|
||||
|
||||
auto digest = hash.digest();
|
||||
auto result_buffer = ByteBuffer::copy(digest.immutable_data(), hash.digest_size());
|
||||
|
@ -17,11 +17,11 @@ DOM::ExceptionOr<String> TextDecoder::decode(JS::Handle<JS::Object> const& input
|
||||
{
|
||||
// FIXME: Implement the streaming stuff.
|
||||
|
||||
auto data_buffer = Bindings::IDL::get_buffer_source_copy(*input.cell());
|
||||
if (!data_buffer.has_value())
|
||||
auto data_buffer_or_error = Bindings::IDL::get_buffer_source_copy(*input.cell());
|
||||
if (data_buffer_or_error.is_error())
|
||||
return DOM::OperationError::create("Failed to copy bytes from ArrayBuffer");
|
||||
|
||||
return m_decoder.to_utf8({ data_buffer->data(), data_buffer->size() });
|
||||
auto& data_buffer = data_buffer_or_error.value();
|
||||
return m_decoder.to_utf8({ data_buffer.data(), data_buffer.size() });
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -71,10 +71,8 @@ ErrorOr<ByteBuffer> Blob::process_blob_parts(Vector<BlobPart> const& blob_parts)
|
||||
},
|
||||
// 2. If element is a BufferSource, get a copy of the bytes held by the buffer source, and append those bytes to bytes.
|
||||
[&](JS::Handle<JS::Object> const& buffer_source) -> ErrorOr<void> {
|
||||
auto data_buffer = Bindings::IDL::get_buffer_source_copy(*buffer_source.cell());
|
||||
if (data_buffer.has_value())
|
||||
return bytes.try_append(data_buffer->bytes());
|
||||
return {};
|
||||
auto data_buffer = TRY(Bindings::IDL::get_buffer_source_copy(*buffer_source.cell()));
|
||||
return bytes.try_append(data_buffer.bytes());
|
||||
},
|
||||
// 3. If element is a Blob, append the bytes it represents to bytes.
|
||||
[&](NonnullRefPtr<Blob> const& blob) -> ErrorOr<void> {
|
||||
|
Loading…
Reference in New Issue
Block a user