jseption 0.0.0-stage → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (114) hide show
  1. package/LICENSE +26 -0
  2. package/README.md +191 -2
  3. package/dist/atoms.d.ts +255 -0
  4. package/dist/atoms.js +445 -0
  5. package/dist/atoms.js.map +1 -0
  6. package/dist/builtins/array.d.ts +3 -0
  7. package/dist/builtins/array.js +116 -0
  8. package/dist/builtins/array.js.map +1 -0
  9. package/dist/builtins/bigint.d.ts +3 -0
  10. package/dist/builtins/bigint.js +122 -0
  11. package/dist/builtins/bigint.js.map +1 -0
  12. package/dist/builtins/boolean.d.ts +2 -0
  13. package/dist/builtins/boolean.js +26 -0
  14. package/dist/builtins/boolean.js.map +1 -0
  15. package/dist/builtins/date.d.ts +3 -0
  16. package/dist/builtins/date.js +1046 -0
  17. package/dist/builtins/date.js.map +1 -0
  18. package/dist/builtins/error.d.ts +2 -0
  19. package/dist/builtins/error.js +154 -0
  20. package/dist/builtins/error.js.map +1 -0
  21. package/dist/builtins/function.d.ts +5 -0
  22. package/dist/builtins/function.js +164 -0
  23. package/dist/builtins/function.js.map +1 -0
  24. package/dist/builtins/global.d.ts +2 -0
  25. package/dist/builtins/global.js +29 -0
  26. package/dist/builtins/global.js.map +1 -0
  27. package/dist/builtins/index.d.ts +3 -0
  28. package/dist/builtins/index.js +139 -0
  29. package/dist/builtins/index.js.map +1 -0
  30. package/dist/builtins/json.d.ts +6 -0
  31. package/dist/builtins/json.js +984 -0
  32. package/dist/builtins/json.js.map +1 -0
  33. package/dist/builtins/mapset.d.ts +4 -0
  34. package/dist/builtins/mapset.js +234 -0
  35. package/dist/builtins/mapset.js.map +1 -0
  36. package/dist/builtins/math.d.ts +2 -0
  37. package/dist/builtins/math.js +62 -0
  38. package/dist/builtins/math.js.map +1 -0
  39. package/dist/builtins/native.d.ts +22 -0
  40. package/dist/builtins/native.js +250 -0
  41. package/dist/builtins/native.js.map +1 -0
  42. package/dist/builtins/number.d.ts +4 -0
  43. package/dist/builtins/number.js +76 -0
  44. package/dist/builtins/number.js.map +1 -0
  45. package/dist/builtins/object.d.ts +12 -0
  46. package/dist/builtins/object.js +575 -0
  47. package/dist/builtins/object.js.map +1 -0
  48. package/dist/builtins/promise.d.ts +13 -0
  49. package/dist/builtins/promise.js +582 -0
  50. package/dist/builtins/promise.js.map +1 -0
  51. package/dist/builtins/proxy.d.ts +3 -0
  52. package/dist/builtins/proxy.js +226 -0
  53. package/dist/builtins/proxy.js.map +1 -0
  54. package/dist/builtins/reflect.d.ts +2 -0
  55. package/dist/builtins/reflect.js +109 -0
  56. package/dist/builtins/reflect.js.map +1 -0
  57. package/dist/builtins/regexp.d.ts +2 -0
  58. package/dist/builtins/regexp.js +203 -0
  59. package/dist/builtins/regexp.js.map +1 -0
  60. package/dist/builtins/string.d.ts +4 -0
  61. package/dist/builtins/string.js +135 -0
  62. package/dist/builtins/string.js.map +1 -0
  63. package/dist/builtins/symbol.d.ts +3 -0
  64. package/dist/builtins/symbol.js +98 -0
  65. package/dist/builtins/symbol.js.map +1 -0
  66. package/dist/builtins/typedarray.d.ts +15 -0
  67. package/dist/builtins/typedarray.js +1838 -0
  68. package/dist/builtins/typedarray.js.map +1 -0
  69. package/dist/builtins/weakref.d.ts +18 -0
  70. package/dist/builtins/weakref.js +130 -0
  71. package/dist/builtins/weakref.js.map +1 -0
  72. package/dist/dump.d.ts +2 -0
  73. package/dist/dump.js +124 -0
  74. package/dist/dump.js.map +1 -0
  75. package/dist/embed.d.ts +18 -0
  76. package/dist/embed.js +286 -0
  77. package/dist/embed.js.map +1 -0
  78. package/dist/interpreter.d.ts +81 -0
  79. package/dist/interpreter.js +3768 -0
  80. package/dist/interpreter.js.map +1 -0
  81. package/dist/jit.d.ts +59 -0
  82. package/dist/jit.js +3440 -0
  83. package/dist/jit.js.map +1 -0
  84. package/dist/jit_hook.d.ts +21 -0
  85. package/dist/jit_hook.js +4 -0
  86. package/dist/jit_hook.js.map +1 -0
  87. package/dist/jit_runtime.d.ts +104 -0
  88. package/dist/jit_runtime.js +721 -0
  89. package/dist/jit_runtime.js.map +1 -0
  90. package/dist/jit_stack.d.ts +24 -0
  91. package/dist/jit_stack.js +227 -0
  92. package/dist/jit_stack.js.map +1 -0
  93. package/dist/jseption.d.ts +15 -0
  94. package/dist/jseption.js +61 -0
  95. package/dist/jseption.js.map +1 -0
  96. package/dist/opcodes.d.ts +347 -0
  97. package/dist/opcodes.js +322 -0
  98. package/dist/opcodes.js.map +1 -0
  99. package/dist/parser.d.ts +283 -0
  100. package/dist/parser.js +9752 -0
  101. package/dist/parser.js.map +1 -0
  102. package/dist/qjs.d.ts +2 -0
  103. package/dist/qjs.js +122 -0
  104. package/dist/qjs.js.map +1 -0
  105. package/dist/quickjs.d.ts +460 -0
  106. package/dist/quickjs.js +3408 -0
  107. package/dist/quickjs.js.map +1 -0
  108. package/dist/std.d.ts +4 -0
  109. package/dist/std.js +273 -0
  110. package/dist/std.js.map +1 -0
  111. package/dist/values.d.ts +447 -0
  112. package/dist/values.js +461 -0
  113. package/dist/values.js.map +1 -0
  114. package/package.json +59 -4
