summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorIdan Horowitz <idan.horowitz@gmail.com>2022-02-08 21:36:48 +0200
committerLinus Groh <mail@linusgroh.de>2022-02-08 23:08:43 +0000
commit20d3869182c25e1255ad57277cd9d84575564d83 (patch)
tree7266bb902d1828f9cb9448d974ae4b701579e6cd /Userland
parentd225b5e94cdca39af32f58ba8100fb1bd126a622 (diff)
downloadserenity-20d3869182c25e1255ad57277cd9d84575564d83.zip
LibJS: Implement the CloneArrayBuffer AO
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibJS/Runtime/ArrayBuffer.cpp26
-rw-r--r--Userland/Libraries/LibJS/Runtime/ArrayBuffer.h1
2 files changed, 27 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;
+}
+
}
diff --git a/Userland/Libraries/LibJS/Runtime/ArrayBuffer.h b/Userland/Libraries/LibJS/Runtime/ArrayBuffer.h
index 5fc60f2d3c..58693e49b7 100644
--- a/Userland/Libraries/LibJS/Runtime/ArrayBuffer.h
+++ b/Userland/Libraries/LibJS/Runtime/ArrayBuffer.h
@@ -76,6 +76,7 @@ private:
};
ThrowCompletionOr<ArrayBuffer*> allocate_array_buffer(GlobalObject&, FunctionObject& constructor, size_t byte_length);
+ThrowCompletionOr<ArrayBuffer*> clone_array_buffer(GlobalObject&, ArrayBuffer& source_buffer, size_t source_byte_offset, size_t source_length, FunctionObject& clone_constructor);
// 25.1.2.9 RawBytesToNumeric ( type, rawBytes, isLittleEndian ), https://tc39.es/ecma262/#sec-rawbytestonumeric
template<typename T>