1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 04:17:35 +00:00

LibJS: Implement the CloneArrayBuffer AO

This commit is contained in:
Idan Horowitz 2022-02-08 21:36:48 +02:00 committed by Linus Groh
parent d225b5e94c
commit 20d3869182
2 changed files with 27 additions and 0 deletions

View file

@ -73,4 +73,30 @@ ThrowCompletionOr<ArrayBuffer*> allocate_array_buffer(GlobalObject& global_objec
return obj;
}
// 25.1.2.4 CloneArrayBuffer ( srcBuffer, srcByteOffset, srcLength, cloneConstructor ), https://tc39.es/ecma262/#sec-clonearraybuffer
ThrowCompletionOr<ArrayBuffer*> clone_array_buffer(GlobalObject& global_object, ArrayBuffer& source_buffer, size_t source_byte_offset, size_t source_length, FunctionObject& clone_constructor)
{
auto& vm = global_object.vm();
// 1. Let targetBuffer be ? AllocateArrayBuffer(cloneConstructor, srcLength).
auto* target_buffer = TRY(allocate_array_buffer(global_object, clone_constructor, source_length));
// 2. If IsDetachedBuffer(srcBuffer) is true, throw a TypeError exception.
if (source_buffer.is_detached())
return vm.throw_completion<TypeError>(global_object, ErrorType::DetachedArrayBuffer);
// 3. Let srcBlock be srcBuffer.[[ArrayBufferData]].
auto& source_block = source_buffer.buffer();
// 4. Let targetBlock be targetBuffer.[[ArrayBufferData]].
auto& target_block = target_buffer->buffer();
// 5. Perform CopyDataBlockBytes(targetBlock, 0, srcBlock, srcByteOffset, srcLength).
// FIXME: This is only correct for ArrayBuffers, once SharedArrayBuffer is implemented, the AO has to be implemented
target_block.overwrite(0, source_block.offset_pointer(source_byte_offset), source_length);
// 6. Return targetBuffer.
return target_buffer;
}
}