TypedArray.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. 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. // FIXME: This belongs to TypedArray.prototype
  90. define_native_property(vm.names.length, length_getter, nullptr);
  91. m_data = (T*)calloc(m_length, sizeof(T));
  92. }
  93. private:
  94. virtual bool is_typed_array() const final { return true; }
  95. JS_DECLARE_NATIVE_GETTER(length_getter);
  96. T* m_data { nullptr };
  97. u32 m_length { 0 };
  98. };
  99. template<typename T>
  100. inline JS_DEFINE_NATIVE_GETTER(TypedArray<T>::length_getter)
  101. {
  102. auto* this_object = vm.this_value(global_object).to_object(global_object);
  103. if (!this_object)
  104. return {};
  105. if (!this_object->is_typed_array()) {
  106. vm.throw_exception<TypeError>(global_object, ErrorType::NotA, "TypedArray");
  107. return {};
  108. }
  109. return Value(static_cast<const TypedArray*>(this_object)->length());
  110. }
  111. #define JS_DECLARE_TYPED_ARRAY(ClassName, snake_name, PrototypeName, ConstructorName, Type) \
  112. class ClassName : public TypedArray<Type> { \
  113. JS_OBJECT(ClassName, TypedArray); \
  114. \
  115. public: \
  116. virtual ~ClassName(); \
  117. static ClassName* create(GlobalObject&, u32 length); \
  118. ClassName(u32 length, Object& prototype); \
  119. }; \
  120. class PrototypeName final : public Object { \
  121. JS_OBJECT(PrototypeName, Object); \
  122. \
  123. public: \
  124. PrototypeName(GlobalObject&); \
  125. virtual void initialize(GlobalObject&) override; \
  126. virtual ~PrototypeName() override; \
  127. }; \
  128. class ConstructorName final : public TypedArrayConstructor { \
  129. JS_OBJECT(ConstructorName, TypedArrayConstructor); \
  130. \
  131. public: \
  132. explicit ConstructorName(GlobalObject&); \
  133. virtual void initialize(GlobalObject&) override; \
  134. virtual ~ConstructorName() override; \
  135. \
  136. virtual Value call() override; \
  137. virtual Value construct(Function& new_target) override; \
  138. \
  139. private: \
  140. virtual bool has_constructor() const override { return true; } \
  141. };
  142. #undef __JS_ENUMERATE
  143. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
  144. JS_DECLARE_TYPED_ARRAY(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType);
  145. JS_ENUMERATE_TYPED_ARRAYS
  146. #undef __JS_ENUMERATE
  147. }