forked from ArtifexSoftware/mujs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jstypedarray.c
398 lines (346 loc) · 11 KB
/
jstypedarray.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#include "jsi.h"
/* https://www.khronos.org/registry/typedarray/specs/latest/ */
#define STR(S) #S
#define XSTR(S) STR(S)
struct ArrayBuffer {
unsigned byteLength;
char ptr[1];
};
static struct ArrayBuffer *new_ArrayBuffer_imp(js_State *J, const char *init, unsigned byteLength)
{
struct ArrayBuffer *buffer = js_malloc(J, offsetof(struct ArrayBuffer, ptr) + byteLength);
buffer->byteLength = byteLength;
if (init)
memcpy(buffer->ptr, init, byteLength);
else
memset(buffer->ptr, 0, byteLength);
js_getglobal(J, "ArrayBuffer");
js_getproperty(J, -1, "prototype");
js_newuserdata(J, "ArrayBuffer", buffer, js_free);
js_rot2pop1(J);
jsB_propn(J, "byteLength", byteLength);
return buffer;
}
static void new_ArrayBuffer(js_State *J)
{
unsigned byteLength = js_touint32(J, 1);
(void) new_ArrayBuffer_imp(J, NULL, byteLength);
}
static void ArrayBuffer_slice(js_State *J)
{
struct ArrayBuffer *self = js_touserdata(J, 0, "ArrayBuffer");
int begin = js_toint32(J, 1);
int end = js_isdefined(J, 2) ? js_toint32(J, 2) : (int) self->byteLength;
if (begin < 0) begin = self->byteLength - begin;
if (end < 0) end = self->byteLength - end;
if (begin < 0) begin = 0;
if (end < 0) end = 0;
if ((unsigned)begin > self->byteLength) begin = self->byteLength;
if ((unsigned)end > self->byteLength) end = self->byteLength;
if (end < begin) end = begin;
(void) new_ArrayBuffer_imp(J, self->ptr + begin, end - begin);
}
struct DataView {
unsigned byteLength;
char *ptr;
};
static void new_DataView(js_State *J)
{
struct ArrayBuffer *buffer = js_touserdata(J, 1, "ArrayBuffer");
struct DataView *view;
unsigned byteOffset = js_touint32(J, 2);
unsigned byteLength = js_isdefined(J, 3) ? js_touint32(J, 3) : buffer->byteLength;
if (byteOffset + byteLength > buffer->byteLength)
js_rangeerror(J, "data view range ouf of bounds");
view = js_malloc(J, sizeof *view);
view->byteLength = byteLength;
view->ptr = buffer->ptr + byteOffset;
js_getglobal(J, "DataView");
js_getproperty(J, -1, "prototype");
js_newuserdata(J, "DataView", view, NULL);
js_rot2pop1(J);
js_copy(J, 1); /* copy buffer argument */
js_defproperty(J, -2, "buffer", JS_READONLY | JS_DONTCONF);
jsB_propn(J, "byteOffset", byteOffset);
jsB_propn(J, "byteLength", byteLength);
}
struct TypedArray {
int length;
char *ptr;
};
static void new_TypedArray(js_State *J, const char *name, int size, js_HasProperty has, js_Put put, js_Finalize fin)
{
struct ArrayBuffer *buffer = NULL;
struct TypedArray *array;
unsigned length, byteLength, byteOffset;
unsigned k;
int copy = 0;
if (js_isuserdata(J, 1, "ArrayBuffer")) {
buffer = js_touserdata(J, 1, "ArrayBuffer");
byteOffset = js_touint32(J, 2);
length = js_isdefined(J, 3) ? js_touint32(J, 3) : buffer->byteLength / size;
byteLength = length * size;
if (byteOffset + byteLength > buffer->byteLength)
js_rangeerror(J, "typed array range ouf of bounds");
} else if (js_isnumber(J, 1)) {
length = js_touint32(J, 1);
byteOffset = 0;
byteLength = length * size;
} else {
/* copy array */
length = js_getlength(J, 1);
byteOffset = 0;
byteLength = length * size;
copy = 1;
}
array = js_malloc(J, sizeof *array);
array->length = length;
js_getglobal(J, name);
js_getproperty(J, -1, "prototype");
js_newuserdatax(J, name, array, has, put, NULL, fin);
js_rot2pop1(J);
jsB_propn(J, "byteOffset", byteOffset);
jsB_propn(J, "byteLength", byteLength);
jsB_propn(J, "length", length);
if (buffer) {
array->ptr = buffer->ptr + byteOffset;
js_copy(J, 1); /* copy buffer argument */
js_defproperty(J, -2, "buffer", JS_READONLY | JS_DONTCONF);
} else {
buffer = new_ArrayBuffer_imp(J, NULL, byteLength);
array->ptr = buffer->ptr;
js_defproperty(J, -2, "buffer", JS_READONLY | JS_DONTCONF);
if (copy) {
for (k = 0; k < length; ++k) {
js_getindex(J, 1, k);
js_setindex(J, -2, k);
}
}
}
}
static int bigendian(void) { static const int one = 1; return *(char*)&one == 0; }
static void swap1(char *p) { }
static void swap2(char *p) { char a=p[0], b=p[1]; p[0]=b; p[1]=a; }
static void swap4(char *p) { char a=p[0], b=p[1], c=p[2], d=p[3]; p[0]=d; p[1]=c; p[2]=b; p[3]=a; }
static void swap8(char *p) {
char a=p[0], b=p[1], c=p[2], d=p[3], e=p[4], f=p[5], g=p[6], h=p[7];
p[0]=h; p[1]=g; p[2]=f; p[3]=e;
p[4]=d; p[5]=c; p[6]=b; p[7]=a;
}
#define AX(N,T,swap) \
static int N ## Array_has(js_State *J, void *v, const char *name) { \
struct TypedArray *ta = v; \
int idx; \
if (js_isarrayindex(J, name, &idx)) { \
if (idx < 0 || idx >= ta->length) \
js_rangeerror(J, "index out of bounds"); \
js_pushnumber(J, ((T*)ta->ptr)[idx]); \
return 1; \
} \
return 0; \
} \
static int N ## Array_put(js_State *J, void *v, const char *name) { \
struct TypedArray *ta = v; \
int idx; \
if (js_isarrayindex(J, name, &idx)) { \
if (idx < 0 || idx >= ta->length) \
js_rangeerror(J, "index out of bounds"); \
((T*)ta->ptr)[idx] = js_tonumber(J, -1); \
return 1; \
} \
return 0; \
} \
static void DataView_set ## N(js_State *J) { \
struct DataView *view = js_touserdata(J, 0, "DataView"); \
unsigned byteOffset = js_touint32(J, 1); \
T value = js_tonumber(J, 2); \
if (byteOffset + sizeof(T) > view->byteLength) \
js_rangeerror(J, "offset out of bounds"); \
if (js_toboolean(J, 3) ^ bigendian()) \
swap((char*)&value); \
*((T*)&view->ptr[byteOffset]) = value; \
} \
static void DataView_get ## N(js_State *J) { \
struct DataView *view = js_touserdata(J, 0, "DataView"); \
unsigned byteOffset = js_touint32(J, 1); \
T value; \
if (byteOffset + sizeof(T) > view->byteLength) \
js_rangeerror(J, "offset out of bounds"); \
value = *((T*)&view->ptr[byteOffset]); \
if (js_toboolean(J, 3) ^ bigendian()) \
swap((char*)&value); \
js_pushnumber(J, value); \
} \
static void new_ ## N ## Array(js_State *J) { \
new_TypedArray(J, XSTR(N ## Array), sizeof(T), N ## Array_has, N ## Array_put, js_free); \
}
AX(Int8, signed char, swap1)
AX(Uint8, unsigned char, swap1)
AX(Int16, signed short, swap2)
AX(Uint16, unsigned short, swap2)
AX(Int32, signed int, swap4)
AX(Uint32, unsigned int, swap4)
AX(Float32, float, swap4)
AX(Float64, double, swap8)
void jsB_inittypedarray(js_State *J)
{
js_newobject(J); /* ArrayBuffer prototype */
{
jsB_propf(J, "slice", ArrayBuffer_slice, 2);
}
js_newcconstructor(J, new_ArrayBuffer, new_ArrayBuffer, "ArrayBuffer", 1);
js_defglobal(J, "ArrayBuffer", JS_DONTENUM);
js_newobject(J); /* DataView prototype */
{
jsB_propf(J, "getInt8", DataView_getInt8, 1);
jsB_propf(J, "getUint8", DataView_getUint8, 1);
jsB_propf(J, "getInt16", DataView_getInt16, 1);
jsB_propf(J, "getUint16", DataView_getUint16, 1);
jsB_propf(J, "getInt32", DataView_getInt32, 1);
jsB_propf(J, "getUint32", DataView_getUint32, 1);
jsB_propf(J, "getFloat32", DataView_getFloat32, 1);
jsB_propf(J, "getFloat64", DataView_getFloat64, 1);
jsB_propf(J, "setInt8", DataView_setInt8, 3);
jsB_propf(J, "setUint8", DataView_setUint8, 3);
jsB_propf(J, "setInt16", DataView_setInt16, 3);
jsB_propf(J, "setUint16", DataView_setUint16, 3);
jsB_propf(J, "setInt32", DataView_setInt32, 3);
jsB_propf(J, "setUint32", DataView_setUint32, 3);
jsB_propf(J, "setFloat32", DataView_setFloat32, 3);
jsB_propf(J, "setFloat64", DataView_setFloat64, 3);
}
js_newcconstructor(J, new_DataView, new_DataView, "DataView", 1);
js_defglobal(J, "DataView", JS_DONTENUM);
#define BX(N,T) \
js_newobject(J); \
jsB_propn(J, "BYTES_PER_ELEMENT", sizeof(T)); \
js_newcconstructor(J, new_ ## N ## Array, new_ ## N ## Array, STR(N), 1); \
js_defglobal(J, STR(N ## Array), JS_DONTENUM);
BX(Int8, signed char)
BX(Uint8, unsigned char)
BX(Int16, signed short)
BX(Uint16, unsigned short)
BX(Int32, signed int)
BX(Uint32, unsigned int)
BX(Float32, float)
BX(Float64, double)
}
void js_newarraybuffer(js_State *J, const void *p, int n)
{
(void) new_ArrayBuffer_imp(J, p, n);
}
void *js_toarraybuffer(js_State *J, int idx, int *n)
{
struct ArrayBuffer *buffer = js_touserdata(J, idx, "ArrayBuffer");
return *n = buffer->byteLength, buffer->ptr;
}
void js_newdataview(js_State *J, const void *p, int n)
{
js_getglobal(J, "DataView");
js_pushnull(J);
js_newarraybuffer(J, p, n);
js_pushnumber(J, 0);
js_pushnumber(J, n);
js_call(J, 3);
}
void *js_todataview(js_State *J, int idx, int *n)
{
struct DataView *view = js_touserdata(J, idx, "DataView");
return *n = view->byteLength, view->ptr;
}
void js_newint8array(js_State *J, const signed char *p, int n)
{
js_getglobal(J, "Int8Array");
js_pushnull(J);
js_newarraybuffer(J, p, n);
js_call(J, 1);
}
void js_newint16array(js_State *J, const signed short *p, int n)
{
js_getglobal(J, "Int16Array");
js_pushnull(J);
js_newarraybuffer(J, p, n * 2);
js_call(J, 1);
}
void js_newint32array(js_State *J, const signed int *p, int n)
{
js_getglobal(J, "Int32Array");
js_pushnull(J);
js_newarraybuffer(J, p, n * 4);
js_call(J, 1);
}
void js_newuint8array(js_State *J, const unsigned char *p, int n)
{
js_getglobal(J, "UInt8Array");
js_pushnull(J);
js_newarraybuffer(J, p, n);
js_call(J, 1);
}
void js_newuint16array(js_State *J, const unsigned short *p, int n)
{
js_getglobal(J, "UInt16Array");
js_pushnull(J);
js_newarraybuffer(J, p, n * 2);
js_call(J, 1);
}
void js_newuint32array(js_State *J, const unsigned int *p, int n)
{
js_getglobal(J, "UInt32Array");
js_pushnull(J);
js_newarraybuffer(J, p, n * 4);
js_call(J, 1);
}
void js_newfloat32array(js_State *J, const float *p, int n)
{
js_getglobal(J, "Float32Array");
js_pushnull(J);
js_newarraybuffer(J, p, n * 4);
js_call(J, 1);
}
void js_newfloat64array(js_State *J, const double *p, int n)
{
js_getglobal(J, "Float64Array");
js_pushnull(J);
js_newarraybuffer(J, p, n * 8);
js_call(J, 1);
}
signed char *js_toint8array(js_State *J, int idx, int *n)
{
struct TypedArray *array = js_touserdata(J, idx, "Int8Array");
return *n = array->length, (signed char *)array->ptr;
}
signed short *js_toint16array(js_State *J, int idx, int *n)
{
struct TypedArray *array = js_touserdata(J, idx, "Int16Array");
return *n = array->length, (signed short *)array->ptr;
}
signed int *js_toint32array(js_State *J, int idx, int *n)
{
struct TypedArray *array = js_touserdata(J, idx, "Int32Array");
return *n = array->length, (signed int *)array->ptr;
}
unsigned char *js_touint8array(js_State *J, int idx, int *n)
{
struct TypedArray *array = js_touserdata(J, idx, "UInt8Array");
return *n = array->length, (unsigned char *)array->ptr;
}
unsigned short *js_touint16array(js_State *J, int idx, int *n)
{
struct TypedArray *array = js_touserdata(J, idx, "UInt16Array");
return *n = array->length, (unsigned short *)array->ptr;
}
unsigned int *js_touint32array(js_State *J, int idx, int *n)
{
struct TypedArray *array = js_touserdata(J, idx, "UInt32Array");
return *n = array->length, (unsigned int *)array->ptr;
}
float *js_tofloat32array(js_State *J, int idx, int *n)
{
struct TypedArray *array = js_touserdata(J, idx, "Float32Array");
return *n = array->length, (float *)array->ptr;
}
double *js_tofloat64array(js_State *J, int idx, int *n)
{
struct TypedArray *array = js_touserdata(J, idx, "Float64Array");
return *n = array->length, (double *)array->ptr;
}