Coverage Report

Created: 2026-12-01 06:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/moddable/xs/sources/xsDataView.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2016-2026  Moddable Tech, Inc.
3
 *
4
 *   This file is part of the Moddable SDK Runtime.
5
 * 
6
 *   The Moddable SDK Runtime is free software: you can redistribute it and/or modify
7
 *   it under the terms of the GNU Lesser General Public License as published by
8
 *   the Free Software Foundation, either version 3 of the License, or
9
 *   (at your option) any later version.
10
 * 
11
 *   The Moddable SDK Runtime is distributed in the hope that it will be useful,
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *   GNU Lesser General Public License for more details.
15
 * 
16
 *   You should have received a copy of the GNU Lesser General Public License
17
 *   along with the Moddable SDK Runtime.  If not, see <http://www.gnu.org/licenses/>.
18
 *
19
 * This file incorporates work covered by the following copyright and  
20
 * permission notice:  
21
 *
22
 *       Copyright (C) 2010-2016 Marvell International Ltd.
23
 *       Copyright (C) 2002-2010 Kinoma, Inc.
24
 *
25
 *       Licensed under the Apache License, Version 2.0 (the "License");
26
 *       you may not use this file except in compliance with the License.
27
 *       You may obtain a copy of the License at
28
 *
29
 *        http://www.apache.org/licenses/LICENSE-2.0
30
 *
31
 *       Unless required by applicable law or agreed to in writing, software
32
 *       distributed under the License is distributed on an "AS IS" BASIS,
33
 *       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34
 *       See the License for the specific language governing permissions and
35
 *       limitations under the License.
36
 */
37
38
#include "xsAll.h"
39
40
static txSlot* fxArgToInstance(txMachine* the, txInteger i);
41
42
static txSlot* fxCheckArrayBufferDetached(txMachine* the, txSlot* slot);
43
static txSlot* fxCheckArrayBufferInstance(txMachine* the, txSlot* slot);
44
static txSlot* fxCheckArrayBufferMutable(txMachine* the, txSlot* slot);
45
static txSlot* fxNewArrayBufferInstance(txMachine* the);
46
47
static txSlot* fxCheckDataViewInstance(txMachine* the, txSlot* slot, txBoolean mutable);
48
static txInteger fxCheckDataViewSize(txMachine* the, txSlot* view, txSlot* buffer, txBoolean mutable);
49
static txSlot* fxNewDataViewInstance(txMachine* the);
50
51
static void fxCallTypedArrayItem(txMachine* the, txSlot* function, txSlot* dispatch, txSlot* view, txSlot* data, txInteger index, txSlot* item);
52
static void fxCheckTypedArrayContentType(txMachine* the, txSlot* dispatch, txSlot* sourceDispatch);
53
static txSlot* fxCheckTypedArrayInstance(txMachine* the, txSlot* slot);
54
static txSlot* fxConstructTypedArray(txMachine* the);
55
static txSlot* fxNewTypedArrayInstance(txMachine* the, txTypeDispatch* dispatch, txTypeAtomics* atomics);
56
static void fxReduceTypedArrayItem(txMachine* the, txSlot* function, txSlot* dispatch, txSlot* view, txSlot* data, txInteger index);
57
58
static txBoolean fxTypedArrayDefineOwnProperty(txMachine* the, txSlot* instance, txID id, txIndex index, txSlot* slot, txFlag mask);
59
static txBoolean fxTypedArrayDeleteProperty(txMachine* the, txSlot* instance, txID id, txIndex index);
60
static txBoolean fxTypedArrayGetOwnProperty(txMachine* the, txSlot* instance, txID id, txIndex index, txSlot* slot);
61
static txSlot* fxTypedArrayGetProperty(txMachine* the, txSlot* instance, txID id, txIndex index, txFlag flag);
62
static txBoolean fxTypedArrayGetPropertyValue(txMachine* the, txSlot* instance, txID id, txIndex index, txSlot* value, txSlot* receiver);
63
static txBoolean fxTypedArrayHasProperty(txMachine* the, txSlot* instance, txID id, txIndex index);
64
static void fxTypedArrayOwnKeys(txMachine* the, txSlot* instance, txFlag flag, txSlot* keys);
65
static txBoolean fxTypedArrayPreventExtensions(txMachine* the, txSlot* instance);
66
static txSlot* fxTypedArraySetProperty(txMachine* the, txSlot* instance, txID id, txIndex index, txFlag flag);
67
static txBoolean fxTypedArraySetPropertyValue(txMachine* the, txSlot* instance, txID id, txIndex index, txSlot* value, txSlot* receiver);
68
69
static void fx_TypedArray_from_object(txMachine* the, txSlot* instance, txSlot* function, txSlot* _this);
70
71
const txBehavior ICACHE_FLASH_ATTR gxTypedArrayBehavior = {
72
  fxTypedArrayGetProperty,
73
  fxTypedArraySetProperty,
74
  fxOrdinaryCall,
75
  fxOrdinaryConstruct,
76
  fxTypedArrayDefineOwnProperty,
77
  fxTypedArrayDeleteProperty,
78
  fxTypedArrayGetOwnProperty,
79
  fxTypedArrayGetPropertyValue,
80
  fxOrdinaryGetPrototype,
81
  fxTypedArrayHasProperty,
82
  fxOrdinaryIsExtensible,
83
  fxTypedArrayOwnKeys,
84
  fxTypedArrayPreventExtensions,
85
  fxTypedArraySetPropertyValue,
86
  fxOrdinarySetPrototype,
87
};
88
89
void *fxArrayBuffer(txMachine* the, txSlot* slot, void* data, txInteger byteLength, txInteger maxByteLength)
90
6.68k
{
91
6.68k
  txSlot* instance;
92
6.68k
  txSlot* arrayBuffer;
93
6.68k
  txSlot* bufferInfo;
94
6.68k
  if (byteLength < 0)
95
0
    mxRangeError("invalid byteLength %ld", byteLength);
96
6.68k
  mxPush(mxArrayBufferPrototype);
97
6.68k
  instance = fxNewArrayBufferInstance(the);
98
6.68k
  arrayBuffer = instance->next;
99
6.68k
  arrayBuffer->value.arrayBuffer.address = fxNewChunk(the, byteLength);
100
6.68k
  bufferInfo = arrayBuffer->next;
101
6.68k
  bufferInfo->value.bufferInfo.length = byteLength;
102
6.68k
  bufferInfo->value.bufferInfo.maxLength = maxByteLength;
103
6.68k
  if (data != NULL)
104
6.68k
    c_memcpy(arrayBuffer->value.arrayBuffer.address, data, byteLength);
105
0
  else
106
0
    c_memset(arrayBuffer->value.arrayBuffer.address, 0, byteLength);
107
6.68k
  mxPullSlot(slot);
108
6.68k
  return arrayBuffer->value.arrayBuffer.address;
109
6.68k
}
110
111
void fxGetArrayBufferData(txMachine* the, txSlot* slot, txInteger byteOffset, void* data, txInteger byteLength)
112
0
{
113
0
  txSlot* instance = fxCheckArrayBufferInstance(the, slot);
114
0
  txSlot* arrayBuffer = instance->next;
115
0
  txSlot* bufferInfo = arrayBuffer->next;
116
0
  txInteger length = bufferInfo->value.bufferInfo.length;
117
0
  if ((byteOffset < 0) || (length < byteOffset))
118
0
    mxRangeError("invalid byteOffset %ld", byteOffset);
119
0
  if ((byteLength < 0) || (length < (byteOffset + byteLength)))
120
0
    mxRangeError("invalid byteLength %ld", byteLength);
121
0
  c_memcpy(data, arrayBuffer->value.arrayBuffer.address + byteOffset, byteLength);
122
0
}
123
124
txInteger fxGetArrayBufferLength(txMachine* the, txSlot* slot)
125
0
{
126
0
  txSlot* instance = fxCheckArrayBufferInstance(the, slot);
127
0
  txSlot* arrayBuffer = instance->next;
128
0
  txSlot* bufferInfo = arrayBuffer->next;
129
0
  return bufferInfo->value.bufferInfo.length;
130
0
}
131
132
txInteger fxGetArrayBufferMaxLength(txMachine* the, txSlot* slot)
133
0
{
134
0
  txSlot* instance = fxCheckArrayBufferInstance(the, slot);
135
0
  txSlot* arrayBuffer = instance->next;
136
0
  txSlot* bufferInfo = arrayBuffer->next;
137
0
  return bufferInfo->value.bufferInfo.maxLength;
138
0
}
139
140
void fxSetArrayBufferData(txMachine* the, txSlot* slot, txInteger byteOffset, void* data, txInteger byteLength)
141
0
{
142
0
  txSlot* instance = fxCheckArrayBufferInstance(the, slot);
143
0
  txSlot* arrayBuffer = instance->next;
144
0
  txSlot* bufferInfo = arrayBuffer->next;
145
0
  txInteger length = bufferInfo->value.bufferInfo.length;
146
0
  if ((byteOffset < 0) || (length < byteOffset))
147
0
    mxRangeError("invalid byteOffset %ld", byteOffset);
148
0
  if ((byteLength < 0) || (length < (byteOffset + byteLength)))
149
0
    mxRangeError("invalid byteLength %ld", byteLength);
150
0
  c_memcpy(arrayBuffer->value.arrayBuffer.address + byteOffset, data, byteLength);
151
0
}
152
153
void fxSetArrayBufferLength(txMachine* the, txSlot* slot, txInteger target)
154
0
{
155
0
  txSlot* instance = fxCheckArrayBufferInstance(the, slot);
156
0
  txSlot* arrayBuffer = instance->next;
157
0
  txSlot* bufferInfo = arrayBuffer->next;
158
0
  txInteger length = bufferInfo->value.bufferInfo.length;
159
0
  txByte* address = arrayBuffer->value.arrayBuffer.address;
160
0
  if (bufferInfo->value.bufferInfo.maxLength < 0)
161
0
    fxReport(the, "# Use xsArrayBufferResizable instead of xsArrayBuffer\n");
162
0
  if (length != target) {
163
0
    if (address)
164
0
      address = (txByte*)fxRenewChunk(the, address, target);
165
0
    if (address) {
166
0
      if (length < target)
167
0
        c_memset(address + length, 0, target - length);
168
0
    }
169
0
    else {
170
0
      address = (txByte*)fxNewChunk(the, target);
171
0
      if (length < target) {
172
0
        c_memcpy(address, arrayBuffer->value.arrayBuffer.address, length);
173
0
        c_memset(address + length, 0, target - length);
174
0
      }
175
0
      else
176
0
        c_memcpy(address, arrayBuffer->value.arrayBuffer.address, target);
177
0
    }
178
0
    arrayBuffer->value.arrayBuffer.address = address;
179
0
    bufferInfo->value.bufferInfo.length = target;
180
0
  }
181
0
}
182
183
void* fxToArrayBuffer(txMachine* the, txSlot* slot)
184
0
{
185
0
  txSlot* instance = fxCheckArrayBufferInstance(the, slot);
186
0
  txSlot* arrayBuffer = instance->next;
187
0
  return arrayBuffer->value.arrayBuffer.address;
188
0
}
189
190
void fxBuildDataView(txMachine* the)
191
6.68k
{
192
6.68k
  txSlot* instance;
193
6.68k
    txSlot* slot;
194
6.68k
  txInteger index;
195
6.68k
  const txTypeDispatch *dispatch;
196
6.68k
  const txTypeAtomics *atomics;
197
6.68k
  txSlot* property;
198
6.68k
    txSlot* constructor;
199
  
200
6.68k
  mxPush(mxObjectPrototype);
201
6.68k
  slot = fxLastProperty(the, fxNewObjectInstance(the));
202
6.68k
  slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_ArrayBuffer_prototype_get_byteLength), C_NULL, mxID(_byteLength), XS_DONT_ENUM_FLAG);
203
6.68k
#if mxECMAScript2023
204
6.68k
  slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_ArrayBuffer_prototype_get_detached), C_NULL, mxID(_detached), XS_DONT_ENUM_FLAG);
205
6.68k
#endif
206
6.68k
#if mxImmutableArrayBuffers
207
6.68k
  slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_ArrayBuffer_prototype_get_immutable), C_NULL, mxID(_immutable), XS_DONT_ENUM_FLAG);
208
6.68k
#endif
209
6.68k
  slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_ArrayBuffer_prototype_get_maxByteLength), C_NULL, mxID(_maxByteLength), XS_DONT_ENUM_FLAG);
210
6.68k
  slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_ArrayBuffer_prototype_get_resizable), C_NULL, mxID(_resizable), XS_DONT_ENUM_FLAG);
211
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_ArrayBuffer_prototype_concat), 1, mxID(_concat), XS_DONT_ENUM_FLAG);
212
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_ArrayBuffer_prototype_resize), 1, mxID(_resize), XS_DONT_ENUM_FLAG);
213
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_ArrayBuffer_prototype_slice), 2, mxID(_slice), XS_DONT_ENUM_FLAG);
214
6.68k
#if mxImmutableArrayBuffers
215
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_ArrayBuffer_prototype_sliceToImmutable), 2, mxID(_sliceToImmutable), XS_DONT_ENUM_FLAG);
216
6.68k
#endif
217
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_ArrayBuffer_prototype_transfer), 0, mxID(_transfer), XS_DONT_ENUM_FLAG);
218
6.68k
#if mxECMAScript2024
219
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_ArrayBuffer_prototype_transferToFixedLength), 0, mxID(_transferToFixedLength), XS_DONT_ENUM_FLAG);
220
6.68k
#endif
221
6.68k
#if mxImmutableArrayBuffers
222
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_ArrayBuffer_prototype_transferToImmutable), 0, mxID(_transferToImmutable), XS_DONT_ENUM_FLAG);
223
6.68k
#endif
224
6.68k
  slot = fxNextStringXProperty(the, slot, "ArrayBuffer", mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
225
6.68k
  mxArrayBufferPrototype = *the->stack;
226
6.68k
  slot = fxBuildHostConstructor(the, mxCallback(fx_ArrayBuffer), 1, mxID(_ArrayBuffer));
227
6.68k
  mxArrayBufferConstructor = *the->stack;
228
6.68k
  slot = fxLastProperty(the, slot);
229
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_ArrayBuffer_fromBigInt), 1, mxID(_fromBigInt), XS_DONT_ENUM_FLAG);
230
#ifndef mxCESU8
231
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_ArrayBuffer_fromString), 1, mxID(_fromString), XS_DONT_ENUM_FLAG);
232
#endif
233
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_ArrayBuffer_isView), 1, mxID(_isView), XS_DONT_ENUM_FLAG);
234
6.68k
  slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_species_get), C_NULL, mxID(_Symbol_species), XS_DONT_ENUM_FLAG);
235
6.68k
  mxPop();
236
  
237
6.68k
  mxPush(mxObjectPrototype);
238
6.68k
  slot = fxLastProperty(the, fxNewObjectInstance(the));
239
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_DataView_prototype_getBigInt64), 1, mxID(_getBigInt64), XS_DONT_ENUM_FLAG);
240
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_DataView_prototype_setBigInt64), 2, mxID(_setBigInt64), XS_DONT_ENUM_FLAG);
241
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_DataView_prototype_getBigUint64), 1, mxID(_getBigUint64), XS_DONT_ENUM_FLAG);
242
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_DataView_prototype_setBigUint64), 2, mxID(_setBigUint64), XS_DONT_ENUM_FLAG);
243
6.68k
#if mxFloat16
244
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_DataView_prototype_getFloat16), 1, mxID(_getFloat16), XS_DONT_ENUM_FLAG);
245
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_DataView_prototype_setFloat16), 2, mxID(_setFloat16), XS_DONT_ENUM_FLAG);
246
6.68k
#endif
247
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_DataView_prototype_getFloat32), 1, mxID(_getFloat32), XS_DONT_ENUM_FLAG);
248
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_DataView_prototype_setFloat32), 2, mxID(_setFloat32), XS_DONT_ENUM_FLAG);
249
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_DataView_prototype_getFloat64), 1, mxID(_getFloat64), XS_DONT_ENUM_FLAG);
250
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_DataView_prototype_setFloat64), 2, mxID(_setFloat64), XS_DONT_ENUM_FLAG);
251
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_DataView_prototype_getInt8), 1, mxID(_getInt8), XS_DONT_ENUM_FLAG);
252
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_DataView_prototype_setInt8), 2, mxID(_setInt8), XS_DONT_ENUM_FLAG);
253
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_DataView_prototype_getInt16), 1, mxID(_getInt16), XS_DONT_ENUM_FLAG);
254
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_DataView_prototype_setInt16), 2, mxID(_setInt16), XS_DONT_ENUM_FLAG);
255
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_DataView_prototype_getInt32), 1, mxID(_getInt32), XS_DONT_ENUM_FLAG);
256
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_DataView_prototype_setInt32), 2, mxID(_setInt32), XS_DONT_ENUM_FLAG);
257
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_DataView_prototype_getUint8), 1, mxID(_getUint8), XS_DONT_ENUM_FLAG);
258
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_DataView_prototype_setUint8), 2, mxID(_setUint8), XS_DONT_ENUM_FLAG);
259
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_DataView_prototype_getUint16), 1, mxID(_getUint16), XS_DONT_ENUM_FLAG);
260
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_DataView_prototype_setUint16), 2, mxID(_setUint16), XS_DONT_ENUM_FLAG);
261
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_DataView_prototype_getUint32), 1, mxID(_getUint32), XS_DONT_ENUM_FLAG);
262
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_DataView_prototype_setUint32), 2, mxID(_setUint32), XS_DONT_ENUM_FLAG);
263
6.68k
  slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_DataView_prototype_buffer_get), C_NULL, mxID(_buffer), XS_DONT_ENUM_FLAG);
264
6.68k
  slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_DataView_prototype_byteLength_get), C_NULL, mxID(_byteLength), XS_DONT_ENUM_FLAG);
265
6.68k
  slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_DataView_prototype_byteOffset_get), C_NULL, mxID(_byteOffset), XS_DONT_ENUM_FLAG);
266
6.68k
  slot = fxNextStringXProperty(the, slot, "DataView", mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
267
6.68k
  mxDataViewPrototype = *the->stack;
268
6.68k
  slot = fxBuildHostConstructor(the, mxCallback(fx_DataView), 1, mxID(_DataView));
269
6.68k
  mxDataViewConstructor = *the->stack;
270
6.68k
  mxPop();
271
  
272
6.68k
  mxPush(mxObjectPrototype);
273
6.68k
  instance = fxNewObjectInstance(the);
274
  
275
6.68k
  fxNewHostFunction(the, mxCallback(fxTypedArrayGetter), 0, XS_NO_ID, XS_NO_ID);
276
6.68k
  property = mxFunctionInstanceHome(the->stack->value.reference);
277
6.68k
  property->value.home.object = instance;
278
6.68k
  fxNewHostFunction(the, mxCallback(fxTypedArraySetter), 1, XS_NO_ID, XS_NO_ID);
279
6.68k
  property = mxFunctionInstanceHome(the->stack->value.reference);
280
6.68k
  property->value.home.object = instance;
281
6.68k
  mxPushUndefined();
282
6.68k
  the->stack->flag = XS_DONT_DELETE_FLAG;
283
6.68k
  the->stack->kind = XS_ACCESSOR_KIND;
284
6.68k
  the->stack->value.accessor.getter = (the->stack + 2)->value.reference;
285
6.68k
  the->stack->value.accessor.setter = (the->stack + 1)->value.reference;
286
6.68k
  mxPull(mxTypedArrayAccessor);
287
6.68k
  mxPop();
288
6.68k
  mxPop();
289
  
290
6.68k
  slot = fxLastProperty(the, instance);
291
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_TypedArray_prototype_at), 1, mxID(_at), XS_DONT_ENUM_FLAG);
292
6.68k
  slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_TypedArray_prototype_buffer_get), C_NULL, mxID(_buffer), XS_DONT_ENUM_FLAG);
293
6.68k
  slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_TypedArray_prototype_byteLength_get), C_NULL, mxID(_byteLength), XS_DONT_ENUM_FLAG);
294
6.68k
  slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_TypedArray_prototype_byteOffset_get), C_NULL, mxID(_byteOffset), XS_DONT_ENUM_FLAG);
295
6.68k
  slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_TypedArray_prototype_length_get), C_NULL, mxID(_length), XS_DONT_ENUM_FLAG);
296
6.68k
  slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_TypedArray_prototype_toStringTag_get), C_NULL, mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG);
297
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_TypedArray_prototype_copyWithin), 2, mxID(_copyWithin), XS_DONT_ENUM_FLAG);
298
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_TypedArray_prototype_entries), 0, mxID(_entries), XS_DONT_ENUM_FLAG);
299
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_TypedArray_prototype_every), 1, mxID(_every), XS_DONT_ENUM_FLAG);
300
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_TypedArray_prototype_fill), 1, mxID(_fill), XS_DONT_ENUM_FLAG);
301
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_TypedArray_prototype_filter), 1, mxID(_filter), XS_DONT_ENUM_FLAG);
302
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_TypedArray_prototype_find), 1, mxID(_find), XS_DONT_ENUM_FLAG);
303
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_TypedArray_prototype_findIndex), 1, mxID(_findIndex), XS_DONT_ENUM_FLAG);
304
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_TypedArray_prototype_findLast), 1, mxID(_findLast), XS_DONT_ENUM_FLAG);
305
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_TypedArray_prototype_findLastIndex), 1, mxID(_findLastIndex), XS_DONT_ENUM_FLAG);
306
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_TypedArray_prototype_forEach), 1, mxID(_forEach), XS_DONT_ENUM_FLAG);
307
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_TypedArray_prototype_includes), 1, mxID(_includes), XS_DONT_ENUM_FLAG);
308
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_TypedArray_prototype_indexOf), 1, mxID(_indexOf), XS_DONT_ENUM_FLAG);
309
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_TypedArray_prototype_join), 1, mxID(_join), XS_DONT_ENUM_FLAG);
310
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_TypedArray_prototype_keys), 0, mxID(_keys), XS_DONT_ENUM_FLAG);
311
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_TypedArray_prototype_lastIndexOf), 1, mxID(_lastIndexOf), XS_DONT_ENUM_FLAG);
312
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_TypedArray_prototype_map), 1, mxID(_map), XS_DONT_ENUM_FLAG);
313
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_TypedArray_prototype_reduce), 1, mxID(_reduce), XS_DONT_ENUM_FLAG);
314
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_TypedArray_prototype_reduceRight), 1, mxID(_reduceRight), XS_DONT_ENUM_FLAG);
315
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_TypedArray_prototype_reverse), 0, mxID(_reverse), XS_DONT_ENUM_FLAG);
316
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_TypedArray_prototype_set), 1, mxID(_set), XS_DONT_ENUM_FLAG);
317
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_TypedArray_prototype_slice), 2, mxID(_slice), XS_DONT_ENUM_FLAG);
318
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_TypedArray_prototype_some), 1, mxID(_some), XS_DONT_ENUM_FLAG);
319
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_TypedArray_prototype_sort), 1, mxID(_sort), XS_DONT_ENUM_FLAG);
320
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_TypedArray_prototype_subarray), 2, mxID(_subarray), XS_DONT_ENUM_FLAG);
321
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_TypedArray_prototype_toLocaleString), 0, mxID(_toLocaleString), XS_DONT_ENUM_FLAG);
322
6.68k
  property = mxBehaviorGetProperty(the, mxArrayPrototype.value.reference, mxID(_toString), 0, XS_OWN);
323
6.68k
  slot = fxNextSlotProperty(the, slot, property, mxID(_toString), XS_DONT_ENUM_FLAG);
324
6.68k
  property = slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_TypedArray_prototype_values), 0, mxID(_values), XS_DONT_ENUM_FLAG);
325
6.68k
  slot = fxNextSlotProperty(the, slot, property, mxID(_Symbol_iterator), XS_DONT_ENUM_FLAG);
326
6.68k
#if mxECMAScript2023
327
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_TypedArray_prototype_toReversed), 0, mxID(_toReversed), XS_DONT_ENUM_FLAG);
328
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_TypedArray_prototype_toSorted), 1, mxID(_toSorted), XS_DONT_ENUM_FLAG);
329
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_TypedArray_prototype_with), 2, mxID(_with), XS_DONT_ENUM_FLAG);
330
6.68k
#endif
331
6.68k
  mxTypedArrayPrototype = *the->stack; 
332
6.68k
  constructor = fxBuildHostConstructor(the, mxCallback(fx_TypedArray), 0, mxID(_TypedArray));
333
6.68k
  mxTypedArrayConstructor = *the->stack;
334
6.68k
  slot = fxLastProperty(the, constructor);
335
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_TypedArray_from), 1, mxID(_from), XS_DONT_ENUM_FLAG);
336
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_TypedArray_of), 0, mxID(_of), XS_DONT_ENUM_FLAG);
337
6.68k
  slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_species_get), C_NULL, mxID(_Symbol_species), XS_DONT_ENUM_FLAG);
