TypedArray.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <LibJS/Runtime/ArrayBuffer.h>
  28. #include <LibJS/Runtime/GlobalObject.h>
  29. #include <LibJS/Runtime/TypedArray.h>
  30. #include <LibJS/Runtime/TypedArrayConstructor.h>
  31. namespace JS {
  32. static void initialize_typed_array_from_array_buffer(GlobalObject& global_object, TypedArrayBase& typed_array, ArrayBuffer& array_buffer, Value byte_offset, Value length)
  33. {
  34. // 22.2.5.1.3 InitializeTypedArrayFromArrayBuffer, https://tc39.es/ecma262/#sec-initializetypedarrayfromarraybuffer
  35. auto& vm = global_object.vm();
  36. auto element_size = typed_array.element_size();
  37. auto offset = byte_offset.to_index(global_object);
  38. if (vm.exception())
  39. return;
  40. if (offset % element_size != 0) {
  41. vm.throw_exception<RangeError>(global_object, ErrorType::TypedArrayInvalidByteOffset, typed_array.class_name(), element_size, offset);
  42. return;
  43. }
  44. size_t new_length { 0 };
  45. if (!length.is_undefined()) {
  46. new_length = length.to_index(global_object);
  47. if (vm.exception())
  48. return;
  49. }
  50. // FIXME: 8. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
  51. auto buffer_byte_length = array_buffer.byte_length();
  52. size_t new_byte_length;
  53. if (length.is_undefined()) {
  54. if (buffer_byte_length % element_size != 0) {
  55. vm.throw_exception<RangeError>(global_object, ErrorType::TypedArrayInvalidBufferLength, typed_array.class_name(), element_size, buffer_byte_length);
  56. return;
  57. }
  58. if (offset > buffer_byte_length) {
  59. vm.throw_exception<RangeError>(global_object, ErrorType::TypedArrayOutOfRangeByteOffset, offset, buffer_byte_length);
  60. return;
  61. }
  62. new_byte_length = buffer_byte_length - offset;
  63. } else {
  64. new_byte_length = new_length * element_size;
  65. if (offset + new_byte_length > buffer_byte_length) {
  66. vm.throw_exception<RangeError>(global_object, ErrorType::TypedArrayOutOfRangeByteOffsetOrLength, offset, offset + new_byte_length, buffer_byte_length);
  67. return;
  68. }
  69. }
  70. typed_array.set_viewed_array_buffer(&array_buffer);
  71. typed_array.set_byte_length(new_byte_length);
  72. typed_array.set_byte_offset(offset);
  73. typed_array.set_array_length(new_byte_length / element_size);
  74. }
  75. void TypedArrayBase::visit_edges(Visitor& visitor)
  76. {
  77. Object::visit_edges(visitor);
  78. visitor.visit(m_viewed_array_buffer);
  79. }
  80. #define JS_DEFINE_TYPED_ARRAY(ClassName, snake_name, PrototypeName, ConstructorName, Type) \
  81. ClassName* ClassName::create(GlobalObject& global_object, u32 length) \
  82. { \
  83. return global_object.heap().allocate<ClassName>(global_object, length, *global_object.snake_name##_prototype()); \
  84. } \
  85. \
  86. ClassName::ClassName(u32 length, Object& prototype) \
  87. : TypedArray(length, prototype) \
  88. { \
  89. } \
  90. ClassName::~ClassName() { } \
  91. \
  92. PrototypeName::PrototypeName(GlobalObject& global_object) \
  93. : Object(*global_object.typed_array_prototype()) \
  94. { \
  95. } \
  96. PrototypeName::~PrototypeName() { } \
  97. \
  98. ConstructorName::ConstructorName(GlobalObject& global_object) \
  99. : TypedArrayConstructor(vm().names.ClassName, *global_object.typed_array_constructor()) \
  100. { \
  101. } \
  102. ConstructorName::~ConstructorName() { } \
  103. void ConstructorName::initialize(GlobalObject& global_object) \
  104. { \
  105. auto& vm = this->vm(); \
  106. NativeFunction::initialize(global_object); \
  107. define_property(vm.names.prototype, global_object.snake_name##_prototype(), 0); \
  108. define_property(vm.names.length, Value(1), Attribute::Configurable); \
  109. define_property(vm.names.BYTES_PER_ELEMENT, Value((i32)sizeof(Type)), 0); \
  110. } \
  111. Value ConstructorName::call() \
  112. { \
  113. auto& vm = this->vm(); \
  114. vm.throw_exception<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, vm.names.ClassName); \
  115. return {}; \
  116. } \
  117. Value ConstructorName::construct(Function&) \
  118. { \
  119. auto& vm = this->vm(); \
  120. if (vm.argument_count() == 0) \
  121. return ClassName::create(global_object(), 0); \
  122. \
  123. auto first_argument = vm.argument(0); \
  124. if (first_argument.is_object()) { \
  125. auto* typed_array = ClassName::create(global_object(), 0); \
  126. if (first_argument.as_object().is_typed_array()) { \
  127. /* FIXME: Initialize from TypedArray */ \
  128. TODO(); \
  129. } else if (is<ArrayBuffer>(first_argument.as_object())) { \
  130. auto& array_buffer = static_cast<ArrayBuffer&>(first_argument.as_object()); \
  131. initialize_typed_array_from_array_buffer(global_object(), *typed_array, array_buffer, vm.argument(1), vm.argument(2)); \
  132. if (vm.exception()) \
  133. return {}; \
  134. } else { \
  135. /* FIXME: Initialize from Iterator or Array-like object */ \
  136. TODO(); \
  137. } \
  138. return typed_array; \
  139. } \
  140. \
  141. auto array_length = first_argument.to_index(global_object()); \
  142. if (vm.exception()) { \
  143. /* Re-throw more specific RangeError */ \
  144. vm.clear_exception(); \
  145. vm.throw_exception<RangeError>(global_object(), ErrorType::InvalidLength, "typed array"); \
  146. return {}; \
  147. } \
  148. return ClassName::create(global_object(), array_length); \
  149. }
  150. #undef __JS_ENUMERATE
  151. #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
  152. JS_DEFINE_TYPED_ARRAY(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType);
  153. JS_ENUMERATE_TYPED_ARRAYS
  154. #undef __JS_ENUMERATE
  155. }