TypedArray.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <LibJS/Runtime/ArrayBuffer.h>
  28. #include <LibJS/Runtime/GlobalObject.h>
  29. #include <LibJS/Runtime/TypedArrayConstructor.h>
  30. #include <LibJS/Runtime/VM.h>
  31. namespace JS {
  32. class TypedArrayBase : public Object {
  33. JS_OBJECT(TypedArrayBase, Object);
  34. public:
  35. u32 array_length() const { return m_array_length; }
  36. u32 byte_length() const { return m_byte_length; }
  37. u32 byte_offset() const { return m_byte_offset; }
  38. ArrayBuffer* viewed_array_buffer() const { return m_viewed_array_buffer; }
  39. void set_array_length(u32 length) { m_array_length = length; }
  40. void set_byte_length(u32 length) { m_byte_length = length; }
  41. void set_byte_offset(u32 offset) { m_byte_offset = offset; }
  42. void set_viewed_array_buffer(ArrayBuffer* array_buffer) { m_viewed_array_buffer = array_buffer; }
  43. virtual size_t element_size() const = 0;
  44. protected:
  45. TypedArrayBase(Object& prototype)
  46. : Object(prototype)
  47. {
  48. }
  49. u32 m_array_length { 0 };
  50. u32 m_byte_length { 0 };
  51. u32 m_byte_offset { 0 };
  52. ArrayBuffer* m_viewed_array_buffer { nullptr };
  53. private:
  54. virtual void visit_edges(Visitor&) override;
  55. };
  56. template<typename T>
  57. class TypedArray : public TypedArrayBase {
  58. JS_OBJECT(TypedArray, TypedArrayBase);
  59. public:
  60. virtual bool put_by_index(u32 property_index, Value value) override
  61. {
  62. property_index += m_byte_offset / sizeof(T);
  63. if (property_index >= m_array_length)
  64. return Base::put_by_index(property_index, value);
  65. if constexpr (sizeof(T) < 4) {
  66. auto number = value.to_i32(global_object());
  67. if (vm().exception())
  68. return {};
  69. data()[property_index] = number;
  70. } else if constexpr (sizeof(T) == 4 || sizeof(T) == 8) {
  71. auto number = value.to_double(global_object());
  72. if (vm().exception())
  73. return {};
  74. data()[property_index] = number;
  75. } else {
  76. static_assert(DependentFalse<T>, "TypedArray::put_by_index with unhandled type size");
  77. }
  78. return true;
  79. }
  80. virtual Value get_by_index(u32 property_index) const override
  81. {
  82. property_index += m_byte_offset / sizeof(T);
  83. if (property_index >= m_array_length)
  84. return Base::get_by_index(property_index);
  85. if constexpr (sizeof(T) < 4) {
  86. return Value((i32)data()[property_index]);
  87. } else if constexpr (sizeof(T) == 4 || sizeof(T) == 8) {
  88. auto value = data()[property_index];
  89. if constexpr (IsFloatingPoint<T>::value) {
  90. return Value((double)value);
  91. } else if constexpr (NumericLimits<T>::is_signed()) {
  92. if (value > NumericLimits<i32>::max() || value < NumericLimits<i32>::min())
  93. return Value((double)value);
  94. } else {
  95. if (value > NumericLimits<i32>::max())
  96. return Value((double)value);
  97. }
  98. return Value((i32)value);
  99. } else {
  100. static_assert(DependentFalse<T>, "TypedArray::get_by_index with unhandled type size");
  101. }
  102. }
  103. T* data() const { return reinterpret_cast<T*>(m_viewed_array_buffer->buffer().data()); }
  104. virtual size_t element_size() const override { return sizeof(T); };
  105. protected:
  106. TypedArray(ArrayBuffer& array_buffer, u32 array_length, Object& prototype)
  107. : TypedArrayBase(prototype)
  108. {
  109. m_viewed_array_buffer = &array_buffer;
  110. m_array_length = array_length;
  111. m_byte_length = m_viewed_array_buffer->byte_length();
  112. }
  113. TypedArray(u32 array_length, Object& prototype)
  114. : TypedArrayBase(prototype)
  115. {
  116. m_viewed_array_buffer = ArrayBuffer::create(global_object(), array_length * sizeof(T));
  117. m_array_length = array_length;
  118. m_byte_length = m_viewed_array_buffer->byte_length();
  119. }
  120. private:
  121. virtual bool is_typed_array() const final { return true; }
  122. };
  123. #define JS_DECLARE_TYPED_ARRAY(ClassName, snake_name, PrototypeName, ConstructorName, Type) \
  124. class ClassName : public TypedArray<Type> { \
  125. JS_OBJECT(ClassName, TypedArray); \
  126. \
  127. public: \
  128. virtual ~ClassName(); \
  129. static ClassName* create(GlobalObject&, u32 length); \
  130. ClassName(u32 length, Object& prototype); \
  131. }; \
  132. class PrototypeName final : public Object { \
  133. JS_OBJECT(PrototypeName, Object); \
  134. \
  135. public: \
  136. PrototypeName(GlobalObject&); \
  137. virtual ~PrototypeName() override; \
  138. }; \
  139. class ConstructorName final : public TypedArrayConstructor { \
  140. JS_OBJECT(ConstructorName, TypedArrayConstructor); \
  141. \
  142. public: \
  143. explicit ConstructorName(GlobalObject&); \
  144. virtual void initialize(GlobalObject&) override; \
  145. virtual ~ConstructorName() override; \
  146. \
  147. virtual Value call() override; \
  148. virtual Value construct(Function& new_target) override; \
  149. \
  150. private: \
  151. virtual bool has_constructor() const override { return true; } \
  152. };
  153. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, Type) \
  154. JS_DECLARE_TYPED_ARRAY(ClassName, snake_name, PrototypeName, ConstructorName, Type);
  155. JS_ENUMERATE_TYPED_ARRAYS
  156. #undef __JS_ENUMERATE
  157. }