mirror of
https://github.com/zebrajr/ladybird.git
synced 2025-12-06 12:20:00 +01:00
LibWeb/WebIDL: Implement 'write' operation for ArrayBufferView
This commit is contained in:
parent
5d8d5375a0
commit
3d3c1d8bf7
|
|
@ -90,6 +90,26 @@ u32 ArrayBufferView::byte_offset() const
|
||||||
[](auto& view) -> u32 { return static_cast<u32>(view->byte_offset()); });
|
[](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: bytes’s length ≤ jsView.[[ByteLength]] − startingOffset.
|
||||||
|
VERIFY(bytes.size() <= byte_length() - starting_offset);
|
||||||
|
|
||||||
|
// 3. Assert: if view is not a DataView, then bytes’s length modulo the element size of view’s 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;
|
BufferSource::~BufferSource() = default;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -69,6 +69,7 @@ public:
|
||||||
using BufferableObjectBase::is_typed_array_base;
|
using BufferableObjectBase::is_typed_array_base;
|
||||||
|
|
||||||
u32 byte_offset() const;
|
u32 byte_offset() const;
|
||||||
|
void write(ReadonlyBytes, u32 starting_offset = 0);
|
||||||
};
|
};
|
||||||
|
|
||||||
// https://webidl.spec.whatwg.org/#BufferSource
|
// https://webidl.spec.whatwg.org/#BufferSource
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user