LibWeb/WebIDL: Implement 'write' operation for ArrayBufferView

This commit is contained in:
Shannon Booth 2024-12-23 16:19:17 +13:00 committed by Tim Flynn
parent 5d8d5375a0
commit 3d3c1d8bf7
Notes: github-actions[bot] 2024-12-27 14:57:54 +00:00
2 changed files with 21 additions and 0 deletions

View file

@ -90,6 +90,26 @@ u32 ArrayBufferView::byte_offset() const
[](auto& view) -> u32 { return static_cast<u32>(view->byte_offset()); });
}
// https://webidl.spec.whatwg.org/#arraybufferview-write
void ArrayBufferView::write(ReadonlyBytes bytes, u32 starting_offset)
{
// 1. Let jsView be the result of converting view to a JavaScript value.
// 2. Assert: bytess length ≤ jsView.[[ByteLength]] startingOffset.
VERIFY(bytes.size() <= byte_length() - starting_offset);
// 3. Assert: if view is not a DataView, then bytess length modulo the element size of views type is 0.
if (!m_bufferable_object.has<GC::Ref<JS::DataView>>()) {
auto element_size = m_bufferable_object.get<GC::Ref<JS::TypedArrayBase>>()->element_size();
VERIFY(bytes.size() % element_size == 0);
}
// 4. Let arrayBuffer be the result of converting jsView.[[ViewedArrayBuffer]] to an IDL value of type ArrayBuffer.
auto array_buffer = viewed_array_buffer();
// 5. Write bytes into arrayBuffer with startingOffset set to jsView.[[ByteOffset]] + startingOffset.
array_buffer->buffer().overwrite(byte_offset() + starting_offset, bytes.data(), bytes.size());
}
BufferSource::~BufferSource() = default;
}

View file

@ -69,6 +69,7 @@ public:
using BufferableObjectBase::is_typed_array_base;
u32 byte_offset() const;
void write(ReadonlyBytes, u32 starting_offset = 0);
};
// https://webidl.spec.whatwg.org/#BufferSource