ArrayBuffer.h 8.5 KB

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