v8-typed-array.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. // Copyright 2021 the V8 project authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef INCLUDE_V8_TYPED_ARRAY_H_
  5. #define INCLUDE_V8_TYPED_ARRAY_H_
  6. #include "v8-array-buffer.h" // NOLINT(build/include_directory)
  7. #include "v8-local-handle.h" // NOLINT(build/include_directory)
  8. #include "v8config.h" // NOLINT(build/include_directory)
  9. namespace v8 {
  10. class SharedArrayBuffer;
  11. /**
  12. * A base class for an instance of TypedArray series of constructors
  13. * (ES6 draft 15.13.6).
  14. */
  15. class V8_EXPORT TypedArray : public ArrayBufferView {
  16. public:
  17. /*
  18. * The largest typed array size that can be constructed using New.
  19. */
  20. static constexpr size_t kMaxLength =
  21. internal::kApiSystemPointerSize == 4
  22. ? internal::kSmiMaxValue
  23. : static_cast<size_t>(uint64_t{1} << 32);
  24. /**
  25. * Number of elements in this typed array
  26. * (e.g. for Int16Array, |ByteLength|/2).
  27. */
  28. size_t Length();
  29. V8_INLINE static TypedArray* Cast(Value* value) {
  30. #ifdef V8_ENABLE_CHECKS
  31. CheckCast(value);
  32. #endif
  33. return static_cast<TypedArray*>(value);
  34. }
  35. private:
  36. TypedArray();
  37. static void CheckCast(Value* obj);
  38. };
  39. /**
  40. * An instance of Uint8Array constructor (ES6 draft 15.13.6).
  41. */
  42. class V8_EXPORT Uint8Array : public TypedArray {
  43. public:
  44. static Local<Uint8Array> New(Local<ArrayBuffer> array_buffer,
  45. size_t byte_offset, size_t length);
  46. static Local<Uint8Array> New(Local<SharedArrayBuffer> shared_array_buffer,
  47. size_t byte_offset, size_t length);
  48. V8_INLINE static Uint8Array* Cast(Value* value) {
  49. #ifdef V8_ENABLE_CHECKS
  50. CheckCast(value);
  51. #endif
  52. return static_cast<Uint8Array*>(value);
  53. }
  54. private:
  55. Uint8Array();
  56. static void CheckCast(Value* obj);
  57. };
  58. /**
  59. * An instance of Uint8ClampedArray constructor (ES6 draft 15.13.6).
  60. */
  61. class V8_EXPORT Uint8ClampedArray : public TypedArray {
  62. public:
  63. static Local<Uint8ClampedArray> New(Local<ArrayBuffer> array_buffer,
  64. size_t byte_offset, size_t length);
  65. static Local<Uint8ClampedArray> New(
  66. Local<SharedArrayBuffer> shared_array_buffer, size_t byte_offset,
  67. size_t length);
  68. V8_INLINE static Uint8ClampedArray* Cast(Value* value) {
  69. #ifdef V8_ENABLE_CHECKS
  70. CheckCast(value);
  71. #endif
  72. return static_cast<Uint8ClampedArray*>(value);
  73. }
  74. private:
  75. Uint8ClampedArray();
  76. static void CheckCast(Value* obj);
  77. };
  78. /**
  79. * An instance of Int8Array constructor (ES6 draft 15.13.6).
  80. */
  81. class V8_EXPORT Int8Array : public TypedArray {
  82. public:
  83. static Local<Int8Array> New(Local<ArrayBuffer> array_buffer,
  84. size_t byte_offset, size_t length);
  85. static Local<Int8Array> New(Local<SharedArrayBuffer> shared_array_buffer,
  86. size_t byte_offset, size_t length);
  87. V8_INLINE static Int8Array* Cast(Value* value) {
  88. #ifdef V8_ENABLE_CHECKS
  89. CheckCast(value);
  90. #endif
  91. return static_cast<Int8Array*>(value);
  92. }
  93. private:
  94. Int8Array();
  95. static void CheckCast(Value* obj);
  96. };
  97. /**
  98. * An instance of Uint16Array constructor (ES6 draft 15.13.6).
  99. */
  100. class V8_EXPORT Uint16Array : public TypedArray {
  101. public:
  102. static Local<Uint16Array> New(Local<ArrayBuffer> array_buffer,
  103. size_t byte_offset, size_t length);
  104. static Local<Uint16Array> New(Local<SharedArrayBuffer> shared_array_buffer,
  105. size_t byte_offset, size_t length);
  106. V8_INLINE static Uint16Array* Cast(Value* value) {
  107. #ifdef V8_ENABLE_CHECKS
  108. CheckCast(value);
  109. #endif
  110. return static_cast<Uint16Array*>(value);
  111. }
  112. private:
  113. Uint16Array();
  114. static void CheckCast(Value* obj);
  115. };
  116. /**
  117. * An instance of Int16Array constructor (ES6 draft 15.13.6).
  118. */
  119. class V8_EXPORT Int16Array : public TypedArray {
  120. public:
  121. static Local<Int16Array> New(Local<ArrayBuffer> array_buffer,
  122. size_t byte_offset, size_t length);
  123. static Local<Int16Array> New(Local<SharedArrayBuffer> shared_array_buffer,
  124. size_t byte_offset, size_t length);
  125. V8_INLINE static Int16Array* Cast(Value* value) {
  126. #ifdef V8_ENABLE_CHECKS
  127. CheckCast(value);
  128. #endif
  129. return static_cast<Int16Array*>(value);
  130. }
  131. private:
  132. Int16Array();
  133. static void CheckCast(Value* obj);
  134. };
  135. /**
  136. * An instance of Uint32Array constructor (ES6 draft 15.13.6).
  137. */
  138. class V8_EXPORT Uint32Array : public TypedArray {
  139. public:
  140. static Local<Uint32Array> New(Local<ArrayBuffer> array_buffer,
  141. size_t byte_offset, size_t length);
  142. static Local<Uint32Array> New(Local<SharedArrayBuffer> shared_array_buffer,
  143. size_t byte_offset, size_t length);
  144. V8_INLINE static Uint32Array* Cast(Value* value) {
  145. #ifdef V8_ENABLE_CHECKS
  146. CheckCast(value);
  147. #endif
  148. return static_cast<Uint32Array*>(value);
  149. }
  150. private:
  151. Uint32Array();
  152. static void CheckCast(Value* obj);
  153. };
  154. /**
  155. * An instance of Int32Array constructor (ES6 draft 15.13.6).
  156. */
  157. class V8_EXPORT Int32Array : public TypedArray {
  158. public:
  159. static Local<Int32Array> New(Local<ArrayBuffer> array_buffer,
  160. size_t byte_offset, size_t length);
  161. static Local<Int32Array> New(Local<SharedArrayBuffer> shared_array_buffer,
  162. size_t byte_offset, size_t length);
  163. V8_INLINE static Int32Array* Cast(Value* value) {
  164. #ifdef V8_ENABLE_CHECKS
  165. CheckCast(value);
  166. #endif
  167. return static_cast<Int32Array*>(value);
  168. }
  169. private:
  170. Int32Array();
  171. static void CheckCast(Value* obj);
  172. };
  173. /**
  174. * An instance of Float32Array constructor (ES6 draft 15.13.6).
  175. */
  176. class V8_EXPORT Float32Array : public TypedArray {
  177. public:
  178. static Local<Float32Array> New(Local<ArrayBuffer> array_buffer,
  179. size_t byte_offset, size_t length);
  180. static Local<Float32Array> New(Local<SharedArrayBuffer> shared_array_buffer,
  181. size_t byte_offset, size_t length);
  182. V8_INLINE static Float32Array* Cast(Value* value) {
  183. #ifdef V8_ENABLE_CHECKS
  184. CheckCast(value);
  185. #endif
  186. return static_cast<Float32Array*>(value);
  187. }
  188. private:
  189. Float32Array();
  190. static void CheckCast(Value* obj);
  191. };
  192. /**
  193. * An instance of Float64Array constructor (ES6 draft 15.13.6).
  194. */
  195. class V8_EXPORT Float64Array : public TypedArray {
  196. public:
  197. static Local<Float64Array> New(Local<ArrayBuffer> array_buffer,
  198. size_t byte_offset, size_t length);
  199. static Local<Float64Array> New(Local<SharedArrayBuffer> shared_array_buffer,
  200. size_t byte_offset, size_t length);
  201. V8_INLINE static Float64Array* Cast(Value* value) {
  202. #ifdef V8_ENABLE_CHECKS
  203. CheckCast(value);
  204. #endif
  205. return static_cast<Float64Array*>(value);
  206. }
  207. private:
  208. Float64Array();
  209. static void CheckCast(Value* obj);
  210. };
  211. /**
  212. * An instance of BigInt64Array constructor.
  213. */
  214. class V8_EXPORT BigInt64Array : public TypedArray {
  215. public:
  216. static Local<BigInt64Array> New(Local<ArrayBuffer> array_buffer,
  217. size_t byte_offset, size_t length);
  218. static Local<BigInt64Array> New(Local<SharedArrayBuffer> shared_array_buffer,
  219. size_t byte_offset, size_t length);
  220. V8_INLINE static BigInt64Array* Cast(Value* value) {
  221. #ifdef V8_ENABLE_CHECKS
  222. CheckCast(value);
  223. #endif
  224. return static_cast<BigInt64Array*>(value);
  225. }
  226. private:
  227. BigInt64Array();
  228. static void CheckCast(Value* obj);
  229. };
  230. /**
  231. * An instance of BigUint64Array constructor.
  232. */
  233. class V8_EXPORT BigUint64Array : public TypedArray {
  234. public:
  235. static Local<BigUint64Array> New(Local<ArrayBuffer> array_buffer,
  236. size_t byte_offset, size_t length);
  237. static Local<BigUint64Array> New(Local<SharedArrayBuffer> shared_array_buffer,
  238. size_t byte_offset, size_t length);
  239. V8_INLINE static BigUint64Array* Cast(Value* value) {
  240. #ifdef V8_ENABLE_CHECKS
  241. CheckCast(value);
  242. #endif
  243. return static_cast<BigUint64Array*>(value);
  244. }
  245. private:
  246. BigUint64Array();
  247. static void CheckCast(Value* obj);
  248. };
  249. } // namespace v8
  250. #endif // INCLUDE_V8_TYPED_ARRAY_H_