338
86.8k
  for (index = 0, dispatch = &gxTypeDispatches[0], atomics = &gxTypeAtomics[0]; index < mxTypeArrayCount; index++, dispatch++, atomics++) {
339
80.1k
    mxPush(mxTypedArrayPrototype);
340
80.1k
    slot = fxLastProperty(the, fxNewObjectInstance(the));
341
80.1k
    slot = fxNextIntegerProperty(the, slot, dispatch->size, mxID(_BYTES_PER_ELEMENT), XS_GET_ONLY);
342
80.1k
    slot = fxBuildHostConstructor(the, mxCallback(fx_TypedArray), 3, mxID(dispatch->constructorID));
343
80.1k
    the->stackIntrinsics[-1 - (txInteger)dispatch->constructorID] = *the->stack; //@@
344
80.1k
    slot->value.instance.prototype = constructor;
345
80.1k
    property = mxFunctionInstanceHome(slot);
346
80.1k
    slot = property->next;
347
80.1k
    property = fxNextTypeDispatchProperty(the, property, (txTypeDispatch*)dispatch, (txTypeAtomics*)atomics, XS_NO_ID, XS_INTERNAL_FLAG);
348
80.1k
    property->next = slot;
349
80.1k
    slot = fxLastProperty(the, slot);
350
80.1k
    slot = fxNextIntegerProperty(the, slot, dispatch->size, mxID(_BYTES_PER_ELEMENT), XS_GET_ONLY);
351
80.1k
    mxPop();
352
80.1k
  }
353
6.68k
  mxPop();
354
  
355
6.68k
#if mxUint8ArrayBase64
356
6.68k
  mxPush(mxUint8ArrayConstructor);
357
6.68k
    instance = fxToInstance(the, the->stack);
358
6.68k
  slot = fxLastProperty(the, instance);
359
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Uint8Array_fromBase64), 1, mxID(_fromBase64), XS_DONT_ENUM_FLAG);
360
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Uint8Array_fromHex), 1, mxID(_fromHex), XS_DONT_ENUM_FLAG);
361
6.68k
    mxGetID(mxID(_prototype));
362
6.68k
    instance = fxToInstance(the, the->stack);
363
6.68k
  slot = fxLastProperty(the, instance);
364
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Uint8Array_prototype_setFromBase64), 1, mxID(_setFromBase64), XS_DONT_ENUM_FLAG);
365
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Uint8Array_prototype_setFromHex), 1, mxID(_setFromHex), XS_DONT_ENUM_FLAG);
366
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Uint8Array_prototype_toBase64), 0, mxID(_toBase64), XS_DONT_ENUM_FLAG);
367
6.68k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Uint8Array_prototype_toHex), 0, mxID(_toHex), XS_DONT_ENUM_FLAG);
368
6.68k
  mxPop();
369
6.68k
#endif
370
6.68k
}
371
372
txInteger fxArgToByteLength(txMachine* the, txInteger argi, txInteger length)
373
0
{
374
0
  txSlot *arg = mxArgv(argi);
375
0
  if ((mxArgc > argi) && (arg->kind != XS_UNDEFINED_KIND)) {
376
0
    txNumber value;
377
0
    if (XS_INTEGER_KIND == arg->kind) {
378
0
      txInteger value = arg->value.integer;
379
0
      if (value < 0)
380
0
        mxRangeError("byteLength < 0");
381
0
      return value;
382
0
    }
383
0
    value = c_trunc(fxToNumber(the, arg));
384
0
    if (c_isnan(value))
385
0
      return 0;
386
0
    if (value < 0) 
387
0
      mxRangeError("byteLength < 0");
388
0
    if (0x7FFFFFFF < value)
389
0
      mxRangeError("byteLength too big");
390
0
    return (txInteger)value;
391
0
  }
392
0
  return length;
393
0
}
394
395
txS8 fxArgToSafeByteLength(txMachine* the, txInteger argi, txInteger length)
396
0
{
397
0
  txSlot *arg = mxArgv(argi);
398
0
  if ((mxArgc > argi) && (arg->kind != XS_UNDEFINED_KIND)) {
399
0
    txNumber value;
400
0
    if (XS_INTEGER_KIND == arg->kind) {
401
0
      txS8 value = arg->value.integer;
402
0
      if (value < 0)
403
0
        mxRangeError("byteLength < 0");
404
0
      return value;
405
0
    }
406
0
    value = c_trunc(fxToNumber(the, arg));
407
0
    if (c_isnan(value))
408
0
      return 0;
409
0
    if (value < 0) 
410
0
      mxRangeError("byteLength < 0");
411
0
    if (C_MAX_SAFE_INTEGER < value)
412
0
      mxRangeError("byteLength too big");
413
0
    return (txS8)value;
414
0
  }
415
0
  return length;
416
0
}
417
418
txSlot* fxArgToInstance(txMachine* the, txInteger i)
419
0
{
420
0
  if (mxArgc > i)
421
0
    return fxToInstance(the, mxArgv(i));
422
0
  mxTypeError("cannot coerce undefined to object");
423
0
  return C_NULL;
424
0
}
425
426
txSlot* fxCheckArrayBufferDetached(txMachine* the, txSlot* slot)
427
0
{
428
0
  slot = slot->value.reference->next;
429
0
  if (slot->value.arrayBuffer.address == C_NULL)
430
0
    mxTypeError("detached buffer");
431
0
  return slot;
432
0
}
433
434
txSlot* fxCheckArrayBufferInstance(txMachine* the, txSlot* slot)
435
0
{
436
0
  if (slot->kind == XS_REFERENCE_KIND) {
437
0
    txSlot* instance = slot->value.reference;
438
0
    txSlot* arrayBuffer = instance->next;
439
0
    if (arrayBuffer && (arrayBuffer->flag & XS_INTERNAL_FLAG) && (arrayBuffer->kind == XS_ARRAY_BUFFER_KIND))
440
0
      return instance;
441
0
  }
442
0
  if (slot == mxThis)
443
0
    mxTypeError("this: not an ArrayBuffer instance");
444
0
  mxTypeError("not an ArrayBuffer instance");
445
0
  return C_NULL;
446
0
}
447
448
txSlot* fxCheckArrayBufferMutable(txMachine* the, txSlot* slot)
449
0
{
450
0
  slot = slot->value.reference->next;
451
0
  if (slot->flag & XS_DONT_SET_FLAG)
452
0
    mxTypeError("ArrayBuffer instance is read-only");
453
0
  return slot;
454
0
}
455
456
void fxConstructArrayBufferResult(txMachine* the, txSlot* constructor, txInteger length)
457
0
{
458
0
  txSlot* instance;
459
0
  if (constructor)
460
0
    mxPushSlot(constructor);
461
0
  else {
462
0
    mxPushSlot(mxThis);
463
0
    mxGetID(mxID(_constructor));
464
0
    fxToSpeciesConstructor(the, &mxArrayBufferConstructor);
465
0
  }
466
0
  mxNew();
467
0
  mxPushInteger(length);
468
0
  mxRunCount(1);
469
0
  if (the->stack->kind != XS_REFERENCE_KIND)
470
0
    mxTypeError("not an object");
471
0
  instance = the->stack->value.reference;
472
0
  if (!(instance->next) || (instance->next->kind != XS_ARRAY_BUFFER_KIND))
473
0
    mxTypeError("not an ArrayBuffer instance");
474
0
  if (instance->next->value.arrayBuffer.address == C_NULL)
475
0
    mxTypeError("detached buffer");
476
0
  if (instance->next->flag & XS_DONT_SET_FLAG)
477
0
    mxTypeError("read-only buffer");
478
0
  if (!constructor && (mxThis->value.reference == instance))
479
0
    mxTypeError("same ArrayBuffer instance");
480
0
  if (instance->next->next->value.bufferInfo.length < length)
481
0
    mxTypeError("smaller ArrayBuffer instance");
482
0
  mxPullSlot(mxResult);
483
0
}
484
485
txSlot* fxNewArrayBufferInstance(txMachine* the)
486
6.68k
{
487
6.68k
  txSlot* instance;
488
6.68k
  txSlot* property;
489
6.68k
  instance = fxNewObjectInstance(the);
490
6.68k
  property = instance->next = fxNewSlot(the);
491
6.68k
  property->flag = XS_INTERNAL_FLAG;
492
6.68k
  property->kind = XS_ARRAY_BUFFER_KIND;
493
6.68k
  property->value.arrayBuffer.address = C_NULL;
494
6.68k
  property->value.arrayBuffer.detachKey = C_NULL;
495
6.68k
  property = property->next = fxNewSlot(the);
496
6.68k
  property->flag = XS_INTERNAL_FLAG;
497
6.68k
  property->kind = XS_BUFFER_INFO_KIND;
498
6.68k
  property->value.bufferInfo.length = 0;
499
6.68k
  property->value.bufferInfo.maxLength = -1;
500
6.68k
  return instance;
501
6.68k
}
502
503
void fx_ArrayBuffer(txMachine* the)
504
0
{
505
0
  txSlot* instance;
506
0
  txS8 byteLength;
507
0
  txS8 maxByteLength = -1;
508
0
  txSlot* property;
509
0
  if (!mxHasTarget)
510
0
    mxTypeError("call: ArrayBuffer");
511
0
  byteLength = fxArgToSafeByteLength(the, 0, 0);
512
0
  if ((mxArgc > 1) && mxIsReference(mxArgv(1))) {
513
0
    mxPushSlot(mxArgv(1));
514
0
    mxGetID(mxID(_maxByteLength));
515
0
    mxPullSlot(mxArgv(1));
516
0
    maxByteLength = fxArgToSafeByteLength(the, 1, -1);
517
0
  }
518
0
  if (maxByteLength >= 0) {
519
0
    if (byteLength > maxByteLength)
520
0
      mxRangeError("byteLength > maxByteLength");
521
0
  }
522
0
  mxPushSlot(mxTarget);
523
0
  fxGetPrototypeFromConstructor(the, &mxArrayBufferPrototype);
524
0
  instance = fxNewArrayBufferInstance(the);
525
0
  mxPullSlot(mxResult);
526
0
  if (byteLength > 0x7FFFFFFF)
527
0
    mxRangeError("byteLength too big");
528
0
  if (maxByteLength > 0x7FFFFFFF)
529
0
    mxRangeError("maxByteLength too big");
530
0
  property = instance->next;
531
0
  property->value.arrayBuffer.address = fxNewChunk(the, (txSize)byteLength);
532
0
  c_memset(property->value.arrayBuffer.address, 0, (txSize)byteLength);
533
0
  property = property->next;
534
0
  property->value.bufferInfo.length = (txSize)byteLength;
535
0
  property->value.bufferInfo.maxLength = (txSize)maxByteLength;
536
0
}
537
538
void fx_ArrayBuffer_fromBigInt(txMachine* the)
539
0
{
540
0
  txU4 minBytes = 0;
541
0
  txBoolean sign = 0;
542
0
  int endian = EndianBig;
543
0
  if (mxArgc < 1)
544
0
    mxTypeError("no argument");
545
0
  if (mxArgc > 1)
546
0
    minBytes = (txU4)fxArgToByteLength(the, 1, 0);
547
0
  if ((mxArgc > 2) && fxToBoolean(the, mxArgv(2)))
548
0
    sign = 1;
549
0
  if ((mxArgc > 3) && fxToBoolean(the, mxArgv(3)))
550
0
    endian = EndianLittle;
551
0
  if (gxTypeBigInt.toArrayBuffer) {
552
0
    gxTypeBigInt.toArrayBuffer(the, mxArgv(0), minBytes, sign, endian);
553
0
  }
554
0
  else {
555
0
    mxUnknownError("not built-in");
556
0
  }
557
0
}
558
559
#ifndef mxCESU8
560
void fx_ArrayBuffer_fromString(txMachine* the)
561
{
562
  txSize length = 0;
563
  if (mxArgc < 1)
564
    mxTypeError("no argument");
565
  
566
  txString c = fxToString(the, mxArgv(0));
567
  txInteger nulls = 0;
568
  while (1) {
569
    uint8_t b = (uint8_t)c_read8(c++);
570
    if (!b) break;
571
572
    length += 1;
573
    if ((0xc0 == b) && (0x80 == (uint8_t)c_read8(c)))
574
      nulls += 1;
575
  } 
576
  
577
  fxConstructArrayBufferResult(the, mxThis, length - nulls);
578
  if (!nulls)
579
    c_memcpy(mxResult->value.reference->next->value.arrayBuffer.address, mxArgv(0)->value.string, length);
580
  else {
581
    txString c = mxArgv(0)->value.string, end = c + length;
582
    txByte *out = mxResult->value.reference->next->value.arrayBuffer.address;
583
    while (c < end) {
584
      uint8_t b = (uint8_t)c_read8(c++);
585
      if ((0xc0 == (uint8_t)b) && (0x80 == (uint8_t)c_read8(c))) {
586
        b = 0;
587
        c += 1;
588
      }
589
      *out++ = b;
590
    }
591
  }
592
}
593
#endif
594
595
void fx_ArrayBuffer_isView(txMachine* the)
596
0
{
597
0
  txSlot* slot;
598
0
  mxResult->kind = XS_BOOLEAN_KIND;
599
0
  mxResult->value.boolean = 0;
600
0
  if (mxArgc > 0) {
601
0
    slot = mxArgv(0);
602
0
    if (slot->kind == XS_REFERENCE_KIND) {
603
0
      slot = slot->value.reference;
604
0
      if (slot->next) {
605
0
        slot = slot->next;
606
0
        if ((slot->kind == XS_DATA_VIEW_KIND) || (slot->kind == XS_TYPED_ARRAY_KIND)) {
607
0
          mxResult->value.boolean = 1;
608
0
        }
609
0
      }
610
0
    }
611
0
  }
612
0
}
613
614
void fx_ArrayBuffer_prototype_get_byteLength(txMachine* the)
615
0
{
616
0
  txSlot* instance = fxCheckArrayBufferInstance(the, mxThis);
617
0
  txSlot* arrayBuffer = instance->next;
618
0
  txSlot* bufferInfo = arrayBuffer->next;
619
0
  mxResult->kind = XS_INTEGER_KIND;
620
0
  if (arrayBuffer->value.arrayBuffer.address == C_NULL)
621
0
    mxResult->value.integer = 0;
622
0
  else
623
0
    mxResult->value.integer = bufferInfo->value.bufferInfo.length;
624
0
}
625
626
void fx_ArrayBuffer_prototype_get_detached(txMachine* the)
627
0
{
628
0
  txSlot* instance = fxCheckArrayBufferInstance(the, mxThis);
629
0
  txSlot* arrayBuffer = instance->next;
630
0
  mxResult->kind = XS_BOOLEAN_KIND;
631
0
  mxResult->value.boolean = (arrayBuffer->value.arrayBuffer.address == C_NULL) ? 1 : 0;
632
0
}
633
634
void fx_ArrayBuffer_prototype_get_immutable(txMachine* the)
635
0
{
636
0
  txSlot* instance = fxCheckArrayBufferInstance(the, mxThis);
637
0
  txSlot* arrayBuffer = instance->next;
638
0
  mxResult->kind = XS_BOOLEAN_KIND;
639
0
  mxResult->value.boolean = (arrayBuffer->flag & XS_DONT_SET_FLAG) ? 1 : 0;
640
0
}
641
642
void fx_ArrayBuffer_prototype_get_maxByteLength(txMachine* the)
643
0
{
644
0
  txSlot* instance = fxCheckArrayBufferInstance(the, mxThis);
645
0
  txSlot* arrayBuffer = instance->next;
646
0
  txSlot* bufferInfo = arrayBuffer->next;
647
0
  mxResult->kind = XS_INTEGER_KIND;
648
0
  if (arrayBuffer->value.arrayBuffer.address == C_NULL)
649
0
    mxResult->value.integer = 0;
650
0
  else if (bufferInfo->value.bufferInfo.maxLength >= 0)
651
0
    mxResult->value.integer = bufferInfo->value.bufferInfo.maxLength;
652
0
  else
653
0
    mxResult->value.integer = bufferInfo->value.bufferInfo.length;
654
0
}
655
656
void fx_ArrayBuffer_prototype_get_resizable(txMachine* the)
657
0
{
658
0
  txSlot* instance = fxCheckArrayBufferInstance(the, mxThis);
659
0
  txSlot* arrayBuffer = instance->next;
660
0
  txSlot* bufferInfo = arrayBuffer->next;
661
0
  mxResult->kind = XS_BOOLEAN_KIND;
662
0
  mxResult->value.boolean = (bufferInfo->value.bufferInfo.maxLength >= 0) ? 1 : 0;
663
0
}
664
665
void fx_ArrayBuffer_prototype_concat(txMachine* the)
666
0
{
667
0
  txSlot* instance = fxCheckArrayBufferInstance(the, mxThis);
668
0
  txSlot* arrayBuffer = instance->next;
669
0
  txSlot* bufferInfo = arrayBuffer->next;
670
0
  txInteger length = bufferInfo->value.bufferInfo.length;
671
0
  txInteger c = mxArgc, i = 0;
672
0
  txByte* address;
673
0
  txSlot* slot;
674
0
  while (i < c) {
675
0
    arrayBuffer = C_NULL;
676
0
    bufferInfo = C_NULL;
677
0
    slot = mxArgv(i);
678
0
    if (slot->kind == XS_REFERENCE_KIND) {
679
0
      slot = slot->value.reference->next;
680
0
      if (slot) {
681
0
        if (slot->kind == XS_ARRAY_BUFFER_KIND) {
682
0
          bufferInfo = slot->next;
683
0
        }
684
0
        else if ((slot->kind == XS_HOST_KIND) && !(slot->flag & XS_HOST_CHUNK_FLAG)) {
685
0
          slot = slot->next;
686
0
          if (slot && (slot->kind == XS_BUFFER_INFO_KIND)) {
687
0
            bufferInfo = slot;
688
0
          }
689
0
        }
690
0
      }
691
0
    }
692
0
    if (bufferInfo) 
693
0
      length = fxAddChunkSizes(the, length, bufferInfo->value.bufferInfo.length);
694
0
    else
695
0
      mxTypeError("arguments[%ld]: not an ArrayBuffer instance", i);
696
0
    i++;
697
0
  }
698
0
  fxConstructArrayBufferResult(the, C_NULL, length);
699
0
  address = mxResult->value.reference->next->value.arrayBuffer.address;
700
0
  slot = instance->next;
701
0
  i = -1;
702
0
  while (length > 0) {
703
0
    txByte* from;
704
0
    txInteger count;
705
0
    if (slot->kind == XS_ARRAY_BUFFER_KIND)
706
0
      from = slot->value.arrayBuffer.address;
707
0
    else
708
0
      from = slot->value.host.data;
709
0
    count = (from) ? slot->next->value.bufferInfo.length : 0;
710
0
    if (count > length)
711
0
      count = length;
712
0
    if (count > 0)
713
0
      c_memmove(address, from, count);
714
0
    address += count;
715
0
    length -= count;
716
0
    if (++i >= c)
717
0
      break;
718
0
    slot = mxArgv(i)->value.reference->next;
719
0
  }
720
0
}
721
722
void fx_ArrayBuffer_prototype_resize(txMachine* the)
723
0
{
724
0
  txSlot* instance = fxCheckArrayBufferInstance(the, mxThis);
725
0
  txSlot* arrayBuffer = instance->next;
726
0
  txSlot* bufferInfo = arrayBuffer->next;
727
0
  fxCheckArrayBufferMutable(the, mxThis);
728
0
  txInteger maxByteLength = bufferInfo->value.bufferInfo.maxLength;
729
0
  if (maxByteLength < 0)
730
0
    mxTypeError("not resizable");
731
0
  txInteger newByteLength = fxArgToByteLength(the, 0, 0);
732
0
  fxCheckArrayBufferDetached(the, mxThis);
733
0
  txInteger oldByteLength;
734
0
  txByte* chunk;
735
0
  oldByteLength = bufferInfo->value.bufferInfo.length;
736
0
  if (newByteLength > maxByteLength)
737
0
    mxRangeError("newLength > maxByteLength");
738
0
  arrayBuffer = fxCheckArrayBufferDetached(the, mxThis);
739
0
  chunk = (txByte*)fxRenewChunk(the, arrayBuffer->value.arrayBuffer.address, newByteLength);
740
0
  if (!chunk) {
741
0
    chunk = (txByte*)fxNewChunk(the, newByteLength);
742
0
    c_memcpy(chunk, arrayBuffer->value.arrayBuffer.address, (newByteLength < oldByteLength) ? newByteLength : oldByteLength);
743
0
  }
744
0
  if (newByteLength > oldByteLength)
745
0
    c_memset(chunk + oldByteLength, 0, newByteLength - oldByteLength);
746
0
  arrayBuffer->value.arrayBuffer.address = chunk;
747
0
  bufferInfo->value.bufferInfo.length = newByteLength;
748
0
}
749
750
void fx_ArrayBuffer_prototype_slice(txMachine* the)
751
0
{
752
0
  /* txSlot* instance = */ fxCheckArrayBufferInstance(the, mxThis);
753
0
  txSlot* arrayBuffer = fxCheckArrayBufferDetached(the, mxThis);
754
0
  txSlot* bufferInfo = arrayBuffer->next;
755
0
  txInteger length = bufferInfo->value.bufferInfo.length;
756
0
  txInteger start = fxArgToIndexInteger(the, 0, 0, length);
757
0
  txInteger stop = fxArgToIndexInteger(the, 1, length, length);
758
0
  txSlot* resultBuffer;
759
0
  if (stop < start) 
760
0
    stop = start;
761
0
  fxConstructArrayBufferResult(the, C_NULL, stop - start);
762
0
  resultBuffer = mxResult->value.reference->next;
763
0
  arrayBuffer = fxCheckArrayBufferDetached(the, mxThis);
764
0
  bufferInfo = arrayBuffer->next;
765
0
  length = bufferInfo->value.bufferInfo.length;
766
0
  if (start < length) {
767
0
    if (stop > length)
768
0
      stop = length;
769
0
    c_memcpy(resultBuffer->value.arrayBuffer.address, arrayBuffer->value.arrayBuffer.address + start, stop - start);
770
0
  }
771
0
}
772
773
void fx_ArrayBuffer_prototype_sliceToImmutable(txMachine* the)
774
0
{
775
0
  /* txSlot* instance = */ fxCheckArrayBufferInstance(the, mxThis);
776
0
  txSlot* arrayBuffer = fxCheckArrayBufferDetached(the, mxThis);
777
0
  txSlot* bufferInfo = arrayBuffer->next;
778
0
  txInteger length = bufferInfo->value.bufferInfo.length;
779
0
  txInteger start = fxArgToIndexInteger(the, 0, 0, length);
780
0
  txInteger stop = fxArgToIndexInteger(the, 1, length, length);
781
0
  txSlot* resultBuffer;
782
0
  if (stop < start) 
783
0
    stop = start;
784
0
  arrayBuffer = fxCheckArrayBufferDetached(the, mxThis);  
785
0
  bufferInfo = arrayBuffer->next;
786
0
  length = bufferInfo->value.bufferInfo.length;
787
0
  if (length < stop)
788
0
    mxRangeError("resized this");
789
0
  fxConstructArrayBufferResult(the,  &mxArrayBufferConstructor, stop - start);
790
0
  resultBuffer = fxCheckArrayBufferDetached(the, mxResult);
791
0
  c_memcpy(resultBuffer->value.arrayBuffer.address, arrayBuffer->value.arrayBuffer.address + start, stop - start);
792
0
  resultBuffer->flag |= XS_DONT_SET_FLAG;
793
0
}
794
795
static void fx_ArrayBuffer_prototype_transferAux(txMachine* the, txFlag flag)
796
0
{
797
0
  txSlot* instance = fxCheckArrayBufferInstance(the, mxThis);
798
0
  txSlot* arrayBuffer = instance->next;
799
0
  txSlot* bufferInfo = arrayBuffer->next;
800
0
  txInteger oldByteLength = bufferInfo->value.bufferInfo.length;
801
0
  txInteger maxByteLength = bufferInfo->value.bufferInfo.maxLength;
802
0
  txInteger newByteLength = fxArgToByteLength(the, 0, oldByteLength);
803
0
  txSlot* resultBuffer;
804
0
  oldByteLength = bufferInfo->value.bufferInfo.length;  // ToIndex may have resized this
805
0
  arrayBuffer = fxCheckArrayBufferDetached(the, mxThis);
806
0
  fxCheckArrayBufferMutable(the, mxThis);
807
0
  if (!(flag & 1) && (maxByteLength >= 0) && (newByteLength > maxByteLength))
808
0
    mxRangeError("newLength > maxByteLength");
809
0
  fxConstructArrayBufferResult(the, &mxArrayBufferConstructor, newByteLength);
810
0
  resultBuffer = mxResult->value.reference->next;
811
0
  c_memcpy(resultBuffer->value.arrayBuffer.address, arrayBuffer->value.arrayBuffer.address, (newByteLength < oldByteLength) ? newByteLength : oldByteLength);
812
0
  if (newByteLength > oldByteLength)
813
0
    c_memset(resultBuffer->value.arrayBuffer.address + oldByteLength, 0, newByteLength - oldByteLength);
814
0
  arrayBuffer->value.arrayBuffer.address = C_NULL;
815
0
  bufferInfo->value.bufferInfo.length = 0;
816
0
  if (flag & 2)
817
0
    resultBuffer->flag |= XS_DONT_SET_FLAG;
818
0
  resultBuffer->next->value.bufferInfo.maxLength = (flag & 1) ? -1 : maxByteLength;
819
0
}
820
821
void fx_ArrayBuffer_prototype_transfer(txMachine* the)
822
0
{
823
0
  fx_ArrayBuffer_prototype_transferAux(the, 0);
824
0
}
825
826
void fx_ArrayBuffer_prototype_transferToFixedLength(txMachine* the)
827
0
{
828
0
  fx_ArrayBuffer_prototype_transferAux(the, 1);
829
0
}
830
831
void fx_ArrayBuffer_prototype_transferToImmutable(txMachine* the)
832
0
{
833
0
  fx_ArrayBuffer_prototype_transferAux(the, 3);
834
0
}
835
836
txSlot* fxCheckDataViewInstance(txMachine* the, txSlot* slot, txBoolean mutable)
837
0
{
838
0
  if (slot->kind == XS_REFERENCE_KIND) {
839
0
    txSlot* instance = slot->value.reference;
840
0
    if (((slot = instance->next)) && (slot->flag & XS_INTERNAL_FLAG) && (slot->kind == XS_DATA_VIEW_KIND)) {
841
0
      if (mutable) {
842
0
        txSlot* arrayBuffer = slot->next->value.reference->next;
843
0
        if (arrayBuffer->flag & XS_DONT_SET_FLAG)
844
0
          mxTypeError("read-only buffer");
845
0
      }
846
0
      return instance;
847
0
    }
848
0
  }
849
0
  mxTypeError("this: not a DataView instance");
850
0
  return C_NULL;
851
0
}
852
853
txInteger fxCheckDataViewSize(txMachine* the, txSlot* view, txSlot* buffer, txBoolean mutable)
854
0
{
855
0
  txInteger size = view->value.dataView.size;
856
0
  txSlot* arrayBuffer = buffer->value.reference->next;
857
0
  txSlot* bufferInfo = arrayBuffer->next;
858
0
  if (arrayBuffer->value.arrayBuffer.address == C_NULL)
859
0
    mxTypeError("detached buffer");
860
0
  if (mutable && (arrayBuffer->flag & XS_DONT_SET_FLAG))
861
0
    mxTypeError("read-only buffer");
862
0
  if (bufferInfo->value.bufferInfo.maxLength >= 0) {
863
0
    txInteger offset = view->value.dataView.offset;
864
0
    txInteger byteLength = bufferInfo->value.bufferInfo.length;
865
0
    if (offset > byteLength)
866
0
      mxTypeError("out of bounds view");
867
0
    else if (size < 0)
868
0
      size = byteLength - offset;
869
0
    else if (offset + size > byteLength)
870
0
      mxTypeError("out of bounds view");
871
0
  }
872
0
  return size;
873
0
}
874
875
txSlot* fxGetBufferInfo(txMachine* the, txSlot* buffer)
876
0
{
877
0
  txSlot* arrayBuffer = buffer->value.reference->next;
878
0
  txSlot* bufferInfo = arrayBuffer->next;
879
0
  if (arrayBuffer->kind == XS_ARRAY_BUFFER_KIND) {
880
0
    if (arrayBuffer->value.arrayBuffer.address == C_NULL)
881
0
      mxTypeError("detached buffer");
882
0
    return bufferInfo;
883
0
  }
884
0
  if (arrayBuffer->kind == XS_HOST_KIND) {
885
0
    if (bufferInfo && (bufferInfo->kind == XS_BUFFER_INFO_KIND))
886
0
      return bufferInfo;
887
0
  }
888
0
  mxTypeError("invalid buffer");
889
0
  return C_NULL;
890
0
}
891
892
txInteger fxGetDataViewSize(txMachine* the, txSlot* view, txSlot* buffer)
893
0
{
894
0
  txInteger size = view->value.dataView.size;
895
0
  txSlot* arrayBuffer = buffer->value.reference->next;
896
0
  txSlot* bufferInfo = arrayBuffer->next;
897
0
  if (arrayBuffer->value.arrayBuffer.address == C_NULL)
898
0
    return 0;
899
0
  if (bufferInfo->value.bufferInfo.maxLength >= 0) {
900
0
    txInteger offset = view->value.dataView.offset;
901
0
    txInteger byteLength = bufferInfo->value.bufferInfo.length;
902
0
    if (offset > byteLength)
903
0
      size = 0;
904
0
    else if (size < 0)
905
0
      size = byteLength - offset;
906
0
    else if (offset + size > byteLength)
907
0
      size = 0;
908
0
  }
909
0
  return size;
910
0
}
911
912
txBoolean fxIsDataViewOutOfBound(txMachine* the, txSlot* view, txSlot* buffer)
913
0
{
914
0
  txInteger size = view->value.dataView.size;
915
0
  txSlot* arrayBuffer = buffer->value.reference->next;
916
0
  txSlot* bufferInfo = arrayBuffer->next;
917
0
  if (arrayBuffer->value.arrayBuffer.address == C_NULL)
918
0
    return 1;
919
0
  if (bufferInfo->value.bufferInfo.maxLength >= 0) {
920
0
    txInteger offset = view->value.dataView.offset;
921
0
    txInteger byteLength = bufferInfo->value.bufferInfo.length;
922
0
    if (offset > byteLength)
923
0
      return 1;
924
0
    if ((size > 0) && (offset + size > byteLength))
925
0
      return 1;
926
0
  }
927
0
  return 0;
928
0
}
929
930
txSlot* fxNewDataViewInstance(txMachine* the)
931
0
{
932
0
  txSlot* instance;
933
0
  txSlot* property;
934
0
  instance = fxNewObjectInstance(the);
935
0
  property = instance->next = fxNewSlot(the);
936
0
  property->flag = XS_INTERNAL_FLAG;
937
0
  property->kind = XS_DATA_VIEW_KIND;
938
0
  property->value.dataView.offset = 0;
939
0
  property->value.dataView.size = 0;
940
0
  property = fxNextNullProperty(the, property, XS_NO_ID, XS_INTERNAL_FLAG);
941
0
  return instance;
942
0
}
943
944
void fx_DataView(txMachine* the)
945
0
{
946
0
  txSlot* slot;
947
0
  txBoolean flag = 0;
948
0
  txInteger offset, size;
949
0
  txSlot* info;
950
0
  txSlot* instance;
951
0
  txSlot* view;
952
0
  txSlot* buffer;
953
0
  if (!mxHasTarget)
954
0
    mxTypeError("call: DataView");
955
0
  if ((mxArgc > 0) && (mxArgv(0)->kind == XS_REFERENCE_KIND)) {
956
0
    slot = mxArgv(0)->value.reference->next;
957
0
    if (slot && ((slot->kind == XS_ARRAY_BUFFER_KIND) || (slot->kind == XS_HOST_KIND))) {
958
0
      flag = 1;
959
0
    }
960
0
  }
961
0
  if (!flag)
962
0
    mxTypeError("buffer: not an ArrayBuffer instance");
963
    
964
0
  offset = fxArgToByteLength(the, 1, 0);
965
0
  info = fxGetBufferInfo(the, mxArgv(0));
966
0
  if (info->value.bufferInfo.length < offset)
967
0
    mxRangeError("invalid byteOffset %ld", offset);
968
0
  size = fxArgToByteLength(the, 2, -1);
969
0
  if (size >= 0) {
970
0
    txInteger end = offset + size;
971
0
    if ((info->value.bufferInfo.length < end) || (end < offset))
972
0
      mxRangeError("invalid byteLength %ld", size);
973
0
  }
974
0
  else {
975
0
    if (info->value.bufferInfo.maxLength < 0)
976
0
      size = info->value.bufferInfo.length - offset;
977
0
  }
978
0
  mxPushSlot(mxTarget);
979
0
  fxGetPrototypeFromConstructor(the, &mxDataViewPrototype);
980
0
  instance = fxNewDataViewInstance(the);
981
0
  mxPullSlot(mxResult);
982
0
  view = instance->next;
983
0
  buffer = view->next;
984
0
  buffer->kind = XS_REFERENCE_KIND;
985
0
  buffer->value.reference = mxArgv(0)->value.reference;
986
0
  info = fxGetBufferInfo(the, buffer);
987
0
  if (info->value.bufferInfo.maxLength >= 0) {
988
0
    if (info->value.bufferInfo.length < offset)
989
0
      mxRangeError("invalid byteOffset %ld", offset);
990
0
    else if (size >= 0) {
991
0
      txInteger end = offset + size;
992
0
      if ((info->value.bufferInfo.length < end) || (end < offset))
993
0
        mxRangeError("invalid byteLength %ld", size);
994
0
    }
995
0
  }
996
0
  view->value.dataView.offset = offset;
997
0
  view->value.dataView.size = size;
998
0
}
999
1000
void fx_DataView_prototype_buffer_get(txMachine* the)
1001
0
{
1002
0
  txSlot* instance = fxCheckDataViewInstance(the, mxThis, XS_IMMUTABLE);
1003
0
  txSlot* view = instance->next;
1004
0
  txSlot* buffer = view->next;
1005
0
  mxResult->kind = buffer->kind;
1006
0
  mxResult->value = buffer->value;
1007
0
}
1008
1009
void fx_DataView_prototype_byteLength_get(txMachine* the)
1010
0
{
1011
0
  txSlot* instance = fxCheckDataViewInstance(the, mxThis, XS_IMMUTABLE);
1012
0
  txSlot* view = instance->next;
1013
0
  txSlot* buffer = view->next;
1014
0
  txInteger size = fxCheckDataViewSize(the, view, buffer, XS_IMMUTABLE);
1015
0
  mxResult->kind = XS_INTEGER_KIND;
1016
0
  mxResult->value.integer = size;
1017
0
}
1018
1019
void fx_DataView_prototype_byteOffset_get(txMachine* the)
1020
0
{
1021
0
  txSlot* instance = fxCheckDataViewInstance(the, mxThis, XS_IMMUTABLE);
1022
0
  txSlot* view = instance->next;
1023
0
  txSlot* buffer = view->next;
1024
0
  fxCheckDataViewSize(the, view, buffer, XS_IMMUTABLE);
1025
0
  mxResult->kind = XS_INTEGER_KIND;
1026
0
  mxResult->value.integer = view->value.dataView.offset;
1027
0
}
1028
1029
void fx_DataView_prototype_get(txMachine* the, txNumber delta, txTypeCallback getter)
1030
0
{
1031
0
  txSlot* instance = fxCheckDataViewInstance(the, mxThis, XS_IMMUTABLE);
1032
0
  txSlot* view = instance->next;
1033
0
  txSlot* buffer = view->next;
1034
0
  txInteger offset = fxArgToByteLength(the, 0, 0);
1035
0
  txInteger size;
1036
0
  int endian = EndianBig;
1037
0
  if ((mxArgc > 1) && fxToBoolean(the, mxArgv(1)))
1038
0
    endian = EndianLittle;
1039
0
  size = fxCheckDataViewSize(the, view, buffer, XS_IMMUTABLE);
1040
0
  if ((size < delta) || ((size - delta) < offset))
1041
0
    mxRangeError("invalid byteOffset");
1042
0
  offset += view->value.dataView.offset;
1043
0
  (*getter)(the, buffer->value.reference->next, offset, mxResult, endian);
1044
0
}
1045
1046
void fx_DataView_prototype_getBigInt64(txMachine* the)
1047
0
{
1048
0
  fx_DataView_prototype_get(the, 8, fxBigInt64Getter);
1049
0
}
1050
1051
void fx_DataView_prototype_getBigUint64(txMachine* the)
1052
0
{
1053
0
  fx_DataView_prototype_get(the, 8, fxBigUint64Getter);
1054
0
}
1055
1056
#if mxFloat16
1057
void fx_DataView_prototype_getFloat16(txMachine* the)
1058
0
{
1059
0
  fx_DataView_prototype_get(the, 2, fxFloat16Getter);
1060
0
}
1061
#endif
1062
1063
void fx_DataView_prototype_getFloat32(txMachine* the)
1064
0
{
1065
0
  fx_DataView_prototype_get(the, 4, fxFloat32Getter);
1066
0
}
1067
1068
void fx_DataView_prototype_getFloat64(txMachine* the)
1069
0
{
1070
0
  fx_DataView_prototype_get(the, 8, fxFloat64Getter);
1071
0
}
1072
1073
void fx_DataView_prototype_getInt8(txMachine* the)
1074
0
{
1075
0
  fx_DataView_prototype_get(the, 1, fxInt8Getter);
1076
0
}
1077
1078
void fx_DataView_prototype_getInt16(txMachine* the)
1079
0
{
1080
0
  fx_DataView_prototype_get(the, 2, fxInt16Getter);
1081
0
}
1082
1083
void fx_DataView_prototype_getInt32(txMachine* the)
1084
0
{
1085
0
  fx_DataView_prototype_get(the, 4, fxInt32Getter);
1086
0
}
1087
1088
void fx_DataView_prototype_getUint8(txMachine* the)
1089
0
{
1090
0
  fx_DataView_prototype_get(the, 1, fxUint8Getter);
1091
0
}
1092
1093
void fx_DataView_prototype_getUint16(txMachine* the)
1094
0
{
1095
0
  fx_DataView_prototype_get(the, 2, fxUint16Getter);
1096
0
}
1097
1098
void fx_DataView_prototype_getUint32(txMachine* the)
1099
0
{
1100
0
  fx_DataView_prototype_get(the, 4, fxUint32Getter);
1101
0
}
1102
1103
void fx_DataView_prototype_set(txMachine* the, txNumber delta, txTypeCoerce coercer, txTypeCallback setter)
1104
0
{
1105
0
  txSlot* instance = fxCheckDataViewInstance(the, mxThis, XS_MUTABLE);
1106
0
  txSlot* view = instance->next;
1107
0
  txSlot* buffer = view->next;
1108
0
  txInteger offset = fxArgToByteLength(the, 0, 0);
1109
0
  txInteger size;
1110
0
  int endian = EndianBig;
1111
0
  txSlot* value;
1112
0
  if (mxArgc > 1)
1113
0
    mxPushSlot(mxArgv(1));
1114
0
  else
1115
0
    mxPushUndefined();
1116
0
  value = the->stack; 
1117
0
  (*coercer)(the, value);
1118
0
  if ((mxArgc > 2) && fxToBoolean(the, mxArgv(2)))
1119
0
    endian = EndianLittle;
1120
0
  size = fxCheckDataViewSize(the, view, buffer, XS_MUTABLE);
1121
0
  if ((size < delta) || ((size - delta) < offset))
1122
0
    mxRangeError("invalid byteOffset");
1123
0
  offset += view->value.dataView.offset;
1124
0
  (*setter)(the, buffer->value.reference->next, offset, value, endian);
1125
0
  mxPop();
1126
0
}
1127
1128
void fx_DataView_prototype_setBigInt64(txMachine* the)
1129
0
{
1130
0
  fx_DataView_prototype_set(the, 8, fxBigIntCoerce, fxBigInt64Setter);
1131
0
}
1132
1133
void fx_DataView_prototype_setBigUint64(txMachine* the)
1134
0
{
1135
0
  fx_DataView_prototype_set(the, 8, fxBigIntCoerce, fxBigUint64Setter);
1136
0
}
1137
1138
#if mxFloat16
1139
void fx_DataView_prototype_setFloat16(txMachine* the)
1140
0
{
1141
0
  fx_DataView_prototype_set(the, 2, fxNumberCoerce, fxFloat16Setter);
1142
0
}
1143
#endif
1144
1145
void fx_DataView_prototype_setFloat32(txMachine* the)
1146
0
{
1147
0
  fx_DataView_prototype_set(the, 4, fxNumberCoerce, fxFloat32Setter);
1148
0
}
1149
1150
void fx_DataView_prototype_setFloat64(txMachine* the)
1151
0
{
1152
0
  fx_DataView_prototype_set(the, 8, fxNumberCoerce, fxFloat64Setter);
1153
0
}
1154
1155
void fx_DataView_prototype_setInt8(txMachine* the)
1156
0
{
1157
0
  fx_DataView_prototype_set(the, 1, fxIntCoerce, fxInt8Setter);
1158
0
}
1159
1160
void fx_DataView_prototype_setInt16(txMachine* the)
1161
0
{
1162
0
  fx_DataView_prototype_set(the, 2, fxIntCoerce, fxInt16Setter);
1163
0
}
1164
1165
void fx_DataView_prototype_setInt32(txMachine* the)
1166
0
{
1167
0
  fx_DataView_prototype_set(the, 4, fxIntCoerce, fxInt32Setter);
1168
0
}
1169
1170
void fx_DataView_prototype_setUint8(txMachine* the)
1171
0
{
1172
0
  fx_DataView_prototype_set(the, 1, fxUintCoerce, fxUint8Setter);
1173
0
}
1174
1175
void fx_DataView_prototype_setUint16(txMachine* the)
1176
0
{
1177
0
  fx_DataView_prototype_set(the, 2, fxUintCoerce, fxUint16Setter);
1178
0
}
1179
1180
void fx_DataView_prototype_setUint32(txMachine* the)
1181
0
{
1182
0
  fx_DataView_prototype_set(the, 4, fxUintCoerce, fxUint32Setter);
1183
0
}
1184
1185
1186
#define mxTypedArrayDeclarations \
1187
0
  txSlot* instance = fxCheckTypedArrayInstance(the, mxThis); \
