1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 03:58:12 +00: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:
Kenneth Myhra 2022-07-22 21:01:36 +02:00 committed by Linus Groh
parent 7a2bef7fe1
commit 9fe12c1851
5 changed files with 15 additions and 18 deletions

View file

@ -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