ArrayBuffer.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibJS/Runtime/AbstractOperations.h>
  8. #include <LibJS/Runtime/ArrayBuffer.h>
  9. #include <LibJS/Runtime/GlobalObject.h>
  10. namespace JS {
  11. ThrowCompletionOr<ArrayBuffer*> ArrayBuffer::create(GlobalObject& global_object, size_t byte_length)
  12. {
  13. auto buffer = ByteBuffer::create_zeroed(byte_length);
  14. if (buffer.is_error())
  15. return global_object.vm().throw_completion<RangeError>(global_object, ErrorType::NotEnoughMemoryToAllocate, byte_length);
  16. return global_object.heap().allocate<ArrayBuffer>(global_object, buffer.release_value(), *global_object.array_buffer_prototype());
  17. }
  18. ArrayBuffer* ArrayBuffer::create(GlobalObject& global_object, ByteBuffer buffer)
  19. {
  20. return global_object.heap().allocate<ArrayBuffer>(global_object, move(buffer), *global_object.array_buffer_prototype());
  21. }
  22. ArrayBuffer* ArrayBuffer::create(GlobalObject& global_object, ByteBuffer* buffer)
  23. {
  24. return global_object.heap().allocate<ArrayBuffer>(global_object, buffer, *global_object.array_buffer_prototype());
  25. }
  26. ArrayBuffer::ArrayBuffer(ByteBuffer buffer, Object& prototype)
  27. : Object(prototype)
  28. , m_buffer(move(buffer))
  29. , m_detach_key(js_undefined())
  30. {
  31. }
  32. ArrayBuffer::ArrayBuffer(ByteBuffer* buffer, Object& prototype)
  33. : Object(prototype)
  34. , m_buffer(buffer)
  35. , m_detach_key(js_undefined())
  36. {
  37. }
  38. ArrayBuffer::~ArrayBuffer()
  39. {
  40. }
  41. // 1.1.5 IsResizableArrayBuffer ( arrayBuffer ), https://tc39.es/proposal-resizablearraybuffer/#sec-isresizablearraybuffer
  42. bool ArrayBuffer::is_resizable_array_buffer() const
  43. {
  44. // 1. Assert: Type(arrayBuffer) is Object and arrayBuffer has an [[ArrayBufferData]] internal slot.
  45. // 2. If buffer has an [[ArrayBufferMaxByteLength]] internal slot, return true.
  46. // 3. Return false.
  47. return m_max_byte_length.has_value();
  48. }
  49. void ArrayBuffer::visit_edges(Cell::Visitor& visitor)
  50. {
  51. Base::visit_edges(visitor);
  52. visitor.visit(m_detach_key);
  53. }
  54. // 25.1.2.1 AllocateArrayBuffer ( constructor, byteLength ), https://tc39.es/ecma262/#sec-allocatearraybuffer
  55. // 1.1.2 AllocateArrayBuffer ( constructor, byteLength [, maxByteLength ] ), https://tc39.es/proposal-resizablearraybuffer/#sec-allocatearraybuffer
  56. ThrowCompletionOr<ArrayBuffer*> allocate_array_buffer(GlobalObject& global_object, FunctionObject& constructor, size_t byte_length, Optional<size_t> max_byte_length)
  57. {
  58. // 1. Let slots be « [[ArrayBufferData]], [[ArrayBufferByteLength]], [[ArrayBufferDetachKey]] ».
  59. // 2. If maxByteLength is present, append [[ArrayBufferMaxByteLength]] to slots.
  60. // 3. Let obj be ? OrdinaryCreateFromConstructor(constructor, "%ArrayBuffer.prototype%", slots).
  61. auto* obj = TRY(ordinary_create_from_constructor<ArrayBuffer>(global_object, constructor, &GlobalObject::array_buffer_prototype, nullptr));
  62. // 4. Let block be ? CreateByteDataBlock(byteLength).
  63. auto block = ByteBuffer::create_zeroed(byte_length);
  64. if (block.is_error())
  65. return global_object.vm().throw_completion<RangeError>(global_object, ErrorType::NotEnoughMemoryToAllocate, byte_length);
  66. // 5. Set obj.[[ArrayBufferData]] to block.
  67. obj->set_buffer(block.release_value());
  68. // 6. Set obj.[[ArrayBufferByteLength]] to byteLength.
  69. // 7. If maxByteLength is present, then
  70. if (max_byte_length.has_value()) {
  71. // a. Assert: byteLength ≤ maxByteLength.
  72. VERIFY(byte_length <= *max_byte_length);
  73. // b. If it is not possible to create a Data Block block consisting of maxByteLength bytes, throw a RangeError exception.
  74. // c. NOTE: Resizable ArrayBuffers are designed to be implementable with in-place growth. Implementations reserve the right to throw if, for example, virtual memory cannot be reserved up front.
  75. // d. Set obj.[[ArrayBufferMaxByteLength]] to maxByteLength.
  76. obj->set_max_byte_length(*max_byte_length);
  77. }
  78. // 8. Return obj.
  79. return obj;
  80. }
  81. // 25.1.2.4 CloneArrayBuffer ( srcBuffer, srcByteOffset, srcLength, cloneConstructor ), https://tc39.es/ecma262/#sec-clonearraybuffer
  82. ThrowCompletionOr<ArrayBuffer*> clone_array_buffer(GlobalObject& global_object, ArrayBuffer& source_buffer, size_t source_byte_offset, size_t source_length, FunctionObject& clone_constructor)
  83. {
  84. auto& vm = global_object.vm();
  85. // 1. Let targetBuffer be ? AllocateArrayBuffer(cloneConstructor, srcLength).
  86. auto* target_buffer = TRY(allocate_array_buffer(global_object, clone_constructor, source_length));
  87. // 2. If IsDetachedBuffer(srcBuffer) is true, throw a TypeError exception.
  88. if (source_buffer.is_detached())
  89. return vm.throw_completion<TypeError>(global_object, ErrorType::DetachedArrayBuffer);
  90. // 3. Let srcBlock be srcBuffer.[[ArrayBufferData]].
  91. auto& source_block = source_buffer.buffer();
  92. // 4. Let targetBlock be targetBuffer.[[ArrayBufferData]].
  93. auto& target_block = target_buffer->buffer();
  94. // 5. Perform CopyDataBlockBytes(targetBlock, 0, srcBlock, srcByteOffset, srcLength).
  95. // FIXME: This is only correct for ArrayBuffers, once SharedArrayBuffer is implemented, the AO has to be implemented
  96. target_block.overwrite(0, source_block.offset_pointer(source_byte_offset), source_length);
  97. // 6. Return targetBuffer.
  98. return target_buffer;
  99. }
  100. }