1188
0
  txSlot* dispatch = instance->next; \
1189
0
  txSlot* view = dispatch->next; \
1190
0
  txSlot* buffer = view->next; \
1191
0
  txInteger length = fxCheckDataViewSize(the, view, buffer, XS_IMMUTABLE) >> dispatch->value.typedArray.dispatch->shift
1192
1193
#define mxMutableTypedArrayDeclarations \
1194
0
  txSlot* instance = fxCheckTypedArrayInstance(the, mxThis); \
1195
0
  txSlot* dispatch = instance->next; \
1196
0
  txSlot* view = dispatch->next; \
1197
0
  txSlot* buffer = view->next; \
1198
0
  txInteger length = fxCheckDataViewSize(the, view, buffer, XS_MUTABLE) >> dispatch->value.typedArray.dispatch->shift
1199
1200
#define mxResultTypedArrayDeclarations \
1201
0
  txSlot* resultInstance = fxCheckTypedArrayInstance(the, mxResult); \
1202
0
  txSlot* resultDispatch = resultInstance->next; \
1203
0
  txSlot* resultView = resultDispatch->next; \
1204
0
  txSlot* resultBuffer = resultView->next; \
1205
0
  txInteger resultLength = fxCheckDataViewSize(the, resultView, resultBuffer, XS_MUTABLE) >> resultDispatch->value.typedArray.dispatch->shift
