TypedArray.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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/GlobalObject.h>
  28. #include <LibJS/Runtime/TypedArrayConstructor.h>
  29. #include <LibJS/Runtime/VM.h>
  30. namespace JS {
  31. class TypedArrayBase : public Object {
  32. JS_OBJECT(TypedArrayBase, Object);
  33. public:
  34. u32 length() const { return m_length; }
  35. protected:
  36. TypedArrayBase(u32 length, Object& prototype)
  37. : Object(prototype)
  38. , m_length(length)
  39. {
  40. }
  41. u32 m_length { 0 };
  42. };
  43. template<typename T>
  44. class TypedArray : public TypedArrayBase {
  45. JS_OBJECT(TypedArray, TypedArrayBase);
  46. public:
  47. virtual ~TypedArray() override
  48. {
  49. ASSERT(m_data);
  50. free(m_data);
  51. m_data = nullptr;
  52. }
  53. virtual bool put_by_index(u32 property_index, Value value) override
  54. {
  55. if (property_index >= m_length)
  56. return Base::put_by_index(property_index, value);
  57. if constexpr (sizeof(T) < 4) {
  58. auto number = value.to_i32(global_object());
  59. if (vm().exception())
  60. return {};
  61. m_data[property_index] = number;
  62. } else if constexpr (sizeof(T) == 4) {
  63. auto number = value.to_double(global_object());
  64. if (vm().exception())
  65. return {};
  66. m_data[property_index] = number;
  67. } else {
  68. static_assert(DependentFalse<T>, "TypedArray::put_by_index with unhandled type size");
  69. }
  70. return true;
  71. }
  72. virtual Value get_by_index(u32 property_index) const override
  73. {
  74. if (property_index >= m_length)
  75. return Base::get_by_index(property_index);
  76. if constexpr (sizeof(T) < 4) {
  77. return Value((i32)m_data[property_index]);
  78. } else if constexpr (sizeof(T) == 4) {
  79. auto value = m_data[property_index];
  80. if constexpr (NumericLimits<T>::is_signed()) {
  81. if (value > NumericLimits<i32>::max() || value < NumericLimits<i32>::min())
  82. return Value((double)value);
  83. } else {
  84. if (value > NumericLimits<i32>::max())
  85. return Value((double)value);
  86. }
  87. return Value((i32)value);
  88. } else {
  89. static_assert(DependentFalse<T>, "TypedArray::get_by_index with unhandled type size");
  90. }
  91. }
  92. T* data() { return m_data; }
  93. const T* data() const { return m_data; }
  94. protected:
  95. TypedArray(u32 length, Object& prototype)
  96. : TypedArrayBase(length, prototype)
  97. {
  98. m_data = (T*)calloc(m_length, sizeof(T));
  99. }
  100. private:
  101. virtual bool is_typed_array() const final { return true; }
  102. T* m_data { nullptr };
  103. };
  104. #define JS_DECLARE_TYPED_ARRAY(ClassName, snake_name, PrototypeName, ConstructorName, Type) \
  105. class ClassName : public TypedArray<Type> { \
  106. JS_OBJECT(ClassName, TypedArray); \
  107. \
  108. public: \
  109. virtual ~ClassName(); \
  110. static ClassName* create(GlobalObject&, u32 length); \
  111. ClassName(u32 length, Object& prototype); \
  112. }; \
  113. class PrototypeName final : public Object { \
  114. JS_OBJECT(PrototypeName, Object); \
  115. \
  116. public: \
  117. PrototypeName(GlobalObject&); \
  118. virtual ~PrototypeName() override; \
  119. }; \
  120. class ConstructorName final : public TypedArrayConstructor { \
  121. JS_OBJECT(ConstructorName, TypedArrayConstructor); \
  122. \
  123. public: \
  124. explicit ConstructorName(GlobalObject&); \
  125. virtual void initialize(GlobalObject&) override; \
  126. virtual ~ConstructorName() override; \
  127. \
  128. virtual Value call() override; \
  129. virtual Value construct(Function& new_target) override; \
  130. \
  131. private: \
  132. virtual bool has_constructor() const override { return true; } \
  133. };
  134. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, Type) \
  135. JS_DECLARE_TYPED_ARRAY(ClassName, snake_name, PrototypeName, ConstructorName, Type);
  136. JS_ENUMERATE_TYPED_ARRAYS
  137. #undef __JS_ENUMERATE
  138. }