Coverage Report

Created: 2025-10-13 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quickjs/quickjs.h
Line
Count
Source
1
/*
2
 * QuickJS Javascript Engine
3
 *
4
 * Copyright (c) 2017-2021 Fabrice Bellard
5
 * Copyright (c) 2017-2021 Charlie Gordon
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to deal
9
 * in the Software without restriction, including without limitation the rights
10
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in
15
 * all copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
 * THE SOFTWARE.
24
 */
25
#ifndef QUICKJS_H
26
#define QUICKJS_H
27
28
#include <stdio.h>
29
#include <stdint.h>
30
#include <string.h>
31
32
#ifdef __cplusplus
33
extern "C" {
34
#endif
35
36
#if defined(__GNUC__) || defined(__clang__)
37
#define js_likely(x)          __builtin_expect(!!(x), 1)
38
#define js_unlikely(x)        __builtin_expect(!!(x), 0)
39
#define js_force_inline       inline __attribute__((always_inline))
40
#define __js_printf_like(f, a)   __attribute__((format(printf, f, a)))
41
#else
42
#define js_likely(x)     (x)
43
#define js_unlikely(x)   (x)
44
#define js_force_inline  inline
45
#define __js_printf_like(a, b)
46
#endif
47
48
#define JS_BOOL int
49
50
typedef struct JSRuntime JSRuntime;
51
typedef struct JSContext JSContext;
52
typedef struct JSClass JSClass;
53
typedef uint32_t JSClassID;
54
typedef uint32_t JSAtom;
55
56
#if INTPTR_MAX >= INT64_MAX
57
#define JS_PTR64
58
#define JS_PTR64_DEF(a) a
59
#else
60
#define JS_PTR64_DEF(a)
61
#endif
62
63
#ifndef JS_PTR64
64
#define JS_NAN_BOXING
65
#endif
66
67
#if defined(__SIZEOF_INT128__) && (INTPTR_MAX >= INT64_MAX)
68
#define JS_LIMB_BITS 64
69
#else
70
#define JS_LIMB_BITS 32
71
#endif
72
73
#define JS_SHORT_BIG_INT_BITS JS_LIMB_BITS
74
    
75
enum {
76
    /* all tags with a reference count are negative */
77
    JS_TAG_FIRST       = -9, /* first negative tag */
78
    JS_TAG_BIG_INT     = -9,
79
    JS_TAG_SYMBOL      = -8,
80
    JS_TAG_STRING      = -7,
81
    JS_TAG_STRING_ROPE = -6,
82
    JS_TAG_MODULE      = -3, /* used internally */
83
    JS_TAG_FUNCTION_BYTECODE = -2, /* used internally */
84
    JS_TAG_OBJECT      = -1,
85
86
    JS_TAG_INT         = 0,
87
    JS_TAG_BOOL        = 1,
88
    JS_TAG_NULL        = 2,
89
    JS_TAG_UNDEFINED   = 3,
90
    JS_TAG_UNINITIALIZED = 4,
91
    JS_TAG_CATCH_OFFSET = 5,
92
    JS_TAG_EXCEPTION   = 6,
93
    JS_TAG_SHORT_BIG_INT = 7,
94
    JS_TAG_FLOAT64     = 8,
95
    /* any larger tag is FLOAT64 if JS_NAN_BOXING */
96
};
97
98
typedef struct JSRefCountHeader {
99
    int ref_count;
100
} JSRefCountHeader;
101
102
#define JS_FLOAT64_NAN NAN
103
104
#ifdef CONFIG_CHECK_JSVALUE
105
/* JSValue consistency : it is not possible to run the code in this
106
   mode, but it is useful to detect simple reference counting
107
   errors. It would be interesting to modify a static C analyzer to
108
   handle specific annotations (clang has such annotations but only
109
   for objective C) */
110
typedef struct __JSValue *JSValue;
111
typedef const struct __JSValue *JSValueConst;
112
113
#define JS_VALUE_GET_TAG(v) (int)((uintptr_t)(v) & 0xf)
114
/* same as JS_VALUE_GET_TAG, but return JS_TAG_FLOAT64 with NaN boxing */
115
#define JS_VALUE_GET_NORM_TAG(v) JS_VALUE_GET_TAG(v)
116
#define JS_VALUE_GET_INT(v) (int)((intptr_t)(v) >> 4)
117
#define JS_VALUE_GET_BOOL(v) JS_VALUE_GET_INT(v)
118
#define JS_VALUE_GET_FLOAT64(v) (double)JS_VALUE_GET_INT(v)
119
#define JS_VALUE_GET_SHORT_BIG_INT(v) JS_VALUE_GET_INT(v)
120
#define JS_VALUE_GET_PTR(v) (void *)((intptr_t)(v) & ~0xf)
121
122
#define JS_MKVAL(tag, val) (JSValue)(intptr_t)(((val) << 4) | (tag))
123
#define JS_MKPTR(tag, p) (JSValue)((intptr_t)(p) | (tag))
124
125
#define JS_TAG_IS_FLOAT64(tag) ((unsigned)(tag) == JS_TAG_FLOAT64)
126
127
#define JS_NAN JS_MKVAL(JS_TAG_FLOAT64, 1)
128
129
static inline JSValue __JS_NewFloat64(JSContext *ctx, double d)
130
{
131
    return JS_MKVAL(JS_TAG_FLOAT64, (int)d);
132
}
133
134
static inline JS_BOOL JS_VALUE_IS_NAN(JSValue v)
135
{
136
    return 0;
137
}
138
139
static inline JSValue __JS_NewShortBigInt(JSContext *ctx, int32_t d)
140
{
141
    return JS_MKVAL(JS_TAG_SHORT_BIG_INT, d);
142
}
143
144
#elif defined(JS_NAN_BOXING)
145
146
typedef uint64_t JSValue;
147
148
#define JSValueConst JSValue
149
150
#define JS_VALUE_GET_TAG(v) (int)((v) >> 32)
151
#define JS_VALUE_GET_INT(v) (int)(v)
152
#define JS_VALUE_GET_BOOL(v) (int)(v)
153
#define JS_VALUE_GET_SHORT_BIG_INT(v) (int)(v)
154
#define JS_VALUE_GET_PTR(v) (void *)(intptr_t)(v)
155
156
#define JS_MKVAL(tag, val) (((uint64_t)(tag) << 32) | (uint32_t)(val))
157
#define JS_MKPTR(tag, ptr) (((uint64_t)(tag) << 32) | (uintptr_t)(ptr))
158
159
#define JS_FLOAT64_TAG_ADDEND (0x7ff80000 - JS_TAG_FIRST + 1) /* quiet NaN encoding */
160
161
static inline double JS_VALUE_GET_FLOAT64(JSValue v)
162
{
163
    union {
164
        JSValue v;
165
        double d;
166
    } u;
167
    u.v = v;
168
    u.v += (uint64_t)JS_FLOAT64_TAG_ADDEND << 32;
169
    return u.d;
170
}
171
172
#define JS_NAN (0x7ff8000000000000 - ((uint64_t)JS_FLOAT64_TAG_ADDEND << 32))
173
174
static inline JSValue __JS_NewFloat64(JSContext *ctx, double d)
175
{
176
    union {
177
        double d;
178
        uint64_t u64;
179
    } u;
180
    JSValue v;
181
    u.d = d;
182
    /* normalize NaN */
183
    if (js_unlikely((u.u64 & 0x7fffffffffffffff) > 0x7ff0000000000000))
184
        v = JS_NAN;
185
    else
186
        v = u.u64 - ((uint64_t)JS_FLOAT64_TAG_ADDEND << 32);
187
    return v;
188
}
189
190
#define JS_TAG_IS_FLOAT64(tag) ((unsigned)((tag) - JS_TAG_FIRST) >= (JS_TAG_FLOAT64 - JS_TAG_FIRST))
191
192
/* same as JS_VALUE_GET_TAG, but return JS_TAG_FLOAT64 with NaN boxing */
193
static inline int JS_VALUE_GET_NORM_TAG(JSValue v)
194
{
195
    uint32_t tag;
196
    tag = JS_VALUE_GET_TAG(v);
197
    if (JS_TAG_IS_FLOAT64(tag))
198
        return JS_TAG_FLOAT64;
199
    else
200
        return tag;
201
}
202
203
static inline JS_BOOL JS_VALUE_IS_NAN(JSValue v)
204
{
205
    uint32_t tag;
206
    tag = JS_VALUE_GET_TAG(v);
207
    return tag == (JS_NAN >> 32);
208
}
209
210
static inline JSValue __JS_NewShortBigInt(JSContext *ctx, int32_t d)
211
{
212
    return JS_MKVAL(JS_TAG_SHORT_BIG_INT, d);
213
}
214
215
#else /* !JS_NAN_BOXING */
216
217
typedef union JSValueUnion {
218
    int32_t int32;
219
    double float64;
220
    void *ptr;
221
#if JS_SHORT_BIG_INT_BITS == 32
222
    int32_t short_big_int;
223
#else
224
    int64_t short_big_int;
225
#endif
226
} JSValueUnion;
227
228
typedef struct JSValue {
229
    JSValueUnion u;
230
    int64_t tag;
231
} JSValue;
232
233
#define JSValueConst JSValue
234
235
#define JS_VALUE_GET_TAG(v) ((int32_t)(v).tag)
236
/* same as JS_VALUE_GET_TAG, but return JS_TAG_FLOAT64 with NaN boxing */
237
#define JS_VALUE_GET_NORM_TAG(v) JS_VALUE_GET_TAG(v)
238
#define JS_VALUE_GET_INT(v) ((v).u.int32)
239
#define JS_VALUE_GET_BOOL(v) ((v).u.int32)
240
#define JS_VALUE_GET_FLOAT64(v) ((v).u.float64)
241
#define JS_VALUE_GET_SHORT_BIG_INT(v) ((v).u.short_big_int)
242
#define JS_VALUE_GET_PTR(v) ((v).u.ptr)
243
244
#define JS_MKVAL(tag, val) (JSValue){ (JSValueUnion){ .int32 = val }, tag }
245
#define JS_MKPTR(tag, p) (JSValue){ (JSValueUnion){ .ptr = p }, tag }
246
247
#define JS_TAG_IS_FLOAT64(tag) ((unsigned)(tag) == JS_TAG_FLOAT64)
248
249
#define JS_NAN (JSValue){ .u.float64 = JS_FLOAT64_NAN, JS_TAG_FLOAT64 }
250
251
static inline JSValue __JS_NewFloat64(JSContext *ctx, double d)
252
0
{
253
0
    JSValue v;
254
0
    v.tag = JS_TAG_FLOAT64;
255
0
    v.u.float64 = d;
256
0
    return v;
257
0
}
258
259
static inline JS_BOOL JS_VALUE_IS_NAN(JSValue v)
260
0
{
261
0
    union {
262
0
        double d;
263
0
        uint64_t u64;
264
0
    } u;
265
0
    if (v.tag != JS_TAG_FLOAT64)
266
0
        return 0;
267
0
    u.d = v.u.float64;
268
0
    return (u.u64 & 0x7fffffffffffffff) > 0x7ff0000000000000;
269
0
}
270
271
static inline JSValue __JS_NewShortBigInt(JSContext *ctx, int64_t d)
272
0
{
273
0
    JSValue v;
274
0
    v.tag = JS_TAG_SHORT_BIG_INT;
275
0
    v.u.short_big_int = d;
276
0
    return v;
277
0
}
278
279
#endif /* !JS_NAN_BOXING */
280
281
#define JS_VALUE_IS_BOTH_INT(v1, v2) ((JS_VALUE_GET_TAG(v1) | JS_VALUE_GET_TAG(v2)) == 0)
282
#define JS_VALUE_IS_BOTH_FLOAT(v1, v2) (JS_TAG_IS_FLOAT64(JS_VALUE_GET_TAG(v1)) && JS_TAG_IS_FLOAT64(JS_VALUE_GET_TAG(v2)))
283
284
#define JS_VALUE_HAS_REF_COUNT(v) ((unsigned)JS_VALUE_GET_TAG(v) >= (unsigned)JS_TAG_FIRST)
285
286
/* special values */
287
#define JS_NULL      JS_MKVAL(JS_TAG_NULL, 0)
288
#define JS_UNDEFINED JS_MKVAL(JS_TAG_UNDEFINED, 0)
289
#define JS_FALSE     JS_MKVAL(JS_TAG_BOOL, 0)
290
#define JS_TRUE      JS_MKVAL(JS_TAG_BOOL, 1)
291
#define JS_EXCEPTION JS_MKVAL(JS_TAG_EXCEPTION, 0)
292
#define JS_UNINITIALIZED JS_MKVAL(JS_TAG_UNINITIALIZED, 0)
293
294
/* flags for object properties */
295
#define JS_PROP_CONFIGURABLE  (1 << 0)
296
#define JS_PROP_WRITABLE      (1 << 1)
297
#define JS_PROP_ENUMERABLE    (1 << 2)
298
#define JS_PROP_C_W_E         (JS_PROP_CONFIGURABLE | JS_PROP_WRITABLE | JS_PROP_ENUMERABLE)
299
#define JS_PROP_LENGTH        (1 << 3) /* used internally in Arrays */
300
#define JS_PROP_TMASK         (3 << 4) /* mask for NORMAL, GETSET, VARREF, AUTOINIT */
301
#define JS_PROP_NORMAL         (0 << 4)
302
#define JS_PROP_GETSET         (1 << 4)
303
#define JS_PROP_VARREF         (2 << 4) /* used internally */
304
#define JS_PROP_AUTOINIT       (3 << 4) /* used internally */
305
306
/* flags for JS_DefineProperty */
307
#define JS_PROP_HAS_SHIFT        8
308
#define JS_PROP_HAS_CONFIGURABLE (1 << 8)
309
#define JS_PROP_HAS_WRITABLE     (1 << 9)
310
#define JS_PROP_HAS_ENUMERABLE   (1 << 10)
311
#define JS_PROP_HAS_GET          (1 << 11)
312
#define JS_PROP_HAS_SET          (1 << 12)
313
#define JS_PROP_HAS_VALUE        (1 << 13)
314
315
/* throw an exception if false would be returned
316
   (JS_DefineProperty/JS_SetProperty) */
317
#define JS_PROP_THROW            (1 << 14)
318
/* throw an exception if false would be returned in strict mode
319
   (JS_SetProperty) */
320
#define JS_PROP_THROW_STRICT     (1 << 15)
321
322
#define JS_PROP_NO_EXOTIC        (1 << 16) /* internal use */
323
324
#ifndef JS_DEFAULT_STACK_SIZE
325
#define JS_DEFAULT_STACK_SIZE (1024 * 1024)
326
#endif
327
328
/* JS_Eval() flags */
329
#define JS_EVAL_TYPE_GLOBAL   (0 << 0) /* global code (default) */
330
#define JS_EVAL_TYPE_MODULE   (1 << 0) /* module code */
331
#define JS_EVAL_TYPE_DIRECT   (2 << 0) /* direct call (internal use) */
332
#define JS_EVAL_TYPE_INDIRECT (3 << 0) /* indirect call (internal use) */
333
#define JS_EVAL_TYPE_MASK     (3 << 0)
334
335
#define JS_EVAL_FLAG_STRICT   (1 << 3) /* force 'strict' mode */
336
/* compile but do not run. The result is an object with a
337
   JS_TAG_FUNCTION_BYTECODE or JS_TAG_MODULE tag. It can be executed
338
   with JS_EvalFunction(). */
339
#define JS_EVAL_FLAG_COMPILE_ONLY (1 << 5)
340
/* don't include the stack frames before this eval in the Error() backtraces */
341
#define JS_EVAL_FLAG_BACKTRACE_BARRIER (1 << 6)
342
/* allow top-level await in normal script. JS_Eval() returns a
343
   promise. Only allowed with JS_EVAL_TYPE_GLOBAL */
344
#define JS_EVAL_FLAG_ASYNC (1 << 7)
345
346
typedef JSValue JSCFunction(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv);
347
typedef JSValue JSCFunctionMagic(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv, int magic);
348
typedef JSValue JSCFunctionData(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv, int magic, JSValue *func_data);
349
350
typedef struct JSMallocState {
351
    size_t malloc_count;
352
    size_t malloc_size;
353
    size_t malloc_limit;
354
    void *opaque; /* user opaque */
355
} JSMallocState;
356
357
typedef struct JSMallocFunctions {
358
    void *(*js_malloc)(JSMallocState *s, size_t size);
359
    void (*js_free)(JSMallocState *s, void *ptr);
360
    void *(*js_realloc)(JSMallocState *s, void *ptr, size_t size);
361
    size_t (*js_malloc_usable_size)(const void *ptr);
362
} JSMallocFunctions;
363
364
typedef struct JSGCObjectHeader JSGCObjectHeader;
365
366
JSRuntime *JS_NewRuntime(void);
367
/* info lifetime must exceed that of rt */
368
void JS_SetRuntimeInfo(JSRuntime *rt, const char *info);
369
void JS_SetMemoryLimit(JSRuntime *rt, size_t limit);
370
void JS_SetGCThreshold(JSRuntime *rt, size_t gc_threshold);
371
/* use 0 to disable maximum stack size check */
372
void JS_SetMaxStackSize(JSRuntime *rt, size_t stack_size);
373
/* should be called when changing thread to update the stack top value
374
   used to check stack overflow. */
375
void JS_UpdateStackTop(JSRuntime *rt);
376
JSRuntime *JS_NewRuntime2(const JSMallocFunctions *mf, void *opaque);
377
void JS_FreeRuntime(JSRuntime *rt);
378
void *JS_GetRuntimeOpaque(JSRuntime *rt);
379
void JS_SetRuntimeOpaque(JSRuntime *rt, void *opaque);
380
typedef void JS_MarkFunc(JSRuntime *rt, JSGCObjectHeader *gp);
381
void JS_MarkValue(JSRuntime *rt, JSValueConst val, JS_MarkFunc *mark_func);
382
void JS_RunGC(JSRuntime *rt);
383
JS_BOOL JS_IsLiveObject(JSRuntime *rt, JSValueConst obj);
384
385
JSContext *JS_NewContext(JSRuntime *rt);
386
void JS_FreeContext(JSContext *s);
387
JSContext *JS_DupContext(JSContext *ctx);
388
void *JS_GetContextOpaque(JSContext *ctx);
389
void JS_SetContextOpaque(JSContext *ctx, void *opaque);
390
JSRuntime *JS_GetRuntime(JSContext *ctx);
391
void JS_SetClassProto(JSContext *ctx, JSClassID class_id, JSValue obj);
392
JSValue JS_GetClassProto(JSContext *ctx, JSClassID class_id);
393
394
/* the following functions are used to select the intrinsic object to
395
   save memory */
396
JSContext *JS_NewContextRaw(JSRuntime *rt);
397
int JS_AddIntrinsicBaseObjects(JSContext *ctx);
398
int JS_AddIntrinsicDate(JSContext *ctx);
399
int JS_AddIntrinsicEval(JSContext *ctx);
400
int JS_AddIntrinsicStringNormalize(JSContext *ctx);
401
void JS_AddIntrinsicRegExpCompiler(JSContext *ctx);
402
int JS_AddIntrinsicRegExp(JSContext *ctx);
403
int JS_AddIntrinsicJSON(JSContext *ctx);
404
int JS_AddIntrinsicProxy(JSContext *ctx);
405
int JS_AddIntrinsicMapSet(JSContext *ctx);
406
int JS_AddIntrinsicTypedArrays(JSContext *ctx);
407
int JS_AddIntrinsicPromise(JSContext *ctx);
408
int JS_AddIntrinsicWeakRef(JSContext *ctx);
409
410
JSValue js_string_codePointRange(JSContext *ctx, JSValueConst this_val,
411
                                 int argc, JSValueConst *argv);
412
413
void *js_malloc_rt(JSRuntime *rt, size_t size);
414
void js_free_rt(JSRuntime *rt, void *ptr);
415
void *js_realloc_rt(JSRuntime *rt, void *ptr, size_t size);
416
size_t js_malloc_usable_size_rt(JSRuntime *rt, const void *ptr);
417
void *js_mallocz_rt(JSRuntime *rt, size_t size);
418
419
void *js_malloc(JSContext *ctx, size_t size);
420
void js_free(JSContext *ctx, void *ptr);
421
void *js_realloc(JSContext *ctx, void *ptr, size_t size);
422
size_t js_malloc_usable_size(JSContext *ctx, const void *ptr);
423
void *js_realloc2(JSContext *ctx, void *ptr, size_t size, size_t *pslack);
424
void *js_mallocz(JSContext *ctx, size_t size);
425
char *js_strdup(JSContext *ctx, const char *str);
426
char *js_strndup(JSContext *ctx, const char *s, size_t n);
427
428
typedef struct JSMemoryUsage {
429
    int64_t malloc_size, malloc_limit, memory_used_size;
430
    int64_t malloc_count;
431
    int64_t memory_used_count;
432
    int64_t atom_count, atom_size;
433
    int64_t str_count, str_size;
434
    int64_t obj_count, obj_size;
435
    int64_t prop_count, prop_size;
436
    int64_t shape_count, shape_size;
437
    int64_t js_func_count, js_func_size, js_func_code_size;
438
    int64_t js_func_pc2line_count, js_func_pc2line_size;
439
    int64_t c_func_count, array_count;
440
    int64_t fast_array_count, fast_array_elements;
441
    int64_t binary_object_count, binary_object_size;
442
} JSMemoryUsage;
443
444
void JS_ComputeMemoryUsage(JSRuntime *rt, JSMemoryUsage *s);
445
void JS_DumpMemoryUsage(FILE *fp, const JSMemoryUsage *s, JSRuntime *rt);
446
447
/* atom support */
448
#define JS_ATOM_NULL 0
449
450
JSAtom JS_NewAtomLen(JSContext *ctx, const char *str, size_t len);
451
JSAtom JS_NewAtom(JSContext *ctx, const char *str);
452
JSAtom JS_NewAtomUInt32(JSContext *ctx, uint32_t n);
453
JSAtom JS_DupAtom(JSContext *ctx, JSAtom v);
454
void JS_FreeAtom(JSContext *ctx, JSAtom v);
455
void JS_FreeAtomRT(JSRuntime *rt, JSAtom v);
456
JSValue JS_AtomToValue(JSContext *ctx, JSAtom atom);
457
JSValue JS_AtomToString(JSContext *ctx, JSAtom atom);
458
const char *JS_AtomToCStringLen(JSContext *ctx, size_t *plen, JSAtom atom);
459
static inline const char *JS_AtomToCString(JSContext *ctx, JSAtom atom)
460
0
{
461
0
    return JS_AtomToCStringLen(ctx, NULL, atom);
462
0
}
463
JSAtom JS_ValueToAtom(JSContext *ctx, JSValueConst val);
464
465
/* object class support */
466
467
typedef struct JSPropertyEnum {
468
    JS_BOOL is_enumerable;
469
    JSAtom atom;
470
} JSPropertyEnum;
471
472
typedef struct JSPropertyDescriptor {
473
    int flags;
474
    JSValue value;
475
    JSValue getter;
476
    JSValue setter;
477
} JSPropertyDescriptor;
478
479
typedef struct JSClassExoticMethods {
480
    /* Return -1 if exception (can only happen in case of Proxy object),
481
       FALSE if the property does not exists, TRUE if it exists. If 1 is
482
       returned, the property descriptor 'desc' is filled if != NULL. */
483
    int (*get_own_property)(JSContext *ctx, JSPropertyDescriptor *desc,
484
                             JSValueConst obj, JSAtom prop);
485
    /* '*ptab' should hold the '*plen' property keys. Return 0 if OK,
486
       -1 if exception. The 'is_enumerable' field is ignored.
487
    */
488
    int (*get_own_property_names)(JSContext *ctx, JSPropertyEnum **ptab,
489
                                  uint32_t *plen,
490
                                  JSValueConst obj);
491
    /* return < 0 if exception, or TRUE/FALSE */
492
    int (*delete_property)(JSContext *ctx, JSValueConst obj, JSAtom prop);
493
    /* return < 0 if exception or TRUE/FALSE */
494
    int (*define_own_property)(JSContext *ctx, JSValueConst this_obj,
495
                               JSAtom prop, JSValueConst val,
496
                               JSValueConst getter, JSValueConst setter,
497
                               int flags);
498
    /* The following methods can be emulated with the previous ones,
499
       so they are usually not needed */
500
    /* return < 0 if exception or TRUE/FALSE */
501
    int (*has_property)(JSContext *ctx, JSValueConst obj, JSAtom atom);
502
    JSValue (*get_property)(JSContext *ctx, JSValueConst obj, JSAtom atom,
503
                            JSValueConst receiver);
504
    /* return < 0 if exception or TRUE/FALSE */
505
    int (*set_property)(JSContext *ctx, JSValueConst obj, JSAtom atom,
506
                        JSValueConst value, JSValueConst receiver, int flags);
507
508
    /* To get a consistent object behavior when get_prototype != NULL,
509
       get_property, set_property and set_prototype must be != NULL
510
       and the object must be created with a JS_NULL prototype. */
511
    JSValue (*get_prototype)(JSContext *ctx, JSValueConst obj);
512
    /* return < 0 if exception or TRUE/FALSE */
513
    int (*set_prototype)(JSContext *ctx, JSValueConst obj, JSValueConst proto_val);
514
    /* return < 0 if exception or TRUE/FALSE */
515
    int (*is_extensible)(JSContext *ctx, JSValueConst obj);
516
    /* return < 0 if exception or TRUE/FALSE */
517
    int (*prevent_extensions)(JSContext *ctx, JSValueConst obj);
518
} JSClassExoticMethods;
519
520
typedef void JSClassFinalizer(JSRuntime *rt, JSValue val);
521
typedef void JSClassGCMark(JSRuntime *rt, JSValueConst val,
522
                           JS_MarkFunc *mark_func);
523
#define JS_CALL_FLAG_CONSTRUCTOR (1 << 0)
524
typedef JSValue JSClassCall(JSContext *ctx, JSValueConst func_obj,
525
                            JSValueConst this_val, int argc, JSValueConst *argv,
526
                            int flags);
527
528
typedef struct JSClassDef {
529
    const char *class_name;
530
    JSClassFinalizer *finalizer;
531
    JSClassGCMark *gc_mark;
532
    /* if call != NULL, the object is a function. If (flags &
533
       JS_CALL_FLAG_CONSTRUCTOR) != 0, the function is called as a
534
       constructor. In this case, 'this_val' is new.target. A
535
       constructor call only happens if the object constructor bit is
536
       set (see JS_SetConstructorBit()). */
537
    JSClassCall *call;
538
    /* XXX: suppress this indirection ? It is here only to save memory
539
       because only a few classes need these methods */
540
    JSClassExoticMethods *exotic;
541
} JSClassDef;
542
543
#define JS_INVALID_CLASS_ID 0
544
JSClassID JS_NewClassID(JSClassID *pclass_id);
545
/* Returns the class ID if `v` is an object, otherwise returns JS_INVALID_CLASS_ID. */
546
JSClassID JS_GetClassID(JSValue v);
547
int JS_NewClass(JSRuntime *rt, JSClassID class_id, const JSClassDef *class_def);
548
int JS_IsRegisteredClass(JSRuntime *rt, JSClassID class_id);
549
550
/* value handling */
551
552
static js_force_inline JSValue JS_NewBool(JSContext *ctx, JS_BOOL val)
553
0
{
554
0
    return JS_MKVAL(JS_TAG_BOOL, (val != 0));
555
0
}
556
557
static js_force_inline JSValue JS_NewInt32(JSContext *ctx, int32_t val)
558
0
{
559
0
    return JS_MKVAL(JS_TAG_INT, val);
560
0
}
561
562
static js_force_inline JSValue JS_NewCatchOffset(JSContext *ctx, int32_t val)
563
0
{
564
0
    return JS_MKVAL(JS_TAG_CATCH_OFFSET, val);
565
0
}
566
567
static js_force_inline JSValue JS_NewInt64(JSContext *ctx, int64_t val)
568
0
{
569
0
    JSValue v;
570
0
    if (val == (int32_t)val) {
571
0
        v = JS_NewInt32(ctx, val);
572
0
    } else {
573
0
        v = __JS_NewFloat64(ctx, val);
574
0
    }
575
0
    return v;
576
0
}
577
578
static js_force_inline JSValue JS_NewUint32(JSContext *ctx, uint32_t val)
579
0
{
580
0
    JSValue v;
581
0
    if (val <= 0x7fffffff) {
582
0
        v = JS_NewInt32(ctx, val);
583
0
    } else {
584
0
        v = __JS_NewFloat64(ctx, val);
585
0
    }
586
0
    return v;
587
0
}
588
589
JSValue JS_NewBigInt64(JSContext *ctx, int64_t v);
590
JSValue JS_NewBigUint64(JSContext *ctx, uint64_t v);
591
592
static js_force_inline JSValue JS_NewFloat64(JSContext *ctx, double d)
593
0
{
594
0
    int32_t val;
595
0
    union {
596
0
        double d;
597
0
        uint64_t u;
598
0
    } u, t;
599
0
    if (d >= INT32_MIN && d <= INT32_MAX) {
600
0
        u.d = d;
601
0
        val = (int32_t)d;
602
0
        t.d = val;
603
0
        /* -0 cannot be represented as integer, so we compare the bit
604
0
           representation */
605
0
        if (u.u == t.u)
606
0
            return JS_MKVAL(JS_TAG_INT, val);
607
0
    }
608
0
    return __JS_NewFloat64(ctx, d);
609
0
}
610
611
static inline JS_BOOL JS_IsNumber(JSValueConst v)
612
0
{
613
0
    int tag = JS_VALUE_GET_TAG(v);
614
0
    return tag == JS_TAG_INT || JS_TAG_IS_FLOAT64(tag);
615
0
}
616
617
static inline JS_BOOL JS_IsBigInt(JSContext *ctx, JSValueConst v)
618
0
{
619
0
    int tag = JS_VALUE_GET_TAG(v);
620
0
    return tag == JS_TAG_BIG_INT || tag == JS_TAG_SHORT_BIG_INT;
621
0
}
622
623
static inline JS_BOOL JS_IsBool(JSValueConst v)
624
0
{
625
0
    return JS_VALUE_GET_TAG(v) == JS_TAG_BOOL;
626
0
}
627
628
static inline JS_BOOL JS_IsNull(JSValueConst v)
629
0
{
630
0
    return JS_VALUE_GET_TAG(v) == JS_TAG_NULL;
631
0
}
632
633
static inline JS_BOOL JS_IsUndefined(JSValueConst v)
634
0
{
635
0
    return JS_VALUE_GET_TAG(v) == JS_TAG_UNDEFINED;
636
0
}
637
638
static inline JS_BOOL JS_IsException(JSValueConst v)
639
0
{
640
0
    return js_unlikely(JS_VALUE_GET_TAG(v) == JS_TAG_EXCEPTION);
641
0
}
642
643
static inline JS_BOOL JS_IsUninitialized(JSValueConst v)
644
0
{
645
0
    return js_unlikely(JS_VALUE_GET_TAG(v) == JS_TAG_UNINITIALIZED);
646
0
}
647
648
static inline JS_BOOL JS_IsString(JSValueConst v)
649
0
{
650
0
    return JS_VALUE_GET_TAG(v) == JS_TAG_STRING ||
651
0
        JS_VALUE_GET_TAG(v) == JS_TAG_STRING_ROPE;
652
0
}
653
654
static inline JS_BOOL JS_IsSymbol(JSValueConst v)
655
0
{
656
0
    return JS_VALUE_GET_TAG(v) == JS_TAG_SYMBOL;
657
0
}
658
659
static inline JS_BOOL JS_IsObject(JSValueConst v)
660
0
{
661
0
    return JS_VALUE_GET_TAG(v) == JS_TAG_OBJECT;
662
0
}
663
664
JSValue JS_Throw(JSContext *ctx, JSValue obj);
665
void JS_SetUncatchableException(JSContext *ctx, JS_BOOL flag);
666
JSValue JS_GetException(JSContext *ctx);
667
JS_BOOL JS_HasException(JSContext *ctx);
668
JS_BOOL JS_IsError(JSContext *ctx, JSValueConst val);
669
JSValue JS_NewError(JSContext *ctx);
670
JSValue __js_printf_like(2, 3) JS_ThrowSyntaxError(JSContext *ctx, const char *fmt, ...);
671
JSValue __js_printf_like(2, 3) JS_ThrowTypeError(JSContext *ctx, const char *fmt, ...);
672
JSValue __js_printf_like(2, 3) JS_ThrowReferenceError(JSContext *ctx, const char *fmt, ...);
673
JSValue __js_printf_like(2, 3) JS_ThrowRangeError(JSContext *ctx, const char *fmt, ...);
674
JSValue __js_printf_like(2, 3) JS_ThrowInternalError(JSContext *ctx, const char *fmt, ...);
675
JSValue JS_ThrowOutOfMemory(JSContext *ctx);
676
677
void __JS_FreeValue(JSContext *ctx, JSValue v);
678
static inline void JS_FreeValue(JSContext *ctx, JSValue v)
679
0
{
680
0
    if (JS_VALUE_HAS_REF_COUNT(v)) {
681
0
        JSRefCountHeader *p = (JSRefCountHeader *)JS_VALUE_GET_PTR(v);
682
0
        if (--p->ref_count <= 0) {
683
0
            __JS_FreeValue(ctx, v);
684
0
        }
685
0
    }
686
0
}
687
void __JS_FreeValueRT(JSRuntime *rt, JSValue v);
688
static inline void JS_FreeValueRT(JSRuntime *rt, JSValue v)
689
0
{
690
0
    if (JS_VALUE_HAS_REF_COUNT(v)) {
691
0
        JSRefCountHeader *p = (JSRefCountHeader *)JS_VALUE_GET_PTR(v);
692
0
        if (--p->ref_count <= 0) {
693
0
            __JS_FreeValueRT(rt, v);
694
0
        }
695
0
    }
696
0
}
697
698
static inline JSValue JS_DupValue(JSContext *ctx, JSValueConst v)
699
0
{
700
0
    if (JS_VALUE_HAS_REF_COUNT(v)) {
701
0
        JSRefCountHeader *p = (JSRefCountHeader *)JS_VALUE_GET_PTR(v);
702
0
        p->ref_count++;
703
0
    }
704
0
    return (JSValue)v;
705
0
}
706
707
static inline JSValue JS_DupValueRT(JSRuntime *rt, JSValueConst v)
708
0
{
709
0
    if (JS_VALUE_HAS_REF_COUNT(v)) {
710
0
        JSRefCountHeader *p = (JSRefCountHeader *)JS_VALUE_GET_PTR(v);
711
0
        p->ref_count++;
712
0
    }
713
0
    return (JSValue)v;
714
0
}
715
716
JS_BOOL JS_StrictEq(JSContext *ctx, JSValueConst op1, JSValueConst op2);
717
JS_BOOL JS_SameValue(JSContext *ctx, JSValueConst op1, JSValueConst op2);
718
JS_BOOL JS_SameValueZero(JSContext *ctx, JSValueConst op1, JSValueConst op2);
719
720
int JS_ToBool(JSContext *ctx, JSValueConst val); /* return -1 for JS_EXCEPTION */
721
int JS_ToInt32(JSContext *ctx, int32_t *pres, JSValueConst val);
722
static inline int JS_ToUint32(JSContext *ctx, uint32_t *pres, JSValueConst val)
723
0
{
724
0
    return JS_ToInt32(ctx, (int32_t*)pres, val);
725
0
}
726
int JS_ToInt64(JSContext *ctx, int64_t *pres, JSValueConst val);
727
int JS_ToIndex(JSContext *ctx, uint64_t *plen, JSValueConst val);
728
int JS_ToFloat64(JSContext *ctx, double *pres, JSValueConst val);
729
/* return an exception if 'val' is a Number */
730
int JS_ToBigInt64(JSContext *ctx, int64_t *pres, JSValueConst val);
731
/* same as JS_ToInt64() but allow BigInt */
732
int JS_ToInt64Ext(JSContext *ctx, int64_t *pres, JSValueConst val);
733
734
JSValue JS_NewStringLen(JSContext *ctx, const char *str1, size_t len1);
735
static inline JSValue JS_NewString(JSContext *ctx, const char *str)
736
0
{
737
0
    return JS_NewStringLen(ctx, str, strlen(str));
738
0
}
739
JSValue JS_NewAtomString(JSContext *ctx, const char *str);
740
JSValue JS_ToString(JSContext *ctx, JSValueConst val);
741
JSValue JS_ToPropertyKey(JSContext *ctx, JSValueConst val);
742
const char *JS_ToCStringLen2(JSContext *ctx, size_t *plen, JSValueConst val1, JS_BOOL cesu8);
743
static inline const char *JS_ToCStringLen(JSContext *ctx, size_t *plen, JSValueConst val1)
744
0
{
745
0
    return JS_ToCStringLen2(ctx, plen, val1, 0);
746
0
}
747
static inline const char *JS_ToCString(JSContext *ctx, JSValueConst val1)
748
0
{
749
0
    return JS_ToCStringLen2(ctx, NULL, val1, 0);
750
0
}
751
void JS_FreeCString(JSContext *ctx, const char *ptr);
752
753
JSValue JS_NewObjectProtoClass(JSContext *ctx, JSValueConst proto, JSClassID class_id);
754
JSValue JS_NewObjectClass(JSContext *ctx, int class_id);
755
JSValue JS_NewObjectProto(JSContext *ctx, JSValueConst proto);
756
JSValue JS_NewObject(JSContext *ctx);
757
758
JS_BOOL JS_IsFunction(JSContext* ctx, JSValueConst val);
759
JS_BOOL JS_IsConstructor(JSContext* ctx, JSValueConst val);
760
JS_BOOL JS_SetConstructorBit(JSContext *ctx, JSValueConst func_obj, JS_BOOL val);
761
762
JSValue JS_NewArray(JSContext *ctx);
763
int JS_IsArray(JSContext *ctx, JSValueConst val);
764
765
JSValue JS_NewDate(JSContext *ctx, double epoch_ms);
766
767
JSValue JS_GetPropertyInternal(JSContext *ctx, JSValueConst obj,
768
                               JSAtom prop, JSValueConst receiver,
769
                               JS_BOOL throw_ref_error);
770
static js_force_inline JSValue JS_GetProperty(JSContext *ctx, JSValueConst this_obj,
771
                                              JSAtom prop)
772
0
{
773
0
    return JS_GetPropertyInternal(ctx, this_obj, prop, this_obj, 0);
774
0
}
775
JSValue JS_GetPropertyStr(JSContext *ctx, JSValueConst this_obj,
776
                          const char *prop);
777
JSValue JS_GetPropertyUint32(JSContext *ctx, JSValueConst this_obj,
778
                             uint32_t idx);
779
780
int JS_SetPropertyInternal(JSContext *ctx, JSValueConst obj,
781
                           JSAtom prop, JSValue val, JSValueConst this_obj,
782
                           int flags);
783
static inline int JS_SetProperty(JSContext *ctx, JSValueConst this_obj,
784
                                 JSAtom prop, JSValue val)
785
0
{
786
0
    return JS_SetPropertyInternal(ctx, this_obj, prop, val, this_obj, JS_PROP_THROW);
787
0
}
788
int JS_SetPropertyUint32(JSContext *ctx, JSValueConst this_obj,
789
                         uint32_t idx, JSValue val);
790
int JS_SetPropertyInt64(JSContext *ctx, JSValueConst this_obj,
791
                        int64_t idx, JSValue val);
792
int JS_SetPropertyStr(JSContext *ctx, JSValueConst this_obj,
793
                      const char *prop, JSValue val);
794
int JS_HasProperty(JSContext *ctx, JSValueConst this_obj, JSAtom prop);
795
int JS_IsExtensible(JSContext *ctx, JSValueConst obj);
796
int JS_PreventExtensions(JSContext *ctx, JSValueConst obj);
797
int JS_DeleteProperty(JSContext *ctx, JSValueConst obj, JSAtom prop, int flags);
798
int JS_SetPrototype(JSContext *ctx, JSValueConst obj, JSValueConst proto_val);
799
JSValue JS_GetPrototype(JSContext *ctx, JSValueConst val);
800
801
#define JS_GPN_STRING_MASK  (1 << 0)
802
#define JS_GPN_SYMBOL_MASK  (1 << 1)
803
#define JS_GPN_PRIVATE_MASK (1 << 2)
804
/* only include the enumerable properties */
805
#define JS_GPN_ENUM_ONLY    (1 << 4)
806
/* set theJSPropertyEnum.is_enumerable field */
807
#define JS_GPN_SET_ENUM     (1 << 5)
808
809
int JS_GetOwnPropertyNames(JSContext *ctx, JSPropertyEnum **ptab,
810
                           uint32_t *plen, JSValueConst obj, int flags);
811
void JS_FreePropertyEnum(JSContext *ctx, JSPropertyEnum *tab,
812
                         uint32_t len);
813
int JS_GetOwnProperty(JSContext *ctx, JSPropertyDescriptor *desc,
814
                      JSValueConst obj, JSAtom prop);
815
816
JSValue JS_Call(JSContext *ctx, JSValueConst func_obj, JSValueConst this_obj,
817
                int argc, JSValueConst *argv);
818
JSValue JS_Invoke(JSContext *ctx, JSValueConst this_val, JSAtom atom,
819
                  int argc, JSValueConst *argv);
820
JSValue JS_CallConstructor(JSContext *ctx, JSValueConst func_obj,
821
                           int argc, JSValueConst *argv);
822
JSValue JS_CallConstructor2(JSContext *ctx, JSValueConst func_obj,
823
                            JSValueConst new_target,
824
                            int argc, JSValueConst *argv);
825
JS_BOOL JS_DetectModule(const char *input, size_t input_len);
826
/* 'input' must be zero terminated i.e. input[input_len] = '\0'. */
827
JSValue JS_Eval(JSContext *ctx, const char *input, size_t input_len,
828
                const char *filename, int eval_flags);
829
/* same as JS_Eval() but with an explicit 'this_obj' parameter */
830
JSValue JS_EvalThis(JSContext *ctx, JSValueConst this_obj,
831
                    const char *input, size_t input_len,
832
                    const char *filename, int eval_flags);
833
JSValue JS_GetGlobalObject(JSContext *ctx);
834
int JS_IsInstanceOf(JSContext *ctx, JSValueConst val, JSValueConst obj);
835
int JS_DefineProperty(JSContext *ctx, JSValueConst this_obj,
836
                      JSAtom prop, JSValueConst val,
837
                      JSValueConst getter, JSValueConst setter, int flags);
838
int JS_DefinePropertyValue(JSContext *ctx, JSValueConst this_obj,
839
                           JSAtom prop, JSValue val, int flags);
840
int JS_DefinePropertyValueUint32(JSContext *ctx, JSValueConst this_obj,
841
                                 uint32_t idx, JSValue val, int flags);
842
int JS_DefinePropertyValueStr(JSContext *ctx, JSValueConst this_obj,
843
                              const char *prop, JSValue val, int flags);
844
int JS_DefinePropertyGetSet(JSContext *ctx, JSValueConst this_obj,
845
                            JSAtom prop, JSValue getter, JSValue setter,
846
                            int flags);
847
void JS_SetOpaque(JSValue obj, void *opaque);
848
void *JS_GetOpaque(JSValueConst obj, JSClassID class_id);
849
void *JS_GetOpaque2(JSContext *ctx, JSValueConst obj, JSClassID class_id);
850
void *JS_GetAnyOpaque(JSValueConst obj, JSClassID *class_id);
851
852
/* 'buf' must be zero terminated i.e. buf[buf_len] = '\0'. */
853
JSValue JS_ParseJSON(JSContext *ctx, const char *buf, size_t buf_len,
854
                     const char *filename);
855
#define JS_PARSE_JSON_EXT (1 << 0) /* allow extended JSON */
856
JSValue JS_ParseJSON2(JSContext *ctx, const char *buf, size_t buf_len,
857
                      const char *filename, int flags);
858
JSValue JS_JSONStringify(JSContext *ctx, JSValueConst obj,
859
                         JSValueConst replacer, JSValueConst space0);
860
861
typedef void JSFreeArrayBufferDataFunc(JSRuntime *rt, void *opaque, void *ptr);
862
JSValue JS_NewArrayBuffer(JSContext *ctx, uint8_t *buf, size_t len,
863
                          JSFreeArrayBufferDataFunc *free_func, void *opaque,
864
                          JS_BOOL is_shared);
865
JSValue JS_NewArrayBufferCopy(JSContext *ctx, const uint8_t *buf, size_t len);
866
void JS_DetachArrayBuffer(JSContext *ctx, JSValueConst obj);
867
uint8_t *JS_GetArrayBuffer(JSContext *ctx, size_t *psize, JSValueConst obj);
868
869
typedef enum JSTypedArrayEnum {
870
    JS_TYPED_ARRAY_UINT8C = 0,
871
    JS_TYPED_ARRAY_INT8,
872
    JS_TYPED_ARRAY_UINT8,
873
    JS_TYPED_ARRAY_INT16,
874
    JS_TYPED_ARRAY_UINT16,
875
    JS_TYPED_ARRAY_INT32,
876
    JS_TYPED_ARRAY_UINT32,
877
    JS_TYPED_ARRAY_BIG_INT64,
878
    JS_TYPED_ARRAY_BIG_UINT64,
879
    JS_TYPED_ARRAY_FLOAT16,
880
    JS_TYPED_ARRAY_FLOAT32,
881
    JS_TYPED_ARRAY_FLOAT64,
882
} JSTypedArrayEnum;
883
884
JSValue JS_NewTypedArray(JSContext *ctx, int argc, JSValueConst *argv,
885
                         JSTypedArrayEnum array_type);
886
JSValue JS_GetTypedArrayBuffer(JSContext *ctx, JSValueConst obj,
887
                               size_t *pbyte_offset,
888
                               size_t *pbyte_length,
889
                               size_t *pbytes_per_element);
890
typedef struct {
891
    void *(*sab_alloc)(void *opaque, size_t size);
892
    void (*sab_free)(void *opaque, void *ptr);
893
    void (*sab_dup)(void *opaque, void *ptr);
894
    void *sab_opaque;
895
} JSSharedArrayBufferFunctions;
896
void JS_SetSharedArrayBufferFunctions(JSRuntime *rt,
897
                                      const JSSharedArrayBufferFunctions *sf);
898
899
typedef enum JSPromiseStateEnum {
900
    JS_PROMISE_PENDING,
901
    JS_PROMISE_FULFILLED,
902
    JS_PROMISE_REJECTED,
903
} JSPromiseStateEnum;
904
905
JSValue JS_NewPromiseCapability(JSContext *ctx, JSValue *resolving_funcs);
906
JSPromiseStateEnum JS_PromiseState(JSContext *ctx, JSValue promise);
907
JSValue JS_PromiseResult(JSContext *ctx, JSValue promise);
908
909
/* is_handled = TRUE means that the rejection is handled */
910
typedef void JSHostPromiseRejectionTracker(JSContext *ctx, JSValueConst promise,
911
                                           JSValueConst reason,
912
                                           JS_BOOL is_handled, void *opaque);
913
void JS_SetHostPromiseRejectionTracker(JSRuntime *rt, JSHostPromiseRejectionTracker *cb, void *opaque);
914
915
/* return != 0 if the JS code needs to be interrupted */
916
typedef int JSInterruptHandler(JSRuntime *rt, void *opaque);
917
void JS_SetInterruptHandler(JSRuntime *rt, JSInterruptHandler *cb, void *opaque);
918
/* if can_block is TRUE, Atomics.wait() can be used */
919
void JS_SetCanBlock(JSRuntime *rt, JS_BOOL can_block);
920
/* select which debug info is stripped from the compiled code */
921
#define JS_STRIP_SOURCE (1 << 0) /* strip source code */
922
#define JS_STRIP_DEBUG  (1 << 1) /* strip all debug info including source code */
923
void JS_SetStripInfo(JSRuntime *rt, int flags);
924
int JS_GetStripInfo(JSRuntime *rt);
925
926
/* set the [IsHTMLDDA] internal slot */
927
void JS_SetIsHTMLDDA(JSContext *ctx, JSValueConst obj);
928
929
typedef struct JSModuleDef JSModuleDef;
930
931
/* return the module specifier (allocated with js_malloc()) or NULL if
932
   exception */
933
typedef char *JSModuleNormalizeFunc(JSContext *ctx,
934
                                    const char *module_base_name,
935
                                    const char *module_name, void *opaque);
936
typedef JSModuleDef *JSModuleLoaderFunc(JSContext *ctx,
937
                                        const char *module_name, void *opaque);
938
typedef JSModuleDef *JSModuleLoaderFunc2(JSContext *ctx,
939
                                         const char *module_name, void *opaque,
940
                                         JSValueConst attributes);
941
/* return -1 if exception, 0 if OK */
942
typedef int JSModuleCheckSupportedImportAttributes(JSContext *ctx, void *opaque,
943
                                                   JSValueConst attributes);
944
                                                   
945
/* module_normalize = NULL is allowed and invokes the default module
946
   filename normalizer */
947
void JS_SetModuleLoaderFunc(JSRuntime *rt,
948
                            JSModuleNormalizeFunc *module_normalize,
949
                            JSModuleLoaderFunc *module_loader, void *opaque);
950
/* same as JS_SetModuleLoaderFunc but with attributes. if
951
   module_check_attrs = NULL, no attribute checking is done. */
952
void JS_SetModuleLoaderFunc2(JSRuntime *rt,
953
                             JSModuleNormalizeFunc *module_normalize,
954
                             JSModuleLoaderFunc2 *module_loader,
955
                             JSModuleCheckSupportedImportAttributes *module_check_attrs,
956
                             void *opaque);
957
/* return the import.meta object of a module */
958
JSValue JS_GetImportMeta(JSContext *ctx, JSModuleDef *m);
959
JSAtom JS_GetModuleName(JSContext *ctx, JSModuleDef *m);
960
JSValue JS_GetModuleNamespace(JSContext *ctx, JSModuleDef *m);
961
962
/* JS Job support */
963
964
typedef JSValue JSJobFunc(JSContext *ctx, int argc, JSValueConst *argv);
965
int JS_EnqueueJob(JSContext *ctx, JSJobFunc *job_func, int argc, JSValueConst *argv);
966
967
JS_BOOL JS_IsJobPending(JSRuntime *rt);
968
int JS_ExecutePendingJob(JSRuntime *rt, JSContext **pctx);
969
970
/* Object Writer/Reader (currently only used to handle precompiled code) */
971
#define JS_WRITE_OBJ_BYTECODE  (1 << 0) /* allow function/module */
972
#define JS_WRITE_OBJ_BSWAP     (1 << 1) /* byte swapped output */
973
#define JS_WRITE_OBJ_SAB       (1 << 2) /* allow SharedArrayBuffer */
974
#define JS_WRITE_OBJ_REFERENCE (1 << 3) /* allow object references to
975
                                           encode arbitrary object
976
                                           graph */
977
uint8_t *JS_WriteObject(JSContext *ctx, size_t *psize, JSValueConst obj,
978
                        int flags);
979
uint8_t *JS_WriteObject2(JSContext *ctx, size_t *psize, JSValueConst obj,
980
                         int flags, uint8_t ***psab_tab, size_t *psab_tab_len);
981
982
#define JS_READ_OBJ_BYTECODE  (1 << 0) /* allow function/module */
983
#define JS_READ_OBJ_ROM_DATA  (1 << 1) /* avoid duplicating 'buf' data */
984
#define JS_READ_OBJ_SAB       (1 << 2) /* allow SharedArrayBuffer */
985
#define JS_READ_OBJ_REFERENCE (1 << 3) /* allow object references */
986
JSValue JS_ReadObject(JSContext *ctx, const uint8_t *buf, size_t buf_len,
987
                      int flags);
988
/* instantiate and evaluate a bytecode function. Only used when
989
   reading a script or module with JS_ReadObject() */
990
JSValue JS_EvalFunction(JSContext *ctx, JSValue fun_obj);
991
/* load the dependencies of the module 'obj'. Useful when JS_ReadObject()
992
   returns a module. */
993
int JS_ResolveModule(JSContext *ctx, JSValueConst obj);
994
995
/* only exported for os.Worker() */
996
JSAtom JS_GetScriptOrModuleName(JSContext *ctx, int n_stack_levels);
997
/* only exported for os.Worker() */
998
JSValue JS_LoadModule(JSContext *ctx, const char *basename,
999
                      const char *filename);
1000
1001
/* C function definition */
1002
typedef enum JSCFunctionEnum {  /* XXX: should rename for namespace isolation */
1003
    JS_CFUNC_generic,
1004
    JS_CFUNC_generic_magic,
1005
    JS_CFUNC_constructor,
1006
    JS_CFUNC_constructor_magic,
1007
    JS_CFUNC_constructor_or_func,
1008
    JS_CFUNC_constructor_or_func_magic,
1009
    JS_CFUNC_f_f,
1010
    JS_CFUNC_f_f_f,
1011
    JS_CFUNC_getter,
1012
    JS_CFUNC_setter,
1013
    JS_CFUNC_getter_magic,
1014
    JS_CFUNC_setter_magic,
1015
    JS_CFUNC_iterator_next,
1016
} JSCFunctionEnum;
1017
1018
typedef union JSCFunctionType {
1019
    JSCFunction *generic;
1020
    JSValue (*generic_magic)(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv, int magic);
1021
    JSCFunction *constructor;
1022
    JSValue (*constructor_magic)(JSContext *ctx, JSValueConst new_target, int argc, JSValueConst *argv, int magic);
1023
    JSCFunction *constructor_or_func;
1024
    double (*f_f)(double);
1025
    double (*f_f_f)(double, double);
1026
    JSValue (*getter)(JSContext *ctx, JSValueConst this_val);
1027
    JSValue (*setter)(JSContext *ctx, JSValueConst this_val, JSValueConst val);
1028
    JSValue (*getter_magic)(JSContext *ctx, JSValueConst this_val, int magic);
1029
    JSValue (*setter_magic)(JSContext *ctx, JSValueConst this_val, JSValueConst val, int magic);
1030
    JSValue (*iterator_next)(JSContext *ctx, JSValueConst this_val,
1031
                             int argc, JSValueConst *argv, int *pdone, int magic);
1032
} JSCFunctionType;
1033
1034
JSValue JS_NewCFunction2(JSContext *ctx, JSCFunction *func,
1035
                         const char *name,
1036
                         int length, JSCFunctionEnum cproto, int magic);
1037
JSValue JS_NewCFunctionData(JSContext *ctx, JSCFunctionData *func,
1038
                            int length, int magic, int data_len,
1039
                            JSValueConst *data);
1040
1041
static inline JSValue JS_NewCFunction(JSContext *ctx, JSCFunction *func, const char *name,
1042
                                      int length)
1043
0
{
1044
0
    return JS_NewCFunction2(ctx, func, name, length, JS_CFUNC_generic, 0);
1045
0
}
1046
1047
static inline JSValue JS_NewCFunctionMagic(JSContext *ctx, JSCFunctionMagic *func,
1048
                                           const char *name,
1049
                                           int length, JSCFunctionEnum cproto, int magic)
1050
0
{
1051
0
    /* Used to squelch a -Wcast-function-type warning. */
1052
0
    JSCFunctionType ft = { .generic_magic = func };
1053
0
    return JS_NewCFunction2(ctx, ft.generic, name, length, cproto, magic);
1054
0
}
1055
int JS_SetConstructor(JSContext *ctx, JSValueConst func_obj,
1056
                      JSValueConst proto);
1057
1058
/* C property definition */
1059
1060
typedef struct JSCFunctionListEntry {
1061
    const char *name;
1062
    uint8_t prop_flags;
1063
    uint8_t def_type;
1064
    int16_t magic;
1065
    union {
1066
        struct {
1067
            uint8_t length; /* XXX: should move outside union */
1068
            uint8_t cproto; /* XXX: should move outside union */
1069
            JSCFunctionType cfunc;
1070
        } func;
1071
        struct {
1072
            JSCFunctionType get;
1073
            JSCFunctionType set;
1074
        } getset;
1075
        struct {
1076
            const char *name;
1077
            int base;
1078
        } alias;
1079
        struct {
1080
            const struct JSCFunctionListEntry *tab;
1081
            int len;
1082
        } prop_list;
1083
        const char *str;
1084
        int32_t i32;
1085
        int64_t i64;
1086
        double f64;
1087
    } u;
1088
} JSCFunctionListEntry;
1089
1090
#define JS_DEF_CFUNC          0
1091
#define JS_DEF_CGETSET        1
1092
#define JS_DEF_CGETSET_MAGIC  2
1093
#define JS_DEF_PROP_STRING    3
1094
#define JS_DEF_PROP_INT32     4
1095
#define JS_DEF_PROP_INT64     5
1096
#define JS_DEF_PROP_DOUBLE    6
1097
#define JS_DEF_PROP_UNDEFINED 7
1098
#define JS_DEF_OBJECT         8
1099
#define JS_DEF_ALIAS          9
1100
#define JS_DEF_PROP_ATOM     10
1101
#define JS_DEF_PROP_BOOL     11
1102
1103
/* Note: c++ does not like nested designators */
1104
#define JS_CFUNC_DEF(name, length, func1) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE, JS_DEF_CFUNC, 0, .u = { .func = { length, JS_CFUNC_generic, { .generic = func1 } } } }
1105
#define JS_CFUNC_MAGIC_DEF(name, length, func1, magic) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE, JS_DEF_CFUNC, magic, .u = { .func = { length, JS_CFUNC_generic_magic, { .generic_magic = func1 } } } }
1106
#define JS_CFUNC_SPECIAL_DEF(name, length, cproto, func1) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE, JS_DEF_CFUNC, 0, .u = { .func = { length, JS_CFUNC_ ## cproto, { .cproto = func1 } } } }
1107
#define JS_ITERATOR_NEXT_DEF(name, length, func1, magic) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE, JS_DEF_CFUNC, magic, .u = { .func = { length, JS_CFUNC_iterator_next, { .iterator_next = func1 } } } }
1108
#define JS_CGETSET_DEF(name, fgetter, fsetter) { name, JS_PROP_CONFIGURABLE, JS_DEF_CGETSET, 0, .u = { .getset = { .get = { .getter = fgetter }, .set = { .setter = fsetter } } } }
1109
#define JS_CGETSET_MAGIC_DEF(name, fgetter, fsetter, magic) { name, JS_PROP_CONFIGURABLE, JS_DEF_CGETSET_MAGIC, magic, .u = { .getset = { .get = { .getter_magic = fgetter }, .set = { .setter_magic = fsetter } } } }
1110
#define JS_PROP_STRING_DEF(name, cstr, prop_flags) { name, prop_flags, JS_DEF_PROP_STRING, 0, .u = { .str = cstr } }
1111
#define JS_PROP_INT32_DEF(name, val, prop_flags) { name, prop_flags, JS_DEF_PROP_INT32, 0, .u = { .i32 = val } }
1112
#define JS_PROP_INT64_DEF(name, val, prop_flags) { name, prop_flags, JS_DEF_PROP_INT64, 0, .u = { .i64 = val } }
1113
#define JS_PROP_DOUBLE_DEF(name, val, prop_flags) { name, prop_flags, JS_DEF_PROP_DOUBLE, 0, .u = { .f64 = val } }
1114
#define JS_PROP_UNDEFINED_DEF(name, prop_flags) { name, prop_flags, JS_DEF_PROP_UNDEFINED, 0, .u = { .i32 = 0 } }
1115
#define JS_PROP_ATOM_DEF(name, val, prop_flags) { name, prop_flags, JS_DEF_PROP_ATOM, 0, .u = { .i32 = val } }
1116
#define JS_PROP_BOOL_DEF(name, val, prop_flags) { name, prop_flags, JS_DEF_PROP_BOOL, 0, .u = { .i32 = val } }
1117
#define JS_OBJECT_DEF(name, tab, len, prop_flags) { name, prop_flags, JS_DEF_OBJECT, 0, .u = { .prop_list = { tab, len } } }
1118
#define JS_ALIAS_DEF(name, from) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE, JS_DEF_ALIAS, 0, .u = { .alias = { from, -1 } } }
1119
#define JS_ALIAS_BASE_DEF(name, from, base) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE, JS_DEF_ALIAS, 0, .u = { .alias = { from, base } } }
1120
1121
int JS_SetPropertyFunctionList(JSContext *ctx, JSValueConst obj,
1122
                               const JSCFunctionListEntry *tab,
1123
                               int len);
1124
1125
/* C module definition */
1126
1127
typedef int JSModuleInitFunc(JSContext *ctx, JSModuleDef *m);
1128
1129
JSModuleDef *JS_NewCModule(JSContext *ctx, const char *name_str,
1130
                           JSModuleInitFunc *func);
1131
/* can only be called before the module is instantiated */
1132
int JS_AddModuleExport(JSContext *ctx, JSModuleDef *m, const char *name_str);
1133
int JS_AddModuleExportList(JSContext *ctx, JSModuleDef *m,
1134
                           const JSCFunctionListEntry *tab, int len);
1135
/* can only be called after the module is instantiated */
1136
int JS_SetModuleExport(JSContext *ctx, JSModuleDef *m, const char *export_name,
1137
                       JSValue val);
1138
int JS_SetModuleExportList(JSContext *ctx, JSModuleDef *m,
1139
                           const JSCFunctionListEntry *tab, int len);
1140
/* associate a JSValue to a C module */
1141
int JS_SetModulePrivateValue(JSContext *ctx, JSModuleDef *m, JSValue val);
1142
JSValue JS_GetModulePrivateValue(JSContext *ctx, JSModuleDef *m);
1143
                        
1144
/* debug value output */
1145
1146
typedef struct {
1147
    JS_BOOL show_hidden : 8; /* only show enumerable properties */
1148
    JS_BOOL raw_dump : 8; /* avoid doing autoinit and avoid any malloc() call (for internal use) */
1149
    uint32_t max_depth; /* recurse up to this depth, 0 = no limit */
1150
    uint32_t max_string_length; /* print no more than this length for
1151
                                   strings, 0 = no limit */
1152
    uint32_t max_item_count; /*  print no more than this count for
1153
                                 arrays or objects, 0 = no limit */
1154
} JSPrintValueOptions;
1155
1156
typedef void JSPrintValueWrite(void *opaque, const char *buf, size_t len);
1157
1158
void JS_PrintValueSetDefaultOptions(JSPrintValueOptions *options);
1159
void JS_PrintValueRT(JSRuntime *rt, JSPrintValueWrite *write_func, void *write_opaque,
1160
                     JSValueConst val, const JSPrintValueOptions *options);
1161
void JS_PrintValue(JSContext *ctx, JSPrintValueWrite *write_func, void *write_opaque,
1162
                   JSValueConst val, const JSPrintValueOptions *options);
1163
1164
#undef js_unlikely
1165
#undef js_force_inline
1166
1167
#ifdef __cplusplus
1168
} /* extern "C" { */
1169
#endif
1170
1171
#endif /* QUICKJS_H */