1206
1207
void fxTypedArrayGetter(txMachine* the)
1208
0
{
1209
0
  txSlot* instance = fxToInstance(the, mxThis);
1210
0
  txSlot* dispatch;
1211
0
  while (instance) {
1212
0
    if (instance->flag & XS_EXOTIC_FLAG) {
1213
0
      dispatch = instance->next;
1214
0
      if (dispatch->ID == XS_TYPED_ARRAY_BEHAVIOR)
1215
0
        break;
1216
0
    }
1217
0
    instance = fxGetPrototype(the, instance);
1218
0
  }
1219
0
  if (instance) {
1220
0
    txID id = the->scratch.value.at.id;
1221
0
    txIndex index = the->scratch.value.at.index;
1222
0
    txSlot* view = dispatch->next;
1223
0
    txSlot* buffer = view->next;
1224
0
    txU2 shift = dispatch->value.typedArray.dispatch->shift;
1225
0
    txIndex length = fxGetDataViewSize(the, view, buffer) >> shift;
1226
0
    if ((!id) && (index < length)) {
1227
0
      (*dispatch->value.typedArray.dispatch->getter)(the, buffer->value.reference->next, view->value.dataView.offset + (index << shift), mxResult, EndianNative);
1228
0
    }
1229
0
  }
1230
0
}
1231
1232
void fxTypedArraySetter(txMachine* the)
1233
0
{
1234
0
  txSlot* instance = fxToInstance(the, mxThis);
1235
0
  while (instance) {
1236
0
    if (instance->flag & XS_EXOTIC_FLAG) {
1237
0
      if (instance->next->ID == XS_TYPED_ARRAY_BEHAVIOR)
1238
0
        break;
1239
0
    }
1240
0
    instance = fxGetPrototype(the, instance);
1241
0
  }
1242
0
  if (instance) {
1243
0
    txSlot* value = mxArgv(0);
1244
0
    txID id = the->scratch.value.at.id;
1245
0
    txIndex index = the->scratch.value.at.index;
1246
0
        if (!fxTypedArraySetPropertyValue(the, instance, id, index, value, mxThis)) {
1247
0
            if (the->frame->next->flag & XS_STRICT_FLAG)
1248
0
        mxTypeError("not extensible or not writable");
1249
0
        }
1250
0
  }
1251
0
}
1252
1253
txBoolean fxTypedArrayDefineOwnProperty(txMachine* the, txSlot* instance, txID id, txIndex index, txSlot* slot, txFlag mask) 
1254
0
{
1255
0
  if ((!id) || fxIsCanonicalIndex(the, id)) {
1256
0
    txSlot* dispatch = instance->next;
1257
0
    txSlot* view = dispatch->next;
1258
0
    txSlot* buffer = view->next;
1259
0
    txU2 shift = dispatch->value.typedArray.dispatch->shift;
1260
0
    txSlot* arrayBuffer = buffer->value.reference->next;
1261
0
    txIndex length = fxGetDataViewSize(the, view, buffer) >> shift;
1262
0
    if (id || (index >= length))
1263
0
      return 0;
1264
0
    if (arrayBuffer->flag & XS_DONT_SET_FLAG) {
1265
0
      if ((mask & XS_DONT_DELETE_FLAG) && !(slot->flag & XS_DONT_DELETE_FLAG))
1266
0
        return 0;
1267
0
      if ((mask & XS_DONT_ENUM_FLAG) && (slot->flag & XS_DONT_ENUM_FLAG))
1268
0
        return 0;
1269
0
      if (mask & XS_ACCESSOR_FLAG)
1270
0
        return 0;
1271
0
      if ((mask & XS_DONT_SET_FLAG) && !(slot->flag & XS_DONT_SET_FLAG))
1272
0
        return 0;
1273
0
      if (slot->kind != XS_UNINITIALIZED_KIND) {
1274
0
        txSlot* property;
1275
0
        txBoolean result = 1;
1276
0
        mxTemporary(property);
1277
0
        (*dispatch->value.typedArray.dispatch->getter)(the, arrayBuffer, view->value.dataView.offset + (index << shift), property, EndianNative);
1278
0
        result = fxIsSameValue(the, property, slot, 0);
1279
0
        mxPop();
1280
0
        return result;
1281
0
      }
1282
0
      return 1;
1283
0
    }
1284
0
    if ((mask & XS_DONT_DELETE_FLAG) && (slot->flag & XS_DONT_DELETE_FLAG))
1285
0
      return 0;
1286
0
    if ((mask & XS_DONT_ENUM_FLAG) && (slot->flag & XS_DONT_ENUM_FLAG))
1287
0
      return 0;
1288
0
    if (mask & XS_ACCESSOR_FLAG)
1289
0
      return 0;
1290
0
    if ((mask & XS_DONT_SET_FLAG) && (slot->flag & XS_DONT_SET_FLAG))
1291
0
      return 0;
1292
0
    if (slot->kind != XS_UNINITIALIZED_KIND) {
1293
0
      dispatch->value.typedArray.dispatch->coerce(the, slot);
1294
0
      length = fxGetDataViewSize(the, view, buffer) >> shift;
1295
0
      if (index < length)
1296
0
        (*dispatch->value.typedArray.dispatch->setter)(the, arrayBuffer, view->value.dataView.offset + (index << shift), slot, EndianNative);
1297
0
    }
1298
0
    return 1;
1299
0
  }
1300
0
  return fxOrdinaryDefineOwnProperty(the, instance, id, index, slot, mask);
1301
0
}
1302
1303
txBoolean fxTypedArrayDeleteProperty(txMachine* the, txSlot* instance, txID id, txIndex index)
1304
0
{
1305
0
  if ((!id) || fxIsCanonicalIndex(the, id)) {
1306
0
    txSlot* dispatch = instance->next;
1307
0
    txSlot* view = dispatch->next;
1308
0
    txSlot* buffer = view->next;
1309
0
    txU2 shift = dispatch->value.typedArray.dispatch->shift;
1310
0
    txIndex length = fxGetDataViewSize(the, view, buffer) >> shift;
1311
0
    return ((!id) && (index < length)) ? 0 : 1;
1312
0
  }
1313
0
  return fxOrdinaryDeleteProperty(the, instance, id, index);
1314
0
}
1315
1316
txBoolean fxTypedArrayGetOwnProperty(txMachine* the, txSlot* instance, txID id, txIndex index, txSlot* slot)
1317
0
{
1318
0
  if ((!id) || fxIsCanonicalIndex(the, id)) {
1319
0
    txSlot* dispatch = instance->next;
1320
0
    txSlot* view = dispatch->next;
1321
0
    txSlot* buffer = view->next;
1322
0
    txU2 shift = dispatch->value.typedArray.dispatch->shift;
1323
0
    txSlot* arrayBuffer = buffer->value.reference->next;
1324
0
    txIndex length = fxGetDataViewSize(the, view, buffer) >> shift;
1325
0
    if ((!id) && (index < length)) {
1326
0
      (*dispatch->value.typedArray.dispatch->getter)(the, buffer->value.reference->next, view->value.dataView.offset + (index << shift), slot, EndianNative);
1327
0
      if (arrayBuffer->flag & XS_DONT_SET_FLAG)
1328
0
        slot->flag |= XS_DONT_DELETE_FLAG | XS_DONT_SET_FLAG;
1329
0
      return 1;
1330
0
    }
1331
0
    slot->kind = XS_UNDEFINED_KIND;
1332
0
    slot->flag = XS_NO_FLAG;
1333
0
    return 0;
1334
0
  }
1335
0
  return fxOrdinaryGetOwnProperty(the, instance, id, index, slot);
1336
0
}
1337
1338
txSlot* fxTypedArrayGetProperty(txMachine* the, txSlot* instance, txID id, txIndex index, txFlag flag)
1339
0
{
1340
0
  if ((!id) || fxIsCanonicalIndex(the, id)) {
1341
0
    the->scratch.value.at.id = id;
1342
0
    the->scratch.value.at.index = index;
1343
0
    return &mxTypedArrayAccessor;
1344
0
  }
1345
0
  return fxOrdinaryGetProperty(the, instance, id, index, flag);
1346
0
}
1347
1348
txBoolean fxTypedArrayGetPropertyValue(txMachine* the, txSlot* instance, txID id, txIndex index, txSlot* receiver, txSlot* value)
1349
0
{
1350
0
  if ((!id) || fxIsCanonicalIndex(the, id)) {
1351
0
    txSlot* dispatch = instance->next;
1352
0
    txSlot* view = dispatch->next;
1353
0
    txSlot* buffer = view->next;
1354
0
    txU2 shift = dispatch->value.typedArray.dispatch->shift;
1355
0
    txIndex length = fxGetDataViewSize(the, view, buffer) >> shift;
1356
0
    if ((!id) && (index < length)) {
1357
0
      (*dispatch->value.typedArray.dispatch->getter)(the, buffer->value.reference->next, view->value.dataView.offset + (index << shift), value, EndianNative);
1358
0
      return 1;
1359
0
    }
1360
0
    value->kind = XS_UNDEFINED_KIND;
1361
0
    return 0;
1362
0
  }
1363
0
  return fxOrdinaryGetPropertyValue(the, instance, id, index, receiver, value);
1364
0
}
1365
1366
txBoolean fxTypedArrayHasProperty(txMachine* the, txSlot* instance, txID id, txIndex index)
1367
0
{
1368
0
  if ((!id) || fxIsCanonicalIndex(the, id)) {
1369
0
    txSlot* dispatch = instance->next;
1370
0
    txSlot* view = dispatch->next;
1371
0
    txSlot* buffer = view->next;
1372
0
    txU2 shift = dispatch->value.typedArray.dispatch->shift;
1373
0
    txIndex length = fxGetDataViewSize(the, view, buffer) >> shift;
1374
0
    return ((!id) && (index < length)) ? 1 : 0;
1375
0
  }
1376
0
  return fxOrdinaryHasProperty(the, instance, id, index);
1377
0
}
1378
1379
void fxTypedArrayOwnKeys(txMachine* the, txSlot* instance, txFlag flag, txSlot* keys)
1380
0
{
1381
0
  if (flag & XS_EACH_NAME_FLAG) {
1382
0
    txSlot* dispatch = instance->next;
1383
0
    txSlot* view = dispatch->next;
1384
0
    txSlot* buffer = view->next;
1385
0
    txU2 shift = dispatch->value.typedArray.dispatch->shift;
1386
0
    txIndex length = fxGetDataViewSize(the, view, buffer) >> shift;
1387
0
    if (length) {
1388
0
      txIndex index;
1389
0
      for (index = 0; index < length; index++)
1390
0
        keys = fxQueueKey(the, 0, index, keys);
1391
0
    }
1392
0
  }
1393
0
  fxOrdinaryOwnKeys(the, instance, flag, keys);
1394
0
}
1395
1396
txBoolean fxTypedArrayPreventExtensions(txMachine* the, txSlot* instance)
1397
0
{
1398
0
  txSlot* dispatch = instance->next;
1399
0
  txSlot* view = dispatch->next;
1400
0
  txSlot* buffer = view->next;
1401
0
  txSlot* arrayBuffer = buffer->value.reference->next;
1402
0
  txSlot* bufferInfo = arrayBuffer->next;
1403
0
  if ((arrayBuffer->kind == XS_ARRAY_BUFFER_KIND) && (bufferInfo->value.bufferInfo.maxLength >= 0))
1404
0
    return 0;
1405
0
  return fxOrdinaryPreventExtensions(the, instance);
1406
0
}
1407
1408
txSlot* fxTypedArraySetProperty(txMachine* the, txSlot* instance, txID id, txIndex index, txFlag flag)
1409
0
{
1410
0
  if ((!id) || fxIsCanonicalIndex(the, id)) {
1411
0
    the->scratch.value.at.id = id;
1412
0
    the->scratch.value.at.index = index;
1413
0
    return &mxTypedArrayAccessor;
1414
0
  }
1415
0
  return fxOrdinarySetProperty(the, instance, id, index, flag);
1416
0
}
1417
1418
txBoolean fxTypedArraySetPropertyValue(txMachine* the, txSlot* instance, txID id, txIndex index, txSlot* value, txSlot* receiver)
1419
0
{
1420
0
  if ((!id) || fxIsCanonicalIndex(the, id)) {
1421
0
    txSlot* dispatch = instance->next;
1422
0
    txSlot* view = dispatch->next;
1423
0
    txSlot* buffer = view->next;
1424
0
    txU2 shift = dispatch->value.typedArray.dispatch->shift;
1425
0
    txSlot* arrayBuffer = buffer->value.reference->next;
1426
0
    txIndex length;
1427
0
    if (arrayBuffer->flag & XS_DONT_SET_FLAG)
1428
0
      return 0;
1429
0
    if ((receiver->kind == XS_REFERENCE_KIND) && (receiver->value.reference == instance)) {
1430
0
      dispatch->value.typedArray.dispatch->coerce(the, value);
1431
0
      length = fxGetDataViewSize(the, view, buffer) >> shift;
1432
0
      if ((!id) && (index < length)) {
1433
0
        (*dispatch->value.typedArray.dispatch->setter)(the, buffer->value.reference->next, view->value.dataView.offset + (index << shift), value, EndianNative);
1434
0
      }
1435
0
      return 1;
1436
0
    }
1437
0
    length = fxGetDataViewSize(the, view, buffer) >> shift;
1438
0
    if ((id) || (index >= length)) {
1439
0
      return 1;
1440
0
    }
1441
0
  }
1442
0
  return fxOrdinarySetPropertyValue(the, instance, id, index, value, receiver);
1443
0
}
1444
1445
void fxCallTypedArrayItem(txMachine* the, txSlot* function, txSlot* dispatch, txSlot* view, txSlot* data, txInteger index, txSlot* item)
1446
0
{
1447
  /* THIS */
1448
0
  if (mxArgc > 1)
1449
0
    mxPushSlot(mxArgv(1));
1450
0
  else
1451
0
    mxPushUndefined();
1452
  /* FUNCTION */
1453
0
  mxPushSlot(function);
1454
0
  mxCall();
1455
  /* ARGUMENTS */
1456
0
  mxPushSlot(mxThis);
1457
0
  mxGetIndex(index);
1458
0
  if (item) {
1459
0
    item->kind = the->stack->kind;
1460
0
    item->value = the->stack->value;
1461
0
  }
1462
0
  mxPushInteger(index);
1463
0
  mxPushSlot(mxThis);
1464
0
  mxRunCount(3);
1465
0
}
1466
1467
void fxCheckTypedArrayContentType(txMachine* the, txSlot* dispatch, txSlot* sourceDispatch)
1468
0
{
1469
0
  txBoolean contentType = (dispatch->value.typedArray.dispatch->constructorID == _BigInt64Array)
1470
0
      || (dispatch->value.typedArray.dispatch->constructorID == _BigUint64Array);
1471
0
  txBoolean sourceContentType = (sourceDispatch->value.typedArray.dispatch->constructorID == _BigInt64Array)
1472
0
      || (sourceDispatch->value.typedArray.dispatch->constructorID == _BigUint64Array);
1473
0
  if (contentType != sourceContentType)
1474
0
    mxTypeError("incompatible content type");
1475
0
}
1476
1477
txSlot* fxCheckTypedArrayInstance(txMachine* the, txSlot* slot)
1478
0
{
1479
0
  if (slot->kind == XS_REFERENCE_KIND) {
1480
0
        txSlot* instance = slot->value.reference;
1481
0
    if (((slot = instance->next)) && (slot->flag & XS_INTERNAL_FLAG) && (slot->kind == XS_TYPED_ARRAY_KIND))
1482
0
      return instance;
1483
0
  }
1484
0
  mxTypeError("this: not a TypedArray instance");
1485
0
  return C_NULL;
1486
0
}
1487
1488
txSlot* fxConstructTypedArray(txMachine* the)
1489
0
{
1490
0
  txSlot* prototype;
1491
0
  txSlot* dispatch;
1492
0
  txSlot* instance;
1493
0
  dispatch = mxFunctionInstanceHome(mxFunction->value.reference);
1494
0
  dispatch = dispatch->next;
1495
0
  prototype = mxBehaviorGetProperty(the, mxFunction->value.reference, mxID(_prototype), 0, XS_ANY);
1496
0
  if (!dispatch || (dispatch->kind != XS_TYPED_ARRAY_KIND))
1497
0
    mxTypeError("new: TypedArray");
1498
0
  mxPushSlot(mxTarget);
1499
0
  fxGetPrototypeFromConstructor(the, prototype);
1500
0
  instance = fxNewTypedArrayInstance(the, dispatch->value.typedArray.dispatch, dispatch->value.typedArray.atomics);
1501
0
  mxPullSlot(mxResult);
1502
0
  return instance;
1503
0
}
1504
1505
void fxCreateTypedArraySpecies(txMachine* the)
1506
0
{
1507
0
  txSlot* instance = fxToInstance(the, mxThis);
1508
0
  txSlot* dispatch = instance->next;
1509
0
  txSlot* constructor = &the->stackIntrinsics[-1 - (txInteger)dispatch->value.typedArray.dispatch->constructorID];
1510
0
  mxPushSlot(mxThis);
1511
0
  mxGetID(mxID(_constructor));
1512
0
  fxToSpeciesConstructor(the, constructor);
1513
0
  mxNew();
1514
0
}
1515
1516
void fxReduceTypedArrayItem(txMachine* the, txSlot* function, txSlot* dispatch, txSlot* view, txSlot* data, txInteger index)
1517
0
{
1518
  /* THIS */
1519
0
  mxPushUndefined();
1520
  /* FUNCTION */
1521
0
  mxPushSlot(function);
1522
0
  mxCall();
1523
  /* ARGUMENTS */
1524
0
  mxPushSlot(mxResult);
1525
0
  mxPushSlot(mxThis);
1526
0
  mxGetIndex(index);
1527
0
  mxPushInteger(index);
1528
0
  mxPushSlot(mxThis);
1529
0
  mxRunCount(4);
1530
0
  mxPullSlot(mxResult);
1531
0
}
1532
1533
txSlot* fxNewTypedArrayInstance(txMachine* the, txTypeDispatch* dispatch, txTypeAtomics* atomics)
1534
0
{
1535
0
  txSlot* instance;
1536
0
  txSlot* property;
1537
0
  instance = fxNewObjectInstance(the);
1538
0
  instance->flag |= XS_EXOTIC_FLAG;
1539
0
  property = fxNextTypeDispatchProperty(the, instance, dispatch, atomics, XS_TYPED_ARRAY_BEHAVIOR, XS_INTERNAL_FLAG);
1540
0
  property = property->next = fxNewSlot(the);
1541
0
  property->flag = XS_INTERNAL_FLAG;
1542
0
  property->kind = XS_DATA_VIEW_KIND;
1543
0
  property->value.dataView.offset = 0;
1544
0
  property->value.dataView.size = 0;
1545
0
  property = fxNextNullProperty(the, property, XS_NO_ID, XS_INTERNAL_FLAG);
1546
0
  return instance;
1547
0
}
1548
1549
void fx_TypedArray(txMachine* the)
1550
0
{
1551
0
  if (!mxHasTarget)
1552
0
    mxTypeError("call: TypedArray");
1553
0
  if (mxArgc > 0) {
1554
0
    if (mxArgv(0)->kind == XS_REFERENCE_KIND) {
1555
0
      txSlot* instance = fxConstructTypedArray(the);
1556
0
      txSlot* dispatch = instance->next;
1557
0
      txSlot* view = dispatch->next;
1558
0
      txSlot* buffer = view->next;
1559
0
      txSlot* data = C_NULL;
1560
0
      txU2 shift = dispatch->value.typedArray.dispatch->shift;
1561
0
      txSlot* slot = mxArgv(0)->value.reference->next;
1562
0
      if (slot && ((slot->kind == XS_ARRAY_BUFFER_KIND) || (slot->kind == XS_HOST_KIND))) {
1563
0
        txInteger offset = fxArgToByteLength(the, 1, 0);
1564
0
        txInteger size;
1565
0
        txSlot* info;
1566
0
        if (offset & ((1 << shift) - 1))
1567
0
          mxRangeError("invalid byteOffset %ld", offset);
1568
0
        size = fxArgToByteLength(the, 2, -1);
1569
0
        info = fxGetBufferInfo(the, mxArgv(0));
1570
0
        if (size >= 0) {
1571
0
          txInteger delta = (size <= (INT_MAX >> shift)) ? size << shift : -1;
1572
0
          txInteger end = fxAddChunkSizes(the, offset, delta);
1573
0
          if ((info->value.bufferInfo.length < end) || (end < offset))
1574
0
            mxRangeError("invalid length %ld", size);
1575
0
          size = delta;
1576
0
        }
1577
0
        else if (info->value.bufferInfo.maxLength >= 0) {
1578
0
          if (info->value.bufferInfo.length < offset)
1579
0
            mxRangeError("invalid offset %ld", offset);
1580
0
          size = -1;
1581
0
        }
1582
0
        else {
1583
0
          if (info->value.bufferInfo.length & ((1 << shift) - 1))
1584
0
            mxRangeError("invalid byteLength %ld", info->value.bufferInfo.length);
1585
0
          size = info->value.bufferInfo.length - offset;
1586
0
          if (size < 0)
1587
0
            mxRangeError("invalid byteLength %ld", size);
1588
0
        }
1589
0
        view->value.dataView.offset = offset;
1590
0
        view->value.dataView.size = size;
1591
0
        buffer->kind = XS_REFERENCE_KIND;
1592
0
        buffer->value.reference = mxArgv(0)->value.reference;
1593
0
      }
1594
0
      else if (slot && (slot->kind == XS_TYPED_ARRAY_KIND)) {
1595
0
        txSlot* sourceDispatch = slot;
1596
0
        txSlot* sourceView = sourceDispatch->next;
1597
0
        txSlot* sourceBuffer = sourceView->next;
1598
0
        txU2 sourceShift = sourceDispatch->value.typedArray.dispatch->shift;
1599
0
        txInteger sourceLength = fxCheckDataViewSize(the, sourceView, sourceBuffer, XS_IMMUTABLE) >> sourceShift;
1600
0
        txSlot* sourceData = sourceBuffer->value.reference->next;
1601
0
        txInteger sourceDelta = sourceDispatch->value.typedArray.dispatch->size;
1602
0
        txInteger sourceOffset = sourceView->value.dataView.offset;
1603
0
        txInteger offset = 0;
1604
0
        if (sourceLength > (INT_MAX >> shift))
1605
0
          fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
1606
0
        txInteger size = sourceLength << shift;
1607
        /* TARGET */
1608
0
        mxPush(mxArrayBufferConstructor);
1609
        /* THIS */
1610
0
        mxPushUninitialized();
1611
        /* FUNCTION */
1612
0
        mxPush(mxArrayBufferConstructor);
1613
        /* RESULT */
1614
0
        mxPushUndefined();
1615
        /* FRAME */
1616
0
        mxPushUninitialized();
1617
0
        the->stack->flag |= XS_TARGET_FLAG; 
1618
        /* ARGUMENTS */
1619
0
        sourceLength = fxGetDataViewSize(the, sourceView, sourceBuffer) >> sourceShift;
1620
0
        size = sourceLength << shift;
1621
0
        mxPushInteger(size);
1622
0
        mxRunCount(1);
1623
0
        mxPullSlot(buffer);
1624
0
        sourceLength = fxCheckDataViewSize(the, sourceView, sourceBuffer, XS_IMMUTABLE) >> sourceShift;
1625
0
        size = sourceLength << shift;
1626
        
1627
0
        data = fxCheckArrayBufferDetached(the, buffer);
1628
0
        fxCheckArrayBufferMutable(the, buffer);
1629
0
        view->value.dataView.offset = offset;
1630
0
        view->value.dataView.size = size;
1631
0
        if (dispatch == sourceDispatch)
1632
0
          c_memcpy(data->value.arrayBuffer.address + offset, sourceData->value.arrayBuffer.address + sourceOffset, size);
1633
0
        else {
1634
0
          fxCheckTypedArrayContentType(the, dispatch, sourceDispatch);
1635
0
          mxPushUndefined();
1636
0
          while (offset < size) {
1637
0
            (*sourceDispatch->value.typedArray.dispatch->getter)(the, sourceData, sourceOffset, the->stack, EndianNative);
1638
0
            (*dispatch->value.typedArray.dispatch->coerce)(the, the->stack);
1639
0
            (*dispatch->value.typedArray.dispatch->setter)(the, data, offset, the->stack, EndianNative);
1640
0
            sourceOffset += sourceDelta;
1641
0
            offset += 1 << shift;
1642
0
          }
1643
0
          mxPop();
1644
0
        }
1645
0
      }
1646
0
      else {
1647
0
        fx_TypedArray_from_object(the, instance, C_NULL, C_NULL);
1648
0
      }
1649
0
      return;
1650
0
    }
1651
0
    fxToNumber(the, mxArgv(0));
1652
0
  }
1653
0
  {
1654
0
        txInteger length = fxArgToByteLength(the, 0, 0);
1655
0
    txSlot* instance = fxConstructTypedArray(the);
1656
0
    txSlot* dispatch = instance->next;
1657
0
    txSlot* view = dispatch->next;
1658
0
    txSlot* buffer = view->next;
1659
0
    txU2 shift = dispatch->value.typedArray.dispatch->shift;
1660
0
       if (length > (0x7FFFFFFF >> shift))
1661
0
      mxRangeError("byteLength too big");
1662
0
        length <<= shift;
1663
0
    mxPush(mxArrayBufferConstructor);
1664
0
    mxNew();
1665
0
    mxPushInteger(length);
1666
0
    mxRunCount(1);
1667
0
    mxPullSlot(buffer);
1668
0
        view->value.dataView.offset = 0;
1669
0
        view->value.dataView.size = length;
1670
0
  }
1671
0
}
1672
1673
void fx_TypedArray_from(txMachine* the)
1674
0
{
1675
0
  txSlot* function = C_NULL;
1676
0
  txSlot* _this = C_NULL;
1677
0
  if (!mxIsReference(mxThis) || !(mxIsConstructor(mxThis->value.reference)))
1678
0
    mxTypeError("this: not a constructor");
1679
0
  if (mxArgc > 1) {
1680
0
    txSlot* slot = mxArgv(1);
1681
0
    if (!mxIsUndefined(slot)) {
1682
0
      function = slot;
1683
0
      if (!fxIsCallable(the, function))
1684
0
        mxTypeError("map: not a function");
1685
0
      if (mxArgc > 2)
1686
0
        _this = mxArgv(2);
1687
0
    }
1688
0
  }
1689
0
  fx_TypedArray_from_object(the, C_NULL, function, _this);
1690
0
}
1691
1692
void fx_TypedArray_from_object(txMachine* the, txSlot* instance, txSlot* function, txSlot* _this)
1693
0
{
1694
0
  txSlot* stack = the->stack;
1695
0
  txSlot* iterator;
1696
0
  txSlot* next;
1697
0
  txSlot* value;
1698
0
  txSlot* list = C_NULL;
1699
0
  txSlot* slot;
1700
0
  txSlot* dispatch;
1701
0
  txSlot* view;
1702
0
  txSlot* buffer;
1703
0
  txSlot* data;
1704
0
  txNumber length;
1705
0
  mxTemporary(iterator);
1706
0
  mxTemporary(next);
1707
0
  if (fxGetIterator(the, mxArgv(0), iterator, next, 1)) {
1708
0
    list = fxNewInstance(the);
1709
0
    slot = list;
1710
0
    length = 0;
1711
0
    mxTemporary(value);
1712
0
    while (fxIteratorNext(the, iterator, next, value)) {
1713
0
      slot = fxNextSlotProperty(the, slot, value, XS_NO_ID, XS_NO_FLAG);
1714
0
      length++;
1715
0
    }
1716
0
  }
1717
0
  else {
1718
0
    mxPushSlot(mxArgv(0));
1719
0
    mxGetID(mxID(_length));
1720
0
    length = fxToLength(the, the->stack);
1721
0
    mxPop();
1722
0
  }
1723
0
  if (instance) {
1724
0
    dispatch = instance->next;
1725
0
    view = dispatch->next;
1726
0
    buffer = view->next;
1727
0
    mxPush(mxArrayBufferConstructor);
1728
0
    mxNew();
1729
0
    mxPushNumber(length * dispatch->value.typedArray.dispatch->size);
1730
0
    mxRunCount(1);
1731
0
    mxPullSlot(buffer);
1732
0
    data = fxCheckArrayBufferDetached(the, buffer);
1733
0
    fxCheckArrayBufferMutable(the, buffer);
1734
0
    view->value.dataView.offset = 0;
1735
0
    view->value.dataView.size = data->next->value.bufferInfo.length;
1736
0
  }
1737
0
  else {
1738
0
    mxPushSlot(mxThis);
1739
0
    mxNew();
1740
0
    mxPushNumber(length);
1741
0
    mxRunCount(1);
1742
0
    mxPullSlot(mxResult);
1743
0
    instance = fxToInstance(the, mxResult);
1744
0
    if (((slot = instance->next)) && (slot->flag & XS_INTERNAL_FLAG) && (slot->kind == XS_TYPED_ARRAY_KIND)) {
1745
0
      dispatch = instance->next;
1746
0
      view = dispatch->next;
1747
0
      buffer = view->next;
1748
0
      data = fxCheckArrayBufferDetached(the, buffer);
1749
0
      fxCheckArrayBufferMutable(the, buffer);
1750
0
      if (length > (fxGetDataViewSize(the, view, buffer) >> dispatch->value.typedArray.dispatch->shift))
1751
0
        mxTypeError("result: too small TypedArray instance");
1752
0
    }
1753
0
    else
1754
0
      mxTypeError("result: not a TypedArray instance");
1755
0
  }
1756
0
  if (list) {
1757
0
    txInteger index = 0;
1758
0
    slot = list->next;
1759
0
    while (slot) {
1760
      /* ARG0 */
1761
0
      if (function) {
1762
        /* THIS */
1763
0
        if (_this)
1764
0
          mxPushSlot(_this);
1765
0
        else
1766
0
          mxPushUndefined();
1767
        /* FUNCTION */
1768
0
        mxPushSlot(function);
1769
0
        mxCall();
1770
        /* ARGUMENTS */
1771
0
        mxPushSlot(slot);
1772
0
        mxPushInteger(index);
1773
0
        mxRunCount(2);
1774
0
      }
1775
0
      else
1776
0
        mxPushSlot(slot);
1777
0
      mxPushSlot(mxResult);
1778
0
      mxSetIndex(index);
1779
0
      mxPop();
1780
0
      index++;
1781
0
      slot = slot->next;
1782
0
    }
1783
0
  }
1784
0
  else {
1785
0
    txInteger index = 0;
1786
0
    txInteger count = (txInteger)length;
1787
0
    while (index < count) {
1788
0
      if (function) {
1789
        /* THIS */
1790
0
        if (_this)
1791
0
          mxPushSlot(_this);
1792
0
        else
1793
0
          mxPushUndefined();
1794
        /* FUNCTION */
1795
0
        mxPushSlot(function);
1796
0
        mxCall();
1797
        /* ARGUMENTS */
1798
0
        mxPushSlot(mxArgv(0));
1799
0
        mxGetIndex(index);
1800
0
        mxPushInteger(index);
1801
0
        mxRunCount(2);
1802
0
      }
1803
0
      else {
1804
0
        mxPushSlot(mxArgv(0));
1805
0
        mxGetIndex(index);
1806
0
      }
1807
0
      mxPushSlot(mxResult);
1808
0
      mxSetIndex(index);
1809
0
      mxPop();
1810
0
      index++;
1811
0
    } 
1812
0
  }
1813
0
  the->stack = stack;
1814
0
}
1815
1816
void fx_TypedArray_of(txMachine* the)
1817
0
{
1818
0
  txInteger count = mxArgc;
1819
0
  txInteger index = 0;
1820
0
  mxPushSlot(mxThis);
1821
0
  mxNew();
1822
0
  mxPushInteger(count);
1823
0
  mxRunCount(1);
1824
0
  mxPullSlot(mxResult);
1825
0
  {
1826
0
    mxResultTypedArrayDeclarations;
1827
0
    txU2 shift = resultDispatch->value.typedArray.dispatch->shift;
1828
0
    if (resultLength < count)
1829
0
      mxTypeError("result: too small TypedArray instance");
1830
0
    while (index < count) {
1831
0
      (*resultDispatch->value.typedArray.dispatch->coerce)(the, mxArgv(index));
1832
0
      if (resultBuffer->value.arrayBuffer.address == C_NULL)
1833
0
        mxTypeError("detached buffer");
1834
0
      (*resultDispatch->value.typedArray.dispatch->setter)(the, resultBuffer->value.reference->next, resultView->value.dataView.offset + (index << shift), mxArgv(index), EndianNative);
1835
0
      index++;
1836
0
    }
1837
0
  }
1838
0
}
1839
1840
void fx_TypedArray_prototype_at(txMachine* the)
1841
0
{
1842
0
  mxTypedArrayDeclarations;
1843
0
  txInteger index = (mxArgc > 0) ? fxToInteger(the, mxArgv(0)) : 0;
1844
0
  if (index < 0)
1845
0
    index = length + index;
1846
0
  if ((0 <= index) && (index < length)) {
1847
0
    mxPushSlot(mxThis);
1848
0
    mxGetIndex(index);
1849
0
    mxPullSlot(mxResult);
1850
0
  }
1851
0
}
1852
1853
void fx_TypedArray_prototype_buffer_get(txMachine* the)
1854
0
{
1855
0
  txSlot* instance = fxCheckTypedArrayInstance(the, mxThis);
1856
0
  txSlot* dispatch = instance->next;
1857
0
  txSlot* view = dispatch->next;
1858
0
  txSlot* buffer = view->next;
1859
0
  mxResult->kind = buffer->kind;
1860
0
  mxResult->value = buffer->value;
1861
0
}
1862
1863
void fx_TypedArray_prototype_byteLength_get(txMachine* the)
1864
0
{
1865
0
  txSlot* instance = fxCheckTypedArrayInstance(the, mxThis);
1866
0
  txSlot* dispatch = instance->next;
1867
0
  txSlot* view = dispatch->next;
1868
0
  txSlot* buffer = view->next;
1869
0
  txU2 shift = dispatch->value.typedArray.dispatch->shift;
1870
0
  mxResult->kind = XS_INTEGER_KIND;
1871
0
  mxResult->value.integer = fxGetDataViewSize(the, view, buffer) & ~((1 << shift) - 1);
1872
0
}
1873
1874
void fx_TypedArray_prototype_byteOffset_get(txMachine* the)
1875
0
{
1876
0
  txSlot* instance = fxCheckTypedArrayInstance(the, mxThis);
1877
0
  txSlot* dispatch = instance->next;
1878
0
  txSlot* view = dispatch->next;
1879
0
  txSlot* buffer = view->next;
1880
0
  txInteger offset = view->value.dataView.offset;
1881
0
  txInteger size = view->value.dataView.size;
1882
0
  txSlot* arrayBuffer = buffer->value.reference->next;
1883
0
  txSlot* bufferInfo = arrayBuffer->next;
1884
0
  mxResult->kind = XS_INTEGER_KIND;
1885
0
  mxResult->value.integer = 0;
1886
0
  if (arrayBuffer->value.arrayBuffer.address == C_NULL)
1887
0
    return;
1888
0
  if (bufferInfo->value.bufferInfo.maxLength >= 0) {
1889
0
    txInteger byteLength = bufferInfo->value.bufferInfo.length;
1890
0
    if (offset > byteLength)
1891
0
      return;
1892
0
    size = (size < 0) ? byteLength : offset + size;
1893
0
    if (size > byteLength)
1894
0
      return;
1895
0
    size -= offset;
1896
0
  }
1897
0
  mxResult->value.integer = offset;
1898
0
}
1899
1900
void fx_TypedArray_prototype_copyWithin(txMachine* the)
1901
0
{
1902
0
  mxMutableTypedArrayDeclarations;
1903
0
  txInteger delta = dispatch->value.typedArray.dispatch->size;
1904
0
  txInteger target = fxArgToIndexInteger(the, 0, 0, length);
1905
0
  txInteger start = fxArgToIndexInteger(the, 1, 0, length);
1906
0
  txInteger end = fxArgToIndexInteger(the, 2, length, length);
1907
0
  txInteger count = end - start;
1908
0
  if (count > length - target)
1909
0
    count = length - target;
1910
0
  if (count > 0) {
1911
0
    txByte* address = buffer->value.reference->next->value.arrayBuffer.address;
1912
0
    txInteger offset = view->value.dataView.offset;
1913
0
    if (fxIsDataViewOutOfBound(the, view, buffer))
1914
0
      mxTypeError("out of bound buffer");
1915
0
    txInteger size = fxCheckDataViewSize(the, view, buffer, XS_MUTABLE) & ~(delta - 1);
1916
0
    target = offset + (target * delta);
1917
0
    start = offset + (start * delta);
1918
0
    end = offset + size;
1919
0
    count = count * delta;
1920
0
    if (count > end - target)
1921
0
      count = end - target;
1922
0
    if (count > end - start)
1923
0
      count = end - start;
1924
0
    if (count > 0) {
1925
0
      c_memmove(address + target, address + start, count);
1926
0
      mxMeterSome((txU4)count * 2);
1927
0
    }
1928
0
  }
1929
0
  mxResult->kind = mxThis->kind;
1930
0
  mxResult->value = mxThis->value;
1931
0
}
1932
1933
void fx_TypedArray_prototype_entries(txMachine* the)
1934
0
{
1935
0
  txSlot* instance = fxCheckTypedArrayInstance(the, mxThis);
1936
0
  txSlot* dispatch = instance->next;
1937
0
  txSlot* view = dispatch->next;
1938
0
  txSlot* buffer = view->next;
1939
0
  txSlot* property;
1940
0
  fxCheckDataViewSize(the, view, buffer, XS_IMMUTABLE);
1941
0
  mxPush(mxArrayIteratorPrototype);
1942
0
  property = fxLastProperty(the, fxNewIteratorInstance(the, mxThis, mxID(_Array)));
1943
0
  property = fxNextIntegerProperty(the, property, 2, XS_NO_ID, XS_INTERNAL_FLAG);
1944
0
  mxPullSlot(mxResult);
1945
0
}
1946
1947
void fx_TypedArray_prototype_every(txMachine* the)
1948
0
{
1949
0
  mxTypedArrayDeclarations;
1950
0
  txSlot* function = fxArgToCallback(the, 0);
1951
0
  txInteger index = 0;
1952
0
  mxResult->kind = XS_BOOLEAN_KIND;
1953
0
  mxResult->value.boolean = 1;
1954
0
  while (index < length) {
1955
0
    fxCallTypedArrayItem(the, function, dispatch, view, buffer->value.reference->next, index, C_NULL);
1956
0
    mxResult->value.boolean = fxToBoolean(the, the->stack++);
1957
0
    if (!mxResult->value.boolean)
1958
0
      break;
1959
0
    index++;
1960
0
  }
1961
0
}
1962
1963
void fx_TypedArray_prototype_fill(txMachine* the)
1964
0
{
1965
0
  mxMutableTypedArrayDeclarations;
1966
0
  txInteger delta, start, end, offset;
1967
0
  if (mxArgc > 0)
1968
0
    mxPushSlot(mxArgv(0));
1969
0
  else
1970
0
    mxPushUndefined();
1971
0
  (*dispatch->value.typedArray.dispatch->coerce)(the, the->stack);
1972
0
  delta = dispatch->value.typedArray.dispatch->size;
1973
0
  start = fxArgToIndexInteger(the, 1, 0, length);
1974
0
  end = fxArgToIndexInteger(the, 2, length, length);
1975
0
  offset = view->value.dataView.offset;
1976
0
  start *= delta;
1977
0
  end *= delta;
1978
0
  start += offset;
1979
0
  end += offset;
1980
0
  txInteger size = fxCheckDataViewSize(the, view, buffer, XS_MUTABLE) & ~(delta - 1);
1981
0
  if (end > offset + size)
1982
0
    end = offset + size;
1983
0
  while (start < end) {
1984
0
    (*dispatch->value.typedArray.dispatch->setter)(the, buffer->value.reference->next, start, the->stack, EndianNative);
1985
0
    start += delta;
1986
0
  }
1987
0
  mxPop();
1988
0
  mxResult->kind = mxThis->kind;
1989
0
  mxResult->value = mxThis->value;
1990
0
}
1991
1992
void fx_TypedArray_prototype_filter(txMachine* the)
1993
0
{
1994
0
  mxTypedArrayDeclarations;
1995
0
  txSlot* function = fxArgToCallback(the, 0);
1996
0
  txSlot* list = fxNewInstance(the);
1997
0
  txSlot* slot = list;
1998
0
  txInteger count = 0;
1999
0
  txInteger index = 0;
2000
0
  mxPushUndefined();
2001
0
  while (index < length) {
2002
0
    fxCallTypedArrayItem(the, function, dispatch, view, buffer->value.reference->next, index, the->stack);
2003
0
    if (fxToBoolean(the, the->stack++)) {
2004
0
      count++;
2005
0
      slot = fxNextSlotProperty(the, slot, the->stack, XS_NO_ID, XS_NO_FLAG);
2006
0
    }
2007
0
    index++;
2008
0
  }
2009
0
  mxPop();
2010
0
  fxCreateTypedArraySpecies(the);
2011
0
  mxPushNumber(count);
2012
0
  mxRunCount(1);
2013
0
  mxPullSlot(mxResult);
2014
0
  {
2015
0
    mxResultTypedArrayDeclarations;
2016
0
    txInteger resultOffset = 0;
2017
0
    txInteger resultSize = resultDispatch->value.typedArray.dispatch->size;
2018
0
    fxCheckTypedArrayContentType(the, resultDispatch, dispatch);
2019
0
    if (resultLength < count)
2020
0
      mxTypeError("result: too small TypedArray instance");
2021
0
    slot = list->next;
2022
0
    while (slot) {
2023
0
      (*resultDispatch->value.typedArray.dispatch->coerce)(the, slot);
2024
0
      (*resultDispatch->value.typedArray.dispatch->setter)(the, resultBuffer->value.reference->next, resultOffset, slot, EndianNative);
2025
0
      resultOffset += resultSize;
2026
0
      slot = slot->next;
2027
0
    }
2028
0
  }
2029
0
  mxPop();
2030
0
}
2031
2032
void fx_TypedArray_prototype_find(txMachine* the)
2033
0
{
2034
0
  mxTypedArrayDeclarations;
2035
0
  txSlot* function = fxArgToCallback(the, 0);
2036
0
  txInteger index = 0;
2037
0
  mxPushUndefined();
2038
0
  while (index < length) {
2039
0
    fxCallTypedArrayItem(the, function, dispatch, view, buffer->value.reference->next, index, the->stack);
2040
0
    if (fxToBoolean(the, the->stack++)) {
2041
0
      mxResult->kind = the->stack->kind;
2042
0
      mxResult->value = the->stack->value;
2043
0
      break;
2044
0
    }
2045
0
    index++;
2046
0
  }
2047
0
  mxPop();
2048
0
}
2049
2050
void fx_TypedArray_prototype_findIndex(txMachine* the)
2051
0
{
2052
0
  mxTypedArrayDeclarations;
2053
0
  txSlot* function = fxArgToCallback(the, 0);
2054
0
  txInteger index = 0;
2055
0
  mxResult->kind = XS_INTEGER_KIND;
2056
0
  mxResult->value.integer = -1;
2057
0
  while (index < length) {
2058
0
    fxCallTypedArrayItem(the, function, dispatch, view, buffer->value.reference->next, index, C_NULL);
2059
0
    if (fxToBoolean(the, the->stack++)) {
2060
0
      mxResult->value.integer = index;
2061
0
      break;
2062
0
    }
2063
0
    index++;
2064
0
  }
2065
0
}
2066
2067
void fx_TypedArray_prototype_findLast(txMachine* the)
2068
0
{
2069
0
  mxTypedArrayDeclarations;
2070
0
  txSlot* function = fxArgToCallback(the, 0);
2071
0
  txInteger index = length;
2072
0
  mxPushUndefined();
2073
0
  while (index > 0) {
2074
0
    index--;
2075
0
    fxCallTypedArrayItem(the, function, dispatch, view, buffer->value.reference->next, index, the->stack);
2076
0
    if (fxToBoolean(the, the->stack++)) {
2077
0
      mxResult->kind = the->stack->kind;
2078
0
      mxResult->value = the->stack->value;
2079
0
      break;
2080
0
    }
2081
0
  }
2082
0
  mxPop();
2083
0
}
2084
2085
void fx_TypedArray_prototype_findLastIndex(txMachine* the)
2086
0
{
2087
0
  mxTypedArrayDeclarations;
2088
0
  txSlot* function = fxArgToCallback(the, 0);
2089
0
  txInteger index = length;
2090
0
  mxResult->kind = XS_INTEGER_KIND;
2091
0
  mxResult->value.integer = -1;
2092
0
  while (index > 0) {
2093
0
    index--;
2094
0
    fxCallTypedArrayItem(the, function, dispatch, view, buffer->value.reference->next, index, C_NULL);
2095
0
    if (fxToBoolean(the, the->stack++)) {
2096
0
      mxResult->value.integer = index;
2097
0
      break;
2098
0
    }
2099
0
  }
2100
0
}
2101
2102
void fx_TypedArray_prototype_forEach(txMachine* the)
2103
0
{
2104
0
  mxTypedArrayDeclarations;
2105
0
  txSlot* function = fxArgToCallback(the, 0);
2106
0
  txInteger index = 0;
2107
0
  while (index < length) {
2108
0
    fxCallTypedArrayItem(the, function, dispatch, view, buffer->value.reference->next, index, C_NULL);
2109
0
    mxPop();
2110
0
    index++;
2111
0
  }
2112
0
}
2113
2114
void fx_TypedArray_prototype_includes(txMachine* the)
2115
0
{
2116
0
  mxTypedArrayDeclarations;
2117
0
  fxBoolean(the, mxResult, 0);
2118
0
  if (length) {
2119
0
    txInteger index = fxArgToIndexInteger(the, 1, 0, length);
2120
0
    txSlot* argument;
2121
0
    if (mxArgc > 0)
2122
0
      mxPushSlot(mxArgv(0));
2123
0
    else
2124
0
      mxPushUndefined();
2125
0
    argument = the->stack;
2126
0
    while (index < length) {
2127
0
      mxPushSlot(mxThis);
2128
0
      mxGetIndex(index);
2129
0
      if (fxIsSameValue(the, the->stack++, argument, 1)) {
2130
0
        mxResult->value.boolean = 1;
2131
0
        break;
2132
0
      }
2133
0
      index++;
2134
0
      mxCheckMetering();
2135
0
    }
2136
0
    mxPop();
2137
0
  }
2138
0
}
2139
2140
void fx_TypedArray_prototype_indexOf(txMachine* the)
2141
0
{
2142
0
  mxTypedArrayDeclarations;
2143
0
  fxInteger(the, mxResult, -1);
2144
0
  if (length) {
2145
0
    txInteger index = fxArgToIndexInteger(the, 1, 0, length);
2146
0
    txSlot* argument;
2147
0
    if (mxArgc > 0)
2148
0
      mxPushSlot(mxArgv(0));
2149
0
    else
2150
0
      mxPushUndefined();
2151
0
    argument = the->stack;
2152
0
    while (index < length) {
2153
0
      mxPushSlot(mxThis);
2154
0
      if (fxHasIndex(the, index)) {
2155
0
        mxPushSlot(mxThis);
2156
0
        mxGetIndex(index);
2157
0
        if (fxIsSameSlot(the, the->stack++, argument)) {
2158
0
          mxResult->value.integer = index;
2159
0
          break;
2160
0
        }
2161
0
      }
2162
0
      index++;
2163
0
      mxCheckMetering();
2164
0
    }
2165
0
    mxPop();
2166
0
  }
2167
0
}
2168
2169
void fx_TypedArray_prototype_join(txMachine* the)
2170
0
{
2171
0
  mxTypedArrayDeclarations;
2172
0
  txInteger delta = dispatch->value.typedArray.dispatch->size;
2173
0
  txInteger offset = view->value.dataView.offset;
2174
0
  txInteger limit = offset + (length << dispatch->value.typedArray.dispatch->shift);
2175
0
  txString string;
2176
0
  txSlot* list = fxNewInstance(the);
2177
0
  txSlot* slot = list;
2178
0
  txBoolean comma = 0;
2179
0
  txInteger size = 0;
2180
0
  if ((mxArgc > 0) && (mxArgv(0)->kind != XS_UNDEFINED_KIND)) {
2181
0
    mxPushSlot(mxArgv(0));
2182
0
    string = fxToString(the, the->stack);
2183
0
    the->stack->kind += XS_KEY_KIND - XS_STRING_KIND;
2184
0
    the->stack->value.key.sum = mxStringLength(the->stack->value.string);
2185
0
  }
2186
0
  else {
2187
0
    mxPushStringX(",");
2188
0
    the->stack->kind += XS_KEY_KIND - XS_STRING_KIND;
2189
0
    the->stack->value.key.sum = 1;
2190
0
  }
2191
0
  length = offset + fxGetDataViewSize(the, view, buffer);
2192
0
  while (offset < limit) {
2193
0
    if (comma) {
2194
0
      slot = fxNextSlotProperty(the, slot, the->stack, XS_NO_ID, XS_NO_FLAG);
2195
0
            size = fxAddChunkSizes(the, size, slot->value.key.sum);
2196
0
    }
2197
0
    else
2198
0
      comma = 1;
2199
0
    if (offset < length) {
2200
0
      mxPushUndefined();
2201
0
      (*dispatch->value.typedArray.dispatch->getter)(the, buffer->value.reference->next, offset, the->stack, EndianNative);
2202
0
      slot = fxNextSlotProperty(the, slot, the->stack, XS_NO_ID, XS_NO_FLAG);
2203
0
      string = fxToString(the, slot);
2204
0
      slot->kind += XS_KEY_KIND - XS_STRING_KIND;
2205
0
      slot->value.key.sum = mxStringLength(string);
2206
0
      size = fxAddChunkSizes(the, size, slot->value.key.sum);
2207
0
      mxPop();
2208
0
    }
2209
0
    offset += delta;
2210
0
  }
2211
0
  mxPop();
2212
0
  string = mxResult->value.string = fxNewChunk(the, fxAddChunkSizes(the, size, 1));
2213
0
  slot = list->next;
2214
0
  while (slot) {
2215
0
    c_memcpy(string, slot->value.key.string, slot->value.key.sum);
2216
0
    string += slot->value.key.sum;
2217
0
    slot = slot->next;
2218
0
  }
2219
0
  *string = 0;
2220
0
  mxResult->kind = XS_STRING_KIND;
2221
0
  mxPop();
2222
0
}
2223
2224
void fx_TypedArray_prototype_keys(txMachine* the)
2225
0
{
2226
0
  txSlot* instance = fxCheckTypedArrayInstance(the, mxThis);
2227
0
  txSlot* dispatch = instance->next;
2228
0
  txSlot* view = dispatch->next;
2229
0
  txSlot* buffer = view->next;
2230
0
  txSlot* property;
2231
0
  fxCheckDataViewSize(the, view, buffer, XS_IMMUTABLE);
2232
0
  mxPush(mxArrayIteratorPrototype);
2233
0
  property = fxLastProperty(the, fxNewIteratorInstance(the, mxThis, mxID(_Array)));
2234
0
  property = fxNextIntegerProperty(the, property, 1, XS_NO_ID, XS_INTERNAL_FLAG);
2235
0
  mxPullSlot(mxResult);
2236
0
}
2237
2238
void fx_TypedArray_prototype_lastIndexOf(txMachine* the)
2239
0
{
2240
0
  mxTypedArrayDeclarations;
2241
0
  fxInteger(the, mxResult, -1);
2242
0
  if (length) {
2243
0
    txIndex index = (txIndex)fxArgToLastIndex(the, 1, length, length);
2244
0
    txSlot* argument;
2245
0
    if (mxArgc > 0)
2246
0
      mxPushSlot(mxArgv(0));
2247
0
    else
2248
0
      mxPushUndefined();
2249
0
    argument = the->stack;
2250
0
    while (index > 0) {
2251
0
      index--;
2252
0
      mxPushSlot(mxThis);
2253
0
      if (fxHasIndex(the, index)) {
2254
0
        mxPushSlot(mxThis);
2255
0
        mxGetIndex(index);
2256
0
        if (fxIsSameSlot(the, the->stack++, argument)) {
2257
0
          mxResult->value.integer = index;
2258
0
          break;
2259
0
        }
2260
0
      }
2261
0
      mxCheckMetering();
2262
0
    }
2263
0
    mxPop();
2264
0
  }
2265
0
}
2266
2267
void fx_TypedArray_prototype_length_get(txMachine* the)
2268
0
{
2269
0
  txSlot* instance = fxCheckTypedArrayInstance(the, mxThis);
2270
0
  txSlot* dispatch = instance->next;
2271
0
  txSlot* view = dispatch->next;
2272
0
  txSlot* buffer = view->next;
2273
0
  txU2 shift = dispatch->value.typedArray.dispatch->shift;
2274
0
  mxResult->kind = XS_INTEGER_KIND;
2275
0
  mxResult->value.integer = fxGetDataViewSize(the, view, buffer) >> shift;
2276
0
}
2277
2278
void fx_TypedArray_prototype_map(txMachine* the)
2279
0
{
2280
0
  mxTypedArrayDeclarations;
2281
0
  txSlot* function = fxArgToCallback(the, 0);
2282
0
  fxCreateTypedArraySpecies(the);
2283
0
  mxPushNumber(length);
2284
0
  mxRunCount(1);
2285
0
  mxPullSlot(mxResult);
2286
0
  {
2287
0
    mxResultTypedArrayDeclarations;
2288
0
    txU2 shift = resultDispatch->value.typedArray.dispatch->shift;
2289
0
    txInteger index = 0;
2290
0
    fxCheckTypedArrayContentType(the, resultDispatch, dispatch);
2291
0
    if (resultLength < length)
2292
0
      mxTypeError("result: too small TypedArray instance");
2293
0
    while (index < length) {
2294
0
      fxCallTypedArrayItem(the, function, dispatch, view, buffer->value.reference->next, index, C_NULL);
2295
0
      if (resultBuffer->value.arrayBuffer.address == C_NULL)
2296
0
        mxTypeError("detached buffer");
2297
0
      (*resultDispatch->value.typedArray.dispatch->coerce)(the, the->stack);
2298
0
      (*resultDispatch->value.typedArray.dispatch->setter)(the, resultBuffer->value.reference->next, resultView->value.dataView.offset + (index << shift), the->stack, EndianNative);
2299
0
      mxPop();
2300
0
      index++;
2301
0
    }
2302
0
  }
2303
0
}
2304
2305
void fx_TypedArray_prototype_reduce(txMachine* the)
2306
0
{
2307
0
  mxTypedArrayDeclarations;
2308
0
  txSlot* function = fxArgToCallback(the, 0);
2309
0
  txInteger index = 0;
2310
0
  if (mxArgc > 1)
2311
0
    *mxResult = *mxArgv(1);
2312
0
  else if (index < length) {
2313
0
    (*dispatch->value.typedArray.dispatch->getter)(the, buffer->value.reference->next, view->value.dataView.offset, mxResult, EndianNative);
2314
0
    index++;
2315
0
  }
2316
0
  else
2317
0
    mxTypeError("no initial value");
2318
0
  while (index < length) {
2319
0
    fxReduceTypedArrayItem(the, function, dispatch, view, buffer->value.reference->next, index);
2320
0
    index++;
2321
0
  }
2322
0
}
2323
2324
void fx_TypedArray_prototype_reduceRight(txMachine* the)
2325
0
{
2326
0
  mxTypedArrayDeclarations;
2327
0
  txInteger delta = dispatch->value.typedArray.dispatch->size;
2328
0
  txSlot* function = fxArgToCallback(the, 0);
2329
0
  txInteger index = length - 1;
2330
0
  if (mxArgc > 1)
2331
0
    *mxResult = *mxArgv(1);
2332
0
  else if (index >= 0) {
2333
0
    (*dispatch->value.typedArray.dispatch->getter)(the, buffer->value.reference->next, view->value.dataView.offset + (index * delta), mxResult, EndianNative);
2334
0
    index--;
2335
0
  }
2336
0
  else
2337
0
    mxTypeError("no initial value");
2338
0
  while (index >= 0) {
2339
0
    fxReduceTypedArrayItem(the, function, dispatch, view, buffer->value.reference->next, index);
2340
0
    index--;
2341
0
  }
2342
0
}
2343
2344
void fx_TypedArray_prototype_reverse(txMachine* the)
2345
0
{
2346
0
  mxMutableTypedArrayDeclarations;
2347
0
  txInteger delta = dispatch->value.typedArray.dispatch->size;
2348
0
  if (length) {
2349
0
    txByte tmp;
2350
0
    txByte* first = buffer->value.reference->next->value.arrayBuffer.address + view->value.dataView.offset;
2351
0
    txByte* last = first + (length << dispatch->value.typedArray.dispatch->shift) - delta;
2352
0
    txInteger offset;
2353
0
    while (first < last) {
2354
0
      for (offset = 0; offset < delta; offset++) {
2355
0
        tmp = last[offset];
2356
0
        last[offset] = first[offset];
2357
0
        first[offset] = tmp;
2358
0
      }
2359
0
      first += delta;
2360
0
      last -= delta;
2361
0
    }
2362
0
    mxMeterSome(length * 4);
2363
0
  }
2364
0
  mxResult->kind = mxThis->kind;
2365
0
  mxResult->value = mxThis->value;
2366
0
}
2367
2368
void fx_TypedArray_prototype_set(txMachine* the)
2369
0
{
2370
0
  mxMutableTypedArrayDeclarations;
2371
0
  txSlot* source = fxArgToInstance(the, 0);
2372
0
  if (source->next && (source->next->kind == XS_TYPED_ARRAY_KIND)) {
2373
0
    txSlot* data = buffer->value.reference->next;
2374
0
    txInteger delta = dispatch->value.typedArray.dispatch->size;
2375
0
    txInteger target = fxArgToByteLength(the, 1, 0);
2376
0
    txInteger offset = view->value.dataView.offset + (target * delta);  
2377
0
    length = fxCheckDataViewSize(the, view, buffer, XS_MUTABLE) >> dispatch->value.typedArray.dispatch->shift;
2378
0
    txSlot* sourceDispatch = source->next;
2379
0
    txSlot* sourceView = sourceDispatch->next;
2380
0
    txSlot* sourceBuffer = sourceView->next;
2381
0
    txU2 shift = sourceDispatch->value.typedArray.dispatch->shift;
2382
0
    txInteger sourceLength = fxCheckDataViewSize(the, sourceView, sourceBuffer, XS_IMMUTABLE) >> shift;
2383
0
    txInteger sourceOffset = sourceView->value.dataView.offset; 
2384
0
    txSlot* sourceData = sourceBuffer->value.reference->next;
2385
0
    txInteger limit = offset + (sourceLength * delta);
2386
0
    if ((length - sourceLength < target))   //@@ target can never be negative?
2387
0
      mxRangeError("invalid offset");
2388
0
    if (data == sourceData) {
2389
0
      txSlot* resultBuffer;
2390
0
      mxPush(mxArrayBufferConstructor);
2391
0
      mxNew();
2392
0
      mxPushInteger(sourceLength << shift);
2393
0
      mxRunCount(1);
2394
0
      resultBuffer = the->stack->value.reference->next;
2395
0
      c_memcpy(resultBuffer->value.arrayBuffer.address, sourceData->value.arrayBuffer.address + sourceOffset, sourceLength << shift);
2396
0
      sourceData = resultBuffer;
2397
0
      sourceOffset = 0;
2398
0
    }
2399
0
    else 
2400
0
      mxPushUndefined();
2401
0
    if (dispatch->value.typedArray.dispatch == sourceDispatch->value.typedArray.dispatch) {
2402
0
      if (data->value.arrayBuffer.address == C_NULL)
2403
0
        mxTypeError("detached buffer");
2404
0
      c_memcpy(data->value.arrayBuffer.address + offset, sourceData->value.arrayBuffer.address + sourceOffset, limit - offset);
2405
0
      mxMeterSome(((txU4)(limit - offset)) * 2);
2406
0
    }
2407
0
    else {
2408
0
      txInteger sourceDelta = 1 << shift;
2409
0
      mxPushUndefined();
2410
0
      while (offset < limit) {
2411
0
        (*sourceDispatch->value.typedArray.dispatch->getter)(the, sourceData, sourceOffset, the->stack, EndianNative);
2412
0
        (*dispatch->value.typedArray.dispatch->coerce)(the, the->stack);
2413
0
                if (data->value.arrayBuffer.address == C_NULL)
2414
0
                    mxTypeError("detached buffer");
2415
0
        (*dispatch->value.typedArray.dispatch->setter)(the, data, offset, the->stack, EndianNative);
2416
0
        sourceOffset += sourceDelta;
2417
0
        offset += delta;
2418
0
      }
2419
0
      mxPop();
2420
0
    }
2421
0
    mxPop();
2422
0
  }
2423
0
  else {
2424
0
    txNumber targetOffset = 0, targetLength, sourceLength;
2425
0
    txSlot *arg = mxArgv(1);
2426
0
    if ((mxArgc > 1) && (arg->kind != XS_UNDEFINED_KIND)) {
2427
0
      targetOffset = c_trunc(fxToNumber(the, arg));
2428
0
      if (c_isnan(targetOffset))
2429
0
        targetOffset = 0;
2430
0
      if (targetOffset < 0) 
2431
0
        mxRangeError("offset < 0");
2432
0
    }
2433
0
    if (fxIsDataViewOutOfBound(the, view, buffer))
2434
0
      mxTypeError("out of bound buffer");
2435
0
    targetLength = fxCheckDataViewSize(the, view, buffer, XS_MUTABLE) >> dispatch->value.typedArray.dispatch->shift;
2436
0
    mxPushSlot(mxArgv(0));
2437
0
    mxGetID(mxID(_length));
2438
0
    sourceLength = fxToLength(the, the->stack);
2439
0
    mxPop();
2440
0
    if (sourceLength + targetOffset > targetLength)
2441
0
      mxRangeError("invalid offset");
2442
0
    txIndex count = (txIndex)sourceLength;
2443
0
    txIndex index = 0;
2444
0
    txIndex delta = (txIndex)targetOffset;
2445
0
    while (index < count) {
2446
0
      mxPushSlot(mxArgv(0));
2447
0
      mxGetIndex(index);
2448
0
      mxPushSlot(mxThis);
2449
0
      mxPushInteger(delta + index);
2450
0
      mxSetAt();
2451
0
      mxPop();
2452
0
      index++;
2453
0
      mxCheckMetering();
2454
0
    } 
2455
0
  }
2456
0
}
2457
2458
void fx_TypedArray_prototype_slice(txMachine* the)
2459
0
{
2460
0
  mxTypedArrayDeclarations;
2461
0
  txInteger delta = dispatch->value.typedArray.dispatch->size;
2462
0
  txInteger start = fxArgToIndexInteger(the, 0, 0, length);
2463
0
  txInteger end = fxArgToIndexInteger(the, 1, length, length);
2464
0
  txInteger count = (end > start) ? end - start : 0;
2465
0
  txInteger index = 0;
2466
0
  fxCreateTypedArraySpecies(the);
2467
0
  mxPushNumber(count);
2468
0
  mxRunCount(1);
2469
0
  mxPullSlot(mxResult);
2470
0
  {
2471
0
    mxResultTypedArrayDeclarations;
2472
0
    fxCheckTypedArrayContentType(the, resultDispatch, dispatch);
2473
0
    if (resultLength < count)
2474
0
      mxTypeError("result: too small TypedArray instance");
2475
0
    if (count) {
2476
0
      length = fxCheckDataViewSize(the, view, buffer, XS_IMMUTABLE) >> dispatch->value.typedArray.dispatch->shift;
2477
0
      if ((end <= length) && (resultDispatch->value.typedArray.dispatch->constructorID == dispatch->value.typedArray.dispatch->constructorID) && (resultBuffer->value.reference != buffer->value.reference)) {
2478
0
        txInteger shift = dispatch->value.typedArray.dispatch->shift;
2479
0
        txSlot* data = buffer->value.reference->next;
2480
0
        txSlot* resultData = resultBuffer->value.reference->next;
2481
0
        txByte* address = data->value.arrayBuffer.address;
2482
0
        txByte* resultAddress = resultData->value.arrayBuffer.address;
2483
0
        address += view->value.dataView.offset;
2484
0
        resultAddress += resultView->value.dataView.offset;
2485
0
        c_memmove(resultAddress, address + (start << shift), count << shift);
2486
0
        mxMeterSome(((txU4)(count)) * 2);
2487
0
      }
2488
0
      else {
2489
0
        mxPushUndefined();
2490
0
        while ((start < length) && (start < end)) {
2491
0
          (*dispatch->value.typedArray.dispatch->getter)(the, buffer->value.reference->next, view->value.dataView.offset + (start * delta), the->stack, EndianNative);
2492
0
          (*resultDispatch->value.typedArray.dispatch->coerce)(the, the->stack);
2493
0
          (*resultDispatch->value.typedArray.dispatch->setter)(the, resultBuffer->value.reference->next, resultView->value.dataView.offset + (index << resultDispatch->value.typedArray.dispatch->shift), the->stack, EndianNative);
2494
0
          start++;
2495
0
          index++;
2496
0
        }
2497
0
        mxPop();
2498
0
      }
2499
0
    }
2500
0
  }
2501
0
}
2502
2503
void fx_TypedArray_prototype_some(txMachine* the)
2504
0
{
2505
0
  mxTypedArrayDeclarations;
2506
0
  txSlot* function = fxArgToCallback(the, 0);
2507
0
  txInteger index = 0;
2508
0
  mxResult->kind = XS_BOOLEAN_KIND;
2509
0
  mxResult->value.boolean = 0;
2510
0
  while (index < length) {
2511
0
    fxCallTypedArrayItem(the, function, dispatch, view, buffer->value.reference->next, index, C_NULL);
2512
0
    mxResult->value.boolean = fxToBoolean(the, the->stack++);
2513
0
    if (mxResult->value.boolean)
2514
0
      break;
2515
0
    index++;
2516
0
  }
2517
0
}
2518
2519
void fx_TypedArray_prototype_sort(txMachine* the)
2520
0
{
2521
0
  mxMutableTypedArrayDeclarations;
2522
0
  txSlot* data = buffer->value.reference->next;
2523
0
  txInteger delta = dispatch->value.typedArray.dispatch->size;
2524
0
  txSlot* function = C_NULL;
2525
0
  if (mxArgc > 0) {
2526
0
    txSlot* slot = mxArgv(0);
2527
0
    if (slot->kind != XS_UNDEFINED_KIND) {
2528
0
      if (fxIsCallable(the, slot))
2529
0
        function = slot;
2530
0
      else
2531
0
        mxTypeError("compare: not a function");
2532
0
    }
2533
0
  }
2534
0
  if (function)
2535
0
    fxSortArrayItems(the, function, C_NULL, length, mxThis);
2536
0
  else
2537
0
    c_qsort(data->value.arrayBuffer.address + view->value.dataView.offset, length, delta, dispatch->value.typedArray.dispatch->compare);
2538
0
  mxResult->kind = mxThis->kind;
2539
0
  mxResult->value = mxThis->value;
2540
0
}
2541
2542
void fx_TypedArray_prototype_subarray(txMachine* the)
2543
0
{
2544
0
  txSlot* instance = fxCheckTypedArrayInstance(the, mxThis);
2545
0
  txSlot* dispatch = instance->next;
2546
0
  txSlot* view = dispatch->next;
2547
0
  txSlot* buffer = view->next;
2548
0
    txU2 shift = dispatch->value.typedArray.dispatch->shift;
2549
0
    txInteger length = fxGetDataViewSize(the, view, buffer) >> shift;
2550
0
  txInteger start = fxArgToIndexInteger(the, 0, 0, length);
2551
0
  if ((view->value.dataView.size < 0) && ((mxArgc < 2) || mxIsUndefined(mxArgv(1)))) {
2552
0
    fxCreateTypedArraySpecies(the);
2553
0
    mxPushSlot(buffer);
2554
0
    mxPushInteger(view->value.dataView.offset + (start << shift));
2555
0
    mxRunCount(2);
2556
0
  }
2557
0
  else {
2558
0
    txInteger stop = fxArgToIndexInteger(the, 1, length, length);
2559
0
    if (stop < start) 
2560
0
      stop = start;
2561
0
    fxCreateTypedArraySpecies(the);
2562
0
    mxPushSlot(buffer);
2563
0
    mxPushInteger(view->value.dataView.offset + (start << shift));
2564
0
    mxPushInteger(stop - start);
2565
0
    mxRunCount(3);
2566
0
  }
2567
0
  mxPullSlot(mxResult);
2568
0
  instance = fxCheckTypedArrayInstance(the, mxResult);
2569
0
  fxCheckTypedArrayContentType(the, instance->next, dispatch);
2570
0
}
2571
2572
void fx_TypedArray_prototype_toLocaleString(txMachine* the)
2573
0
{
2574
0
  mxTypedArrayDeclarations;
2575
0
  txInteger index = 0;
2576
0
  txString string;
2577
0
  txSlot* list = fxNewInstance(the);
2578
0
  txSlot* slot = list;
2579
0
  txBoolean comma = 0;
2580
0
  txInteger size = 0;
2581
0
  mxPushStringX(",");
2582
0
  the->stack->kind += XS_KEY_KIND - XS_STRING_KIND;
2583
0
  the->stack->value.key.sum = 1;
2584
0
  while (index < length) {
2585
0
    if (comma) {
2586
0
      slot = fxNextSlotProperty(the, slot, the->stack, XS_NO_ID, XS_NO_FLAG);
2587
0
      size += slot->value.key.sum;
2588
0
    }
2589
0
    else
2590
0
      comma = 1;
2591
0
    mxPushSlot(mxThis);
2592
0
    mxGetIndex(index);
2593
0
    if ((the->stack->kind != XS_UNDEFINED_KIND) && (the->stack->kind != XS_NULL_KIND)) {
2594
0
      mxDub();
2595
0
      mxGetID(mxID(_toLocaleString));
2596
0
      mxCall();
2597
0
      mxRunCount(0);
2598
0
      slot = fxNextSlotProperty(the, slot, the->stack, XS_NO_ID, XS_NO_FLAG);
2599
0
      string = fxToString(the, slot);
2600
0
      slot->kind += XS_KEY_KIND - XS_STRING_KIND;
2601
0
      slot->value.key.sum = mxStringLength(string);
2602
0
      size = fxAddChunkSizes(the, size, slot->value.key.sum);
2603
0
    }
2604
0
    mxPop();
2605
0
    index++;
2606
0
  }
2607
0
  string = mxResult->value.string = fxNewChunk(the, fxAddChunkSizes(the, size, 1));
2608
0
  slot = list->next;
2609
0
  while (slot) {
2610
0
    c_memcpy(string, slot->value.key.string, slot->value.key.sum);
2611
0
    string += slot->value.key.sum;
2612
0
    slot = slot->next;
2613
0
  }
2614
0
  *string = 0;
2615
0
  mxResult->kind = XS_STRING_KIND;
2616
0
  mxPop();
2617
0
}
2618
2619
void fx_TypedArray_prototype_toReversed(txMachine* the)
2620
0
{
2621
0
  mxTypedArrayDeclarations;
2622
0
  txInteger delta = dispatch->value.typedArray.dispatch->size;
2623
0
  txSlot* constructor = &the->stackIntrinsics[-1 - (txInteger)dispatch->value.typedArray.dispatch->constructorID];
2624
0
  mxPushSlot(constructor);
2625
0
  mxNew();
2626
0
  mxPushInteger(length);
2627
0
  mxRunCount(1);
2628
0
  mxPullSlot(mxResult);
2629
0
  if (length) {
2630
0
    mxResultTypedArrayDeclarations;
2631
0
    txByte* base = buffer->value.reference->next->value.arrayBuffer.address + view->value.dataView.offset;
2632
0
    txByte* from = base + (resultLength << dispatch->value.typedArray.dispatch->shift) - delta;
2633
0
    txByte* to = resultBuffer->value.reference->next->value.arrayBuffer.address;
2634
0
    while (from >= base) {
2635
0
      txInteger offset;
2636
0
      for (offset = 0; offset < delta; offset++)
2637
0
        *to++ = *from++;
2638
0
      from -= delta << 1;
2639
0
    }
2640
0
    mxMeterSome((txU4)length * 4);
2641
0
  }
2642
0
}
2643
2644
void fx_TypedArray_prototype_toSorted(txMachine* the)
2645
0
{
2646
0
  mxTypedArrayDeclarations;
2647
0
  txInteger delta = dispatch->value.typedArray.dispatch->size;
2648
0
  txSlot* constructor = &the->stackIntrinsics[-1 - (txInteger)dispatch->value.typedArray.dispatch->constructorID];
2649
0
  txSlot* function = C_NULL;
2650
0
  if (mxArgc > 0) {
2651
0
    txSlot* slot = mxArgv(0);
2652
0
    if (slot->kind != XS_UNDEFINED_KIND) {
2653
0
      if (fxIsCallable(the, slot))
2654
0
        function = slot;
2655
0
      else
2656
0
        mxTypeError("compare: not a function");
2657
0
    }
2658
0
  }
2659
0
  mxPushSlot(constructor);
2660
0
  mxNew();
2661
0
  mxPushInteger(length);
2662
0
  mxRunCount(1);
2663
0
  mxPullSlot(mxResult);
2664
0
  if (function)
2665
0
    fxSortArrayItems(the, function, C_NULL, length, mxResult);
2666
0
  else {
2667
0
    mxResultTypedArrayDeclarations;
2668
0
    txByte* from = buffer->value.reference->next->value.arrayBuffer.address + view->value.dataView.offset;
2669
0
    txByte* to = resultBuffer->value.reference->next->value.arrayBuffer.address;
2670
0
    c_memcpy(to, from,  resultLength << dispatch->value.typedArray.dispatch->shift);    
2671
0
    c_qsort(to, length, delta, dispatch->value.typedArray.dispatch->compare);
2672
0
  }
2673
0
}
2674
2675
void fx_TypedArray_prototype_toStringTag_get(txMachine* the)
2676
0
{
2677
0
  if (mxThis->kind == XS_REFERENCE_KIND) {
2678
0
        txSlot* instance = mxThis->value.reference;
2679
0
        txSlot* slot = instance->next;
2680
0
    if (slot && (slot->flag & XS_INTERNAL_FLAG) && (slot->kind == XS_TYPED_ARRAY_KIND)) {
2681
0
      txTypeDispatch *dispatch = instance->next->value.typedArray.dispatch;
2682
0
      txSlot* key = fxGetKey(the, mxID(dispatch->constructorID));
2683
0
      if (key->kind == XS_KEY_X_KIND)
2684
0
        mxResult->kind = XS_STRING_X_KIND;
2685
0
      else
2686
0
        mxResult->kind = XS_STRING_KIND;
2687
0
      mxResult->value.string = key->value.key.string;
2688
0
    }
2689
0
  }
2690
0
}
2691
2692
void fx_TypedArray_prototype_values(txMachine* the)
2693
0
{
2694
0
  txSlot* instance = fxCheckTypedArrayInstance(the, mxThis);
2695
0
  txSlot* dispatch = instance->next;
2696
0
  txSlot* view = dispatch->next;
2697
0
  txSlot* buffer = view->next;
2698
0
  txSlot* property;
2699
0
  fxCheckDataViewSize(the, view, buffer, XS_IMMUTABLE);
2700
0
  mxPush(mxArrayIteratorPrototype);
2701
0
  property = fxLastProperty(the, fxNewIteratorInstance(the, mxThis, mxID(_Array)));
2702
0
  property = fxNextIntegerProperty(the, property, 0, XS_NO_ID, XS_INTERNAL_FLAG);
2703
0
  mxPullSlot(mxResult);
2704
0
}
2705
2706
void fx_TypedArray_prototype_with(txMachine* the)
2707
0
{
2708
0
  mxTypedArrayDeclarations;
2709
0
  txSlot* constructor = &the->stackIntrinsics[-1 - (txInteger)dispatch->value.typedArray.dispatch->constructorID];
2710
0
  txInteger index = (txInteger)fxArgToRelativeIndex(the, 0, 0, length), count, i;
2711
0
  txSlot* value;
2712
0
  if (mxArgc > 1)
2713
0
    mxPushSlot(mxArgv(1));
2714
0
  else
2715
0
    mxPushUndefined();
2716
0
  value = the->stack; 
2717
0
  (*dispatch->value.typedArray.dispatch->coerce)(the, value);
2718
0
  count = fxGetDataViewSize(the, view, buffer) >> dispatch->value.typedArray.dispatch->shift;
2719
0
  if ((index < 0) || (count <= index))
2720
0
    mxRangeError("invalid index");
2721
0
  mxPushSlot(constructor);
2722
0
  mxNew();
2723
0
  mxPushInteger(length);
2724
0
  mxRunCount(1);
2725
0
  mxPullSlot(mxResult);
2726
0
  i = 0;
2727
0
  while (i < index) {
2728
0
    mxPushSlot(mxThis);
2729
0
    mxPushInteger(i);
2730
0
    mxGetAt();
2731
0
    mxPushSlot(mxResult);
2732
0
    mxPushInteger(i);
2733
0
    mxSetAt();
2734
0
    mxPop();
2735
0
    i++;
2736
0
    mxCheckMetering();
2737
0
  }
2738
0
  mxPushSlot(value);
2739
0
  mxPushSlot(mxResult);
2740
0
  mxPushInteger(i);
2741
0
  mxSetAt();
2742
0
  mxPop();
2743
0
  i++;
2744
0
  while (i < length) {
2745
0
    mxPushSlot(mxThis);
2746
0
    mxPushInteger(i);
2747
0
    mxGetAt();
2748
0
    mxPushSlot(mxResult);
2749
0
    mxPushInteger(i);
2750
0
    mxSetAt();
2751
0
    mxPop();
2752
0
    i++;
2753
0
    mxCheckMetering();
2754
0
  }
2755
0
  mxPop();
2756
0
}
2757
2758
#if mxBigEndian
2759
  #define mxEndianFloat16_BtoN(a) (a)
