Coverage Report

Created: 2026-01-10 06:27

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