diff options
author | Idan Horowitz <idan.horowitz@gmail.com> | 2022-02-08 21:36:48 +0200 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-02-08 23:08:43 +0000 |
commit | 20d3869182c25e1255ad57277cd9d84575564d83 (patch) | |
tree | 7266bb902d1828f9cb9448d974ae4b701579e6cd /Userland/Libraries/LibJS/Runtime/ArrayBuffer.cpp | |
parent | d225b5e94cdca39af32f58ba8100fb1bd126a622 (diff) | |
download | serenity-20d3869182c25e1255ad57277cd9d84575564d83.zip |
LibJS: Implement the CloneArrayBuffer AO
Diffstat (limited to 'Userland/Libraries/LibJS/Runtime/ArrayBuffer.cpp')
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/ArrayBuffer.cpp | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/ArrayBuffer.cpp b/Userland/Libraries/LibJS/Runtime/ArrayBuffer.cpp index 8cfae30b8c..c0b10ec221 100644 --- a/Userland/Libraries/LibJS/Runtime/ArrayBuffer.cpp +++ b/Userland/Libraries/LibJS/Runtime/ArrayBuffer.cpp @@ -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; +} + } |