ArrayBuffer.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/ByteBuffer.h>
  8. #include <AK/Function.h>
  9. #include <AK/Variant.h>
  10. #include <LibJS/Runtime/Completion.h>
  11. #include <LibJS/Runtime/GlobalObject.h>
  12. #include <LibJS/Runtime/Object.h>
  13. namespace JS {
  14. struct ClampedU8 {
  15. };
  16. // 25.1.1 Notation (read-modify-write modification function), https://tc39.es/ecma262/#sec-arraybuffer-notation
  17. using ReadWriteModifyFunction = Function<ByteBuffer(ByteBuffer, ByteBuffer)>;
  18. class ArrayBuffer : public Object {
  19. JS_OBJECT(ArrayBuffer, Object);
  20. public:
  21. static ArrayBuffer* create(GlobalObject&, size_t);
  22. static ArrayBuffer* create(GlobalObject&, ByteBuffer*);
  23. ArrayBuffer(ByteBuffer buffer, Object& prototype);
  24. ArrayBuffer(ByteBuffer* buffer, Object& prototype);
  25. virtual ~ArrayBuffer() override;
  26. size_t byte_length() const { return buffer_impl().size(); }
  27. ByteBuffer& buffer() { return buffer_impl(); }
  28. const ByteBuffer& buffer() const { return buffer_impl(); }
  29. // Used by allocate_array_buffer() to attach the data block after construction
  30. void set_buffer(ByteBuffer buffer) { m_buffer = move(buffer); }
  31. Value detach_key() const { return m_detach_key; }
  32. void set_detach_key(Value detach_key) { m_detach_key = detach_key; }
  33. void detach_buffer() { m_buffer = Empty {}; }
  34. bool is_detached() const { return m_buffer.has<Empty>(); }
  35. enum Order {
  36. SeqCst,
  37. Unordered
  38. };
  39. template<typename type>
  40. Value get_value(size_t byte_index, bool is_typed_array, Order, bool is_little_endian = true);
  41. template<typename type>
  42. Value set_value(size_t byte_index, Value value, bool is_typed_array, Order, bool is_little_endian = true);
  43. template<typename T>
  44. Value get_modify_set_value(size_t byte_index, Value value, ReadWriteModifyFunction operation, bool is_little_endian = true);
  45. private:
  46. virtual void visit_edges(Visitor&) override;
  47. ByteBuffer& buffer_impl()
  48. {
  49. ByteBuffer* ptr { nullptr };
  50. m_buffer.visit([&](Empty) { VERIFY_NOT_REACHED(); }, [&](auto* pointer) { ptr = pointer; }, [&](auto& value) { ptr = &value; });
  51. return *ptr;
  52. }
  53. const ByteBuffer& buffer_impl() const { return const_cast<ArrayBuffer*>(this)->buffer_impl(); }
  54. Variant<Empty, ByteBuffer, ByteBuffer*> m_buffer;
  55. // The various detach related members of ArrayBuffer are not used by any ECMA262 functionality,
  56. // but are required to be available for the use of various harnesses like the Test262 test runner.
  57. Value m_detach_key;
  58. };
  59. ThrowCompletionOr<ArrayBuffer*> allocate_array_buffer(GlobalObject&, FunctionObject& constructor, size_t byte_length);
  60. // 25.1.2.9 RawBytesToNumeric ( type, rawBytes, isLittleEndian ), https://tc39.es/ecma262/#sec-rawbytestonumeric
  61. template<typename T>
  62. static Value raw_bytes_to_numeric(GlobalObject& global_object, ByteBuffer raw_value, bool is_little_endian)
  63. {
  64. if (!is_little_endian) {
  65. VERIFY(raw_value.size() % 2 == 0);
  66. for (size_t i = 0; i < raw_value.size() / 2; ++i)
  67. swap(raw_value[i], raw_value[raw_value.size() - 1 - i]);
  68. }
  69. using UnderlyingBufferDataType = Conditional<IsSame<ClampedU8, T>, u8, T>;
  70. if constexpr (IsSame<UnderlyingBufferDataType, float>) {
  71. float value;
  72. raw_value.span().copy_to({ &value, sizeof(float) });
  73. if (isnan(value))
  74. return js_nan();
  75. return Value(value);
  76. }
  77. if constexpr (IsSame<UnderlyingBufferDataType, double>) {
  78. double value;
  79. raw_value.span().copy_to({ &value, sizeof(double) });
  80. if (isnan(value))
  81. return js_nan();
  82. return Value(value);
  83. }
  84. if constexpr (!IsIntegral<UnderlyingBufferDataType>)
  85. VERIFY_NOT_REACHED();
  86. UnderlyingBufferDataType int_value = 0;
  87. raw_value.span().copy_to({ &int_value, sizeof(UnderlyingBufferDataType) });
  88. if constexpr (sizeof(UnderlyingBufferDataType) == 8) {
  89. if constexpr (IsSigned<UnderlyingBufferDataType>)
  90. return js_bigint(global_object.heap(), Crypto::SignedBigInteger::create_from(int_value));
  91. else
  92. return js_bigint(global_object.heap(), Crypto::SignedBigInteger { Crypto::UnsignedBigInteger::create_from(int_value) });
  93. } else {
  94. return Value(int_value);
  95. }
  96. }
  97. // Implementation for 25.1.2.10 GetValueFromBuffer, used in TypedArray<T>::get_value_from_buffer().
  98. template<typename T>
  99. Value ArrayBuffer::get_value(size_t byte_index, [[maybe_unused]] bool is_typed_array, Order, bool is_little_endian)
  100. {
  101. auto element_size = sizeof(T);
  102. // FIXME: Check for shared buffer
  103. auto raw_value = buffer_impl().slice(byte_index, element_size);
  104. return raw_bytes_to_numeric<T>(global_object(), move(raw_value), is_little_endian);
  105. }
  106. // 25.1.2.11 NumericToRawBytes ( type, value, isLittleEndian ), https://tc39.es/ecma262/#sec-numerictorawbytes
  107. template<typename T>
  108. static ByteBuffer numeric_to_raw_bytes(GlobalObject& global_object, Value value, bool is_little_endian)
  109. {
  110. using UnderlyingBufferDataType = Conditional<IsSame<ClampedU8, T>, u8, T>;
  111. ByteBuffer raw_bytes = ByteBuffer::create_uninitialized(sizeof(UnderlyingBufferDataType)).release_value(); // FIXME: Handle possible OOM situation.
  112. auto flip_if_needed = [&]() {
  113. if (is_little_endian)
  114. return;
  115. VERIFY(sizeof(UnderlyingBufferDataType) % 2 == 0);
  116. for (size_t i = 0; i < sizeof(UnderlyingBufferDataType) / 2; ++i)
  117. swap(raw_bytes[i], raw_bytes[sizeof(UnderlyingBufferDataType) - 1 - i]);
  118. };
  119. if constexpr (IsSame<UnderlyingBufferDataType, float>) {
  120. float raw_value = value.to_double(global_object);
  121. ReadonlyBytes { &raw_value, sizeof(float) }.copy_to(raw_bytes);
  122. flip_if_needed();
  123. return raw_bytes;
  124. }
  125. if constexpr (IsSame<UnderlyingBufferDataType, double>) {
  126. double raw_value = value.to_double(global_object);
  127. ReadonlyBytes { &raw_value, sizeof(double) }.copy_to(raw_bytes);
  128. flip_if_needed();
  129. return raw_bytes;
  130. }
  131. if constexpr (!IsIntegral<UnderlyingBufferDataType>)
  132. VERIFY_NOT_REACHED();
  133. if constexpr (sizeof(UnderlyingBufferDataType) == 8) {
  134. UnderlyingBufferDataType int_value;
  135. if constexpr (IsSigned<UnderlyingBufferDataType>)
  136. int_value = value.to_bigint_int64(global_object);
  137. else
  138. int_value = value.to_bigint_uint64(global_object);
  139. ReadonlyBytes { &int_value, sizeof(UnderlyingBufferDataType) }.copy_to(raw_bytes);
  140. flip_if_needed();
  141. return raw_bytes;
  142. } else {
  143. UnderlyingBufferDataType int_value;
  144. if constexpr (IsSigned<UnderlyingBufferDataType>) {
  145. if constexpr (sizeof(UnderlyingBufferDataType) == 4)
  146. int_value = value.to_i32(global_object);
  147. else if constexpr (sizeof(UnderlyingBufferDataType) == 2)
  148. int_value = value.to_i16(global_object);
  149. else
  150. int_value = value.to_i8(global_object);
  151. } else {
  152. if constexpr (sizeof(UnderlyingBufferDataType) == 4)
  153. int_value = value.to_u32(global_object);
  154. else if constexpr (sizeof(UnderlyingBufferDataType) == 2)
  155. int_value = value.to_u16(global_object);
  156. else if constexpr (!IsSame<T, ClampedU8>)
  157. int_value = value.to_u8(global_object);
  158. else
  159. int_value = value.to_u8_clamp(global_object);
  160. }
  161. ReadonlyBytes { &int_value, sizeof(UnderlyingBufferDataType) }.copy_to(raw_bytes);
  162. if constexpr (sizeof(UnderlyingBufferDataType) % 2 == 0)
  163. flip_if_needed();
  164. return raw_bytes;
  165. }
  166. }
  167. // 25.1.2.12 SetValueInBuffer ( arrayBuffer, byteIndex, type, value, isTypedArray, order [ , isLittleEndian ] ), https://tc39.es/ecma262/#sec-setvalueinbuffer
  168. template<typename T>
  169. Value ArrayBuffer::set_value(size_t byte_index, Value value, [[maybe_unused]] bool is_typed_array, Order, bool is_little_endian)
  170. {
  171. auto raw_bytes = numeric_to_raw_bytes<T>(global_object(), value, is_little_endian);
  172. // FIXME: Check for shared buffer
  173. raw_bytes.span().copy_to(buffer_impl().span().slice(byte_index));
  174. return js_undefined();
  175. }
  176. // 25.1.2.13 GetModifySetValueInBuffer ( arrayBuffer, byteIndex, type, value, op [ , isLittleEndian ] ), https://tc39.es/ecma262/#sec-getmodifysetvalueinbuffer
  177. template<typename T>
  178. Value ArrayBuffer::get_modify_set_value(size_t byte_index, Value value, ReadWriteModifyFunction operation, bool is_little_endian)
  179. {
  180. auto raw_bytes = numeric_to_raw_bytes<T>(global_object(), value, is_little_endian);
  181. // FIXME: Check for shared buffer
  182. auto raw_bytes_read = buffer_impl().slice(byte_index, sizeof(T));
  183. auto raw_bytes_modified = operation(raw_bytes_read, raw_bytes);
  184. raw_bytes_modified.span().copy_to(buffer_impl().span().slice(byte_index));
  185. return raw_bytes_to_numeric<T>(global_object(), raw_bytes_read, is_little_endian);
  186. }
  187. }