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