2760
  #define mxEndianFloat32_BtoN(a) (a)
2761
  #define mxEndianFloat64_BtoN(a) (a)
2762
  #define mxEndianS64_BtoN(a) (a)
2763
  #define mxEndianU64_BtoN(a) (a)
2764
  #define mxEndianS32_BtoN(a) (a)
2765
  #define mxEndianU32_BtoN(a) (a)
2766
  #define mxEndianS16_BtoN(a) (a)
2767
  #define mxEndianU16_BtoN(a) (a)
2768
2769
  #define mxEndianFloat16_NtoB(a) (a)
2770
  #define mxEndianFloat32_NtoB(a) (a)
2771
  #define mxEndianFloat64_NtoB(a) (a)
2772
  #define mxEndianS64_NtoB(a) (a)
2773
  #define mxEndianU64_NtoB(a) (a)
2774
  #define mxEndianS32_NtoB(a) (a)
2775
  #define mxEndianU32_NtoB(a) (a)
2776
  #define mxEndianS16_NtoB(a) (a)
2777
  #define mxEndianU16_NtoB(a) (a)
2778
#else
2779
0
  #define mxEndianFloat16_LtoN(a) (a)
2780
0
  #define mxEndianFloat32_LtoN(a) (a)
2781
0
  #define mxEndianFloat64_LtoN(a) (a)
