Forráskód Böngészése

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().
Kenneth Myhra 3 éve
szülő
commit
9fe12c1851

+ 4 - 6
Userland/Libraries/LibWeb/Bindings/IDLAbstractOperations.cpp

@@ -17,7 +17,7 @@
 namespace Web::Bindings::IDL {
 namespace Web::Bindings::IDL {
 
 
 // https://webidl.spec.whatwg.org/#dfn-get-buffer-source-copy
 // 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.
     // 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 {};
         return ByteBuffer {};
 
 
     // 8. Let bytes be a new byte sequence of length equal to length.
     // 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).
     // 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) {
     for (u64 i = offset; i <= offset + length - 1; ++i) {
         auto value = es_array_buffer->get_value<u8>(i, true, JS::ArrayBuffer::Unordered);
         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.
     // 10. Return bytes.
-    return bytes.release_value();
+    return bytes;
 }
 }
 
 
 // https://webidl.spec.whatwg.org/#invoke-a-callback-function
 // https://webidl.spec.whatwg.org/#invoke-a-callback-function

+ 1 - 1
Userland/Libraries/LibWeb/Bindings/IDLAbstractOperations.h

@@ -16,7 +16,7 @@
 
 
 namespace Web::Bindings::IDL {
 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
 // 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)
 inline JS::Completion clean_up_on_return(HTML::EnvironmentSettingsObject& stored_settings, HTML::EnvironmentSettingsObject& relevant_settings, JS::Completion& completion)

+ 4 - 3
Userland/Libraries/LibWeb/Crypto/SubtleCrypto.cpp

@@ -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.
     // 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.
     // 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* error = wrap(wrapper()->global_object(), DOM::OperationError::create("Failed to copy bytes from ArrayBuffer"));
         auto* promise = JS::Promise::create(global_object);
         auto* promise = JS::Promise::create(global_object);
         promise->reject(error);
         promise->reject(error);
         return promise;
         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".
     // 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.
     // 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.
     // 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 };
     ::Crypto::Hash::Manager hash { hash_kind };
-    hash.update(*data_buffer);
+    hash.update(data_buffer);
 
 
     auto digest = hash.digest();
     auto digest = hash.digest();
     auto result_buffer = ByteBuffer::copy(digest.immutable_data(), hash.digest_size());
     auto result_buffer = ByteBuffer::copy(digest.immutable_data(), hash.digest_size());

+ 4 - 4
Userland/Libraries/LibWeb/Encoding/TextDecoder.cpp

@@ -17,11 +17,11 @@ DOM::ExceptionOr<String> TextDecoder::decode(JS::Handle<JS::Object> const& input
 {
 {
     // FIXME: Implement the streaming stuff.
     // 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 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() });
 }
 }
 
 
 }
 }

+ 2 - 4
Userland/Libraries/LibWeb/FileAPI/Blob.cpp

@@ -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.
             // 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> {
             [&](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.
             // 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> {