TypedArray.h 7.3 KB

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