@@ -0,0 +1,1838 @@
1
+ /*
2
+ * ArrayBuffer, SharedArrayBuffer, typed arrays and DataView (port of the
3
+ * typed array section of quickjs.c).
4
+ *
5
+ * Typed arrays are exotic objects: the integer indexed element access is
6
+ * implemented by the class exotic methods registered on the runtime. The
7
+ * raw bytes live in the Uint8Array 'data' of the JSArrayBuffer; element
8
+ * access goes through host typed array / DataView views created over it.
9
+ */
10
+ import * as Q from "../quickjs.js";
11
+ import { JS_EXCEPTION, JS_IsException, JS_IsUndefined, JS_IsObject, JS_IsNull, JSArrayBuffer, JSTypedArray, JS_NewObject, JS_NewObjectProto, JS_NewArray, JS_NewCFunction2, JS_NewCFunction3, JS_DefinePropertyValue, JS_DefinePropertyValueStr, JS_DefinePropertyValueInt64, JS_PROP_WRITABLE, JS_PROP_CONFIGURABLE, JS_PROP_C_W_E, JS_PROP_THROW, JS_GetProperty, JS_GetPropertyInt64, JS_SetPropertyInt64, JS_GetPropertyUint32, JS_SetPropertyUint32, JS_GetPropertyValue, JS_SetPropertyValue, JS_ToString, JS_ToBoolFree, JS_ToFloat64, JS_ToUint32Free, JS_ToUint8ClampFree, JS_ToBigInt64, JS_ToIndex, JS_ToInt64Sat, JS_ToInt32Clamp, JS_ToInt64Clamp, JS_ToLengthFree, JS_ToPrimitive, HINT_NUMBER, JS_ToObject, js_get_length64, JS_ThrowTypeError, JS_ThrowRangeError, JS_ThrowTypeErrorInvalidClass, JS_IsFunction, JS_Call, JS_CallConstructor, JS_Invoke, js_create_from_ctor, js_same_value, JS_GetIterator2, JS_IteratorNext, JS_CreateDataPropertyUint32, JS_GetOpaque2, JS_AtomToString, JS_SetPropertyFunctionList, JS_NewGlobalCConstructorOnly, JS_NewGlobalCConstructor2, JS_SetConstructor, JS_CFUNC_DEF, JS_CFUNC_MAGIC_DEF, JS_NATIVE_DEF, JS_CGETSET_DEF, JS_CGETSET_MAGIC_DEF, JS_ALIAS_DEF, JS_PROP_STRING_DEF, JS_ATOM_length, JS_ATOM_toString, JS_ATOM_toLocaleString, JS_ATOM_join, JS_ATOM_next, JS_ATOM_set, JS_ATOM_Symbol_iterator, JS_ATOM_Symbol_species, JS_ATOM_Symbol_toStringTag, JS_ATOM_Uint8ClampedArray, } from "../quickjs.js";
12
+ import { JS_SpeciesConstructor, check_function } from "./promise.js";
13
+ import { fix_array_iterator } from "./native.js";
14
+ const HOST_TYPED_ARRAY_PROTO = Object.getPrototypeOf(Uint8Array.prototype);
15
+ /* JS_ITERATOR_KIND_xxx (same values as JSIteratorKind in index.ts) */
16
+ const JS_ITERATOR_KIND_KEY = 0;
17
+ const JS_ITERATOR_KIND_VALUE = 1;
18
+ const JS_ITERATOR_KIND_KEY_AND_VALUE = 2;
19
+ const JS_TYPED_ARRAY_COUNT = 31 /* JSClassID.FLOAT64_ARRAY */ - 21 /* JSClassID.UINT8C_ARRAY */ + 1;
20
+ const typed_array_size_log2_tab = [
21
+ 0, 0, 0, 1, 1, 2, 2,
22
+ 3, 3, /* BigInt64Array, BigUint64Array */
23
+ 2, 3,
24
+ ];
25
+ function typed_array_size_log2(class_id) {
26
+ return typed_array_size_log2_tab[class_id - 21 /* JSClassID.UINT8C_ARRAY */];
27
+ }
28
+ const host_ta_ctors = [
29
+ Uint8ClampedArray, Int8Array, Uint8Array, Int16Array, Uint16Array, Int32Array, Uint32Array,
30
+ BigInt64Array, BigUint64Array, Float32Array, Float64Array,
31
+ ];
32
+ /* ---------------------------------------------------------------------
33
+ ArrayBuffer
34
+ --------------------------------------------------------------------- */
35
+ /* array buffer data with the resizable extension. The host buffer is
36
+ resizable (growable when shared) when max_len >= 0; 'data' is a length
37
+ tracking view over it. */
38
+ export class JSArrayBufferData extends JSArrayBuffer {
39
+ /* maximum byte length for resizable buffers, -1 otherwise */
40
+ max_byte_length = -1;
41
+ /* cached host DataView over 'data' */
42
+ dv = null;
43
+ dv_data = null;
44
+ constructor(len, shared, max_len) {
45
+ let buffer;
46
+ if (shared)
47
+ buffer = max_len >= 0 ? new SharedArrayBuffer(len, { maxByteLength: max_len }) : new SharedArrayBuffer(len);
48
+ else
49
+ buffer = max_len >= 0 ? new ArrayBuffer(len, { maxByteLength: max_len }) : new ArrayBuffer(len);
50
+ super(buffer, shared);
51
+ this.max_byte_length = max_len;
52
+ }
53
+ }
54
+ function array_buffer_get_dv(abuf) {
55
+ const a = abuf;
56
+ if (a.dv === null || a.dv_data !== abuf.data) {
57
+ a.dv = new DataView(abuf.data.buffer, abuf.data.byteOffset, abuf.data.byteLength);
58
+ a.dv_data = abuf.data;
59
+ }
60
+ return a.dv;
61
+ }
62
+ function array_buffer_byte_length(abuf) {
63
+ return abuf.detached ? 0 : abuf.data.length;
64
+ }
65
+ function array_buffer_max_byte_length(abuf) {
66
+ const m = abuf.max_byte_length;
67
+ return (typeof m === 'number') ? m : -1;
68
+ }
69
+ function array_buffer_is_resizable(abuf) {
70
+ return array_buffer_max_byte_length(abuf) >= 0;
71
+ }
72
+ function JS_ThrowTypeErrorDetachedArrayBuffer(ctx) {
73
+ return JS_ThrowTypeError(ctx, 'ArrayBuffer is detached');
74
+ }
75
+ function JS_ThrowTypeErrorArrayBufferOOB(ctx) {
76
+ return JS_ThrowTypeError(ctx, 'ArrayBuffer is detached or resized');
77
+ }
78
+ /* XXX: we are currently limited to 2 GB */
79
+ const ARRAY_BUFFER_MAX_LENGTH = 0x7fffffff;
80
+ function js_array_buffer_constructor3(ctx, new_target, len, max_len, class_id, buf) {
81
+ const obj = js_create_from_ctor(ctx, new_target, class_id);
82
+ if (JS_IsException(obj))
83
+ return obj;
84
+ if (len > ARRAY_BUFFER_MAX_LENGTH)
85
+ return JS_ThrowRangeError(ctx, 'invalid array buffer length');
86
+ if (max_len > ARRAY_BUFFER_MAX_LENGTH)
87
+ return JS_ThrowRangeError(ctx, 'invalid array buffer maximum length');
88
+ const abuf = new JSArrayBufferData(len, class_id === 20 /* JSClassID.SHARED_ARRAY_BUFFER */, max_len);
89
+ if (buf)
90
+ abuf.data.set(buf.subarray(0, len));
91
+ Q.JS_SetOpaque(obj, abuf);
92
+ return obj;
93
+ }
94
+ function js_array_buffer_constructor2(ctx, new_target, len, class_id) {
95
+ return js_array_buffer_constructor3(ctx, new_target, len, -1, class_id, null);
96
+ }
97
+ function js_array_buffer_constructor1(ctx, new_target, len) {
98
+ return js_array_buffer_constructor2(ctx, new_target, len, 19 /* JSClassID.ARRAY_BUFFER */);
99
+ }
100
+ export function JS_NewArrayBufferCopy(ctx, buf) {
101
+ return js_array_buffer_constructor3(ctx, undefined, buf.length, -1, 19 /* JSClassID.ARRAY_BUFFER */, buf);
102
+ }
103
+ /* ArrayBuffer(length, options) / SharedArrayBuffer(length, options) */
104
+ function js_array_buffer_constructor0(ctx, new_target, argv, class_id) {
105
+ const len = JS_ToIndex(ctx, argv[0]);
106
+ if (len < 0)
107
+ return JS_EXCEPTION;
108
+ let max_len = -1;
109
+ const options = argv[1];
110
+ if (JS_IsObject(options)) {
111
+ const val = JS_GetProperty(ctx, options, JS_NewAtomStr(ctx, 'maxByteLength'));
112
+ if (JS_IsException(val))
113
+ return JS_EXCEPTION;
114
+ if (!JS_IsUndefined(val)) {
115
+ max_len = JS_ToIndex(ctx, val);
116
+ if (max_len < 0)
117
+ return JS_EXCEPTION;
118
+ if (len > max_len)
119
+ return JS_ThrowRangeError(ctx, 'invalid array buffer length');
120
+ }
121
+ }
122
+ return js_array_buffer_constructor3(ctx, new_target, len, max_len, class_id, null);
123
+ }
124
+ function JS_NewAtomStr(ctx, s) { return ctx.rt.atoms.newAtomStr(s); }
125
+ function js_array_buffer_constructor(ctx, new_target, argv) {
126
+ return js_array_buffer_constructor0(ctx, new_target, argv, 19 /* JSClassID.ARRAY_BUFFER */);
127
+ }
128
+ function js_shared_array_buffer_constructor(ctx, new_target, argv) {
129
+ return js_array_buffer_constructor0(ctx, new_target, argv, 20 /* JSClassID.SHARED_ARRAY_BUFFER */);
130
+ }
131
+ function js_array_buffer_isView(ctx, this_val, argv) {
132
+ const v = argv[0];
133
+ return Q.JS_IsObject(v) && Q.JS_GetClassID(v) >= 21 /* JSClassID.UINT8C_ARRAY */ && Q.JS_GetClassID(v) <= 32 /* JSClassID.DATAVIEW */;
134
+ }
135
+ function js_get_this(ctx, this_val) { return this_val; }
136
+ const js_array_buffer_funcs = [
137
+ JS_CFUNC_DEF('isView', 1, js_array_buffer_isView),
138
+ JS_CGETSET_DEF(JS_ATOM_Symbol_species, js_get_this, null),
139
+ ];
140
+ function js_array_buffer_get_byteLength(ctx, this_val, class_id) {
141
+ const abuf = JS_GetOpaque2(ctx, this_val, class_id);
142
+ if (!abuf)
143
+ return JS_EXCEPTION;
144
+ /* return 0 if detached */
145
+ return array_buffer_byte_length(abuf);
146
+ }
147
+ function js_array_buffer_get_maxByteLength(ctx, this_val, class_id) {
148
+ const abuf = JS_GetOpaque2(ctx, this_val, class_id);
149
+ if (!abuf)
150
+ return JS_EXCEPTION;
151
+ if (abuf.detached)
152
+ return 0;
153
+ if (array_buffer_is_resizable(abuf))
154
+ return array_buffer_max_byte_length(abuf);
155
+ return abuf.data.length;
156
+ }
157
+ function js_array_buffer_get_resizable(ctx, this_val, class_id) {
158
+ const abuf = JS_GetOpaque2(ctx, this_val, class_id);
159
+ if (!abuf)
160
+ return JS_EXCEPTION;
161
+ return array_buffer_is_resizable(abuf);
162
+ }
163
+ function js_array_buffer_get_detached(ctx, this_val) {
164
+ const abuf = JS_GetOpaque2(ctx, this_val, 19 /* JSClassID.ARRAY_BUFFER */);
165
+ if (!abuf)
166
+ return JS_EXCEPTION;
167
+ return abuf.detached;
168
+ }
169
+ export function JS_DetachArrayBuffer(ctx, obj) {
170
+ if (!Q.JS_IsObject(obj) || Q.JS_GetClassID(obj) !== 19 /* JSClassID.ARRAY_BUFFER */)
171
+ return;
172
+ const abuf = Q.JS_GetAnyOpaque(obj);
173
+ if (!abuf || abuf.detached)
174
+ return;
175
+ /* detach the host buffer so that the host views see a length of 0 */
176
+ try {
177
+ abuf.buffer.transfer(0);
178
+ }
179
+ catch { /* not transferable: keep the data */ }
180
+ abuf.data = new Uint8Array(0);
181
+ abuf.detached = true;
182
+ /* Note: the typed array length and offset fields are not modified */
183
+ }
184
+ /* ArrayBuffer.prototype.resize / SharedArrayBuffer.prototype.grow */
185
+ function js_array_buffer_resize(ctx, this_val, argv, class_id) {
186
+ const abuf = JS_GetOpaque2(ctx, this_val, class_id);
187
+ if (!abuf)
188
+ return JS_EXCEPTION;
189
+ if (!array_buffer_is_resizable(abuf))
190
+ return JS_ThrowTypeError(ctx, 'array buffer is not resizable');
191
+ const len = JS_ToIndex(ctx, argv[0]);
192
+ if (len < 0)
193
+ return JS_EXCEPTION;
194
+ if (abuf.detached)
195
+ return JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
196
+ if (len > array_buffer_max_byte_length(abuf))
197
+ return JS_ThrowRangeError(ctx, 'invalid array buffer length');
198
+ if (abuf.shared && len < abuf.data.length)
199
+ return JS_ThrowRangeError(ctx, 'invalid array buffer length');
200
+ try {
201
+ if (abuf.shared)
202
+ abuf.buffer.grow(len);
203
+ else
204
+ abuf.buffer.resize(len);
205
+ }
206
+ catch (e) {
207
+ return Q.js_host_exception_to_guest(ctx, e);
208
+ }
209
+ /* 'data' is a length tracking view: nothing else to do */
210
+ return undefined;
211
+ }
212
+ /* ArrayBuffer.prototype.transfer / transferToFixedLength (magic = 1) */
213
+ function js_array_buffer_transfer(ctx, this_val, argv, magic) {
214
+ const abuf = JS_GetOpaque2(ctx, this_val, 19 /* JSClassID.ARRAY_BUFFER */);
215
+ if (!abuf)
216
+ return JS_EXCEPTION;
217
+ let new_len;
218
+ if (JS_IsUndefined(argv[0])) {
219
+ new_len = array_buffer_byte_length(abuf);
220
+ }
221
+ else {
222
+ new_len = JS_ToIndex(ctx, argv[0]);
223
+ if (new_len < 0)
224
+ return JS_EXCEPTION;
225
+ }
226
+ if (abuf.detached)
227
+ return JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
228
+ let max_len = -1;
229
+ if (magic === 0 && array_buffer_is_resizable(abuf)) {
230
+ max_len = array_buffer_max_byte_length(abuf);
231
+ if (new_len > max_len)
232
+ return JS_ThrowRangeError(ctx, 'invalid array buffer length');
233
+ }
234
+ const new_obj = js_array_buffer_constructor3(ctx, undefined, new_len, max_len, 19 /* JSClassID.ARRAY_BUFFER */, abuf.data);
235
+ if (JS_IsException(new_obj))
236
+ return JS_EXCEPTION;
237
+ JS_DetachArrayBuffer(ctx, this_val);
238
+ return new_obj;
239
+ }
240
+ /* get an ArrayBuffer or SharedArrayBuffer */
241
+ function js_get_array_buffer(ctx, obj) {
242
+ if (Q.JS_IsObject(obj) && (Q.JS_GetClassID(obj) === 19 /* JSClassID.ARRAY_BUFFER */ || Q.JS_GetClassID(obj) === 20 /* JSClassID.SHARED_ARRAY_BUFFER */)) {
243
+ return Q.JS_GetAnyOpaque(obj);
244
+ }
245
+ JS_ThrowTypeErrorInvalidClass(ctx, 19 /* JSClassID.ARRAY_BUFFER */);
246
+ return null;
247
+ }
248
+ function js_array_buffer_slice(ctx, this_val, argv, class_id) {
249
+ const abuf = JS_GetOpaque2(ctx, this_val, class_id);
250
+ if (!abuf)
251
+ return JS_EXCEPTION;
252
+ if (abuf.detached)
253
+ return JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
254
+ const len = abuf.data.length;
255
+ const start = JS_ToInt64Clamp(ctx, argv[0], 0, len, len);
256
+ if (JS_IsException(start))
257
+ return JS_EXCEPTION;
258
+ let end = len;
259
+ if (!JS_IsUndefined(argv[1])) {
260
+ end = JS_ToInt64Clamp(ctx, argv[1], 0, len, len);
261
+ if (JS_IsException(end))
262
+ return JS_EXCEPTION;
263
+ }
264
+ const new_len = Math.max(end - start, 0);
265
+ const ctor = JS_SpeciesConstructor(ctx, this_val, undefined);
266
+ if (JS_IsException(ctor))
267
+ return ctor;
268
+ let new_obj;
269
+ if (JS_IsUndefined(ctor)) {
270
+ new_obj = js_array_buffer_constructor2(ctx, undefined, new_len, class_id);
271
+ }
272
+ else {
273
+ new_obj = JS_CallConstructor(ctx, ctor, [new_len]);
274
+ }
275
+ if (JS_IsException(new_obj))
276
+ return new_obj;
277
+ const new_abuf = JS_GetOpaque2(ctx, new_obj, class_id);
278
+ if (!new_abuf)
279
+ return JS_EXCEPTION;
280
+ if (js_same_value(ctx, new_obj, this_val))
281
+ return JS_ThrowTypeError(ctx, 'cannot use identical ArrayBuffer');
282
+ if (new_abuf.detached)
283
+ return JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
284
+ if (new_abuf.data.length < new_len)
285
+ return JS_ThrowTypeError(ctx, 'new ArrayBuffer is too small');
286
+ /* must test again because of side effects */
287
+ if (abuf.detached)
288
+ return JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
289
+ new_abuf.data.set(abuf.data.subarray(start, start + new_len));
290
+ return new_obj;
291
+ }
292
+ const js_array_buffer_proto_funcs = [
293
+ JS_CGETSET_MAGIC_DEF('byteLength', js_array_buffer_get_byteLength, null, 19 /* JSClassID.ARRAY_BUFFER */),
294
+ JS_CGETSET_MAGIC_DEF('maxByteLength', js_array_buffer_get_maxByteLength, null, 19 /* JSClassID.ARRAY_BUFFER */),
295
+ JS_CGETSET_MAGIC_DEF('resizable', js_array_buffer_get_resizable, null, 19 /* JSClassID.ARRAY_BUFFER */),
296
+ JS_CGETSET_DEF('detached', js_array_buffer_get_detached, null),
297
+ JS_CFUNC_MAGIC_DEF('resize', 1, js_array_buffer_resize, 19 /* JSClassID.ARRAY_BUFFER */),
298
+ JS_CFUNC_MAGIC_DEF('slice', 2, js_array_buffer_slice, 19 /* JSClassID.ARRAY_BUFFER */),
299
+ JS_CFUNC_MAGIC_DEF('transfer', 0, js_array_buffer_transfer, 0),
300
+ JS_CFUNC_MAGIC_DEF('transferToFixedLength', 0, js_array_buffer_transfer, 1),
301
+ JS_PROP_STRING_DEF(JS_ATOM_Symbol_toStringTag, 'ArrayBuffer', JS_PROP_CONFIGURABLE),
302
+ ];
303
+ /* SharedArrayBuffer */
304
+ const js_shared_array_buffer_funcs = [
305
+ JS_CGETSET_DEF(JS_ATOM_Symbol_species, js_get_this, null),
306
+ ];
307
+ const js_shared_array_buffer_proto_funcs = [
308
+ JS_CGETSET_MAGIC_DEF('byteLength', js_array_buffer_get_byteLength, null, 20 /* JSClassID.SHARED_ARRAY_BUFFER */),
309
+ JS_CGETSET_MAGIC_DEF('maxByteLength', js_array_buffer_get_maxByteLength, null, 20 /* JSClassID.SHARED_ARRAY_BUFFER */),
310
+ JS_CGETSET_MAGIC_DEF('growable', js_array_buffer_get_resizable, null, 20 /* JSClassID.SHARED_ARRAY_BUFFER */),
311
+ JS_CFUNC_MAGIC_DEF('grow', 1, js_array_buffer_resize, 20 /* JSClassID.SHARED_ARRAY_BUFFER */),
312
+ JS_CFUNC_MAGIC_DEF('slice', 2, js_array_buffer_slice, 20 /* JSClassID.SHARED_ARRAY_BUFFER */),
313
+ JS_PROP_STRING_DEF(JS_ATOM_Symbol_toStringTag, 'SharedArrayBuffer', JS_PROP_CONFIGURABLE),
314
+ ];
315
+ /* ---------------------------------------------------------------------
316
+ Typed arrays
317
+ --------------------------------------------------------------------- */
318
+ export function is_typed_array_class(class_id) {
319
+ return class_id >= 21 /* JSClassID.UINT8C_ARRAY */ && class_id <= 31 /* JSClassID.FLOAT64_ARRAY */;
320
+ }
321
+ function get_typed_array(ctx, this_val, is_dataview) {
322
+ if (Q.JS_IsObject(this_val)) {
323
+ if (is_dataview) {
324
+ if (Q.JS_GetClassID(this_val) === 32 /* JSClassID.DATAVIEW */)
325
+ return this_val;
326
+ }
327
+ else {
328
+ if (is_typed_array_class(Q.JS_GetClassID(this_val)))
329
+ return this_val;
330
+ }
331
+ }
332
+ JS_ThrowTypeError(ctx, `not a ${is_dataview ? 'DataView' : 'TypedArray'}`);
333
+ return null;
334
+ }
335
+ /* WARNING: 'p' must be a typed array or a DataView */
336
+ function typed_array_is_detached(ctx, p) {
337
+ const ta = Q.JS_GetAnyOpaque(p);
338
+ const abuf = Q.JS_GetAnyOpaque(ta.buffer);
339
+ return abuf.detached;
340
+ }
341
+ /* byte length of a typed array (size_log2 = element size) or DataView
342
+ (size_log2 = 0), taking the resizable buffer tracking into account.
343
+ Returns -1 if out of bounds or detached. */
344
+ function typed_array_get_byte_length(ta, size_log2) {
345
+ const abuf = Q.JS_GetAnyOpaque(ta.buffer);
346
+ if (abuf.detached)
347
+ return -1;
348
+ const buf_len = abuf.data.length;
349
+ if (ta.track_rab) {
350
+ if (ta.offset > buf_len)
351
+ return -1;
352
+ return ((buf_len - ta.offset) >> size_log2) << size_log2;
353
+ }
354
+ if (ta.offset + ta.length > buf_len)
355
+ return -1;
356
+ return ta.length;
357
+ }
358
+ /* byte length of a typed array or DataView object (-1 if detached/OOB) */
359
+ function js_typed_array_byte_length(p) {
360
+ const size_log2 = Q.JS_GetClassID(p) === 32 /* JSClassID.DATAVIEW */ ? 0 : typed_array_size_log2(Q.JS_GetClassID(p));
361
+ return typed_array_get_byte_length(Q.JS_GetAnyOpaque(p), size_log2);
362
+ }
363
+ /* return true if the typed array is detached or out of bounds (resized buffer) */
364
+ function typed_array_is_oob(ctx, p) {
365
+ return js_typed_array_byte_length(p) < 0;
366
+ }
367
+ /* WARNING: 'p' must be a typed array. Works even if the array buffer is
368
+ detached (returns 0). This is p->u.array.count in the C version. */
369
+ function typed_array_get_length(ctx, p) {
370
+ const byte_len = js_typed_array_byte_length(p);
371
+ if (byte_len < 0)
372
+ return 0;
373
+ return byte_len >> typed_array_size_log2(Q.JS_GetClassID(p));
374
+ }
375
+ const ta_view_cache = new WeakMap();
376
+ function typed_array_get_view(p) {
377
+ const ta = Q.JS_GetAnyOpaque(p);
378
+ const abuf = Q.JS_GetAnyOpaque(ta.buffer);
379
+ const byte_len = js_typed_array_byte_length(p);
380
+ if (byte_len < 0)
381
+ return null;
382
+ const len = byte_len >> typed_array_size_log2(Q.JS_GetClassID(p));
383
+ const c = ta_view_cache.get(ta);
384
+ if (c && c.data === abuf.data && c.len === len && c.offset === ta.offset)
385
+ return c.view;
386
+ const ctor = host_ta_ctors[Q.JS_GetClassID(p) - 21 /* JSClassID.UINT8C_ARRAY */];
387
+ const view = new ctor(abuf.data.buffer, abuf.data.byteOffset + ta.offset, len);
388
+ ta_view_cache.set(ta, { data: abuf.data, len, offset: ta.offset, view });
389
+ return view;
390
+ }
391
+ function validate_typed_array(ctx, this_val) {
392
+ const p = get_typed_array(ctx, this_val, false);
393
+ if (!p)
394
+ return -1;
395
+ if (typed_array_is_detached(ctx, p)) {
396
+ JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
397
+ return -1;
398
+ }
399
+ if (typed_array_is_oob(ctx, p)) {
400
+ JS_ThrowTypeErrorArrayBufferOOB(ctx);
401
+ return -1;
402
+ }
403
+ return 0;
404
+ }
405
+ function js_typed_array_get_length(ctx, this_val) {
406
+ const p = get_typed_array(ctx, this_val, false);
407
+ if (!p)
408
+ return JS_EXCEPTION;
409
+ return typed_array_get_length(ctx, p);
410
+ }
411
+ function js_typed_array_get_buffer(ctx, this_val, is_dataview) {
412
+ const p = get_typed_array(ctx, this_val, is_dataview !== 0);
413
+ if (!p)
414
+ return JS_EXCEPTION;
415
+ return Q.JS_GetAnyOpaque(p).buffer;
416
+ }
417
+ function js_typed_array_get_byteLength(ctx, this_val, is_dataview) {
418
+ const p = get_typed_array(ctx, this_val, is_dataview !== 0);
419
+ if (!p)
420
+ return JS_EXCEPTION;
421
+ const ta = Q.JS_GetAnyOpaque(p);
422
+ if (typed_array_is_detached(ctx, p)) {
423
+ if (is_dataview)
424
+ return JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
425
+ return 0;
426
+ }
427
+ const len = js_typed_array_byte_length(p);
428
+ if (len < 0) {
429
+ if (is_dataview)
430
+ return JS_ThrowTypeErrorArrayBufferOOB(ctx);
431
+ return 0;
432
+ }
433
+ void ta;
434
+ return len;
435
+ }
436
+ function js_typed_array_get_byteOffset(ctx, this_val, is_dataview) {
437
+ const p = get_typed_array(ctx, this_val, is_dataview !== 0);
438
+ if (!p)
439
+ return JS_EXCEPTION;
440
+ const ta = Q.JS_GetAnyOpaque(p);
441
+ if (typed_array_is_detached(ctx, p)) {
442
+ if (is_dataview)
443
+ return JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
444
+ return 0;
445
+ }
446
+ if (js_typed_array_byte_length(p) < 0) {
447
+ if (is_dataview)
448
+ return JS_ThrowTypeErrorArrayBufferOOB(ctx);
449
+ return 0;
450
+ }
451
+ return ta.offset;
452
+ }
453
+ function js_typed_array_get_toStringTag(ctx, this_val) {
454
+ if (!Q.JS_IsObject(this_val))
455
+ return undefined;
456
+ if (!is_typed_array_class(Q.JS_GetClassID(this_val)))
457
+ return undefined;
458
+ return JS_AtomToString(ctx, ctx.rt.class_array[Q.JS_GetClassID(this_val)].class_name);
459
+ }
460
+ /* element access */
461
+ function typed_array_get_element(p, idx) {
462
+ const view = typed_array_get_view(p);
463
+ if (!view || idx >= view.length)
464
+ return undefined;
465
+ return view[idx];
466
+ }
467
+ /* convert 'val' to the element type. Returns the converted value (number
468
+ or bigint) or JS_EXCEPTION */
469
+ function typed_array_convert_value(ctx, class_id, val) {
470
+ if (class_id === 21 /* JSClassID.UINT8C_ARRAY */) {
471
+ const v = JS_ToUint8ClampFree(ctx, val);
472
+ if (JS_IsException(v))
473
+ return JS_EXCEPTION;
474
+ return v;
475
+ }
476
+ else if (class_id <= 27 /* JSClassID.UINT32_ARRAY */) {
477
+ const v = JS_ToUint32Free(ctx, val);
478
+ if (JS_IsException(v))
479
+ return JS_EXCEPTION;
480
+ return v;
481
+ }
482
+ else if (class_id <= 29 /* JSClassID.BIG_UINT64_ARRAY */) {
483
+ const v = JS_ToBigInt64(ctx, val);
484
+ if (JS_IsException(v))
485
+ return JS_EXCEPTION;
486
+ return v;
487
+ }
488
+ else {
489
+ const v = JS_ToFloat64(ctx, val);
490
+ if (JS_IsException(v))
491
+ return JS_EXCEPTION;
492
+ return v;
493
+ }
494
+ }
495
+ /* TypedArraySetElement: the value is converted even if the index is out
496
+ of bounds. Return -1 if exception, TRUE otherwise. */
497
+ function typed_array_set_element(ctx, p, idx, val) {
498
+ const v = typed_array_convert_value(ctx, Q.JS_GetClassID(p), val);
499
+ if (JS_IsException(v))
500
+ return -1;
501
+ const view = typed_array_get_view(p);
502
+ if (view && idx >= 0 && idx < view.length)
503
+ view[idx] = v;
504
+ return 1;
505
+ }
506
+ /* methods */
507
+ function js_typed_array_set_internal(ctx, dst, src, off) {
508
+ const p = get_typed_array(ctx, dst, false);
509
+ if (!p)
510
+ return JS_EXCEPTION;
511
+ const offset = JS_ToInt64Sat(ctx, off);
512
+ if (JS_IsException(offset))
513
+ return JS_EXCEPTION;
514
+ if (offset < 0)
515
+ return JS_ThrowRangeError(ctx, 'invalid array length');
516
+ if (typed_array_is_detached(ctx, p))
517
+ return JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
518
+ const src_obj = JS_ToObject(ctx, src);
519
+ if (JS_IsException(src_obj))
520
+ return JS_EXCEPTION;
521
+ const src_p = src_obj;
522
+ const dst_len = typed_array_get_length(ctx, p);
523
+ let src_len;
524
+ if (is_typed_array_class(Q.JS_GetClassID(src_p))) {
525
+ if (typed_array_is_detached(ctx, src_p))
526
+ return JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
527
+ src_len = typed_array_get_length(ctx, src_p);
528
+ if (offset > dst_len - src_len)
529
+ return JS_ThrowRangeError(ctx, 'invalid array length');
530
+ /* copying between typed objects */
531
+ if (Q.JS_GetClassID(src_p) === Q.JS_GetClassID(p)) {
532
+ /* same type, use memmove */
533
+ const dst_view = typed_array_get_view(p);
534
+ const src_view = typed_array_get_view(src_p);
535
+ dst_view.set(src_view, offset);
536
+ return undefined;
537
+ }
538
+ /* otherwise, default behavior is slow but correct */
539
+ }
540
+ else {
541
+ src_len = js_get_length64(ctx, src_obj);
542
+ if (src_len < 0)
543
+ return JS_EXCEPTION;
544
+ if (offset > dst_len - src_len)
545
+ return JS_ThrowRangeError(ctx, 'invalid array length');
546
+ }
547
+ for (let i = 0; i < src_len; i++) {
548
+ const val = JS_GetPropertyUint32(ctx, src_obj, i);
549
+ if (JS_IsException(val))
550
+ return JS_EXCEPTION;
551
+ if (JS_SetPropertyUint32(ctx, dst, offset + i, val) < 0)
552
+ return JS_EXCEPTION;
553
+ }
554
+ return undefined;
555
+ }
556
+ function js_typed_array_at(ctx, this_val, argv) {
557
+ const p = get_typed_array(ctx, this_val, false);
558
+ if (!p)
559
+ return JS_EXCEPTION;
560
+ if (typed_array_is_detached(ctx, p))
561
+ return JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
562
+ const r = JS_ToInt64Sat(ctx, argv[0]);
563
+ if (JS_IsException(r))
564
+ return JS_EXCEPTION;
565
+ let idx = r;
566
+ const len = typed_array_get_length(ctx, p);
567
+ if (idx < 0)
568
+ idx = len + idx;
569
+ if (idx < 0 || idx >= len)
570
+ return undefined;
571
+ return JS_GetPropertyInt64(ctx, this_val, idx);
572
+ }
573
+ function js_typed_array_with(ctx, this_val, argv) {
574
+ const p = get_typed_array(ctx, this_val, false);
575
+ if (!p)
576
+ return JS_EXCEPTION;
577
+ if (typed_array_is_detached(ctx, p))
578
+ return JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
579
+ const r = JS_ToInt64Sat(ctx, argv[0]);
580
+ if (JS_IsException(r))
581
+ return JS_EXCEPTION;
582
+ let idx = r;
583
+ const len = typed_array_get_length(ctx, p);
584
+ if (idx < 0)
585
+ idx = len + idx;
586
+ const val = JS_ToPrimitive(ctx, argv[1], HINT_NUMBER);
587
+ if (JS_IsException(val))
588
+ return JS_EXCEPTION;
589
+ if (typed_array_is_detached(ctx, p) || idx < 0 || idx >= len)
590
+ return JS_ThrowRangeError(ctx, 'invalid array index');
591
+ const arr = js_typed_array_constructor_ta(ctx, undefined, this_val, Q.JS_GetClassID(p));
592
+ if (JS_IsException(arr))
593
+ return JS_EXCEPTION;
594
+ if (JS_SetPropertyInt64(ctx, arr, idx, val) < 0)
595
+ return JS_EXCEPTION;
596
+ return arr;
597
+ }
598
+ function js_typed_array_set(ctx, this_val, argv) {
599
+ let offset = undefined;
600
+ if (argv.length > 1)
601
+ offset = argv[1];
602
+ return js_typed_array_set_internal(ctx, this_val, argv[0], offset);
603
+ }
604
+ /* return < 0 if exception */
605
+ function js_typed_array_get_length_internal(ctx, obj) {
606
+ const p = get_typed_array(ctx, obj, false);
607
+ if (!p)
608
+ return -1;
609
+ if (typed_array_is_detached(ctx, p)) {
610
+ JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
611
+ return -1;
612
+ }
613
+ if (typed_array_is_oob(ctx, p)) {
614
+ JS_ThrowTypeErrorArrayBufferOOB(ctx);
615
+ return -1;
616
+ }
617
+ return typed_array_get_length(ctx, p);
618
+ }
619
+ function js_typed_array_create(ctx, ctor, argv) {
620
+ const ret = JS_CallConstructor(ctx, ctor, argv);
621
+ if (JS_IsException(ret))
622
+ return ret;
623
+ /* validate the typed array */
624
+ const new_len = js_typed_array_get_length_internal(ctx, ret);
625
+ if (new_len < 0)
626
+ return JS_EXCEPTION;
627
+ if (argv.length === 1) {
628
+ /* ensure that it is large enough */
629
+ const len = JS_ToLengthFree(ctx, argv[0]);
630
+ if (len < 0)
631
+ return JS_EXCEPTION;
632
+ if (new_len < len)
633
+ return JS_ThrowTypeError(ctx, 'TypedArray length is too small');
634
+ }
635
+ return ret;
636
+ }
637
+ function js_typed_array___speciesCreate(ctx, this_val, argv) {
638
+ const obj = argv[0];
639
+ const p = get_typed_array(ctx, obj, false);
640
+ if (!p)
641
+ return JS_EXCEPTION;
642
+ const ctor = JS_SpeciesConstructor(ctx, obj, undefined);
643
+ if (JS_IsException(ctor))
644
+ return ctor;
645
+ const args1 = argv.slice(1);
646
+ if (JS_IsUndefined(ctor)) {
647
+ return js_typed_array_constructor(ctx, undefined, args1, Q.JS_GetClassID(p));
648
+ }
649
+ return js_typed_array_create(ctx, ctor, args1);
650
+ }
651
+ function js_array_from_iterator(ctx, obj, method) {
652
+ const arr = JS_NewArray(ctx);
653
+ if (JS_IsException(arr))
654
+ return null;
655
+ const iter = JS_GetIterator2(ctx, obj, method);
656
+ if (JS_IsException(iter))
657
+ return null;
658
+ const next_method = JS_GetProperty(ctx, iter, JS_ATOM_next);
659
+ if (JS_IsException(next_method))
660
+ return null;
661
+ let k = 0;
662
+ const pdone = { done: 0 };
663
+ for (;;) {
664
+ const val = JS_IteratorNext(ctx, iter, next_method, [], pdone);
665
+ if (JS_IsException(val))
666
+ return null;
667
+ if (pdone.done)
668
+ break;
669
+ if (JS_CreateDataPropertyUint32(ctx, arr, k, val, JS_PROP_THROW) < 0)
670
+ return null;
671
+ k++;
672
+ }
673
+ return { arr, len: k };
674
+ }
675
+ function js_typed_array_from(ctx, this_val, argv) {
676
+ // from(items, mapfn = void 0, this_arg = void 0)
677
+ const items = argv[0];
678
+ let mapfn = undefined, this_arg = undefined;
679
+ let mapping = false;
680
+ if (argv.length > 1) {
681
+ mapfn = argv[1];
682
+ if (!JS_IsUndefined(mapfn)) {
683
+ if (check_function(ctx, mapfn))
684
+ return JS_EXCEPTION;
685
+ mapping = true;
686
+ if (argv.length > 2)
687
+ this_arg = argv[2];
688
+ }
689
+ }
690
+ const iter = JS_GetProperty(ctx, items, JS_ATOM_Symbol_iterator);
691
+ if (JS_IsException(iter))
692
+ return JS_EXCEPTION;
693
+ let arr;
694
+ let len;
695
+ if (!JS_IsUndefined(iter) && !JS_IsNull(iter)) {
696
+ if (!JS_IsFunction(ctx, iter))
697
+ return JS_ThrowTypeError(ctx, 'value is not iterable');
698
+ const r = js_array_from_iterator(ctx, items, iter);
699
+ if (!r)
700
+ return JS_EXCEPTION;
701
+ arr = r.arr;
702
+ len = r.len;
703
+ }
704
+ else {
705
+ arr = JS_ToObject(ctx, items);
706
+ if (JS_IsException(arr))
707
+ return JS_EXCEPTION;
708
+ len = js_get_length64(ctx, arr);
709
+ if (len < 0)
710
+ return JS_EXCEPTION;
711
+ }
712
+ const r = js_typed_array_create(ctx, this_val, [len]);
713
+ if (JS_IsException(r))
714
+ return JS_EXCEPTION;
715
+ for (let k = 0; k < len; k++) {
716
+ let v = JS_GetPropertyInt64(ctx, arr, k);
717
+ if (JS_IsException(v))
718
+ return JS_EXCEPTION;
719
+ if (mapping) {
720
+ v = JS_Call(ctx, mapfn, this_arg, [v, k]);
721
+ if (JS_IsException(v))
722
+ return JS_EXCEPTION;
723
+ }
724
+ if (JS_SetPropertyInt64(ctx, r, k, v) < 0)
725
+ return JS_EXCEPTION;
726
+ }
727
+ return r;
728
+ }
729
+ function js_typed_array_of(ctx, this_val, argv) {
730
+ const obj = js_typed_array_create(ctx, this_val, [argv.length]);
731
+ if (JS_IsException(obj))
732
+ return obj;
733
+ for (let i = 0; i < argv.length; i++) {
734
+ if (JS_SetPropertyUint32(ctx, obj, i, argv[i]) < 0)
735
+ return JS_EXCEPTION;
736
+ }
737
+ return obj;
738
+ }
739
+ function js_typed_array_copyWithin(ctx, this_val, argv) {
740
+ const len = js_typed_array_get_length_internal(ctx, this_val);
741
+ if (len < 0)
742
+ return JS_EXCEPTION;
743
+ const to = JS_ToInt32Clamp(ctx, argv[0], 0, len, len);
744
+ if (JS_IsException(to))
745
+ return JS_EXCEPTION;
746
+ const from = JS_ToInt32Clamp(ctx, argv[1], 0, len, len);
747
+ if (JS_IsException(from))
748
+ return JS_EXCEPTION;
749
+ let final = len;
750
+ if (argv.length > 2 && !JS_IsUndefined(argv[2])) {
751
+ final = JS_ToInt32Clamp(ctx, argv[2], 0, len, len);
752
+ if (JS_IsException(final))
753
+ return JS_EXCEPTION;
754
+ }
755
+ const count = Math.min(final - from, len - to);
756
+ if (count > 0) {
757
+ const p = this_val;
758
+ if (typed_array_is_detached(ctx, p))
759
+ return JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
760
+ const view = typed_array_get_view(p);
761
+ if (view) {
762
+ /* the buffer may have been resized */
763
+ const cur_len = view.length;
764
+ const cnt = Math.min(count, cur_len - from, cur_len - to);
765
+ if (cnt > 0)
766
+ view.copyWithin(to, from, from + cnt);
767
+ }
768
+ }
769
+ return this_val;
770
+ }
771
+ function js_typed_array_fill(ctx, this_val, argv) {
772
+ const len = js_typed_array_get_length_internal(ctx, this_val);
773
+ if (len < 0)
774
+ return JS_EXCEPTION;
775
+ const p = this_val;
776
+ const v = typed_array_convert_value(ctx, Q.JS_GetClassID(p), argv[0]);
777
+ if (JS_IsException(v))
778
+ return JS_EXCEPTION;
779
+ let k = 0;
780
+ if (argv.length > 1) {
781
+ k = JS_ToInt32Clamp(ctx, argv[1], 0, len, len);
782
+ if (JS_IsException(k))
783
+ return JS_EXCEPTION;
784
+ }
785
+ let final = len;
786
+ if (argv.length > 2 && !JS_IsUndefined(argv[2])) {
787
+ final = JS_ToInt32Clamp(ctx, argv[2], 0, len, len);
788
+ if (JS_IsException(final))
789
+ return JS_EXCEPTION;
790
+ }
791
+ if (typed_array_is_detached(ctx, p))
792
+ return JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
793
+ const view = typed_array_get_view(p);
794
+ if (view) {
795
+ /* RAB may have been resized by evil .valueOf method */
796
+ const end = Math.min(final, view.length);
797
+ if (k < end)
798
+ view.fill(v, k, end);
799
+ }
800
+ return this_val;
801
+ }
802
+ function js_typed_array_find(ctx, this_val, argv, mode) {
803
+ const len = js_typed_array_get_length_internal(ctx, this_val);
804
+ if (len < 0)
805
+ return JS_EXCEPTION;
806
+ const func = argv[0];
807
+ if (check_function(ctx, func))
808
+ return JS_EXCEPTION;
809
+ let this_arg = undefined;
810
+ if (argv.length > 1)
811
+ this_arg = argv[1];
812
+ let k = 0, dir = 1, end = len;
813
+ if (mode === 2 /* ArrayFindMode.ArrayFindLast */ || mode === 3 /* ArrayFindMode.ArrayFindLastIndex */) {
814
+ k = len - 1;
815
+ dir = -1;
816
+ end = -1;
817
+ }
818
+ for (; k !== end; k += dir) {
819
+ const index_val = k;
820
+ const val = JS_GetPropertyValue(ctx, this_val, index_val);
821
+ if (JS_IsException(val))
822
+ return JS_EXCEPTION;
823
+ const res = JS_Call(ctx, func, this_arg, [val, index_val, this_val]);
824
+ if (JS_IsException(res))
825
+ return JS_EXCEPTION;
826
+ if (JS_ToBoolFree(ctx, res)) {
827
+ if (mode === 1 /* ArrayFindMode.ArrayFindIndex */ || mode === 3 /* ArrayFindMode.ArrayFindLastIndex */)
828
+ return index_val;
829
+ return val;
830
+ }
831
+ }
832
+ if (mode === 1 /* ArrayFindMode.ArrayFindIndex */ || mode === 3 /* ArrayFindMode.ArrayFindLastIndex */)
833
+ return -1;
834
+ return undefined;
835
+ }
836
+ const special_indexOf = 0;
837
+ const special_lastIndexOf = 1;
838
+ const special_includes = -1;
839
+ /* value range of the integer typed arrays */
840
+ const int_ranges = [
841
+ [0, 255], [-128, 127], [0, 255], [-32768, 32767], [0, 65535], [-2147483648, 2147483647], [0, 4294967295],
842
+ ];
843
+ function js_typed_array_indexOf(ctx, this_val, argv, special) {
844
+ let res = -1;
845
+ const len = js_typed_array_get_length_internal(ctx, this_val);
846
+ if (len < 0)
847
+ return JS_EXCEPTION;
848
+ const done = () => special === special_includes ? res >= 0 : res;
849
+ if (len === 0)
850
+ return done();
851
+ let k, stop, inc;
852
+ if (special === special_lastIndexOf) {
853
+ k = len - 1;
854
+ if (argv.length > 1) {
855
+ const r = JS_ToFloat64(ctx, argv[1]);
856
+ if (JS_IsException(r))
857
+ return JS_EXCEPTION;
858
+ let d = r;
859
+ if (Number.isNaN(d)) {
860
+ k = 0;
861
+ }
862
+ else {
863
+ if (d >= 0) {
864
+ if (d < k)
865
+ k = Math.trunc(d);
866
+ }
867
+ else {
868
+ d += len;
869
+ if (d < 0)
870
+ return done();
871
+ k = Math.trunc(d);
872
+ }
873
+ }
874
+ }
875
+ stop = -1;
876
+ inc = -1;
877
+ }
878
+ else {
879
+ k = 0;
880
+ if (argv.length > 1) {
881
+ const r = JS_ToInt32Clamp(ctx, argv[1], 0, len, len);
882
+ if (JS_IsException(r))
883
+ return JS_EXCEPTION;
884
+ k = r;
885
+ }
886
+ stop = len;
887
+ inc = 1;
888
+ }
889
+ const p = this_val;
890
+ /* if the array was detached, no need to go further (but no
891
+ exception is raised) */
892
+ if (typed_array_is_detached(ctx, p) || typed_array_is_oob(ctx, p)) {
893
+ /* "includes" scans all the properties, so "undefined" can match */
894
+ if (special === special_includes && JS_IsUndefined(argv[0]) && len > 0)
895
+ res = 0;
896
+ return done();
897
+ }
898
+ const view = typed_array_get_view(p);
899
+ /* the buffer may have been resized: only scan the current elements */
900
+ if (view.length < len) {
901
+ if (inc > 0)
902
+ stop = view.length;
903
+ else if (k >= view.length)
904
+ k = view.length - 1;
905
+ }
906
+ const arg = argv[0];
907
+ let is_int = false, is_bigint = false;
908
+ let d = 0;
909
+ let v64 = 0n;
910
+ if (typeof arg === 'number') {
911
+ d = arg;
912
+ if (d >= -9223372036854775808 && d < 9223372036854775808 && Number.isInteger(d))
913
+ is_int = true;
914
+ }
915
+ else if (typeof arg === 'bigint') {
916
+ if (Q.JS_GetClassID(p) === 28 /* JSClassID.BIG_INT64_ARRAY */) {
917
+ if (arg < -(1n << 63n) || arg >= (1n << 63n))
918
+ return done(); /* does not fit an int64 : cannot be found */
919
+ }
920
+ else if (Q.JS_GetClassID(p) === 29 /* JSClassID.BIG_UINT64_ARRAY */) {
921
+ if (arg < 0n || arg >= (1n << 64n))
922
+ return done();
923
+ }
924
+ else {
925
+ return done();
926
+ }
927
+ v64 = arg;
928
+ is_bigint = true;
929
+ }
930
+ else {
931
+ return done();
932
+ }
933
+ const scan = (v) => {
934
+ for (; k !== stop; k += inc) {
935
+ if (view[k] === v) {
936
+ res = k;
937
+ break;
938
+ }
939
+ }
940
+ };
941
+ switch (Q.JS_GetClassID(p)) {
942
+ case 22 /* JSClassID.INT8_ARRAY */:
943
+ case 21 /* JSClassID.UINT8C_ARRAY */:
944
+ case 23 /* JSClassID.UINT8_ARRAY */:
945
+ case 24 /* JSClassID.INT16_ARRAY */:
946
+ case 25 /* JSClassID.UINT16_ARRAY */:
947
+ case 26 /* JSClassID.INT32_ARRAY */:
948
+ case 27 /* JSClassID.UINT32_ARRAY */: {
949
+ const [min, max] = int_ranges[Q.JS_GetClassID(p) - 21 /* JSClassID.UINT8C_ARRAY */];
950
+ if (is_int && d >= min && d <= max)
951
+ scan(d);
952
+ break;
953
+ }
954
+ case 30 /* JSClassID.FLOAT32_ARRAY */:
955
+ if (is_bigint)
956
+ break;
957
+ if (Number.isNaN(d)) {
958
+ /* special case: indexOf returns -1, includes finds NaN */
959
+ if (special !== special_includes)
960
+ return done();
961
+ for (; k !== stop; k += inc) {
962
+ if (Number.isNaN(view[k])) {
963
+ res = k;
964
+ break;
965
+ }
966
+ }
967
+ }
968
+ else if (Math.fround(d) === d) {
969
+ scan(d);
970
+ }
971
+ break;
972
+ case 31 /* JSClassID.FLOAT64_ARRAY */:
973
+ if (is_bigint)
974
+ break;
975
+ if (Number.isNaN(d)) {
976
+ /* special case: indexOf returns -1, includes finds NaN */
977
+ if (special !== special_includes)
978
+ return done();
979
+ for (; k !== stop; k += inc) {
980
+ if (Number.isNaN(view[k])) {
981
+ res = k;
982
+ break;
983
+ }
984
+ }
985
+ }
986
+ else {
987
+ scan(d);
988
+ }
989
+ break;
990
+ case 28 /* JSClassID.BIG_INT64_ARRAY */:
991
+ case 29 /* JSClassID.BIG_UINT64_ARRAY */:
992
+ if (is_bigint)
993
+ scan(v64);
994
+ break;
995
+ }
996
+ return done();
997
+ }
998
+ function JS_ToLocaleStringFree(ctx, val) {
999
+ if (JS_IsUndefined(val) || JS_IsNull(val))
1000
+ return JS_ToString(ctx, val);
1001
+ return JS_Invoke(ctx, val, JS_ATOM_toLocaleString, []);
1002
+ }
1003
+ function js_typed_array_join(ctx, this_val, argv, toLocaleString) {
1004
+ const n = js_typed_array_get_length_internal(ctx, this_val);
1005
+ if (n < 0)
1006
+ return JS_EXCEPTION;
1007
+ let sep = ','; /* default separator */
1008
+ if (!toLocaleString && argv.length > 0 && !JS_IsUndefined(argv[0])) {
1009
+ const s = JS_ToString(ctx, argv[0]);
1010
+ if (JS_IsException(s))
1011
+ return JS_EXCEPTION;
1012
+ sep = s;
1013
+ }
1014
+ let b = '';
1015
+ /* XXX: optimize with direct access */
1016
+ for (let i = 0; i < n; i++) {
1017
+ if (i > 0)
1018
+ b += sep;
1019
+ let el = JS_GetPropertyUint32(ctx, this_val, i);
1020
+ /* Can return undefined for example if the typed array is detached */
1021
+ if (!JS_IsNull(el) && !JS_IsUndefined(el)) {
1022
+ if (JS_IsException(el))
1023
+ return JS_EXCEPTION;
1024
+ if (toLocaleString) {
1025
+ el = JS_ToLocaleStringFree(ctx, el);
1026
+ if (JS_IsException(el))
1027
+ return JS_EXCEPTION;
1028
+ }
1029
+ const s = JS_ToString(ctx, el);
1030
+ if (JS_IsException(s))
1031
+ return JS_EXCEPTION;
1032
+ b += s;
1033
+ }
1034
+ }
1035
+ return b;
1036
+ }
1037
+ function js_typed_array_reverse(ctx, this_val, argv) {
1038
+ const len = js_typed_array_get_length_internal(ctx, this_val);
1039
+ if (len < 0)
1040
+ return JS_EXCEPTION;
1041
+ if (len > 0) {
1042
+ const view = typed_array_get_view(this_val);
1043
+ if (view)
1044
+ view.reverse();
1045
+ }
1046
+ return this_val;
1047
+ }
1048
+ function js_typed_array_toReversed(ctx, this_val, argv) {
1049
+ const p = get_typed_array(ctx, this_val, false);
1050
+ if (!p)
1051
+ return JS_EXCEPTION;
1052
+ const arr = js_typed_array_constructor_ta(ctx, undefined, this_val, Q.JS_GetClassID(p));
1053
+ if (JS_IsException(arr))
1054
+ return JS_EXCEPTION;
1055
+ return js_typed_array_reverse(ctx, arr, argv);
1056
+ }
1057
+ /* copy 'len' bytes; the spec mandates a byte copy when the regions overlap */
1058
+ function slice_memcpy(dst, dst_pos, src, src_pos, len) {
1059
+ if (dst.buffer !== src.buffer) {
1060
+ dst.set(src.subarray(src_pos, src_pos + len), dst_pos);
1061
+ }
1062
+ else {
1063
+ const d0 = dst.byteOffset + dst_pos, s0 = src.byteOffset + src_pos;
1064
+ if (d0 + len <= s0 || d0 >= s0 + len) {
1065
+ dst.set(src.subarray(src_pos, src_pos + len), dst_pos);
1066
+ }
1067
+ else {
1068
+ for (let i = 0; i < len; i++)
1069
+ dst[dst_pos + i] = src[src_pos + i];
1070
+ }
1071
+ }
1072
+ }
1073
+ function js_typed_array_slice(ctx, this_val, argv) {
1074
+ const len = js_typed_array_get_length_internal(ctx, this_val);
1075
+ if (len < 0)
1076
+ return JS_EXCEPTION;
1077
+ const start = JS_ToInt32Clamp(ctx, argv[0], 0, len, len);
1078
+ if (JS_IsException(start))
1079
+ return JS_EXCEPTION;
1080
+ let final = len;
1081
+ if (!JS_IsUndefined(argv[1])) {
1082
+ final = JS_ToInt32Clamp(ctx, argv[1], 0, len, len);
1083
+ if (JS_IsException(final))
1084
+ return JS_EXCEPTION;
1085
+ }
1086
+ const count = Math.max(final - start, 0);
1087
+ const p = get_typed_array(ctx, this_val, false);
1088
+ if (!p)
1089
+ return JS_EXCEPTION;
1090
+ const shift = typed_array_size_log2(Q.JS_GetClassID(p));
1091
+ const arr = js_typed_array___speciesCreate(ctx, undefined, [this_val, count]);
1092
+ if (JS_IsException(arr))
1093
+ return JS_EXCEPTION;
1094
+ if (count > 0) {
1095
+ if (validate_typed_array(ctx, this_val) || validate_typed_array(ctx, arr))
1096
+ return JS_EXCEPTION;
1097
+ const p1 = get_typed_array(ctx, arr, false);
1098
+ if (p1 !== null && Q.JS_GetClassID(p) === Q.JS_GetClassID(p1) &&
1099
+ typed_array_get_length(ctx, p1) >= count &&
1100
+ typed_array_get_length(ctx, p) >= start + count) {
1101
+ const ta = Q.JS_GetAnyOpaque(p), ta1 = Q.JS_GetAnyOpaque(p1);
1102
+ const src = Q.JS_GetAnyOpaque(ta.buffer).data, dst = Q.JS_GetAnyOpaque(ta1.buffer).data;
1103
+ slice_memcpy(dst, ta1.offset, src, ta.offset + (start << shift), count << shift);
1104
+ }
1105
+ else {
1106
+ for (let n = 0; n < count; n++) {
1107
+ const val = JS_GetPropertyValue(ctx, this_val, start + n);
1108
+ if (JS_IsException(val))
1109
+ return JS_EXCEPTION;
1110
+ if (JS_SetPropertyValue(ctx, arr, n, val, JS_PROP_THROW) < 0)
1111
+ return JS_EXCEPTION;
1112
+ }
1113
+ }
1114
+ }
1115
+ return arr;
1116
+ }
1117
+ function js_typed_array_subarray(ctx, this_val, argv) {
1118
+ const p = get_typed_array(ctx, this_val, false);
1119
+ if (!p)
1120
+ return JS_EXCEPTION;
1121
+ const len = typed_array_get_length(ctx, p);
1122
+ const start = JS_ToInt32Clamp(ctx, argv[0], 0, len, len);
1123
+ if (JS_IsException(start))
1124
+ return JS_EXCEPTION;
1125
+ let final = len;
1126
+ if (!JS_IsUndefined(argv[1])) {
1127
+ final = JS_ToInt32Clamp(ctx, argv[1], 0, len, len);
1128
+ if (JS_IsException(final))
1129
+ return JS_EXCEPTION;
1130
+ }
1131
+ const count = Math.max(final - start, 0);
1132
+ const byteOffset = js_typed_array_get_byteOffset(ctx, this_val, 0);
1133
+ if (JS_IsException(byteOffset))
1134
+ return JS_EXCEPTION;
1135
+ const shift = typed_array_size_log2(Q.JS_GetClassID(p));
1136
+ const offset = byteOffset + (start << shift);
1137
+ const ta_buffer = js_typed_array_get_buffer(ctx, this_val, 0);
1138
+ if (JS_IsException(ta_buffer))
1139
+ return JS_EXCEPTION;
1140
+ const ta = Q.JS_GetAnyOpaque(p);
1141
+ if (ta.track_rab && JS_IsUndefined(argv[1])) {
1142
+ /* length tracking subarray of a length tracking array */
1143
+ return js_typed_array___speciesCreate(ctx, undefined, [this_val, ta_buffer, offset]);
1144
+ }
1145
+ return js_typed_array___speciesCreate(ctx, undefined, [this_val, ta_buffer, offset, count]);
1146
+ }
1147
+ /* TypedArray.prototype.sort */
1148
+ function js_cmp_doubles(x, y) {
1149
+ if (Number.isNaN(x))
1150
+ return Number.isNaN(y) ? 0 : +1;
1151
+ if (Number.isNaN(y))
1152
+ return -1;
1153
+ if (x < y)
1154
+ return -1;
1155
+ if (x > y)
1156
+ return 1;
1157
+ if (x !== 0)
1158
+ return 0;
1159
+ if (1 / x < 0)
1160
+ return 1 / y < 0 ? 0 : -1;
1161
+ return 1 / y < 0 ? 1 : 0;
1162
+ }
1163
+ function js_TA_cmp_default(a, b) {
1164
+ if (typeof a === 'bigint')
1165
+ return a < b ? -1 : a > b ? 1 : 0;
1166
+ return js_cmp_doubles(a, b);
1167
+ }
1168
+ /* stable merge sort of an index array */
1169
+ function merge_sort_indices(idx, cmp) {
1170
+ const n = idx.length;
1171
+ let src = idx, dst = new Array(n);
1172
+ for (let width = 1; width < n; width *= 2) {
1173
+ for (let i = 0; i < n; i += 2 * width) {
1174
+ let l = i, r = Math.min(i + width, n);
1175
+ const lend = r, rend = Math.min(i + 2 * width, n);
1176
+ let k = i;
1177
+ while (l < lend && r < rend) {
1178
+ if (cmp(src[l], src[r]) <= 0)
1179
+ dst[k++] = src[l++];
1180
+ else
1181
+ dst[k++] = src[r++];
1182
+ }
1183
+ while (l < lend)
1184
+ dst[k++] = src[l++];
1185
+ while (r < rend)
1186
+ dst[k++] = src[r++];
1187
+ }
1188
+ const t = src;
1189
+ src = dst;
1190
+ dst = t;
1191
+ }
1192
+ return src;
1193
+ }
1194
+ function js_typed_array_sort(ctx, this_val, argv) {
1195
+ const cmp = argv[0];
1196
+ if (!JS_IsUndefined(cmp) && check_function(ctx, cmp))
1197
+ return JS_EXCEPTION;
1198
+ const len = js_typed_array_get_length_internal(ctx, this_val);
1199
+ if (len < 0)
1200
+ return JS_EXCEPTION;
1201
+ if (len > 1) {
1202
+ const p = this_val;
1203
+ const view = typed_array_get_view(p);
1204
+ const vals = new Array(len);
1205
+ for (let i = 0; i < len; i++)
1206
+ vals[i] = view[i];
1207
+ if (!JS_IsUndefined(cmp)) {
1208
+ let exception = 0;
1209
+ const idx = new Array(len);
1210
+ for (let i = 0; i < len; i++)
1211
+ idx[i] = i;
1212
+ const sorted = merge_sort_indices(idx, (a, b) => {
1213
+ if (exception)
1214
+ return 0;
1215
+ const res = JS_Call(ctx, cmp, undefined, [vals[a], vals[b]]);
1216
+ if (JS_IsException(res)) {
1217
+ exception = 1;
1218
+ return 0;
1219
+ }
1220
+ const d = JS_ToFloat64(ctx, res);
1221
+ if (JS_IsException(d)) {
1222
+ exception = 1;
1223
+ return 0;
1224
+ }
1225
+ let v = d;
1226
+ if (Number.isNaN(v))
1227
+ v = 0;
1228
+ /* make sort stable: compare array offsets */
1229
+ if (v === 0)
1230
+ return a - b;
1231
+ return v < 0 ? -1 : 1;
1232
+ });
1233
+ if (exception)
1234
+ return JS_EXCEPTION;
1235
+ /* the array may have been detached or resized during the sort: no error */
1236
+ const view1 = typed_array_get_view(p);
1237
+ if (view1) {
1238
+ const n = Math.min(len, view1.length);
1239
+ for (let i = 0; i < n; i++)
1240
+ view1[i] = vals[sorted[i]];
1241
+ }
1242
+ }
1243
+ else {
1244
+ const idx = new Array(len);
1245
+ for (let i = 0; i < len; i++)
1246
+ idx[i] = i;
1247
+ const sorted = merge_sort_indices(idx, (a, b) => js_TA_cmp_default(vals[a], vals[b]));
1248
+ for (let i = 0; i < len; i++)
1249
+ view[i] = vals[sorted[i]];
1250
+ }
1251
+ }
1252
+ return this_val;
1253
+ }
1254
+ function js_typed_array_toSorted(ctx, this_val, argv) {
1255
+ const p = get_typed_array(ctx, this_val, false);
1256
+ if (!p)
1257
+ return JS_EXCEPTION;
1258
+ const arr = js_typed_array_constructor_ta(ctx, undefined, this_val, Q.JS_GetClassID(p));
1259
+ if (JS_IsException(arr))
1260
+ return JS_EXCEPTION;
1261
+ return js_typed_array_sort(ctx, arr, argv);
1262
+ }
1263
+ /* every/some/forEach/map/filter (typed array variants of js_array_every) */
1264
+ const special_every = 0;
1265
+ const special_some = 1;
1266
+ const special_forEach = 2;
1267
+ const special_map = 3;
1268
+ const special_filter = 4;
1269
+ const special_TA = 8;
1270
+ function js_array_every(ctx, this_val, argv, special) {
1271
+ const obj = this_val;
1272
+ const len = js_typed_array_get_length_internal(ctx, obj);
1273
+ if (len < 0)
1274
+ return JS_EXCEPTION;
1275
+ const func = argv[0];
1276
+ let this_arg = undefined;
1277
+ if (argv.length > 1)
1278
+ this_arg = argv[1];
1279
+ if (check_function(ctx, func))
1280
+ return JS_EXCEPTION;
1281
+ let ret = undefined;
1282
+ switch (special) {
1283
+ case special_every | special_TA:
1284
+ ret = true;
1285
+ break;
1286
+ case special_some | special_TA:
1287
+ ret = false;
1288
+ break;
1289
+ case special_map | special_TA:
1290
+ ret = js_typed_array___speciesCreate(ctx, undefined, [obj, len]);
1291
+ if (JS_IsException(ret))
1292
+ return JS_EXCEPTION;
1293
+ break;
1294
+ case special_filter | special_TA:
1295
+ ret = JS_NewArray(ctx);
1296
+ if (JS_IsException(ret))
1297
+ return JS_EXCEPTION;
1298
+ break;
1299
+ }
1300
+ let n = 0;
1301
+ let k;
1302
+ loop: for (k = 0; k < len; k++) {
1303
+ const val = JS_GetPropertyInt64(ctx, obj, k);
1304
+ if (JS_IsException(val))
1305
+ return JS_EXCEPTION;
1306
+ const res = JS_Call(ctx, func, this_arg, [val, k, obj]);
1307
+ if (JS_IsException(res))
1308
+ return JS_EXCEPTION;
1309
+ switch (special) {
1310
+ case special_every | special_TA:
1311
+ if (!JS_ToBoolFree(ctx, res)) {
1312
+ ret = false;
1313
+ break loop;
1314
+ }
1315
+ break;
1316
+ case special_some | special_TA:
1317
+ if (JS_ToBoolFree(ctx, res)) {
1318
+ ret = true;
1319
+ break loop;
1320
+ }
1321
+ break;
1322
+ case special_map | special_TA:
1323
+ if (JS_SetPropertyValue(ctx, ret, k, res, JS_PROP_THROW) < 0)
1324
+ return JS_EXCEPTION;
1325
+ break;
1326
+ case special_filter | special_TA:
1327
+ if (JS_ToBoolFree(ctx, res)) {
1328
+ if (JS_DefinePropertyValueInt64(ctx, ret, n++, val, JS_PROP_C_W_E | JS_PROP_THROW) < 0)
1329
+ return JS_EXCEPTION;
1330
+ }
1331
+ break;
1332
+ default:
1333
+ break;
1334
+ }
1335
+ }
1336
+ if (special === (special_filter | special_TA)) {
1337
+ const arr = js_typed_array___speciesCreate(ctx, undefined, [obj, n]);
1338
+ if (JS_IsException(arr))
1339
+ return JS_EXCEPTION;
1340
+ const res = JS_Invoke(ctx, arr, JS_ATOM_set, [ret]);
1341
+ if (JS_IsException(res))
1342
+ return JS_EXCEPTION;
1343
+ ret = arr;
1344
+ }
1345
+ return ret;
1346
+ }
1347
+ const special_reduce = 0;
1348
+ const special_reduceRight = 1;
1349
+ function js_array_reduce(ctx, this_val, argv, special) {
1350
+ const obj = this_val;
1351
+ const len = js_typed_array_get_length_internal(ctx, obj);
1352
+ if (len < 0)
1353
+ return JS_EXCEPTION;
1354
+ const func = argv[0];
1355
+ if (check_function(ctx, func))
1356
+ return JS_EXCEPTION;
1357
+ let k = 0;
1358
+ let acc;
1359
+ if (argv.length > 1) {
1360
+ acc = argv[1];
1361
+ }
1362
+ else {
1363
+ if (k >= len)
1364
+ return JS_ThrowTypeError(ctx, 'empty array');
1365
+ const k1 = (special & special_reduceRight) ? len - k - 1 : k;
1366
+ k++;
1367
+ acc = JS_GetPropertyInt64(ctx, obj, k1);
1368
+ if (JS_IsException(acc))
1369
+ return JS_EXCEPTION;
1370
+ }
1371
+ for (; k < len; k++) {
1372
+ const k1 = (special & special_reduceRight) ? len - k - 1 : k;
1373
+ const val = JS_GetPropertyInt64(ctx, obj, k1);
1374
+ if (JS_IsException(val))
1375
+ return JS_EXCEPTION;
1376
+ const acc1 = JS_Call(ctx, func, undefined, [acc, val, k1, obj]);
1377
+ if (JS_IsException(acc1))
1378
+ return JS_EXCEPTION;
1379
+ acc = acc1;
1380
+ }
1381
+ return acc;
1382
+ }
1383
+ /* fallback Array.prototype.toString (used if the Array builtins are not
1384
+ installed yet): port of js_array_toString */
1385
+ function js_array_toString(ctx, this_val, argv) {
1386
+ const obj = JS_ToObject(ctx, this_val);
1387
+ if (JS_IsException(obj))
1388
+ return JS_EXCEPTION;
1389
+ const method = JS_GetProperty(ctx, obj, JS_ATOM_join);
1390
+ if (JS_IsException(method))
1391
+ return JS_EXCEPTION;
1392
+ if (!JS_IsFunction(ctx, method)) {
1393
+ /* Use intrinsic Object.prototype.toString */
1394
+ const objproto = ctx.class_proto[1 /* JSClassID.OBJECT */];
1395
+ const ts = objproto ? JS_GetProperty(ctx, objproto, JS_ATOM_toString) : undefined;
1396
+ if (JS_IsException(ts))
1397
+ return JS_EXCEPTION;
1398
+ if (JS_IsFunction(ctx, ts))
1399
+ return JS_Call(ctx, ts, obj, []);
1400
+ return '[object Object]';
1401
+ }
1402
+ return JS_Call(ctx, method, obj, []);
1403
+ }
1404
+ const js_typed_array_base_funcs = [
1405
+ JS_CFUNC_DEF('from', 1, js_typed_array_from),
1406
+ JS_CFUNC_DEF('of', 0, js_typed_array_of),
1407
+ JS_CGETSET_DEF(JS_ATOM_Symbol_species, js_get_this, null),
1408
+ ];
1409
+ const js_typed_array_base_proto_funcs = [
1410
+ JS_CGETSET_DEF(JS_ATOM_length, js_typed_array_get_length, null),
1411
+ JS_CFUNC_DEF('at', 1, js_typed_array_at),
1412
+ JS_CFUNC_DEF('with', 2, js_typed_array_with),
1413
+ JS_CGETSET_MAGIC_DEF('buffer', js_typed_array_get_buffer, null, 0),
1414
+ JS_CGETSET_MAGIC_DEF('byteLength', js_typed_array_get_byteLength, null, 0),
1415
+ JS_CGETSET_MAGIC_DEF('byteOffset', js_typed_array_get_byteOffset, null, 0),
1416
+ JS_CFUNC_DEF('set', 1, js_typed_array_set),
1417
+ /* typed arrays are host typed arrays: the host iterators are used, with
1418
+ the guest Array Iterator prototype */
1419
+ JS_NATIVE_DEF('values', 0, HOST_TYPED_ARRAY_PROTO.values, fix_array_iterator),
1420
+ JS_ALIAS_DEF(JS_ATOM_Symbol_iterator, 'values'),
1421
+ JS_NATIVE_DEF('keys', 0, HOST_TYPED_ARRAY_PROTO.keys, fix_array_iterator),
1422
+ JS_NATIVE_DEF('entries', 0, HOST_TYPED_ARRAY_PROTO.entries, fix_array_iterator),
1423
+ JS_CGETSET_DEF(JS_ATOM_Symbol_toStringTag, js_typed_array_get_toStringTag, null),
1424
+ JS_CFUNC_DEF('copyWithin', 2, js_typed_array_copyWithin),
1425
+ JS_CFUNC_MAGIC_DEF('every', 1, js_array_every, special_every | special_TA),
1426
+ JS_CFUNC_MAGIC_DEF('some', 1, js_array_every, special_some | special_TA),
1427
+ JS_CFUNC_MAGIC_DEF('forEach', 1, js_array_every, special_forEach | special_TA),
1428
+ JS_CFUNC_MAGIC_DEF('map', 1, js_array_every, special_map | special_TA),
1429
+ JS_CFUNC_MAGIC_DEF('filter', 1, js_array_every, special_filter | special_TA),
1430
+ JS_CFUNC_MAGIC_DEF('reduce', 1, js_array_reduce, special_reduce | special_TA),
1431
+ JS_CFUNC_MAGIC_DEF('reduceRight', 1, js_array_reduce, special_reduceRight | special_TA),
1432
+ JS_CFUNC_DEF('fill', 1, js_typed_array_fill),
1433
+ JS_CFUNC_MAGIC_DEF('find', 1, js_typed_array_find, 0 /* ArrayFindMode.ArrayFind */),
1434
+ JS_CFUNC_MAGIC_DEF('findIndex', 1, js_typed_array_find, 1 /* ArrayFindMode.ArrayFindIndex */),
1435
+ JS_CFUNC_MAGIC_DEF('findLast', 1, js_typed_array_find, 2 /* ArrayFindMode.ArrayFindLast */),
1436
+ JS_CFUNC_MAGIC_DEF('findLastIndex', 1, js_typed_array_find, 3 /* ArrayFindMode.ArrayFindLastIndex */),
1437
+ JS_CFUNC_DEF('reverse', 0, js_typed_array_reverse),
1438
+ JS_CFUNC_DEF('toReversed', 0, js_typed_array_toReversed),
1439
+ JS_CFUNC_DEF('slice', 2, js_typed_array_slice),
1440
+ JS_CFUNC_DEF('subarray', 2, js_typed_array_subarray),
1441
+ JS_CFUNC_DEF('sort', 1, js_typed_array_sort),
1442
+ JS_CFUNC_DEF('toSorted', 1, js_typed_array_toSorted),
1443
+ JS_CFUNC_MAGIC_DEF('join', 1, js_typed_array_join, 0),
1444
+ JS_CFUNC_MAGIC_DEF('toLocaleString', 0, js_typed_array_join, 1),
1445
+ JS_CFUNC_MAGIC_DEF('indexOf', 1, js_typed_array_indexOf, special_indexOf),
1446
+ JS_CFUNC_MAGIC_DEF('lastIndexOf', 1, js_typed_array_indexOf, special_lastIndexOf),
1447
+ JS_CFUNC_MAGIC_DEF('includes', 1, js_typed_array_indexOf, special_includes),
1448
+ ];
1449
+ function js_typed_array_base_constructor(ctx, this_val, argv) {
1450
+ return JS_ThrowTypeError(ctx, 'cannot be called');
1451
+ }
1452
+ /* 'obj' is the allocated placeholder object (its prototype and class are
1453
+ used). Returns the guest typed array object: a host typed array over
1454
+ the buffer, re-prototyped, carrying the metadata. */
1455
+ function typed_array_init(ctx, obj, buffer, offset, len, track_rab) {
1456
+ const class_id = Q.JS_GetClassID(obj);
1457
+ const size_log2 = typed_array_size_log2(class_id);
1458
+ const ta = new JSTypedArray(buffer, offset, len << size_log2);
1459
+ ta.track_rab = track_rab;
1460
+ const abuf = Q.JS_GetAnyOpaque(buffer);
1461
+ const ctor = host_ta_ctors[class_id - 21 /* JSClassID.UINT8C_ARRAY */];
1462
+ let view;
1463
+ try {
1464
+ view = track_rab ? new ctor(abuf.buffer, offset) : new ctor(abuf.buffer, offset, len);
1465
+ }
1466
+ catch (e) {
1467
+ return Q.js_host_exception_to_guest(ctx, e);
1468
+ }
1469
+ return host_view_adopt(ctx, obj, view, ta);
1470
+ }
1471
+ /* move the class metadata of the placeholder 'obj' to the host view */
1472
+ function host_view_adopt(ctx, obj, view, ta) {
1473
+ Object.setPrototypeOf(view, Reflect.getPrototypeOf(obj));
1474
+ const m = Q.js_meta.get(obj);
1475
+ m.u = ta;
1476
+ Q.js_meta.set(view, m);
1477
+ Q.js_meta.delete(obj);
1478
+ return view;
1479
+ }
1480
+ function js_typed_array_constructor_obj(ctx, new_target, obj, classid) {
1481
+ const size_log2 = typed_array_size_log2(classid);
1482
+ let ret = js_create_from_ctor(ctx, new_target, classid);
1483
+ if (JS_IsException(ret))
1484
+ return JS_EXCEPTION;
1485
+ const iter = JS_GetProperty(ctx, obj, JS_ATOM_Symbol_iterator);
1486
+ if (JS_IsException(iter))
1487
+ return JS_EXCEPTION;
1488
+ let arr;
1489
+ let len;
1490
+ if (!JS_IsUndefined(iter) && !JS_IsNull(iter)) {
1491
+ const r = js_array_from_iterator(ctx, obj, iter);
1492
+ if (!r)
1493
+ return JS_EXCEPTION;
1494
+ arr = r.arr;
1495
+ len = r.len;
1496
+ }
1497
+ else {
1498
+ len = js_get_length64(ctx, obj);
1499
+ if (len < 0)
1500
+ return JS_EXCEPTION;
1501
+ arr = obj;
1502
+ }
1503
+ const buffer = js_array_buffer_constructor1(ctx, undefined, len << size_log2);
1504
+ if (JS_IsException(buffer))
1505
+ return JS_EXCEPTION;
1506
+ ret = typed_array_init(ctx, ret, buffer, 0, len, false);
1507
+ if (JS_IsException(ret))
1508
+ return JS_EXCEPTION;
1509
+ for (let i = 0; i < len; i++) {
1510
+ const val = JS_GetPropertyUint32(ctx, arr, i);
1511
+ if (JS_IsException(val))
1512
+ return JS_EXCEPTION;
1513
+ if (JS_SetPropertyUint32(ctx, ret, i, val) < 0)
1514
+ return JS_EXCEPTION;
1515
+ }
1516
+ return ret;
1517
+ }
1518
+ function js_typed_array_constructor_ta(ctx, new_target, src_obj, classid) {
1519
+ let obj = js_create_from_ctor(ctx, new_target, classid);
1520
+ if (JS_IsException(obj))
1521
+ return obj;
1522
+ const p = src_obj;
1523
+ if (typed_array_is_detached(ctx, p))
1524
+ return JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
1525
+ if (typed_array_is_oob(ctx, p))
1526
+ return JS_ThrowTypeErrorArrayBufferOOB(ctx);
1527
+ const ta = Q.JS_GetAnyOpaque(p);
1528
+ const len = typed_array_get_length(ctx, p);
1529
+ const src_abuf = Q.JS_GetAnyOpaque(ta.buffer);
1530
+ const size_log2 = typed_array_size_log2(classid);
1531
+ const buffer = js_array_buffer_constructor1(ctx, undefined, len << size_log2);
1532
+ if (JS_IsException(buffer))
1533
+ return JS_EXCEPTION;
1534
+ /* necessary because it could have been detached */
1535
+ if (typed_array_is_detached(ctx, p))
1536
+ return JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
1537
+ const abuf = Q.JS_GetAnyOpaque(buffer);
1538
+ obj = typed_array_init(ctx, obj, buffer, 0, len, false);
1539
+ if (JS_IsException(obj))
1540
+ return JS_EXCEPTION;
1541
+ if (Q.JS_GetClassID(p) === classid) {
1542
+ /* same type: copy the content */
1543
+ abuf.data.set(src_abuf.data.subarray(ta.offset, ta.offset + abuf.data.length));
1544
+ }
1545
+ else {
1546
+ for (let i = 0; i < len; i++) {
1547
+ const val = JS_GetPropertyUint32(ctx, src_obj, i);
1548
+ if (JS_IsException(val))
1549
+ return JS_EXCEPTION;
1550
+ if (JS_SetPropertyUint32(ctx, obj, i, val) < 0)
1551
+ return JS_EXCEPTION;
1552
+ }
1553
+ }
1554
+ return obj;
1555
+ }
1556
+ function js_typed_array_constructor(ctx, new_target, argv, classid) {
1557
+ const size_log2 = typed_array_size_log2(classid);
1558
+ let buffer;
1559
+ let len, offset;
1560
+ let track_rab = false;
1561
+ const arg0 = argv[0];
1562
+ if (!Q.JS_IsObject(arg0)) {
1563
+ len = JS_ToIndex(ctx, arg0);
1564
+ if (len < 0)
1565
+ return JS_EXCEPTION;
1566
+ buffer = js_array_buffer_constructor1(ctx, undefined, len << size_log2);
1567
+ if (JS_IsException(buffer))
1568
+ return JS_EXCEPTION;
1569
+ offset = 0;
1570
+ }
1571
+ else {
1572
+ const p = arg0;
1573
+ if (Q.JS_GetClassID(p) === 19 /* JSClassID.ARRAY_BUFFER */ || Q.JS_GetClassID(p) === 20 /* JSClassID.SHARED_ARRAY_BUFFER */) {
1574
+ const abuf = Q.JS_GetAnyOpaque(p);
1575
+ offset = JS_ToIndex(ctx, argv[1]);
1576
+ if (offset < 0)
1577
+ return JS_EXCEPTION;
1578
+ if (abuf.detached)
1579
+ return JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
1580
+ if ((offset & ((1 << size_log2) - 1)) !== 0 || offset > abuf.data.length)
1581
+ return JS_ThrowRangeError(ctx, 'invalid offset');
1582
+ if (JS_IsUndefined(argv[2])) {
1583
+ if (array_buffer_is_resizable(abuf)) {
1584
+ track_rab = true;
1585
+ len = (abuf.data.length - offset) >> size_log2;
1586
+ }
1587
+ else {
1588
+ if ((abuf.data.length & ((1 << size_log2) - 1)) !== 0)
1589
+ return JS_ThrowRangeError(ctx, 'invalid length');
1590
+ len = (abuf.data.length - offset) >> size_log2;
1591
+ }
1592
+ }
1593
+ else {
1594
+ len = JS_ToIndex(ctx, argv[2]);
1595
+ if (len < 0)
1596
+ return JS_EXCEPTION;
1597
+ if (abuf.detached)
1598
+ return JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
1599
+ if ((offset + (len << size_log2)) > abuf.data.length)
1600
+ return JS_ThrowRangeError(ctx, 'invalid length');
1601
+ }
1602
+ buffer = arg0;
1603
+ }
1604
+ else {
1605
+ if (is_typed_array_class(Q.JS_GetClassID(p))) {
1606
+ return js_typed_array_constructor_ta(ctx, new_target, arg0, classid);
1607
+ }
1608
+ else {
1609
+ return js_typed_array_constructor_obj(ctx, new_target, arg0, classid);
1610
+ }
1611
+ }
1612
+ }
1613
+ const obj = js_create_from_ctor(ctx, new_target, classid);
1614
+ if (JS_IsException(obj))
1615
+ return JS_EXCEPTION;
1616
+ return typed_array_init(ctx, obj, buffer, offset, len, track_rab);
1617
+ }
1618
+ /* ---------------------------------------------------------------------
1619
+ DataView
1620
+ --------------------------------------------------------------------- */
1621
+ function js_dataview_constructor(ctx, new_target, argv) {
1622
+ const buffer = argv[0];
1623
+ const abuf = js_get_array_buffer(ctx, buffer);
1624
+ if (!abuf)
1625
+ return JS_EXCEPTION;
1626
+ let offset = 0;
1627
+ if (argv.length > 1) {
1628
+ offset = JS_ToIndex(ctx, argv[1]);
1629
+ if (offset < 0)
1630
+ return JS_EXCEPTION;
1631
+ }
1632
+ if (abuf.detached)
1633
+ return JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
1634
+ if (offset > abuf.data.length)
1635
+ return JS_ThrowRangeError(ctx, 'invalid byteOffset');
1636
+ let len = abuf.data.length - offset;
1637
+ let track_rab = false;
1638
+ if (argv.length > 2 && !JS_IsUndefined(argv[2])) {
1639
+ const l = JS_ToIndex(ctx, argv[2]);
1640
+ if (l < 0)
1641
+ return JS_EXCEPTION;
1642
+ if (l > len)
1643
+ return JS_ThrowRangeError(ctx, 'invalid byteLength');
1644
+ len = l;
1645
+ }
1646
+ else {
1647
+ track_rab = array_buffer_is_resizable(abuf);
1648
+ }
1649
+ const obj = js_create_from_ctor(ctx, new_target, 32 /* JSClassID.DATAVIEW */);
1650
+ if (JS_IsException(obj))
1651
+ return JS_EXCEPTION;
1652
+ if (abuf.detached) {
1653
+ /* could have been detached in js_create_from_ctor() */
1654
+ return JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
1655
+ }
1656
+ /* the buffer could have been resized in js_create_from_ctor() */
1657
+ if (offset > abuf.data.length || (!track_rab && offset + len > abuf.data.length))
1658
+ return JS_ThrowTypeErrorArrayBufferOOB(ctx);
1659
+ const ta = new JSTypedArray(buffer, offset, len);
1660
+ ta.track_rab = track_rab;
1661
+ let view;
1662
+ try {
1663
+ view = track_rab ? new DataView(abuf.buffer, offset) : new DataView(abuf.buffer, offset, len);
1664
+ }
1665
+ catch (e) {
1666
+ return Q.js_host_exception_to_guest(ctx, e);
1667
+ }
1668
+ return host_view_adopt(ctx, obj, view, ta);
1669
+ }
1670
+ function js_dataview_getValue(ctx, this_obj, argv, class_id) {
1671
+ const ta = JS_GetOpaque2(ctx, this_obj, 32 /* JSClassID.DATAVIEW */);
1672
+ if (!ta)
1673
+ return JS_EXCEPTION;
1674
+ const size = 1 << typed_array_size_log2(class_id);
1675
+ const pos = JS_ToIndex(ctx, argv[0]);
1676
+ if (pos < 0)
1677
+ return JS_EXCEPTION;
1678
+ const littleEndian = argv.length > 1 && JS_ToBoolFree(ctx, argv[1]);
1679
+ const abuf = Q.JS_GetAnyOpaque(ta.buffer);
1680
+ if (abuf.detached)
1681
+ return JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
1682
+ const ta_len = typed_array_get_byte_length(ta, 0);
1683
+ if (ta_len < 0)
1684
+ return JS_ThrowTypeErrorArrayBufferOOB(ctx);
1685
+ if ((pos + size) > ta_len)
1686
+ return JS_ThrowRangeError(ctx, 'out of bound');
1687
+ const dv = array_buffer_get_dv(abuf);
1688
+ const ptr = ta.offset + pos;
1689
+ switch (class_id) {
1690
+ case 22 /* JSClassID.INT8_ARRAY */: return dv.getInt8(ptr);
1691
+ case 23 /* JSClassID.UINT8_ARRAY */: return dv.getUint8(ptr);
1692
+ case 24 /* JSClassID.INT16_ARRAY */: return dv.getInt16(ptr, littleEndian);
1693
+ case 25 /* JSClassID.UINT16_ARRAY */: return dv.getUint16(ptr, littleEndian);
1694
+ case 26 /* JSClassID.INT32_ARRAY */: return dv.getInt32(ptr, littleEndian);
1695
+ case 27 /* JSClassID.UINT32_ARRAY */: return dv.getUint32(ptr, littleEndian);
1696
+ case 28 /* JSClassID.BIG_INT64_ARRAY */: return dv.getBigInt64(ptr, littleEndian);
1697
+ case 29 /* JSClassID.BIG_UINT64_ARRAY */: return dv.getBigUint64(ptr, littleEndian);
1698
+ case 30 /* JSClassID.FLOAT32_ARRAY */: return dv.getFloat32(ptr, littleEndian);
1699
+ case 31 /* JSClassID.FLOAT64_ARRAY */: return dv.getFloat64(ptr, littleEndian);
1700
+ default: return JS_ThrowTypeError(ctx, 'invalid DataView type');
1701
+ }
1702
+ }
1703
+ function js_dataview_setValue(ctx, this_obj, argv, class_id) {
1704
+ const ta = JS_GetOpaque2(ctx, this_obj, 32 /* JSClassID.DATAVIEW */);
1705
+ if (!ta)
1706
+ return JS_EXCEPTION;
1707
+ const size = 1 << typed_array_size_log2(class_id);
1708
+ const pos = JS_ToIndex(ctx, argv[0]);
1709
+ if (pos < 0)
1710
+ return JS_EXCEPTION;
1711
+ const val = argv[1];
1712
+ let v = 0;
1713
+ let v64 = 0n;
1714
+ if (class_id <= 27 /* JSClassID.UINT32_ARRAY */) {
1715
+ const r = JS_ToUint32Free(ctx, val);
1716
+ if (JS_IsException(r))
1717
+ return JS_EXCEPTION;
1718
+ v = r;
1719
+ }
1720
+ else if (class_id <= 29 /* JSClassID.BIG_UINT64_ARRAY */) {
1721
+ const r = JS_ToBigInt64(ctx, val);
1722
+ if (JS_IsException(r))
1723
+ return JS_EXCEPTION;
1724
+ v64 = r;
1725
+ }
1726
+ else {
1727
+ const r = JS_ToFloat64(ctx, val);
1728
+ if (JS_IsException(r))
1729
+ return JS_EXCEPTION;
1730
+ v = r;
1731
+ }
1732
+ const littleEndian = argv.length > 2 && JS_ToBoolFree(ctx, argv[2]);
1733
+ const abuf = Q.JS_GetAnyOpaque(ta.buffer);
1734
+ if (abuf.detached)
1735
+ return JS_ThrowTypeErrorDetachedArrayBuffer(ctx);
1736
+ const ta_len = typed_array_get_byte_length(ta, 0);
1737
+ if (ta_len < 0)
1738
+ return JS_ThrowTypeErrorArrayBufferOOB(ctx);
1739
+ if ((pos + size) > ta_len)
1740
+ return JS_ThrowRangeError(ctx, 'out of bound');
1741
+ const dv = array_buffer_get_dv(abuf);
1742
+ const ptr = ta.offset + pos;
1743
+ switch (class_id) {
1744
+ case 22 /* JSClassID.INT8_ARRAY */:
1745
+ case 23 /* JSClassID.UINT8_ARRAY */:
1746
+ dv.setUint8(ptr, v);
1747
+ break;
1748
+ case 24 /* JSClassID.INT16_ARRAY */:
1749
+ case 25 /* JSClassID.UINT16_ARRAY */:
1750
+ dv.setUint16(ptr, v, littleEndian);
1751
+ break;
1752
+ case 26 /* JSClassID.INT32_ARRAY */:
1753
+ case 27 /* JSClassID.UINT32_ARRAY */:
1754
+ dv.setUint32(ptr, v, littleEndian);
1755
+ break;
1756
+ case 30 /* JSClassID.FLOAT32_ARRAY */:
1757
+ dv.setFloat32(ptr, v, littleEndian);
1758
+ break;
1759
+ case 28 /* JSClassID.BIG_INT64_ARRAY */:
1760
+ case 29 /* JSClassID.BIG_UINT64_ARRAY */:
1761
+ dv.setBigInt64(ptr, v64, littleEndian);
1762
+ break;
1763
+ case 31 /* JSClassID.FLOAT64_ARRAY */:
1764
+ dv.setFloat64(ptr, v, littleEndian);
1765
+ break;
1766
+ default:
1767
+ return JS_ThrowTypeError(ctx, 'invalid DataView type');
1768
+ }
1769
+ return undefined;
1770
+ }
1771
+ const js_dataview_proto_funcs = [
1772
+ JS_CGETSET_MAGIC_DEF('buffer', js_typed_array_get_buffer, null, 1),
1773
+ JS_CGETSET_MAGIC_DEF('byteLength', js_typed_array_get_byteLength, null, 1),
1774
+ JS_CGETSET_MAGIC_DEF('byteOffset', js_typed_array_get_byteOffset, null, 1),
1775
+ JS_CFUNC_MAGIC_DEF('getInt8', 1, js_dataview_getValue, 22 /* JSClassID.INT8_ARRAY */),
1776
+ JS_CFUNC_MAGIC_DEF('getUint8', 1, js_dataview_getValue, 23 /* JSClassID.UINT8_ARRAY */),
1777
+ JS_CFUNC_MAGIC_DEF('getInt16', 1, js_dataview_getValue, 24 /* JSClassID.INT16_ARRAY */),
1778
+ JS_CFUNC_MAGIC_DEF('getUint16', 1, js_dataview_getValue, 25 /* JSClassID.UINT16_ARRAY */),
1779
+ JS_CFUNC_MAGIC_DEF('getInt32', 1, js_dataview_getValue, 26 /* JSClassID.INT32_ARRAY */),
1780
+ JS_CFUNC_MAGIC_DEF('getUint32', 1, js_dataview_getValue, 27 /* JSClassID.UINT32_ARRAY */),
1781
+ JS_CFUNC_MAGIC_DEF('getBigInt64', 1, js_dataview_getValue, 28 /* JSClassID.BIG_INT64_ARRAY */),
1782
+ JS_CFUNC_MAGIC_DEF('getBigUint64', 1, js_dataview_getValue, 29 /* JSClassID.BIG_UINT64_ARRAY */),
1783
+ JS_CFUNC_MAGIC_DEF('getFloat32', 1, js_dataview_getValue, 30 /* JSClassID.FLOAT32_ARRAY */),
1784
+ JS_CFUNC_MAGIC_DEF('getFloat64', 1, js_dataview_getValue, 31 /* JSClassID.FLOAT64_ARRAY */),
1785
+ JS_CFUNC_MAGIC_DEF('setInt8', 2, js_dataview_setValue, 22 /* JSClassID.INT8_ARRAY */),
1786
+ JS_CFUNC_MAGIC_DEF('setUint8', 2, js_dataview_setValue, 23 /* JSClassID.UINT8_ARRAY */),
1787
+ JS_CFUNC_MAGIC_DEF('setInt16', 2, js_dataview_setValue, 24 /* JSClassID.INT16_ARRAY */),
1788
+ JS_CFUNC_MAGIC_DEF('setUint16', 2, js_dataview_setValue, 25 /* JSClassID.UINT16_ARRAY */),
1789
+ JS_CFUNC_MAGIC_DEF('setInt32', 2, js_dataview_setValue, 26 /* JSClassID.INT32_ARRAY */),
1790
+ JS_CFUNC_MAGIC_DEF('setUint32', 2, js_dataview_setValue, 27 /* JSClassID.UINT32_ARRAY */),
1791
+ JS_CFUNC_MAGIC_DEF('setBigInt64', 2, js_dataview_setValue, 28 /* JSClassID.BIG_INT64_ARRAY */),
1792
+ JS_CFUNC_MAGIC_DEF('setBigUint64', 2, js_dataview_setValue, 29 /* JSClassID.BIG_UINT64_ARRAY */),
1793
+ JS_CFUNC_MAGIC_DEF('setFloat32', 2, js_dataview_setValue, 30 /* JSClassID.FLOAT32_ARRAY */),
1794
+ JS_CFUNC_MAGIC_DEF('setFloat64', 2, js_dataview_setValue, 31 /* JSClassID.FLOAT64_ARRAY */),
1795
+ JS_PROP_STRING_DEF(JS_ATOM_Symbol_toStringTag, 'DataView', JS_PROP_CONFIGURABLE),
1796
+ ];
1797
+ /* ---------------------------------------------------------------------
1798
+ Intrinsics
1799
+ --------------------------------------------------------------------- */
1800
+ export function JS_AddIntrinsicTypedArrays(ctx) {
1801
+ const ab_proto = JS_NewObject(ctx);
1802
+ ctx.class_proto[19 /* JSClassID.ARRAY_BUFFER */] = ab_proto;
1803
+ JS_SetPropertyFunctionList(ctx, ab_proto, js_array_buffer_proto_funcs);
1804
+ const array_buffer_func = JS_NewGlobalCConstructorOnly(ctx, 'ArrayBuffer', js_array_buffer_constructor, 1, ab_proto);
1805
+ JS_SetPropertyFunctionList(ctx, array_buffer_func, js_array_buffer_funcs);
1806
+ const sab_proto = JS_NewObject(ctx);
1807
+ ctx.class_proto[20 /* JSClassID.SHARED_ARRAY_BUFFER */] = sab_proto;
1808
+ JS_SetPropertyFunctionList(ctx, sab_proto, js_shared_array_buffer_proto_funcs);
1809
+ const shared_array_buffer_func = JS_NewGlobalCConstructorOnly(ctx, 'SharedArrayBuffer', js_shared_array_buffer_constructor, 1, sab_proto);
1810
+ JS_SetPropertyFunctionList(ctx, shared_array_buffer_func, js_shared_array_buffer_funcs);
1811
+ const typed_array_base_proto = JS_NewObject(ctx);
1812
+ JS_SetPropertyFunctionList(ctx, typed_array_base_proto, js_typed_array_base_proto_funcs);
1813
+ /* TypedArray.prototype.toString must be the same object as Array.prototype.toString */
1814
+ let obj = JS_GetProperty(ctx, ctx.class_proto[2 /* JSClassID.ARRAY */], JS_ATOM_toString);
1815
+ if (JS_IsException(obj) || !JS_IsFunction(ctx, obj)) {
1816
+ obj = JS_NewCFunction3(ctx, js_array_toString, 'toString', 0, 0 /* JSCFunctionEnum.generic */, 0, ctx.function_proto);
1817
+ }
1818
+ JS_DefinePropertyValue(ctx, typed_array_base_proto, JS_ATOM_toString, obj, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
1819
+ const typed_array_base_func = JS_NewCFunction2(ctx, js_typed_array_base_constructor, 'TypedArray', 0, 4 /* JSCFunctionEnum.constructor_or_func */, 0);
1820
+ JS_SetPropertyFunctionList(ctx, typed_array_base_func, js_typed_array_base_funcs);
1821
+ JS_SetConstructor(ctx, typed_array_base_func, typed_array_base_proto);
1822
+ for (let i = 21 /* JSClassID.UINT8C_ARRAY */; i < 21 /* JSClassID.UINT8C_ARRAY */ + JS_TYPED_ARRAY_COUNT; i++) {
1823
+ const proto = JS_NewObjectProto(ctx, typed_array_base_proto);
1824
+ ctx.class_proto[i] = proto;
1825
+ JS_DefinePropertyValueStr(ctx, proto, 'BYTES_PER_ELEMENT', 1 << typed_array_size_log2(i), 0);
1826
+ const name = JS_AtomToString(ctx, JS_ATOM_Uint8ClampedArray + i - 21 /* JSClassID.UINT8C_ARRAY */);
1827
+ const func_obj = JS_NewCFunction3(ctx, js_typed_array_constructor, name, 3, 3 /* JSCFunctionEnum.constructor_magic */, i, typed_array_base_func);
1828
+ JS_NewGlobalCConstructor2(ctx, func_obj, name, proto);
1829
+ JS_DefinePropertyValueStr(ctx, func_obj, 'BYTES_PER_ELEMENT', 1 << typed_array_size_log2(i), 0);
1830
+ }
1831
+ /* DataView */
1832
+ const dv_proto = JS_NewObject(ctx);
1833
+ ctx.class_proto[32 /* JSClassID.DATAVIEW */] = dv_proto;
1834
+ JS_SetPropertyFunctionList(ctx, dv_proto, js_dataview_proto_funcs);
1835
+ JS_NewGlobalCConstructorOnly(ctx, 'DataView', js_dataview_constructor, 1, dv_proto);
1836
+ }
1837
+ export { typed_array_get_length as js_typed_array_get_length_elements, typed_array_is_detached, validate_typed_array };
1838
+ //# sourceMappingURL=typedarray.js.map