2782
0
  #define mxEndianS64_LtoN(a) (a)
2783
0
  #define mxEndianU64_LtoN(a) (a)
2784
0
  #define mxEndianS32_LtoN(a) (a)
2785
0
  #define mxEndianU32_LtoN(a) (a)
2786
0
  #define mxEndianS16_LtoN(a) (a)
2787
0
  #define mxEndianU16_LtoN(a) (a)
2788
2789
  #define mxEndianFloat16_NtoL(a) (a)
2790
  #define mxEndianFloat32_NtoL(a) (a)
2791
  #define mxEndianFloat64_NtoL(a) (a)
2792
  #define mxEndianS64_NtoL(a) (a)
2793
  #define mxEndianU64_NtoL(a) (a)
2794
  #define mxEndianS32_NtoL(a) (a)
2795
  #define mxEndianU32_NtoL(a) (a)
2796
  #define mxEndianS16_NtoL(a) (a)
2797
  #define mxEndianU16_NtoL(a) (a)
2798
#endif
2799
2800
#if mxLittleEndian
2801
  #define mxEndianFloat16_BtoN(a) (mxEndianFloat16_Swap(a))
2802
0
  #define mxEndianFloat32_BtoN(a) (mxEndianFloat32_Swap(a))
2803
0
  #define mxEndianFloat64_BtoN(a) (mxEndianFloat64_Swap(a))
2804
0
  #define mxEndianS64_BtoN(a) ((txS8) mxEndian64_Swap(a))
2805
0
  #define mxEndianU64_BtoN(a) ((txU8) mxEndian64_Swap(a))
2806
0
  #define mxEndianS32_BtoN(a) ((txS4) mxEndian32_Swap(a))
2807
0
  #define mxEndianU32_BtoN(a) ((txU4) mxEndian32_Swap(a))
2808
0
  #define mxEndianS16_BtoN(a) ((txS2) mxEndian16_Swap(a))
2809
0
  #define mxEndianU16_BtoN(a) ((txU2) mxEndian16_Swap(a))
2810
2811
0
  #define mxEndianFloat16_NtoB(a) (mxEndianFloat16_Swap(a))
2812
0
  #define mxEndianFloat32_NtoB(a) (mxEndianFloat32_Swap(a))
2813
0
  #define mxEndianFloat64_NtoB(a) (mxEndianFloat64_Swap(a))
2814
0
  #define mxEndianS64_NtoB(a) ((txS8) mxEndian64_Swap(a))
2815
0
  #define mxEndianU64_NtoB(a) ((txU8) mxEndian64_Swap(a))
2816
0
  #define mxEndianS32_NtoB(a) ((txS4) mxEndian32_Swap(a))
2817
0
  #define mxEndianU32_NtoB(a) ((txU4) mxEndian32_Swap(a))
2818
0
  #define mxEndianS16_NtoB(a) ((txS2) mxEndian16_Swap(a))
2819
0
  #define mxEndianU16_NtoB(a) ((txU2) mxEndian16_Swap(a))
2820
#else
2821
  #define mxEndianFloat16_LtoN(a) (mxEndianFloat16_Swap(a))
2822
  #define mxEndianFloat32_LtoN(a) (mxEndianFloat32_Swap(a))
2823
  #define mxEndianFloat64_LtoN(a) (mxEndianFloat64_Swap(a))
2824
  #define mxEndianS64_LtoN(a) ((txS8) mxEndian64_Swap(a))
