forked from bellard/quickjs
-
Notifications
You must be signed in to change notification settings - Fork 4
/
quickjs-wamr.c
515 lines (486 loc) · 17.5 KB
/
quickjs-wamr.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
#include "quickjs.h"
#include "quickjs-wamr.h"
JSValue JS_GetGlobalVar_wrapper(JSContext *ctx, JSAtom prop,
JS_BOOL throw_ref_error)
{
return JS_GetGlobalVar(ctx, prop, throw_ref_error);
}
JSValue JS_CallConstructorInternal_wrapper(JSContext *ctx,
JSValueConst func_obj,
JSValueConst new_target,
int argc, JSValue *argv, int flags)
{
return JS_CallConstructorInternal(ctx, func_obj, new_target, argc, argv, flags);
}
JSAtom find_atom_wrapper(JSContext *ctx, const char *name)
{
return find_atom(ctx, name);
}
const JSCFunctionListEntry js_map_proto_funcs1[] = {
JS_CFUNC_MAGIC_DEF("set", 2, js_map_set, 0 ),
JS_CFUNC_MAGIC_DEF("get", 1, js_map_get, 0 ),
JS_CFUNC_MAGIC_DEF("has", 1, js_map_has, 0 ),
JS_CFUNC_MAGIC_DEF("delete", 1, js_map_delete, 0 ),
JS_CFUNC_MAGIC_DEF("clear", 0, js_map_clear, 0 ),
JS_CGETSET_MAGIC_DEF("size", js_map_get_size, NULL, 0),
JS_CFUNC_MAGIC_DEF("forEach", 1, js_map_forEach, 0 ),
JS_CFUNC_MAGIC_DEF("values", 0, js_create_map_iterator, (JS_ITERATOR_KIND_VALUE << 2) | 0 ),
JS_CFUNC_MAGIC_DEF("keys", 0, js_create_map_iterator, (JS_ITERATOR_KIND_KEY << 2) | 0 ),
JS_CFUNC_MAGIC_DEF("entries", 0, js_create_map_iterator, (JS_ITERATOR_KIND_KEY_AND_VALUE << 2) | 0 ),
JS_ALIAS_DEF("[Symbol.iterator]", "entries" ),
JS_PROP_STRING_DEF("[Symbol.toStringTag]", "Map", JS_PROP_CONFIGURABLE ),
};
const JSCFunctionListEntry js_set_proto_funcs1[] = {
JS_CFUNC_MAGIC_DEF("add", 1, js_map_set, MAGIC_SET ),
JS_CFUNC_MAGIC_DEF("has", 1, js_map_has, MAGIC_SET ),
JS_CFUNC_MAGIC_DEF("delete", 1, js_map_delete, MAGIC_SET ),
JS_CFUNC_MAGIC_DEF("clear", 0, js_map_clear, MAGIC_SET ),
JS_CGETSET_MAGIC_DEF("size", js_map_get_size, NULL, MAGIC_SET ),
JS_CFUNC_MAGIC_DEF("forEach", 1, js_map_forEach, MAGIC_SET ),
JS_CFUNC_MAGIC_DEF("values", 0, js_create_map_iterator, (JS_ITERATOR_KIND_KEY << 2) | MAGIC_SET ),
JS_ALIAS_DEF("keys", "values" ),
JS_ALIAS_DEF("[Symbol.iterator]", "values" ),
JS_CFUNC_MAGIC_DEF("entries", 0, js_create_map_iterator, (JS_ITERATOR_KIND_KEY_AND_VALUE << 2) | MAGIC_SET ),
JS_PROP_STRING_DEF("[Symbol.toStringTag]", "Set", JS_PROP_CONFIGURABLE ),
};
int set_array_length_wrapper(JSContext *ctx, JSObject *p, JSValue val,
int flags)
{
return set_array_length(ctx, p, val, flags);
}
int JS_DefinePropertyDesc_wrapper(JSContext *ctx, JSValueConst obj, JSAtom prop,
JSValueConst desc, int flags) {
return JS_DefinePropertyDesc(ctx, obj, prop, desc, flags);
}
int js_operator_typeof_wrapper(JSContext *ctx, JSValueConst op1) {
return js_operator_typeof(ctx, op1);
}
void JS_Dump_wrapper(JSRuntime *rt, JSValue *p) {
if (JS_IsObject(*p)) {
JSObject *js_obj = JS_VALUE_GET_OBJ(*p);
JS_DumpObject(rt, js_obj);
} else {
JS_DumpValueShort(rt, *p);
}
}
static int JS_DumpValueShortWithBuffer(JSRuntime *rt, JSValueConst val, char *buffer, uint32_t len)
{
uint32_t tag = JS_VALUE_GET_NORM_TAG(val);
const char *str;
char *cur = buffer;
uint32_t remain = len;
int res = -1;
switch(tag) {
case JS_TAG_INT:
res = snprintf(cur, remain, "%d", JS_VALUE_GET_INT(val));
if (res >= remain) {
return -1;
}
break;
case JS_TAG_BOOL:
if (JS_VALUE_GET_BOOL(val))
str = "true";
else
str = "false";
goto print_str;
case JS_TAG_NULL:
str = "null";
goto print_str;
case JS_TAG_EXCEPTION:
str = "exception";
goto print_str;
case JS_TAG_UNINITIALIZED:
str = "uninitialized";
goto print_str;
case JS_TAG_UNDEFINED:
str = "undefined";
print_str:
res = snprintf(cur, remain, "%s", str);
if (res >= remain) {
return -1;
}
break;
case JS_TAG_FLOAT64:
res = snprintf(cur, remain, "%.14g", JS_VALUE_GET_FLOAT64(val));
if (res >= remain) {
return -1;
}
break;
#ifdef CONFIG_BIGNUM
case JS_TAG_BIG_INT:
{
JSBigFloat *p = JS_VALUE_GET_PTR(val);
char *str;
str = bf_ftoa(NULL, &p->num, 10, 0,
BF_RNDZ | BF_FTOA_FORMAT_FRAC);
printf("%sn", str);
bf_realloc(&rt->bf_ctx, str, 0);
}
break;
case JS_TAG_BIG_FLOAT:
{
JSBigFloat *p = JS_VALUE_GET_PTR(val);
char *str;
str = bf_ftoa(NULL, &p->num, 16, BF_PREC_INF,
BF_RNDZ | BF_FTOA_FORMAT_FREE | BF_FTOA_ADD_PREFIX);
printf("%sl", str);
bf_free(&rt->bf_ctx, str);
}
break;
case JS_TAG_BIG_DECIMAL:
{
JSBigDecimal *p = JS_VALUE_GET_PTR(val);
char *str;
str = bfdec_ftoa(NULL, &p->num, BF_PREC_INF,
BF_RNDZ | BF_FTOA_FORMAT_FREE);
printf("%sm", str);
bf_free(&rt->bf_ctx, str);
}
break;
#endif
case JS_TAG_STRING:
{
JSString *p;
p = JS_VALUE_GET_STRING(val);
// JS_DumpString(rt, p);
int i, c, sep;
if (p == NULL) {
res = snprintf(cur, remain, "<null>");
if (res >= remain) {
return -1;
}
cur = cur + strlen(buffer);
remain = len - strlen(buffer);
break;;
}
res = snprintf(cur, remain, "%d", p->header.ref_count);
if (res >= remain) {
return -1;
}
cur = buffer + strlen(buffer);
remain = len - strlen(buffer);
sep = (p->header.ref_count == 1) ? '\"' : '\'';
res = snprintf(cur, remain, (p->header.ref_count == 1) ? "\"" : "\'");
if (res >= remain) {
return -1;
}
cur = buffer + strlen(buffer);
remain = len - strlen(buffer);
for(i = 0; i < p->len; i++) {
if (p->is_wide_char)
c = p->u.str16[i];
else
c = p->u.str8[i];
if (c == sep || c == '\\') {
res = snprintf(cur, remain, "\\%c", c);
if (res >= remain) {
return -1;
}
cur = buffer + strlen(buffer);
remain = len - strlen(buffer);
} else if (c >= ' ' && c <= 126) {
res = snprintf(cur, remain, "%c", c);
if (res >= remain) {
return -1;
}
cur = buffer + strlen(buffer);
remain = len - strlen(buffer);
} else if (c == '\n') {
res = snprintf(cur, remain, "\\n");
if (res >= remain) {
return -1;
}
cur = buffer + strlen(buffer);
remain = len - strlen(buffer);
} else {
res = snprintf(cur, remain, "\\u%04x", c);
if (res >= remain) {
return -1;
}
cur = buffer + strlen(buffer);
remain = len - strlen(buffer);
}
}
res = snprintf(cur, remain, (p->header.ref_count == 1) ? "\"" : "\'");
if (res >= remain) {
return -1;
}
cur = buffer + strlen(buffer);
remain = len - strlen(buffer);
}
break;
case JS_TAG_FUNCTION_BYTECODE:
{
JSFunctionBytecode *b = JS_VALUE_GET_PTR(val);
char buf[ATOM_GET_STR_BUF_SIZE];
res = snprintf(cur, remain, "[bytecode %s]", JS_AtomGetStrRT(rt, buf, sizeof(buf), b->func_name));
if (res >= remain) {
return -1;
}
}
break;
case JS_TAG_OBJECT:
{
JSObject *p = JS_VALUE_GET_OBJ(val);
JSAtom atom = rt->class_array[p->class_id].class_name;
char atom_buf[ATOM_GET_STR_BUF_SIZE];
res = snprintf(cur, remain, "[%s %p]",
JS_AtomGetStrRT(rt, atom_buf, sizeof(atom_buf), atom), (void *)p);
if (res >= remain) {
return -1;
}
}
break;
case JS_TAG_SYMBOL:
{
JSAtomStruct *p = JS_VALUE_GET_PTR(val);
char atom_buf[ATOM_GET_STR_BUF_SIZE];
res = snprintf(cur, remain, "Symbol(%s)",
JS_AtomGetStrRT(rt, atom_buf, sizeof(atom_buf), js_get_atom_index(rt, p)));
if (res >= remain) {
return -1;
}
}
break;
case JS_TAG_MODULE:
res = snprintf(cur, remain, "[module]");
if (res >= remain) {
return -1;
}
break;
case JS_TAG_EXT_OBJ:
res = snprintf(cur, remain, "EXT_OBJ: %p", JS_VALUE_GET_PTR(val));
if (res >= remain) {
return -1;
}
break;
case JS_TAG_EXT_FUNC:
res = snprintf(cur, remain, "EXT_FUNC: %p", JS_VALUE_GET_PTR(val));
if (res >= remain) {
return -1;
}
break;
case JS_TAG_EXT_INFC:
res = snprintf(cur, remain, "EXT_INFC: %p", JS_VALUE_GET_PTR(val));
if (res >= remain) {
return -1;
}
break;
default:
res = snprintf(cur, remain, "[unknown tag %d]", tag);
if (res >= remain) {
return -1;
}
break;
}
return strlen(buffer) + 1;
}
static int JS_DumpObjectWithBuffer(JSRuntime *rt, JSObject *p, char *buffer, uint32_t len)
{
uint32_t i;
char atom_buf[ATOM_GET_STR_BUF_SIZE];
JSShape *sh;
JSShapeProperty *prs;
JSProperty *pr;
BOOL is_first = TRUE;
/* XXX: should encode atoms with special characters */
sh = p->shape; /* the shape can be NULL while freeing an object */
char *cur = buffer;
uint32_t remain = len;
int res = -1;
res = snprintf(cur, remain, "%10s ",
JS_AtomGetStrRT(rt, atom_buf, sizeof(atom_buf), rt->class_array[p->class_id].class_name));
if (res >= remain) {
return -1;
}
cur = buffer + strlen(buffer);
remain = len - strlen(buffer);
if (p->is_exotic && p->fast_array) {
res = snprintf(cur, remain, "[ ");
if (res >= remain) {
return -1;
}
cur = buffer + strlen(buffer);
remain = len - strlen(buffer);
for(i = 0; i < p->u.array.count; i++) {
if (i != 0) {
res = snprintf(cur, remain, ", ");
if (res >= remain) {
return -1;
}
cur = buffer + strlen(buffer);
remain = len - strlen(buffer);
}
switch (p->class_id) {
case JS_CLASS_ARRAY:
case JS_CLASS_ARGUMENTS:
if (JS_DumpValueShortWithBuffer(rt, p->u.array.u.values[i], cur, remain) == -1) {
return -1;
}
cur = buffer + strlen(buffer);
remain = len - strlen(buffer);
break;
case JS_CLASS_UINT8C_ARRAY:
case JS_CLASS_INT8_ARRAY:
case JS_CLASS_UINT8_ARRAY:
case JS_CLASS_INT16_ARRAY:
case JS_CLASS_UINT16_ARRAY:
case JS_CLASS_INT32_ARRAY:
case JS_CLASS_UINT32_ARRAY:
#ifdef CONFIG_BIGNUM
case JS_CLASS_BIG_INT64_ARRAY:
case JS_CLASS_BIG_UINT64_ARRAY:
#endif
case JS_CLASS_FLOAT32_ARRAY:
case JS_CLASS_FLOAT64_ARRAY:
{
int size = 1 << typed_array_size_log2(p->class_id);
const uint8_t *b = p->u.array.u.uint8_ptr + i * size;
while (size-- > 0) {
res = snprintf(cur, remain, "%02X", *b++);
if (res >= remain) {
return -1;
}
cur = cur + strlen(buffer);
remain = len - strlen(buffer);
}
}
break;
}
}
res = snprintf(cur, remain, " ] ");
if (res >= remain) {
return -1;
}
cur = cur + strlen(buffer);
remain = len - strlen(buffer);
}
if (sh) {
res = snprintf(cur, remain, "{ ");
if (res >= remain) {
return -1;
}
cur = buffer + strlen(buffer);
remain = len - strlen(buffer);
for(i = 0, prs = get_shape_prop(sh); i < sh->prop_count; i++, prs++) {
if (prs->atom != JS_ATOM_NULL) {
pr = &p->prop[i];
if (!is_first) {
res = snprintf(cur, remain, ", ");
if (res >= remain) {
return -1;
}
cur = buffer + strlen(buffer);
remain = len - strlen(buffer);
}
res = snprintf(cur, remain, "%s: ",
JS_AtomGetStrRT(rt, atom_buf, sizeof(atom_buf), prs->atom));
if (res >= remain) {
return -1;
}
cur = buffer + strlen(buffer);
remain = len - strlen(buffer);
if ((prs->flags & JS_PROP_TMASK) == JS_PROP_GETSET) {
res = snprintf(cur, remain, "[getset %p %p]", (void *)pr->u.getset.getter,
(void *)pr->u.getset.setter);
if (res >= remain) {
return -1;
}
cur = buffer + strlen(buffer);
remain = len - strlen(buffer);
} else if ((prs->flags & JS_PROP_TMASK) == JS_PROP_VARREF) {
res = snprintf(cur, remain, "[varref %p]", (void *)pr->u.var_ref);
if (res >= remain) {
return -1;
}
cur = buffer + strlen(buffer);
remain = len - strlen(buffer);
} else if ((prs->flags & JS_PROP_TMASK) == JS_PROP_AUTOINIT) {
res = snprintf(cur, remain, "[autoinit %p %d %p]", (void *)js_autoinit_get_realm(pr),
js_autoinit_get_id(pr), (void *)pr->u.init.opaque);
if (res >= remain) {
return -1;
}
cur = buffer + strlen(buffer);
remain = len - strlen(buffer);
} else {
if (JS_DumpValueShortWithBuffer(rt, pr->u.value, cur, remain) == -1) {
return -1;
}
cur = buffer + strlen(buffer);
remain = len - strlen(buffer);
}
is_first = FALSE;
}
}
res = snprintf(cur, remain, " }");
if (res >= remain) {
return -1;
}
cur = buffer + strlen(buffer);
remain = len - strlen(buffer);
}
if (js_class_has_bytecode(p->class_id)) {
JSFunctionBytecode *b = p->u.func.function_bytecode;
JSVarRef **var_refs;
if (b->closure_var_count) {
var_refs = p->u.func.var_refs;
res = snprintf(cur, remain, " Closure:");
if (res >= remain) {
return -1;
}
cur = buffer + strlen(buffer);
remain = len - strlen(buffer);
for(i = 0; i < b->closure_var_count; i++) {
res = snprintf(cur, remain, " ");
if (res >= remain) {
return -1;
}
cur = buffer + strlen(buffer);
remain = len - strlen(buffer);
if (JS_DumpValueShortWithBuffer(rt, var_refs[i]->value, cur, remain) == -1) {
return -1;
}
cur = buffer + strlen(buffer);
remain = len - strlen(buffer);
}
if (p->u.func.home_object) {
res = snprintf(cur, remain, " HomeObject: ");
if (res >= remain) {
return -1;
}
cur = buffer + strlen(buffer);
remain = len - strlen(buffer);
if (JS_DumpValueShortWithBuffer(rt, JS_MKPTR(JS_TAG_OBJECT, p->u.func.home_object),
cur, remain) == -1) {
return -1;
}
cur = buffer + strlen(buffer);
remain = len - strlen(buffer);
}
}
}
res = snprintf(cur, remain, "\n");
if (res >= remain) {
return -1;
}
return strlen(buffer) + 1;
}
int JS_DumpWithBuffer(JSRuntime *rt, JSValue *p, void *buffer, uint32_t len) {
char *buf = (char *)buffer;
if (JS_IsObject(*p)) {
JSObject *js_obj = JS_VALUE_GET_OBJ(*p);
return JS_DumpObjectWithBuffer(rt, js_obj, buf, len);
}
return JS_DumpValueShortWithBuffer(rt, *p, buf, len);
}
int JS_OrdinaryIsInstanceOf_wrapper(JSContext *ctx, JSValueConst val,
JSValueConst obj) {
return JS_OrdinaryIsInstanceOf(ctx, val, obj);
}
uint32_t getClassIdFromObject(JSObject *obj) {
return obj->class_id;
}
JSClassCall* getCallByClassId(JSRuntime *rt, uint32_t classId) {
return rt->class_array[classId].call;
}