2825
  #define mxEndianU64_LtoN(a) ((txU8) mxEndian64_Swap(a))
2826
  #define mxEndianS32_LtoN(a) ((txS4) mxEndian32_Swap(a))
2827
  #define mxEndianU32_LtoN(a) ((txU4) mxEndian32_Swap(a))
2828
  #define mxEndianS16_LtoN(a) ((txS2) mxEndian16_Swap(a))
2829
  #define mxEndianU16_LtoN(a) ((txU2) mxEndian16_Swap(a))
2830
2831
  #define mxEndianFloat16_NtoL(a) (mxEndianFloat16_Swap(a))
2832
  #define mxEndianFloat32_NtoL(a) (mxEndianFloat32_Swap(a))
2833
  #define mxEndianFloat64_NtoL(a) (mxEndianFloat64_Swap(a))
2834
  #define mxEndianS64_NtoL(a) ((txS8) mxEndian64_Swap(a))
2835
  #define mxEndianU64_NtoL(a) ((txU8) mxEndian64_Swap(a))
2836
  #define mxEndianS32_NtoL(a) ((txS4) mxEndian32_Swap(a))
2837
  #define mxEndianU32_NtoL(a) ((txU4) mxEndian32_Swap(a))
2838
  #define mxEndianS16_NtoL(a) ((txS2) mxEndian16_Swap(a))
2839
  #define mxEndianU16_NtoL(a) ((txU2) mxEndian16_Swap(a))
2840
#endif
2841
2842
#if defined(__GNUC__) || defined(__llvm__)
2843
0
  #define mxEndian16_Swap(a) __builtin_bswap16(a)
2844
#else
2845
  static txU2 mxEndian16_Swap(txU2 a)
2846
  {
2847
    txU2 b;
2848
    txU1 *p1 = (txU1 *) &a, *p2 = (txU1 *) &b;
2849
    int i;
2850
    for (i = 0; i < 2; i++)
2851
      p2[i] = p1[1 - i];
2852
    return b;
2853
  }
2854
#endif
2855
2856
#if defined(__GNUC__) || defined(__llvm__)
2857
0
  #define mxEndian32_Swap(a) __builtin_bswap32(a)
2858
#else
2859
  static txU4 mxEndian32_Swap(txU4 a)
2860
  {
2861
    txU4 b;
2862
    txU1 *p1 = (txU1 *) &a, *p2 = (txU1 *) &b;
2863
    int i;
2864
    for (i = 0; i < 4; i++)
2865
      p2[i] = p1[3 - i];
2866
    return b;
2867
  }
2868
#endif
2869
2870
#if defined(__GNUC__) || defined(__llvm__)
2871
0
  #define mxEndian64_Swap(a) __builtin_bswap64(a)
2872
#else
2873
  static txU8 mxEndian64_Swap(txU8 a)
2874
  {
2875
    txU8 b;
2876
    txU1 *p1 = (txU1 *) &a, *p2 = (txU1 *) &b;
2877
    int i;
2878
    for (i = 0; i < 8; i++)
2879
      p2[i] = p1[7 - i];
2880
    return b;
2881
  }
2882
#endif
2883
2884
0
#define toNative(size, endian) mxEndian##size##_##endian##toN
2885
0
#define fromNative(size, endian) mxEndian##size##_Nto##endian
2886
0
#define IMPORT(size) (endian == EndianBig ? toNative(size, B)(value) : endian == EndianLittle ? toNative(size, L)(value) : (value))
2887
0
#define EXPORT(size) (endian == EndianBig ? fromNative(size, B)(value) : endian == EndianLittle ? toNative(size, L)(value) : (value))
2888
2889
int fxBigInt64Compare(const void* p, const void* q)
2890
0
{
2891
0
  txS8 a = *((txS8*)p);
2892
0
  txS8 b = *((txS8*)q);
2893
0
  return (a < b) ? -1 : (a > b) ? 1 : 0;
2894
0
}
2895
2896
void fxBigInt64Getter(txMachine* the, txSlot* data, txInteger offset, txSlot* slot, int endian)
2897
0
{
2898
0
  txS8 value;
2899
#ifdef mxMisalignedSettersCrash
2900
  c_memcpy(&value, data->value.arrayBuffer.address + offset, sizeof(value));
2901
#else
2902
0
  value = *((txS8*)(data->value.arrayBuffer.address + offset));
2903
0
#endif
2904
0
  value = IMPORT(S64);
2905
0
  fxFromBigInt64(the, slot, value);
2906
0
  mxMeterOne();
2907
0
}
2908
2909
void fxBigInt64Setter(txMachine* the, txSlot* data, txInteger offset, txSlot* slot, int endian)
2910
0
{
2911
0
  txS8 value = (txS8)fxToBigInt64(the, slot);
2912
#ifdef mxMisalignedSettersCrash
2913
  value = EXPORT(S64);
2914
  c_memcpy(data->value.arrayBuffer.address + offset, &value, sizeof(txS8));
2915
#else
2916
0
  *((txS8*)(data->value.arrayBuffer.address + offset)) = EXPORT(S64);
2917
0
#endif
2918
0
  mxMeterOne();
2919
0
}
2920
2921
int fxBigUint64Compare(const void* p, const void* q)
2922
0
{
2923
0
  txU8 a = *((txU8*)p);
2924
0
  txU8 b = *((txU8*)q);
2925
0
  return (a < b) ? -1 : (a > b) ? 1 : 0;
2926
0
}
2927
2928
void fxBigUint64Getter(txMachine* the, txSlot* data, txInteger offset, txSlot* slot, int endian)
2929
0
{
2930
0
  txU8 value;
2931
#ifdef mxMisalignedSettersCrash
2932
  c_memcpy(&value, data->value.arrayBuffer.address + offset, sizeof(value));
2933
#else
2934
0
  value = *((txU8*)(data->value.arrayBuffer.address + offset));
2935
0
#endif
2936
0
  value = IMPORT(U64);
2937
0
  fxFromBigUint64(the, slot, value);
2938
0
  mxMeterOne();
2939
0
}
2940
2941
void fxBigUint64Setter(txMachine* the, txSlot* data, txInteger offset, txSlot* slot, int endian)
2942
0
{
2943
0
  txU8 value = (txU8)fxToBigUint64(the, slot);
2944
#ifdef mxMisalignedSettersCrash
2945
  value = EXPORT(U64);
2946
  c_memcpy(data->value.arrayBuffer.address + offset, &value, sizeof(txU8));
2947
#else
2948
0
  *((txU8*)(data->value.arrayBuffer.address + offset)) = EXPORT(U64);
2949
0
#endif
2950
0
  mxMeterOne();
2951
0
}
2952
2953
#if mxFloat16
2954
2955
#ifdef mxUseFloat16
2956
typedef _Float16 txFloat16;
2957
#define mxFloat16to64(X) ((double)X) 
2958
#define mxFloat64to16(X) ((_Float16)X) 
2959
#else
2960
typedef uint16_t txFloat16;
2961
0
#define mxFloat16to64(X) fxFloat16to64(X) 
2962
0
#define mxFloat64to16(X) fxFloat64to16(X) 
2963
2964
// adapted from https://half.sourceforge.net
2965
2966
static double fxFloat16to64(uint16_t value)
2967
0
{
2968
0
  uint32_t hi, abs;
2969
0
  uint64_t dbits;
2970
0
  double out;
2971
  
2972
0
  hi = (uint32_t)(value & 0x8000) << 16;
2973
0
  abs = value & 0x7FFF;
2974
0
  if (abs) {
2975
0
    hi |= 0x3F000000 << (abs >= 0x7C00);
2976
0
    for (; abs < 0x400; abs <<= 1, hi -= 0x100000);
2977
0
    hi += abs << 10;
2978
0
  }
2979
0
  dbits = (uint64_t)(hi) << 32;
2980
0
  c_memcpy(&out, &dbits, sizeof(double));
2981
0
  return out;
2982
0
}
2983
2984
0
static uint32_t fxRoundFloat16(uint32_t value, uint32_t g, uint32_t s) {
2985
0
  return value + (g & (s | value));
2986
0
}
2987
2988
static uint16_t fxFloat64to16(double value)
2989
0
{
2990
0
  uint64_t dbits;
2991
0
  uint32_t hi, lo, sign;
2992
  
2993
0
  c_memcpy(&dbits, &value, sizeof(double));
2994
0
  hi = dbits >> 32;
2995
0
  lo = dbits & 0xFFFFFFFF;
2996
0
  sign = (hi >> 16) & 0x8000;
2997
0
  hi &= 0x7FFFFFFF;
2998
0
  if (hi >= 0x7FF00000)
2999
0
    return sign | 0x7C00 | ((dbits & 0xFFFFFFFFFFFFF) ? (0x200 | ((hi >> 10) & 0x3FF)) : 0);
3000
0
  if (hi >= 0x40F00000)
3001
0
    return sign | 0x7C00;
3002
0
  if (hi >= 0x3F100000)
3003
0
    return fxRoundFloat16(sign | (((hi >> 20) - 1008) << 10) | ((hi >> 10) & 0x3FF), (hi >> 9) & 1, ((hi & 0x1FF) | lo) != 0);
3004
0
  if (hi >= 0x3E600000) {
3005
0
    int i = 1018 - (hi >> 20);
3006
0
    hi = (hi & 0xFFFFF) | 0x100000;
3007
0
    return fxRoundFloat16(sign | (hi >> (i + 1)), (hi >> i) & 1, ((hi & (((uint32_t)(1) << i) - 1)) | lo) != 0);
3008
0
  }
3009
0
  return sign;
3010
0
}
3011
3012
#endif
3013
3014
#if mxCanonicalNaN
3015
  #if mxBigEndian
3016
    static uint8_t gxCanonicalNaN16Bytes[2] = { 0x7E, 0 };
3017
  #else
3018
    static uint8_t gxCanonicalNaN16Bytes[2] = { 0, 0x7E };
3019
  #endif
3020
  static txFloat16* gxCanonicalNaN16 = (txFloat16*)gxCanonicalNaN16Bytes;
3021
#endif
3022
3023
static txFloat16 mxEndianFloat16_Swap(txFloat16 a)
3024
0
{
3025
0
#if defined(__GNUC__) || defined(__llvm__)
3026
0
  uint32_t result = __builtin_bswap16(*(uint16_t *)&a);
3027
0
  return *(txFloat16 *)&result;
3028
#else
3029
  txFloat16 b;
3030
  txU1 *p1 = (txU1 *) &a, *p2 = (txU1 *) &b;
3031
  int i;
3032
  for (i = 0; i < 2; i++)
3033
    p2[i] = p1[1 - i];
3034
  return b;
3035
#endif
3036
0
}
3037
3038
int fxFloat16Compare(const void* p, const void* q)
3039
0
{
3040
0
  double a = mxFloat16to64(*((txFloat16*)p));
3041
0
  double b = mxFloat16to64(*((txFloat16*)q));
3042
0
  if (c_isnan(a)) {
3043
0
    if (c_isnan(b)) 
3044
0
      return 0;
3045
0
    return 1;
3046
0
  }
3047
0
  if (c_isnan(b))
3048
0
    return -1;
3049
0
  if (a < b)
3050
0
    return -1;
3051
0
  if (a > b)
3052
0
    return 1;
3053
0
  if (a == 0) {
3054
0
    if (c_signbit(a)) {
3055
0
      if (c_signbit(b)) 
3056
0
        return 0;
3057
0
      return -1;
3058
0
    }
3059
0
    if (c_signbit(b))
3060
0
      return 1;
3061
0
  }
3062
0
  return 0;
3063
0
}
3064
3065
void fxFloat16Getter(txMachine* the, txSlot* data, txInteger offset, txSlot* slot, int endian)
3066
0
{
3067
0
  txFloat16 value;
3068
0
  slot->kind = XS_NUMBER_KIND;
3069
#ifdef mxMisalignedSettersCrash
3070
  c_memcpy(&value, data->value.arrayBuffer.address + offset, sizeof(value));
3071
#else
3072
0
  value = *((txFloat16*)(data->value.arrayBuffer.address + offset));
3073
0
#endif
3074
0
  slot->value.number = mxFloat16to64(IMPORT(Float16));
3075
0
  mxMeterOne();
3076
0
}
3077
3078
void fxFloat16Setter(txMachine* the, txSlot* data, txInteger offset, txSlot* slot, int endian)
3079
0
{
3080
0
#if mxCanonicalNaN
3081
0
  txFloat16 value = (c_isnan(slot->value.number)) ? *gxCanonicalNaN16 : mxFloat64to16(slot->value.number);
3082
#else
3083
  txFloat16 value = mxFloat64to16(slot->value.number);
3084
#endif
3085
#ifdef mxMisalignedSettersCrash
3086
  value = EXPORT(Float16);
3087
  c_memcpy(data->value.arrayBuffer.address + offset, &value, sizeof(value));
3088
#else
3089
0
  *((txFloat16*)(data->value.arrayBuffer.address + offset)) = EXPORT(Float16);
3090
0
#endif
3091
0
  mxMeterOne();
3092
0
}
3093
3094
void fx_Math_f16round(txMachine* the)
3095
0
{
3096
0
  txFloat16 value = mxFloat64to16((mxArgc < 1) ? C_NAN : fxToNumber(the, mxArgv(0)));
3097
0
  mxResult->kind = XS_NUMBER_KIND;
3098
0
  mxResult->value.number = mxFloat16to64(value);
3099
0
}
3100
3101
#endif
3102
3103
#if mxCanonicalNaN
3104
  #if mxBigEndian
3105
    static uint8_t gxCanonicalNaN32Bytes[4] = { 0x7F, 0xC0, 0, 0 };
3106
  #else
3107
    static uint8_t gxCanonicalNaN32Bytes[4] = { 0, 0, 0xC0, 0x7F };
3108
  #endif
3109
  static float* gxCanonicalNaN32 = (float*)gxCanonicalNaN32Bytes;
3110
#endif
3111
3112
static float mxEndianFloat32_Swap(float a)
3113
0
{
3114
0
#if defined(__GNUC__) || defined(__llvm__)
3115
0
  uint32_t result = __builtin_bswap32(*(uint32_t *)&a);
3116
0
  return *(float *)&result;
3117
#else
3118
  float b;
3119
  txU1 *p1 = (txU1 *) &a, *p2 = (txU1 *) &b;
3120
  int i;
3121
  for (i = 0; i < 4; i++)
3122
    p2[i] = p1[3 - i];
3123
  return b;
3124
#endif
3125
0
}
3126
3127
int fxFloat32Compare(const void* p, const void* q)
3128
0
{
3129
0
  float a = *((float*)p);
3130
0
  float b = *((float*)q);
3131
0
  if (c_isnan(a)) {
3132
0
    if (c_isnan(b)) 
3133
0
      return 0;
3134
0
    return 1;
3135
0
  }
3136
0
  if (c_isnan(b))
3137
0
    return -1;
3138
0
  if (a < b)
3139
0
    return -1;
3140
0
  if (a > b)
3141
0
    return 1;
3142
0
  if (a == 0) {
3143
0
    if (c_signbit(a)) {
3144
0
      if (c_signbit(b)) 
3145
0
        return 0;
3146
0
      return -1;
3147
0
    }
3148
0
    if (c_signbit(b))
3149
0
      return 1;
3150
0
  }
3151
0
  return 0;
3152
0
}
3153
3154
void fxFloat32Getter(txMachine* the, txSlot* data, txInteger offset, txSlot* slot, int endian)
3155
0
{
3156
0
  float value;
3157
0
  slot->kind = XS_NUMBER_KIND;
3158
#ifdef mxMisalignedSettersCrash
3159
  c_memcpy(&value, data->value.arrayBuffer.address + offset, sizeof(value));
3160
#else
3161
0
  value = *((float*)(data->value.arrayBuffer.address + offset));
3162
0
#endif
3163
0
  slot->value.number = IMPORT(Float32);
3164
0
  mxMeterOne();
3165
0
}
3166
3167
void fxFloat32Setter(txMachine* the, txSlot* data, txInteger offset, txSlot* slot, int endian)
3168
0
{
3169
0
#if mxCanonicalNaN
3170
0
  float value = (c_isnan(slot->value.number)) ? *gxCanonicalNaN32 : (float)slot->value.number;
3171
#else
3172
  float value = (float)slot->value.number;
3173
#endif
3174
#ifdef mxMisalignedSettersCrash
3175
  value = EXPORT(Float32);
3176
  c_memcpy(data->value.arrayBuffer.address + offset, &value, sizeof(value));
3177
#else
3178
0
  *((float*)(data->value.arrayBuffer.address + offset)) = EXPORT(Float32);
3179
0
#endif
3180
0
  mxMeterOne();
3181
0
}
3182
3183
#if mxCanonicalNaN
3184
  #if mxBigEndian
3185
    static uint8_t gxCanonicalNaN64Bytes[8] = { 0x7F, 0xF8, 0, 0, 0, 0, 0, 0 };
3186
  #else
3187
    static uint8_t gxCanonicalNaN64Bytes[8] = { 0, 0, 0, 0, 0, 0, 0xF8, 0x7F };
3188
  #endif
3189
  double* gxCanonicalNaN64 = (double*)gxCanonicalNaN64Bytes;
3190
#endif
3191
3192
static double mxEndianFloat64_Swap(double a)
3193
0
{
3194
0
#if defined(__GNUC__) || defined(__llvm__)
3195
0
  uint64_t result = __builtin_bswap64(*(uint64_t *)&a);
3196
0
  return *(double *)&result;
3197
#else
3198
  double b;
3199
  txU1 *p1 = (txU1 *) &a, *p2 = (txU1 *) &b;
3200
  int i;
3201
  for (i = 0; i < 8; i++)
3202
    p2[i] = p1[7 - i];
3203
  return b;
3204
#endif
3205
0
}
3206
3207
int fxFloat64Compare(const void* p, const void* q)
3208
0
{
3209
0
  double a = *((double*)p);
3210
0
  double b = *((double*)q);
3211
0
  if (c_isnan(a)) {
3212
0
    if (c_isnan(b)) 
3213
0
      return 0;
3214
0
    return 1;
3215
0
  }
3216
0
  if (c_isnan(b))
3217
0
    return -1;
3218
0
  if (a < b)
3219
0
    return -1;
3220
0
  if (a > b)
3221
0
    return 1;
3222
0
  if (a == 0) {
3223
0
    if (c_signbit(a)) {
3224
0
      if (c_signbit(b)) 
3225
0
        return 0;
3226
0
      return -1;
3227
0
    }
3228
0
    if (c_signbit(b))
3229
0
      return 1;
3230
0
  }
3231
0
  return 0;
3232
0
}
3233
3234
void fxFloat64Getter(txMachine* the, txSlot* data, txInteger offset, txSlot* slot, int endian)
3235
0
{
3236
0
  double value;
3237
0
  slot->kind = XS_NUMBER_KIND;
3238
#ifdef mxMisalignedSettersCrash
3239
  c_memcpy(&value, data->value.arrayBuffer.address + offset, sizeof(value));
3240
#else
3241
0
  value = *((double*)(data->value.arrayBuffer.address + offset));
3242
0
#endif
3243
0
  slot->value.number = IMPORT(Float64);
3244
0
  mxMeterOne();
3245
0
}
3246
3247
void fxFloat64Setter(txMachine* the, txSlot* data, txInteger offset, txSlot* slot, int endian)
3248
0
{
3249
0
#if mxCanonicalNaN
3250
0
  double value = (c_isnan(slot->value.number)) ? *gxCanonicalNaN32 : slot->value.number;
3251
#else
3252
  double value = slot->value.number;
3253
#endif
3254
#ifdef mxMisalignedSettersCrash
3255
  value = EXPORT(Float64);
3256
  c_memcpy(data->value.arrayBuffer.address + offset, &value, sizeof(value));
3257
#else
3258
0
  *((double*)(data->value.arrayBuffer.address + offset)) = EXPORT(Float64);
3259
0
#endif
3260
0
  mxMeterOne();
3261
0
}
3262
3263
void fxIntCoerce(txMachine* the, txSlot* slot)
3264
0
{
3265
0
  fxToInteger(the, slot);
3266
0
}
3267
3268
void fxUintCoerce(txMachine* the, txSlot* slot)
3269
0
{
3270
0
  fxToUnsigned(the, slot);
3271
0
}
3272
3273
int fxInt8Compare(const void* p, const void* q)
3274
0
{
3275
0
  txS1 a = *((txS1*)p);
3276
0
  txS1 b = *((txS1*)q);
3277
0
  return (a < b) ? -1 : (a > b) ? 1 : 0;
3278
0
}
3279
3280
void fxInt8Getter(txMachine* the, txSlot* data, txInteger offset, txSlot* slot, int endian)
3281
0
{
3282
0
  slot->kind = XS_INTEGER_KIND;
3283
0
  slot->value.integer = *((txS1*)(data->value.arrayBuffer.address + offset));
3284
0
  mxMeterOne();
3285
0
}
3286
3287
void fxInt8Setter(txMachine* the, txSlot* data, txInteger offset, txSlot* slot, int endian)
3288
0
{
3289
0
  *((txS1*)(data->value.arrayBuffer.address + offset)) = (txS1)slot->value.integer;
3290
0
  mxMeterOne();
3291
0
}
3292
3293
int fxInt16Compare(const void* p, const void* q)
3294
0
{
3295
0
  txS2 a = *((txS2*)p);
3296
0
  txS2 b = *((txS2*)q);
3297
0
  return (a < b) ? -1 : (a > b) ? 1 : 0;
3298
0
}
3299
3300
void fxInt16Getter(txMachine* the, txSlot* data, txInteger offset, txSlot* slot, int endian)
3301
0
{
3302
0
  txS2 value;
3303
0
  slot->kind = XS_INTEGER_KIND;
3304
#ifdef mxMisalignedSettersCrash
3305
  c_memcpy(&value, data->value.arrayBuffer.address + offset, sizeof(value));
3306
#else
3307
0
  value = *((txS2*)(data->value.arrayBuffer.address + offset));
3308
0
#endif
3309
0
  slot->value.integer = IMPORT(S16);
3310
0
  mxMeterOne();
3311
0
}
3312
3313
void fxInt16Setter(txMachine* the, txSlot* data, txInteger offset, txSlot* slot, int endian)
3314
0
{
3315
0
  txS2 value = (txS2)slot->value.integer;
3316
#ifdef mxMisalignedSettersCrash
3317
  value = EXPORT(S16);
3318
  c_memcpy(data->value.arrayBuffer.address + offset, &value, sizeof(txS2));
3319
#else
3320
0
  *((txS2*)(data->value.arrayBuffer.address + offset)) = EXPORT(S16);
3321
0
#endif
3322
0
  mxMeterOne();
3323
0
}
3324
3325
int fxInt32Compare(const void* p, const void* q)
3326
0
{
3327
0
  txS4 a = *((txS4*)p);
3328
0
  txS4 b = *((txS4*)q);
3329
0
  return (a < b) ? -1 : (a > b) ? 1 : 0;
3330
0
}
3331
3332
void fxInt32Getter(txMachine* the, txSlot* data, txInteger offset, txSlot* slot, int endian)
3333
0
{
3334
0
  txS4 value;
3335
0
  slot->kind = XS_INTEGER_KIND;
3336
#ifdef mxMisalignedSettersCrash
3337
  value = c_read32(data->value.arrayBuffer.address + offset);
3338
#else
3339
0
  value = *((txS4*)(data->value.arrayBuffer.address + offset));
3340
0
#endif
3341
0
  slot->value.integer = IMPORT(S32);
3342
0
  mxMeterOne();
3343
0
}
3344
3345
void fxInt32Setter(txMachine* the, txSlot* data, txInteger offset, txSlot* slot, int endian)
3346
0
{
3347
0
  txS4 value = (txS4)slot->value.integer;
3348
#ifdef mxMisalignedSettersCrash
3349
  value = EXPORT(S32);
3350
  c_memcpy(data->value.arrayBuffer.address + offset, &value, sizeof(txS4));
3351
#else
3352
0
  *((txS4*)(data->value.arrayBuffer.address + offset)) = EXPORT(S32);
3353
0
#endif
3354
0
  mxMeterOne();
3355
0
}
3356
3357
int fxUint8Compare(const void* p, const void* q)
3358
0
{
3359
0
  txU1 a = c_read8((txU1*)p);
3360
0
  txU1 b = c_read8((txU1*)q);
3361
0
  return (a < b) ? -1 : (a > b) ? 1 : 0;
3362
0
}
3363
3364
void fxUint8Getter(txMachine* the, txSlot* data, txInteger offset, txSlot* slot, int endian)
3365
0
{
3366
0
  slot->kind = XS_INTEGER_KIND;
3367
0
  slot->value.integer = c_read8((txU1*)(data->value.arrayBuffer.address + offset));
3368
0
  mxMeterOne();
3369
0
}
3370
3371
void fxUint8Setter(txMachine* the, txSlot* data, txInteger offset, txSlot* slot, int endian)
3372
0
{
3373
0
  txUnsigned tmp = (slot->kind == XS_INTEGER_KIND) ? (txUnsigned)slot->value.integer : (txUnsigned)slot->value.number;
3374
0
  *((txU1*)(data->value.arrayBuffer.address + offset)) = (txU1)tmp;
3375
0
  mxMeterOne();
3376
0
}
3377
3378
int fxUint16Compare(const void* p, const void* q)
3379
0
{
3380
0
  txU2 a = *((txU2*)p);
3381
0
  txU2 b = *((txU2*)q);
3382
0
  return (a < b) ? -1 : (a > b) ? 1 : 0;
3383
0
}
3384
3385
void fxUint16Getter(txMachine* the, txSlot* data, txInteger offset, txSlot* slot, int endian)
3386
0
{
3387
0
  txU2 value;
3388
0
  slot->kind = XS_INTEGER_KIND;
3389
#ifdef mxMisalignedSettersCrash
3390
  c_memcpy(&value, data->value.arrayBuffer.address + offset, sizeof(value));
3391
#else
3392
0
  value = *((txU2*)(data->value.arrayBuffer.address + offset));
3393
0
#endif
3394
0
  slot->value.integer = IMPORT(U16);
3395
0
  mxMeterOne();
3396
0
}
3397
3398
void fxUint16Setter(txMachine* the, txSlot* data, txInteger offset, txSlot* slot, int endian)
3399
0
{
3400
0
  txUnsigned tmp = (slot->kind == XS_INTEGER_KIND) ? (txUnsigned)slot->value.integer : (txUnsigned)slot->value.number;
3401
0
  txU2 value = (txU2)tmp;
3402
#ifdef mxMisalignedSettersCrash
3403
  value = EXPORT(U16);
3404
  c_memcpy(data->value.arrayBuffer.address + offset, &value, sizeof(txU2));
3405
#else
3406
0
  *((txU2*)(data->value.arrayBuffer.address + offset)) = EXPORT(U16);
3407
0
#endif
3408
0
  mxMeterOne();
3409
0
}
3410
3411
int fxUint32Compare(const void* p, const void* q)
3412
0
{
3413
0
  txU4 a = *((txU4*)p);
3414
0
  txU4 b = *((txU4*)q);
3415
0
  return (a < b) ? -1 : (a > b) ? 1 : 0;
3416
0
}
3417
3418
void fxUint32Getter(txMachine* the, txSlot* data, txInteger offset, txSlot* slot, int endian)
3419
0
{
3420
#ifdef mxMisalignedSettersCrash
3421
  txUnsigned value = c_read32(data->value.arrayBuffer.address + offset);
3422
#else
3423
0
  txUnsigned value = *((txU4*)(data->value.arrayBuffer.address + offset));
3424
0
#endif
3425
0
  value = IMPORT(U32);
3426
0
  if (((txInteger)value) >= 0) {
3427
0
    slot->kind = XS_INTEGER_KIND;
3428
0
    slot->value.integer = value;
3429
0
  }
3430
0
  else {
3431
0
    slot->kind = XS_NUMBER_KIND;
3432
0
    slot->value.number = value;
3433
0
  }
3434
0
  mxMeterOne();
3435
0
}
3436
3437
void fxUint32Setter(txMachine* the, txSlot* data, txInteger offset, txSlot* slot, int endian)
3438
0
{
3439
0
  txU4 value = (slot->kind == XS_INTEGER_KIND) ? (txU4)slot->value.integer : (txU4)slot->value.number;
3440
#ifdef mxMisalignedSettersCrash
3441
  value = EXPORT(U32);
3442
  c_memcpy(data->value.arrayBuffer.address + offset, &value, sizeof(txU4));
3443
#else
3444
0
  *((txU4*)(data->value.arrayBuffer.address + offset)) = EXPORT(U32);
3445
0
#endif
3446
0
  mxMeterOne();
3447
0
}
3448
3449
void fxUint8ClampedSetter(txMachine* the, txSlot* data, txInteger offset, txSlot* slot, int endian)
3450
0
{
3451
0
  txNumber value = fxToNumber(the, slot);
3452
0
  if (value <= 0)
3453
0
    value = 0;
3454
0
  else if (value >= 255)
3455
0
    value = 255;
3456
0
  else if (c_isnan(value))
3457
0
    value = 0;
3458
0
  else
3459
0
    value = c_nearbyint(value);
3460
0
  *((txU1*)(data->value.arrayBuffer.address + offset)) = (txU1)value;
3461
0
  mxMeterOne();
3462
0
}
3463
3464
#if mxUint8ArrayBase64
3465
3466
static const char gxBase64Alphabet[] ICACHE_FLASH_ATTR = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
3467
static const char gxBase64URLAlphabet[] ICACHE_FLASH_ATTR = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
3468
static const char gxHexAlphabet[] ICACHE_FLASH_ATTR = "0123456789abcdef";
3469
3470
enum {
3471
  mxBase64Loose,
3472
  mxBase64StopBeforePartial,
3473
  mxBase64Strict,
3474
};
3475
3476
static txU1 fxSkipAsciiWhitespace(txU1** src)
3477
0
{
3478
0
  txU1* p = *src;
3479
0
  txU1 c;
3480
0
  while ((c = c_read8(p))) {
3481
0
    if ((c != 0x09) && (c != 0x0A) && (c != 0x0C) && (c != 0x0D) && (c != 0x20))
3482
0
       break;
3483
0
    p++;
3484
0
  }
3485
0
  *src = p;
3486
0
  return c;
3487
0
}
3488
3489
static void fxUint8ArrayFromBase64(txMachine* the, txU1* srcStart, txU1* dstStart, txSize* read, txSize* written, txU1* alphabet, txInteger lastChunkHandling)
3490
0
{
3491
0
  txU1* src = srcStart;
3492
0
  txU1* dst = dstStart;
3493
0
  txSize remaining = *written;
3494
0
  txBoolean url = (alphabet == (txU1*)gxBase64URLAlphabet) ? 1 : 0, done = 0;
3495
0
  txU1 byte, buffer[4];
3496
0
  txSize bufferIndex, bufferLength = 0;
3497
  
3498
0
  if (remaining == 0) {
3499
0
    *read = 0;
3500
0
    return;
3501
0
  }
3502
0
  for (;;) {
3503
0
    byte = fxSkipAsciiWhitespace(&src);
3504
0
    if (byte == 0) {
3505
0
      if (bufferLength == 0)
3506
0
        break;
3507
0
      if (lastChunkHandling == mxBase64StopBeforePartial)
3508
0
        break;
3509
0
      if ((lastChunkHandling == mxBase64Strict) || (bufferLength == 1))
3510
0
        mxSyntaxError("invalid string");
3511
0
      if (bufferLength == 2)
3512
0
        buffer[2] = 0;
3513
0
      buffer[3] = 0;
3514
0
      done = 1;
3515
0
    }
3516
0
    else if (byte == '=') {
3517
0
      if (bufferLength < 2) 
3518
0
        mxSyntaxError("invalid string");
3519
0
      src++;
3520
0
      byte = fxSkipAsciiWhitespace(&src);
3521
0
      if (bufferLength == 2) {
3522
0
        if (byte == 0) {
3523
0
          if (lastChunkHandling == mxBase64StopBeforePartial)
3524
0
            break;
3525
0
          mxSyntaxError("invalid string");
3526
0
        }
3527
0
        if (byte == '=') {
3528
0
          src++;
3529
0
          byte = fxSkipAsciiWhitespace(&src);
3530
0
        }
3531
0
        buffer[2] = 0;
3532
0
      }
3533
0
      if (byte != 0)
3534
0
        mxSyntaxError("invalid string");
3535
0
      buffer[3] = 0;
3536
0
      done = 1;
3537
0
    } 
3538
0
    else {
3539
0
      if (('A' <= byte) && (byte <= 'Z'))
3540
0
        byte = byte - 'A';
3541
0
      else if (('a' <= byte) && (byte <= 'z'))
3542
0
        byte = byte - 'a' + 26;
3543
0
      else if (('0' <= byte) && (byte <= '9'))
3544
0
        byte = byte - '0' + 52;
3545
0
      else if ((byte == '+') && !url)
3546
0
        byte = 62;
3547
0
      else if ((byte == '-') && url)
3548
0
        byte = 62;
3549
0
      else if ((byte == '/') && !url)
3550
0
        byte = 63;
3551
0
      else if ((byte == '_') && url)
3552
0
        byte = 63;
3553
0
      else
3554
0
        mxSyntaxError("invalid string");
3555
0
      if (((remaining == 1) && (bufferLength == 2)) || ((remaining == 2) && (bufferLength == 3)))
3556
0
        break;
3557
0
      buffer[bufferLength] = byte;
3558
0
      bufferLength++;
3559
0
      src++;
3560
0
      if (bufferLength < 4)
3561
0
        continue;
3562
0
    }
3563
0
    *read = (txSize)(src - srcStart);
3564
0
    buffer[0] = (buffer[0] << 2) | ((buffer[1] & 0x30) >> 4);
3565
0
    buffer[1] = ((buffer[1] & 0x0F) << 4) | ((buffer[2] & 0x3C) >> 2);
3566
0
    buffer[2] = ((buffer[2] & 0x03) << 6) | (buffer[3] & 0x3F);
3567
0
    bufferLength--;
3568
0
    if ((lastChunkHandling == mxBase64Strict) && (bufferLength < 3) && (buffer[bufferLength] != 0))
3569
0
      mxSyntaxError("invalid string");
3570
0
    bufferIndex = 0;
3571
0
    while ((bufferIndex < bufferLength) && (remaining > 0)) {
3572
0
      *dst++ = buffer[bufferIndex];
3573
0
      bufferIndex++;
3574
0
      remaining--;
3575
0
    }
3576
0
    bufferLength = 0;
3577
0
    if ((done) || (remaining == 0))
3578
0
      break;
3579
0
  }
3580
0
  *written = (txSize)(dst - dstStart);
3581
0
}
3582
3583
static void fxUint8ArrayFromHex(txMachine* the, txU1* srcStart, txU1* dstStart, txSize* read, txSize* written)
3584
0
{
3585
0
#define mxFromHex(X) \
3586
0
  if (('0' <= X) && (X <= '9')) X = X - '0'; \
3587
0
  else if (('A' <= X) && (X <= 'F')) X = X - 'A' + 10; \
3588
0
  else if (('a' <= X) && (X <= 'f')) X =  X - 'a' + 10; \
3589
0
  else mxSyntaxError("invalid string")
3590
3591
0
  txU1* src = srcStart;
3592
0
  txU1* dst = dstStart;
3593
0
  txSize srcSize = *read;
3594
0
  txSize dstSize = *written;
3595
0
  while ((srcSize > 0) && (dstSize > 0)) {
3596
0
    txU1 high = c_read8(src++); 
3597
0
    txU1 low = c_read8(src++); 
3598
0
    mxFromHex(high);
3599
0
    mxFromHex(low);
3600
0
    *dst++ = (high << 4) + low;
3601
0
    srcSize -= 2;
3602
0
    dstSize--;
3603
0
  }
3604
0
  *read = (txSize)(src - srcStart);
3605
0
  *written = (txSize)(dst - dstStart);
3606
0
}
3607
3608
static void fxUint8ArrayGetBase64Options(txMachine* the, txInteger argi, txU1** alphabet, txInteger* lastChunkHandling, txBoolean* omitPadding)
3609
0
{
3610
0
  if ((mxArgc > argi) && !mxIsUndefined(mxArgv(argi))) {
3611
0
    if (!mxIsReference(mxArgv(argi)))
3612
0
      mxTypeError("options: not an object");
3613
0
    mxPushSlot(mxArgv(argi));
3614
0
    mxGetID(mxID(_alphabet));
3615
0
    if (!mxIsUndefined(the->stack)) {
3616
0
      if (!mxIsStringPrimitive(the->stack))
3617
0
        mxTypeError("options.alphabet: not a string");
3618
0
      if (!c_strcmp(the->stack->value.string, "base64url"))
3619
0
        *alphabet = (txU1*)gxBase64URLAlphabet;
3620
0
      else if (c_strcmp(the->stack->value.string, "base64"))
3621
0
        mxTypeError("options.alphabet: neither 'base64' nor 'base64url'");
3622
0
    }
3623
0
    mxPop();
3624
0
    if (lastChunkHandling) {
3625
0
      mxPushSlot(mxArgv(argi));
3626
0
      mxGetID(mxID(_lastChunkHandling));
3627
0
      if (!mxIsUndefined(the->stack)) {
3628
0
        if (!mxIsStringPrimitive(the->stack))
3629
0
          mxTypeError("options.lastChunkHandling: not a string");
3630
0
        if (!c_strcmp(the->stack->value.string, "stop-before-partial"))
3631
0
          *lastChunkHandling = mxBase64StopBeforePartial;
3632
0
        else if (!c_strcmp(the->stack->value.string, "strict"))
3633
0
          *lastChunkHandling = mxBase64Strict;
3634
0
        else if (c_strcmp(the->stack->value.string, "loose"))
3635
0
          mxTypeError("options.lastChunkHandling: neither 'loose' nor 'strict' nor 'stop-before-partial'");
3636
0
      }
3637
0
      mxPop();
3638
0
    }
3639
0
    if (omitPadding) {
3640
0
      mxPushSlot(mxArgv(argi));
3641
0
      mxGetID(mxID(_omitPadding));
3642
0
      fxToBoolean(the, the->stack);
3643
0
      *omitPadding = the->stack->value.boolean;
3644
0
      mxPop();
3645
0
    }
3646
0
  }
3647
0
}
3648
3649
void fx_Uint8Array_fromBase64(txMachine* the)
3650
0
{
3651
0
  txSize srcSize;
3652
0
  txSize dstSize;
3653
0
  txU1* alphabet = (txU1*)gxBase64Alphabet;
3654
0
  txInteger lastChunkHandling = mxBase64Loose;
3655
0
  txU1* src;
3656
0
  txU1* dst;
3657
0
  if ((mxArgc < 1) || !mxIsStringPrimitive(mxArgv(0)))
3658
0
    mxTypeError("string: not a string");
3659
0
  fxUint8ArrayGetBase64Options(the, 1, &alphabet, &lastChunkHandling, NULL);
3660
0
  srcSize = (txSize)c_strlen(mxArgv(0)->value.string);
3661
0
  dstSize = (((srcSize + 3) / 4) * 3);
3662
0
  mxPush(mxUint8ArrayConstructor);
3663
0
  mxNew();
3664
0
  mxPushInteger(dstSize);
3665
0
  mxRunCount(1);
3666
0
  mxPullSlot(mxResult);
3667
0
  {
3668
0
    txSlot* resultInstance = fxCheckTypedArrayInstance(the, mxResult); \
3669
0
    txSlot* resultDispatch = resultInstance->next; \
3670
0
    txSlot* resultView = resultDispatch->next; \
3671
0
    txSlot* resultBuffer = resultView->next; \
3672
0
    src = (txU1*)(mxArgv(0)->value.string);
3673
0
    dst = (txU1*)(resultBuffer->value.reference->next->value.arrayBuffer.address + resultView->value.dataView.offset);
3674
0
    resultBuffer->value.reference->next->next->value.bufferInfo.maxLength = dstSize;
3675
0
    fxUint8ArrayFromBase64(the, src, dst, &srcSize, &dstSize, alphabet, lastChunkHandling);
3676
0
    fxSetArrayBufferLength(the, resultBuffer, dstSize);
3677
0
    resultBuffer->value.reference->next->next->value.bufferInfo.maxLength = -1;
3678
0
    resultView->value.dataView.size = dstSize;
3679
0
  }
3680
0
}
3681
3682
void fx_Uint8Array_fromHex(txMachine* the)
3683
0
{
3684
0
  txSize srcSize;
3685
0
  txSize dstSize;
3686
0
  txU1* src;
3687
0
  txU1* dst;
3688
0
  if ((mxArgc < 1) || !mxIsStringPrimitive(mxArgv(0)))
3689
0
    mxTypeError("string: not a string");
3690
0
  srcSize = (txSize)c_strlen(mxArgv(0)->value.string);
3691
0
  if (srcSize & 1)
3692
0
    mxSyntaxError("string: odd length");
3693
0
  dstSize = srcSize >> 1;
3694
0
  mxPush(mxUint8ArrayConstructor);
3695
0
  mxNew();
3696
0
  mxPushInteger(dstSize);
3697
0
  mxRunCount(1);
3698
0
  mxPullSlot(mxResult);
3699
0
  {
3700
0
    txSlot* resultInstance = fxCheckTypedArrayInstance(the, mxResult); \
3701
0
    txSlot* resultDispatch = resultInstance->next; \
3702
0
    txSlot* resultView = resultDispatch->next; \
3703
0
    txSlot* resultBuffer = resultView->next; \
3704
0
    src = (txU1*)(mxArgv(0)->value.string);
3705
0
    dst = (txU1*)(resultBuffer->value.reference->next->value.arrayBuffer.address + resultView->value.dataView.offset);
3706
0
    fxUint8ArrayFromHex(the, src, dst, &srcSize, &dstSize);
3707
0
  }
3708
0
}
3709
3710
void fx_Uint8Array_prototype_setFromBase64(txMachine* the)
3711
0
{
3712
0
  txSlot* instance = fxCheckTypedArrayInstance(the, mxThis);
3713
0
  txSlot* dispatch = instance->next;
3714
0
  txSlot* view = dispatch->next;
3715
0
  txSlot* buffer = view->next;
3716
0
  txU1* alphabet = (txU1*)gxBase64Alphabet;
3717
0
  txInteger lastChunkHandling = mxBase64Loose;
3718
0
  txSize srcSize;
3719
0
  txSize dstSize;
3720
0
  txU1* src;
3721
0
  txU1* dst;
3722
0
  txSlot* property;
3723
0
  if (dispatch->value.typedArray.dispatch->constructorID != mxID(_Uint8Array))
3724
0
    mxTypeError("this: not a Uint8Array instance");
3725
0
  if ((mxArgc < 1) || !mxIsStringPrimitive(mxArgv(0)))
3726
0
    mxTypeError("string: not a string");
3727
0
  fxUint8ArrayGetBase64Options(the, 1, &alphabet, &lastChunkHandling, NULL);
3728
0
  srcSize = (txSize)c_strlen(mxArgv(0)->value.string);
3729
0
  dstSize = fxCheckDataViewSize(the, view, buffer, XS_MUTABLE);
3730
0
  src = (txU1*)(mxArgv(0)->value.string);
3731
0
  dst = (txU1*)(buffer->value.reference->next->value.arrayBuffer.address + view->value.dataView.offset);
3732
0
  fxUint8ArrayFromBase64(the, src, dst, &srcSize, &dstSize, alphabet, lastChunkHandling);
3733
0
  mxPush(mxObjectPrototype);
3734
0
  property = fxLastProperty(the, fxNewObjectInstance(the));
3735
0
  property = fxNextIntegerProperty(the, property, srcSize, mxID(_read_), XS_NO_FLAG);
3736
0
  property = fxNextIntegerProperty(the, property, dstSize, mxID(_written), XS_NO_FLAG);
3737
0
  mxPullSlot(mxResult);
3738
0
}
3739
3740
void fx_Uint8Array_prototype_setFromHex(txMachine* the)
3741
0
{
3742
0
  txSlot* instance = fxCheckTypedArrayInstance(the, mxThis);
3743
0
  txSlot* dispatch = instance->next;
3744
0
  txSlot* view = dispatch->next;
3745
0
  txSlot* buffer = view->next;
3746
0
  txSize srcSize;
3747
0
  txSize dstSize;
3748
0
  txU1* src;
3749
0
  txU1* dst;
3750
0
  txSlot* property;
3751
0
  if (dispatch->value.typedArray.dispatch->constructorID != mxID(_Uint8Array))
3752
0
    mxTypeError("this: not a Uint8Array instance");
3753
0
  if ((mxArgc < 1) || !mxIsStringPrimitive(mxArgv(0)))
3754
0
    mxTypeError("string: not a string");
3755
0
  dstSize = fxCheckDataViewSize(the, view, buffer, XS_MUTABLE);
3756
0
  srcSize = (txSize)c_strlen(mxArgv(0)->value.string);
3757
0
  if (srcSize & 1)
3758
0
    mxSyntaxError("string: odd length");
3759
0
  src = (txU1*)(mxArgv(0)->value.string);
3760
0
  dst = (txU1*)(buffer->value.reference->next->value.arrayBuffer.address + view->value.dataView.offset);
3761
0
  fxUint8ArrayFromHex(the, src, dst, &srcSize, &dstSize);
3762
0
  mxPush(mxObjectPrototype);
3763
0
  property = fxLastProperty(the, fxNewObjectInstance(the));
3764
0
  property = fxNextIntegerProperty(the, property, srcSize, mxID(_read_), XS_NO_FLAG);
3765
0
  property = fxNextIntegerProperty(the, property, dstSize, mxID(_written), XS_NO_FLAG);
3766
0
  mxPullSlot(mxResult);
3767
0
}
3768
3769
void fx_Uint8Array_prototype_toBase64(txMachine* the)
3770
0
{
3771
0
  txSlot* instance = fxCheckTypedArrayInstance(the, mxThis);
3772
0
  txSlot* dispatch = instance->next;
3773
0
  txSlot* view = dispatch->next;
3774
0
  txSlot* buffer = view->next;
3775
0
  txU1* alphabet = (txU1*)gxBase64Alphabet;
3776
0
  txBoolean omitPadding = 0;
3777
0
  txU1* src;
3778
0
  txU1* dst;
3779
0
  txSize srcSize;
3780
0
  txSize dstSize;
3781
0
  txU1 a, b, c;
3782
0
  if (dispatch->value.typedArray.dispatch->constructorID != mxID(_Uint8Array))
3783
0
    mxTypeError("this: not a Uint8Array instance");
3784
0
  fxUint8ArrayGetBase64Options(the, 0, &alphabet, C_NULL, &omitPadding);
3785
0
  srcSize = fxCheckDataViewSize(the, view, buffer, XS_IMMUTABLE);
3786
0
  if (srcSize > (((0x7FFFFFFF >> 2) * 3) - 2))
3787
0
    fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
3788
0
  dstSize = (((srcSize + 2) / 3) << 2);
3789
0
  fxStringBuffer(the, mxResult, C_NULL, dstSize);
3790
0
  src = (txU1*)buffer->value.reference->next->value.arrayBuffer.address + view->value.dataView.offset;
3791
0
  dst = (txU1*)mxResult->value.string;
3792
0
  while (srcSize > 2) {
3793
0
    a = c_read8(src++);
3794
0
    b = c_read8(src++);
3795
0
    c = c_read8(src++);
3796
0
    *dst++ = c_read8(alphabet + ((a & 0xfc) >> 2));
3797
0
    *dst++ = c_read8(alphabet + (((a & 0x3) << 4) | ((b & 0xf0) >> 4)));
3798
0
    *dst++ = c_read8(alphabet + (((b & 0xf) << 2) | ((c & 0xc0) >> 6)));
3799
0
    *dst++ = c_read8(alphabet + (c & 0x3f));
3800
0
    srcSize -= 3;
3801
0
  }
3802
0
  if (srcSize == 2) {
3803
0
    a = c_read8(src++);
3804
0
    b = c_read8(src++);
3805
0
    *dst++ = c_read8(alphabet + ((a & 0xfc) >> 2));
3806
0
    *dst++ = c_read8(alphabet + (((a & 0x3) << 4) | ((b & 0xf0) >> 4)));
3807
0
    *dst++ = c_read8(alphabet + ((b & 0xf) << 2));
3808
0
    if (!omitPadding)
3809
0
      *dst++ = '=';
3810
0
  }
3811
0
  else if (srcSize == 1) {
3812
0
    a = c_read8(src++);
3813
0
    *dst++ = c_read8(alphabet + ((a & 0xfc) >> 2));
3814
0
    *dst++ = c_read8(alphabet + ((a & 0x3) << 4));
3815
0
    if (!omitPadding) {
3816
0
      *dst++ = '=';
3817
0
      *dst++ = '=';
3818
0
    }
3819
0
  }
3820
0
  *dst++ = 0;
3821
0
}
3822
3823
void fx_Uint8Array_prototype_toHex(txMachine* the)
3824
0
{
3825
0
  txSlot* instance = fxCheckTypedArrayInstance(the, mxThis);
3826
0
  txSlot* dispatch = instance->next;
3827
0
  txSlot* view = dispatch->next;
3828
0
  txSlot* buffer = view->next;
3829
0
  txU1* alphabet = (txU1*)gxHexAlphabet;
3830
0
  txU1* src;
3831
0
  txU1* dst;
3832
0
  txSize srcSize;
3833
0
  txSize dstSize;
3834
0
  txU1 a;
3835
0
  if (dispatch->value.typedArray.dispatch->constructorID != mxID(_Uint8Array))
3836
0
    mxTypeError("this: not a Uint8Array instance");
3837
0
  srcSize = fxCheckDataViewSize(the, view, buffer, XS_IMMUTABLE);
3838
0
  if (srcSize > (0x7FFFFFFF >> 1))
3839
0
    fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
3840
0
  dstSize = (srcSize << 1);
3841
0
  fxStringBuffer(the, mxResult, C_NULL, dstSize);
3842
0
  src = (txU1*)buffer->value.reference->next->value.arrayBuffer.address + view->value.dataView.offset;
3843
0
  dst = (txU1*)mxResult->value.string;
3844
0
  while (srcSize > 0) {
3845
0
    a = c_read8(src++);
3846
0
    *dst++ = c_read8(alphabet + ((a & 0xf0) >> 4));
3847
0
    *dst++ = c_read8(alphabet + (a & 0x0f));
3848
0
    srcSize--;
3849
0
  }
3850
0
  *dst++ = 0;
3851
0
}
3852
3853
#endif