quickjs.h 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218
  1. /*
  2. * QuickJS Javascript Engine
  3. *
  4. * Copyright (c) 2017-2024 Fabrice Bellard
  5. * Copyright (c) 2017-2024 Charlie Gordon
  6. * Copyright (c) 2023-2025 Ben Noordhuis
  7. * Copyright (c) 2023-2025 Saúl Ibarra Corretgé
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  22. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #ifndef QUICKJS_H
  28. #define QUICKJS_H
  29. #include <stdbool.h>
  30. #include <stdio.h>
  31. #include <stdint.h>
  32. #include <string.h>
  33. #include <math.h>
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. #define QUICKJS_NG 1
  38. #if defined(__GNUC__) || defined(__clang__)
  39. #define js_force_inline inline __attribute__((always_inline))
  40. #define JS_EXTERN __attribute__((visibility("default")))
  41. #else
  42. #define js_force_inline inline
  43. #define JS_EXTERN /* nothing */
  44. #endif
  45. /* Borrowed from Folly */
  46. #ifndef JS_PRINTF_FORMAT
  47. #ifdef _MSC_VER
  48. #include <sal.h>
  49. #define JS_PRINTF_FORMAT _Printf_format_string_
  50. #define JS_PRINTF_FORMAT_ATTR(format_param, dots_param)
  51. #else
  52. #define JS_PRINTF_FORMAT
  53. #if !defined(__clang__) && defined(__GNUC__)
  54. #define JS_PRINTF_FORMAT_ATTR(format_param, dots_param) \
  55. __attribute__((format(gnu_printf, format_param, dots_param)))
  56. #else
  57. #define JS_PRINTF_FORMAT_ATTR(format_param, dots_param) \
  58. __attribute__((format(printf, format_param, dots_param)))
  59. #endif
  60. #endif
  61. #endif
  62. typedef struct JSRuntime JSRuntime;
  63. typedef struct JSContext JSContext;
  64. typedef struct JSObject JSObject;
  65. typedef struct JSClass JSClass;
  66. typedef uint32_t JSClassID;
  67. typedef uint32_t JSAtom;
  68. /* Unless documented otherwise, C string pointers (`char *` or `const char *`)
  69. are assumed to verify these constraints:
  70. - unless a length is passed separately, the string has a null terminator
  71. - string contents is either pure ASCII or is UTF-8 encoded.
  72. */
  73. /* Overridable purely for testing purposes; don't touch. */
  74. #ifndef JS_NAN_BOXING
  75. #if INTPTR_MAX < INT64_MAX
  76. #define JS_NAN_BOXING 1 /* Use NAN boxing for 32bit builds. */
  77. #endif
  78. #endif
  79. enum {
  80. /* all tags with a reference count are negative */
  81. JS_TAG_FIRST = -9, /* first negative tag */
  82. JS_TAG_BIG_INT = -9,
  83. JS_TAG_SYMBOL = -8,
  84. JS_TAG_STRING = -7,
  85. JS_TAG_MODULE = -3, /* used internally */
  86. JS_TAG_FUNCTION_BYTECODE = -2, /* used internally */
  87. JS_TAG_OBJECT = -1,
  88. JS_TAG_INT = 0,
  89. JS_TAG_BOOL = 1,
  90. JS_TAG_NULL = 2,
  91. JS_TAG_UNDEFINED = 3,
  92. JS_TAG_UNINITIALIZED = 4,
  93. JS_TAG_CATCH_OFFSET = 5,
  94. JS_TAG_EXCEPTION = 6,
  95. JS_TAG_FLOAT64 = 7,
  96. /* any larger tag is FLOAT64 if JS_NAN_BOXING */
  97. };
  98. #if !defined(JS_CHECK_JSVALUE)
  99. #define JSValueConst JSValue
  100. #endif
  101. // JS_CHECK_JSVALUE build mode does not produce working code but is here to
  102. // help catch reference counting bugs at compile time, by making it harder
  103. // to mix up JSValue and JSValueConst
  104. //
  105. // rules:
  106. //
  107. // - a function with a JSValue parameter takes ownership;
  108. // caller must *not* call JS_FreeValue
  109. //
  110. // - a function with a JSValueConst parameter does not take ownership;
  111. // caller *must* call JS_FreeValue
  112. //
  113. // - a function returning a JSValue transfers ownership to caller;
  114. // caller *must* call JS_FreeValue
  115. //
  116. // - a function returning a JSValueConst does *not* transfer ownership;
  117. // caller must *not* call JS_FreeValue
  118. #if defined(JS_CHECK_JSVALUE)
  119. typedef struct JSValue *JSValue;
  120. typedef const struct JSValue *JSValueConst;
  121. #define JS_MKVAL(tag, val) ((JSValue)((tag) | (intptr_t)(val) << 4))
  122. #define JS_MKPTR(tag, ptr) ((JSValue)((tag) | (intptr_t)(ptr)))
  123. #define JS_VALUE_GET_NORM_TAG(v) ((int)((intptr_t)(v) & 15))
  124. #define JS_VALUE_GET_TAG(v) ((int)((intptr_t)(v) & 15))
  125. #define JS_VALUE_GET_PTR(v) ((void *)((intptr_t)(v) & ~15))
  126. #define JS_VALUE_GET_INT(v) ((int)((intptr_t)(v) >> 4))
  127. #define JS_VALUE_GET_BOOL(v) ((int)((intptr_t)(v) >> 4))
  128. #define JS_VALUE_GET_FLOAT64(v) ((double)((intptr_t)(v) >> 4))
  129. #define JS_TAG_IS_FLOAT64(tag) ((int)(tag) == JS_TAG_FLOAT64)
  130. #define JS_NAN JS_MKVAL(JS_TAG_FLOAT64, 0)
  131. static inline JSValue __JS_NewFloat64(double d)
  132. {
  133. return JS_MKVAL(JS_TAG_FLOAT64, (int)d);
  134. }
  135. static inline bool JS_VALUE_IS_NAN(JSValue v)
  136. {
  137. (void)&v;
  138. return false;
  139. }
  140. #elif defined(JS_NAN_BOXING) && JS_NAN_BOXING
  141. typedef uint64_t JSValue;
  142. #define JS_VALUE_GET_TAG(v) (int)((v) >> 32)
  143. #define JS_VALUE_GET_INT(v) (int)(v)
  144. #define JS_VALUE_GET_BOOL(v) (int)(v)
  145. #define JS_VALUE_GET_PTR(v) (void *)(intptr_t)(v)
  146. #define JS_MKVAL(tag, val) (((uint64_t)(tag) << 32) | (uint32_t)(val))
  147. #define JS_MKPTR(tag, ptr) (((uint64_t)(tag) << 32) | (uintptr_t)(ptr))
  148. #define JS_FLOAT64_TAG_ADDEND (0x7ff80000 - JS_TAG_FIRST + 1) /* quiet NaN encoding */
  149. static inline double JS_VALUE_GET_FLOAT64(JSValue v)
  150. {
  151. union {
  152. JSValue v;
  153. double d;
  154. } u;
  155. u.v = v;
  156. u.v += (uint64_t)JS_FLOAT64_TAG_ADDEND << 32;
  157. return u.d;
  158. }
  159. #define JS_NAN (0x7ff8000000000000 - ((uint64_t)JS_FLOAT64_TAG_ADDEND << 32))
  160. static inline JSValue __JS_NewFloat64(double d)
  161. {
  162. union {
  163. double d;
  164. uint64_t u64;
  165. } u;
  166. JSValue v;
  167. u.d = d;
  168. /* normalize NaN */
  169. if ((u.u64 & 0x7fffffffffffffff) > 0x7ff0000000000000)
  170. v = JS_NAN;
  171. else
  172. v = u.u64 - ((uint64_t)JS_FLOAT64_TAG_ADDEND << 32);
  173. return v;
  174. }
  175. #define JS_TAG_IS_FLOAT64(tag) ((unsigned)((tag) - JS_TAG_FIRST) >= (JS_TAG_FLOAT64 - JS_TAG_FIRST))
  176. /* same as JS_VALUE_GET_TAG, but return JS_TAG_FLOAT64 with NaN boxing */
  177. static inline int JS_VALUE_GET_NORM_TAG(JSValue v)
  178. {
  179. uint32_t tag;
  180. tag = JS_VALUE_GET_TAG(v);
  181. if (JS_TAG_IS_FLOAT64(tag))
  182. return JS_TAG_FLOAT64;
  183. else
  184. return tag;
  185. }
  186. static inline bool JS_VALUE_IS_NAN(JSValue v)
  187. {
  188. uint32_t tag;
  189. tag = JS_VALUE_GET_TAG(v);
  190. return tag == (JS_NAN >> 32);
  191. }
  192. #else /* !JS_NAN_BOXING */
  193. typedef union JSValueUnion {
  194. int32_t int32;
  195. double float64;
  196. void *ptr;
  197. } JSValueUnion;
  198. typedef struct JSValue {
  199. JSValueUnion u;
  200. int64_t tag;
  201. } JSValue;
  202. #define JS_VALUE_GET_TAG(v) ((int32_t)(v).tag)
  203. /* same as JS_VALUE_GET_TAG, but return JS_TAG_FLOAT64 with NaN boxing */
  204. #define JS_VALUE_GET_NORM_TAG(v) JS_VALUE_GET_TAG(v)
  205. #define JS_VALUE_GET_INT(v) ((v).u.int32)
  206. #define JS_VALUE_GET_BOOL(v) ((v).u.int32)
  207. #define JS_VALUE_GET_FLOAT64(v) ((v).u.float64)
  208. #define JS_VALUE_GET_PTR(v) ((v).u.ptr)
  209. /* msvc doesn't understand designated initializers without /std:c++20 */
  210. #ifdef __cplusplus
  211. static inline JSValue JS_MKPTR(int64_t tag, void *ptr)
  212. {
  213. JSValue v;
  214. v.u.ptr = ptr;
  215. v.tag = tag;
  216. return v;
  217. }
  218. static inline JSValue JS_MKVAL(int64_t tag, int32_t int32)
  219. {
  220. JSValue v;
  221. v.u.int32 = int32;
  222. v.tag = tag;
  223. return v;
  224. }
  225. static inline JSValue JS_MKNAN(void)
  226. {
  227. JSValue v;
  228. v.u.float64 = NAN;
  229. v.tag = JS_TAG_FLOAT64;
  230. return v;
  231. }
  232. /* provide as macros for consistency and backward compat reasons */
  233. #define JS_MKPTR(tag, ptr) JS_MKPTR(tag, ptr)
  234. #define JS_MKVAL(tag, val) JS_MKVAL(tag, val)
  235. #define JS_NAN JS_MKNAN() /* alas, not a constant expression */
  236. #else
  237. #define JS_MKPTR(tag, p) (JSValue){ (JSValueUnion){ .ptr = p }, tag }
  238. #define JS_MKVAL(tag, val) (JSValue){ (JSValueUnion){ .int32 = val }, tag }
  239. #define JS_NAN (JSValue){ (JSValueUnion){ .float64 = NAN }, JS_TAG_FLOAT64 }
  240. #endif
  241. #define JS_TAG_IS_FLOAT64(tag) ((unsigned)(tag) == JS_TAG_FLOAT64)
  242. static inline JSValue __JS_NewFloat64(double d)
  243. {
  244. JSValue v;
  245. v.tag = JS_TAG_FLOAT64;
  246. v.u.float64 = d;
  247. return v;
  248. }
  249. static inline bool JS_VALUE_IS_NAN(JSValue v)
  250. {
  251. union {
  252. double d;
  253. uint64_t u64;
  254. } u;
  255. if (v.tag != JS_TAG_FLOAT64)
  256. return 0;
  257. u.d = v.u.float64;
  258. return (u.u64 & 0x7fffffffffffffff) > 0x7ff0000000000000;
  259. }
  260. #endif /* !JS_NAN_BOXING */
  261. #define JS_VALUE_IS_BOTH_INT(v1, v2) ((JS_VALUE_GET_TAG(v1) | JS_VALUE_GET_TAG(v2)) == 0)
  262. #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)))
  263. #define JS_VALUE_GET_OBJ(v) ((JSObject *)JS_VALUE_GET_PTR(v))
  264. #define JS_VALUE_HAS_REF_COUNT(v) ((unsigned)JS_VALUE_GET_TAG(v) >= (unsigned)JS_TAG_FIRST)
  265. /* special values */
  266. #define JS_NULL JS_MKVAL(JS_TAG_NULL, 0)
  267. #define JS_UNDEFINED JS_MKVAL(JS_TAG_UNDEFINED, 0)
  268. #define JS_FALSE JS_MKVAL(JS_TAG_BOOL, 0)
  269. #define JS_TRUE JS_MKVAL(JS_TAG_BOOL, 1)
  270. #define JS_EXCEPTION JS_MKVAL(JS_TAG_EXCEPTION, 0)
  271. #define JS_UNINITIALIZED JS_MKVAL(JS_TAG_UNINITIALIZED, 0)
  272. /* flags for object properties */
  273. #define JS_PROP_CONFIGURABLE (1 << 0)
  274. #define JS_PROP_WRITABLE (1 << 1)
  275. #define JS_PROP_ENUMERABLE (1 << 2)
  276. #define JS_PROP_C_W_E (JS_PROP_CONFIGURABLE | JS_PROP_WRITABLE | JS_PROP_ENUMERABLE)
  277. #define JS_PROP_LENGTH (1 << 3) /* used internally in Arrays */
  278. #define JS_PROP_TMASK (3 << 4) /* mask for NORMAL, GETSET, VARREF, AUTOINIT */
  279. #define JS_PROP_NORMAL (0 << 4)
  280. #define JS_PROP_GETSET (1 << 4)
  281. #define JS_PROP_VARREF (2 << 4) /* used internally */
  282. #define JS_PROP_AUTOINIT (3 << 4) /* used internally */
  283. /* flags for JS_DefineProperty */
  284. #define JS_PROP_HAS_SHIFT 8
  285. #define JS_PROP_HAS_CONFIGURABLE (1 << 8)
  286. #define JS_PROP_HAS_WRITABLE (1 << 9)
  287. #define JS_PROP_HAS_ENUMERABLE (1 << 10)
  288. #define JS_PROP_HAS_GET (1 << 11)
  289. #define JS_PROP_HAS_SET (1 << 12)
  290. #define JS_PROP_HAS_VALUE (1 << 13)
  291. /* throw an exception if false would be returned
  292. (JS_DefineProperty/JS_SetProperty) */
  293. #define JS_PROP_THROW (1 << 14)
  294. /* throw an exception if false would be returned in strict mode
  295. (JS_SetProperty) */
  296. #define JS_PROP_THROW_STRICT (1 << 15)
  297. #define JS_PROP_NO_ADD (1 << 16) /* internal use */
  298. #define JS_PROP_NO_EXOTIC (1 << 17) /* internal use */
  299. #define JS_PROP_DEFINE_PROPERTY (1 << 18) /* internal use */
  300. #define JS_PROP_REFLECT_DEFINE_PROPERTY (1 << 19) /* internal use */
  301. #ifndef JS_DEFAULT_STACK_SIZE
  302. #define JS_DEFAULT_STACK_SIZE (1024 * 1024)
  303. #endif
  304. /* JS_Eval() flags */
  305. #define JS_EVAL_TYPE_GLOBAL (0 << 0) /* global code (default) */
  306. #define JS_EVAL_TYPE_MODULE (1 << 0) /* module code */
  307. #define JS_EVAL_TYPE_DIRECT (2 << 0) /* direct call (internal use) */
  308. #define JS_EVAL_TYPE_INDIRECT (3 << 0) /* indirect call (internal use) */
  309. #define JS_EVAL_TYPE_MASK (3 << 0)
  310. #define JS_EVAL_FLAG_STRICT (1 << 3) /* force 'strict' mode */
  311. #define JS_EVAL_FLAG_UNUSED (1 << 4) /* unused */
  312. /* compile but do not run. The result is an object with a
  313. JS_TAG_FUNCTION_BYTECODE or JS_TAG_MODULE tag. It can be executed
  314. with JS_EvalFunction(). */
  315. #define JS_EVAL_FLAG_COMPILE_ONLY (1 << 5)
  316. /* don't include the stack frames before this eval in the Error() backtraces */
  317. #define JS_EVAL_FLAG_BACKTRACE_BARRIER (1 << 6)
  318. /* allow top-level await in normal script. JS_Eval() returns a
  319. promise. Only allowed with JS_EVAL_TYPE_GLOBAL */
  320. #define JS_EVAL_FLAG_ASYNC (1 << 7)
  321. typedef JSValue JSCFunction(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv);
  322. typedef JSValue JSCFunctionMagic(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv, int magic);
  323. typedef JSValue JSCFunctionData(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv, int magic, JSValueConst *func_data);
  324. typedef struct JSMallocFunctions {
  325. void *(*js_calloc)(void *opaque, size_t count, size_t size);
  326. void *(*js_malloc)(void *opaque, size_t size);
  327. void (*js_free)(void *opaque, void *ptr);
  328. void *(*js_realloc)(void *opaque, void *ptr, size_t size);
  329. size_t (*js_malloc_usable_size)(const void *ptr);
  330. } JSMallocFunctions;
  331. // Debug trace system: the debug output will be produced to the dump stream (currently
  332. // stdout) if dumps are enabled and JS_SetDumpFlags is invoked with the corresponding
  333. // bit set.
  334. #define JS_DUMP_BYTECODE_FINAL 0x01 /* dump pass 3 final byte code */
  335. #define JS_DUMP_BYTECODE_PASS2 0x02 /* dump pass 2 code */
  336. #define JS_DUMP_BYTECODE_PASS1 0x04 /* dump pass 1 code */
  337. #define JS_DUMP_BYTECODE_HEX 0x10 /* dump bytecode in hex */
  338. #define JS_DUMP_BYTECODE_PC2LINE 0x20 /* dump line number table */
  339. #define JS_DUMP_BYTECODE_STACK 0x40 /* dump compute_stack_size */
  340. #define JS_DUMP_BYTECODE_STEP 0x80 /* dump executed bytecode */
  341. #define JS_DUMP_READ_OBJECT 0x100 /* dump the marshalled objects at load time */
  342. #define JS_DUMP_FREE 0x200 /* dump every object free */
  343. #define JS_DUMP_GC 0x400 /* dump the occurrence of the automatic GC */
  344. #define JS_DUMP_GC_FREE 0x800 /* dump objects freed by the GC */
  345. #define JS_DUMP_MODULE_RESOLVE 0x1000 /* dump module resolution steps */
  346. #define JS_DUMP_PROMISE 0x2000 /* dump promise steps */
  347. #define JS_DUMP_LEAKS 0x4000 /* dump leaked objects and strings in JS_FreeRuntime */
  348. #define JS_DUMP_ATOM_LEAKS 0x8000 /* dump leaked atoms in JS_FreeRuntime */
  349. #define JS_DUMP_MEM 0x10000 /* dump memory usage in JS_FreeRuntime */
  350. #define JS_DUMP_OBJECTS 0x20000 /* dump objects in JS_FreeRuntime */
  351. #define JS_DUMP_ATOMS 0x40000 /* dump atoms in JS_FreeRuntime */
  352. #define JS_DUMP_SHAPES 0x80000 /* dump shapes in JS_FreeRuntime */
  353. // Finalizers run in LIFO order at the very end of JS_FreeRuntime.
  354. // Intended for cleanup of associated resources; the runtime itself
  355. // is no longer usable.
  356. typedef void JSRuntimeFinalizer(JSRuntime *rt, void *arg);
  357. typedef struct JSGCObjectHeader JSGCObjectHeader;
  358. JS_EXTERN JSRuntime *JS_NewRuntime(void);
  359. /* info lifetime must exceed that of rt */
  360. JS_EXTERN void JS_SetRuntimeInfo(JSRuntime *rt, const char *info);
  361. /* use 0 to disable memory limit */
  362. JS_EXTERN void JS_SetMemoryLimit(JSRuntime *rt, size_t limit);
  363. JS_EXTERN void JS_SetDumpFlags(JSRuntime *rt, uint64_t flags);
  364. JS_EXTERN uint64_t JS_GetDumpFlags(JSRuntime *rt);
  365. JS_EXTERN size_t JS_GetGCThreshold(JSRuntime *rt);
  366. JS_EXTERN void JS_SetGCThreshold(JSRuntime *rt, size_t gc_threshold);
  367. /* use 0 to disable maximum stack size check */
  368. JS_EXTERN void JS_SetMaxStackSize(JSRuntime *rt, size_t stack_size);
  369. /* should be called when changing thread to update the stack top value
  370. used to check stack overflow. */
  371. JS_EXTERN void JS_UpdateStackTop(JSRuntime *rt);
  372. JS_EXTERN JSRuntime *JS_NewRuntime2(const JSMallocFunctions *mf, void *opaque);
  373. JS_EXTERN void JS_FreeRuntime(JSRuntime *rt);
  374. JS_EXTERN void *JS_GetRuntimeOpaque(JSRuntime *rt);
  375. JS_EXTERN void JS_SetRuntimeOpaque(JSRuntime *rt, void *opaque);
  376. JS_EXTERN int JS_AddRuntimeFinalizer(JSRuntime *rt,
  377. JSRuntimeFinalizer *finalizer, void *arg);
  378. typedef void JS_MarkFunc(JSRuntime *rt, JSGCObjectHeader *gp);
  379. JS_EXTERN void JS_MarkValue(JSRuntime *rt, JSValueConst val,
  380. JS_MarkFunc *mark_func);
  381. JS_EXTERN void JS_RunGC(JSRuntime *rt);
  382. JS_EXTERN bool JS_IsLiveObject(JSRuntime *rt, JSValueConst obj);
  383. JS_EXTERN JSContext *JS_NewContext(JSRuntime *rt);
  384. JS_EXTERN void JS_FreeContext(JSContext *s);
  385. JS_EXTERN JSContext *JS_DupContext(JSContext *ctx);
  386. JS_EXTERN void *JS_GetContextOpaque(JSContext *ctx);
  387. JS_EXTERN void JS_SetContextOpaque(JSContext *ctx, void *opaque);
  388. JS_EXTERN JSRuntime *JS_GetRuntime(JSContext *ctx);
  389. JS_EXTERN void JS_SetClassProto(JSContext *ctx, JSClassID class_id, JSValue obj);
  390. JS_EXTERN JSValue JS_GetClassProto(JSContext *ctx, JSClassID class_id);
  391. JS_EXTERN JSValue JS_GetFunctionProto(JSContext *ctx);
  392. /* the following functions are used to select the intrinsic object to
  393. save memory */
  394. JS_EXTERN JSContext *JS_NewContextRaw(JSRuntime *rt);
  395. JS_EXTERN void JS_AddIntrinsicBaseObjects(JSContext *ctx);
  396. JS_EXTERN void JS_AddIntrinsicDate(JSContext *ctx);
  397. JS_EXTERN void JS_AddIntrinsicEval(JSContext *ctx);
  398. JS_EXTERN void JS_AddIntrinsicRegExpCompiler(JSContext *ctx);
  399. JS_EXTERN void JS_AddIntrinsicRegExp(JSContext *ctx);
  400. JS_EXTERN void JS_AddIntrinsicJSON(JSContext *ctx);
  401. JS_EXTERN void JS_AddIntrinsicProxy(JSContext *ctx);
  402. JS_EXTERN void JS_AddIntrinsicMapSet(JSContext *ctx);
  403. JS_EXTERN void JS_AddIntrinsicTypedArrays(JSContext *ctx);
  404. JS_EXTERN void JS_AddIntrinsicPromise(JSContext *ctx);
  405. JS_EXTERN void JS_AddIntrinsicBigInt(JSContext *ctx);
  406. JS_EXTERN void JS_AddIntrinsicWeakRef(JSContext *ctx);
  407. JS_EXTERN void JS_AddPerformance(JSContext *ctx);
  408. /* for equality comparisons and sameness */
  409. JS_EXTERN int JS_IsEqual(JSContext *ctx, JSValueConst op1, JSValueConst op2);
  410. JS_EXTERN bool JS_IsStrictEqual(JSContext *ctx, JSValueConst op1, JSValueConst op2);
  411. JS_EXTERN bool JS_IsSameValue(JSContext *ctx, JSValueConst op1, JSValueConst op2);
  412. /* Similar to same-value equality, but +0 and -0 are considered equal. */
  413. JS_EXTERN bool JS_IsSameValueZero(JSContext *ctx, JSValueConst op1, JSValueConst op2);
  414. /* Only used for running 262 tests. TODO(saghul) add build time flag. */
  415. JS_EXTERN JSValue js_string_codePointRange(JSContext *ctx, JSValueConst this_val,
  416. int argc, JSValueConst *argv);
  417. JS_EXTERN void *js_calloc_rt(JSRuntime *rt, size_t count, size_t size);
  418. JS_EXTERN void *js_malloc_rt(JSRuntime *rt, size_t size);
  419. JS_EXTERN void js_free_rt(JSRuntime *rt, void *ptr);
  420. JS_EXTERN void *js_realloc_rt(JSRuntime *rt, void *ptr, size_t size);
  421. JS_EXTERN size_t js_malloc_usable_size_rt(JSRuntime *rt, const void *ptr);
  422. JS_EXTERN void *js_mallocz_rt(JSRuntime *rt, size_t size);
  423. JS_EXTERN void *js_calloc(JSContext *ctx, size_t count, size_t size);
  424. JS_EXTERN void *js_malloc(JSContext *ctx, size_t size);
  425. JS_EXTERN void js_free(JSContext *ctx, void *ptr);
  426. JS_EXTERN void *js_realloc(JSContext *ctx, void *ptr, size_t size);
  427. JS_EXTERN size_t js_malloc_usable_size(JSContext *ctx, const void *ptr);
  428. JS_EXTERN void *js_realloc2(JSContext *ctx, void *ptr, size_t size, size_t *pslack);
  429. JS_EXTERN void *js_mallocz(JSContext *ctx, size_t size);
  430. JS_EXTERN char *js_strdup(JSContext *ctx, const char *str);
  431. JS_EXTERN char *js_strndup(JSContext *ctx, const char *s, size_t n);
  432. typedef struct JSMemoryUsage {
  433. int64_t malloc_size, malloc_limit, memory_used_size;
  434. int64_t malloc_count;
  435. int64_t memory_used_count;
  436. int64_t atom_count, atom_size;
  437. int64_t str_count, str_size;
  438. int64_t obj_count, obj_size;
  439. int64_t prop_count, prop_size;
  440. int64_t shape_count, shape_size;
  441. int64_t js_func_count, js_func_size, js_func_code_size;
  442. int64_t js_func_pc2line_count, js_func_pc2line_size;
  443. int64_t c_func_count, array_count;
  444. int64_t fast_array_count, fast_array_elements;
  445. int64_t binary_object_count, binary_object_size;
  446. } JSMemoryUsage;
  447. JS_EXTERN void JS_ComputeMemoryUsage(JSRuntime *rt, JSMemoryUsage *s);
  448. JS_EXTERN void JS_DumpMemoryUsage(FILE *fp, const JSMemoryUsage *s, JSRuntime *rt);
  449. /* atom support */
  450. #define JS_ATOM_NULL 0
  451. JS_EXTERN JSAtom JS_NewAtomLen(JSContext *ctx, const char *str, size_t len);
  452. JS_EXTERN JSAtom JS_NewAtom(JSContext *ctx, const char *str);
  453. JS_EXTERN JSAtom JS_NewAtomUInt32(JSContext *ctx, uint32_t n);
  454. JS_EXTERN JSAtom JS_DupAtom(JSContext *ctx, JSAtom v);
  455. JS_EXTERN void JS_FreeAtom(JSContext *ctx, JSAtom v);
  456. JS_EXTERN void JS_FreeAtomRT(JSRuntime *rt, JSAtom v);
  457. JS_EXTERN JSValue JS_AtomToValue(JSContext *ctx, JSAtom atom);
  458. JS_EXTERN JSValue JS_AtomToString(JSContext *ctx, JSAtom atom);
  459. JS_EXTERN const char *JS_AtomToCString(JSContext *ctx, JSAtom atom);
  460. JS_EXTERN JSAtom JS_ValueToAtom(JSContext *ctx, JSValueConst val);
  461. /* object class support */
  462. typedef struct JSPropertyEnum {
  463. bool is_enumerable;
  464. JSAtom atom;
  465. } JSPropertyEnum;
  466. typedef struct JSPropertyDescriptor {
  467. int flags;
  468. JSValue value;
  469. JSValue getter;
  470. JSValue setter;
  471. } JSPropertyDescriptor;
  472. typedef struct JSClassExoticMethods {
  473. /* Return -1 if exception (can only happen in case of Proxy object),
  474. false if the property does not exists, true if it exists. If 1 is
  475. returned, the property descriptor 'desc' is filled if != NULL. */
  476. int (*get_own_property)(JSContext *ctx, JSPropertyDescriptor *desc,
  477. JSValueConst obj, JSAtom prop);
  478. /* '*ptab' should hold the '*plen' property keys. Return 0 if OK,
  479. -1 if exception. The 'is_enumerable' field is ignored.
  480. */
  481. int (*get_own_property_names)(JSContext *ctx, JSPropertyEnum **ptab,
  482. uint32_t *plen, JSValueConst obj);
  483. /* return < 0 if exception, or true/false */
  484. int (*delete_property)(JSContext *ctx, JSValueConst obj, JSAtom prop);
  485. /* return < 0 if exception or true/false */
  486. int (*define_own_property)(JSContext *ctx, JSValueConst this_obj,
  487. JSAtom prop, JSValueConst val,
  488. JSValueConst getter, JSValueConst setter,
  489. int flags);
  490. /* The following methods can be emulated with the previous ones,
  491. so they are usually not needed */
  492. /* return < 0 if exception or true/false */
  493. int (*has_property)(JSContext *ctx, JSValueConst obj, JSAtom atom);
  494. JSValue (*get_property)(JSContext *ctx, JSValueConst obj, JSAtom atom,
  495. JSValueConst receiver);
  496. /* return < 0 if exception or true/false */
  497. int (*set_property)(JSContext *ctx, JSValueConst obj, JSAtom atom,
  498. JSValueConst value, JSValueConst receiver, int flags);
  499. } JSClassExoticMethods;
  500. typedef void JSClassFinalizer(JSRuntime *rt, JSValueConst val);
  501. typedef void JSClassGCMark(JSRuntime *rt, JSValueConst val,
  502. JS_MarkFunc *mark_func);
  503. #define JS_CALL_FLAG_CONSTRUCTOR (1 << 0)
  504. typedef JSValue JSClassCall(JSContext *ctx, JSValueConst func_obj,
  505. JSValueConst this_val, int argc,
  506. JSValueConst *argv, int flags);
  507. typedef struct JSClassDef {
  508. const char *class_name; /* pure ASCII only! */
  509. JSClassFinalizer *finalizer;
  510. JSClassGCMark *gc_mark;
  511. /* if call != NULL, the object is a function. If (flags &
  512. JS_CALL_FLAG_CONSTRUCTOR) != 0, the function is called as a
  513. constructor. In this case, 'this_val' is new.target. A
  514. constructor call only happens if the object constructor bit is
  515. set (see JS_SetConstructorBit()). */
  516. JSClassCall *call;
  517. /* XXX: suppress this indirection ? It is here only to save memory
  518. because only a few classes need these methods */
  519. JSClassExoticMethods *exotic;
  520. } JSClassDef;
  521. #define JS_EVAL_OPTIONS_VERSION 1
  522. typedef struct JSEvalOptions {
  523. int version;
  524. int eval_flags;
  525. const char *filename;
  526. int line_num;
  527. // can add new fields in ABI-compatible manner by incrementing JS_EVAL_OPTIONS_VERSION
  528. } JSEvalOptions;
  529. #define JS_INVALID_CLASS_ID 0
  530. JS_EXTERN JSClassID JS_NewClassID(JSRuntime *rt, JSClassID *pclass_id);
  531. /* Returns the class ID if `v` is an object, otherwise returns JS_INVALID_CLASS_ID. */
  532. JS_EXTERN JSClassID JS_GetClassID(JSValueConst v);
  533. JS_EXTERN int JS_NewClass(JSRuntime *rt, JSClassID class_id, const JSClassDef *class_def);
  534. JS_EXTERN bool JS_IsRegisteredClass(JSRuntime *rt, JSClassID class_id);
  535. /* value handling */
  536. static js_force_inline JSValue JS_NewBool(JSContext *ctx, bool val)
  537. {
  538. (void)&ctx;
  539. return JS_MKVAL(JS_TAG_BOOL, (val != 0));
  540. }
  541. static js_force_inline JSValue JS_NewInt32(JSContext *ctx, int32_t val)
  542. {
  543. (void)&ctx;
  544. return JS_MKVAL(JS_TAG_INT, val);
  545. }
  546. static js_force_inline JSValue JS_NewFloat64(JSContext *ctx, double val)
  547. {
  548. (void)&ctx;
  549. return __JS_NewFloat64(val);
  550. }
  551. static js_force_inline JSValue JS_NewCatchOffset(JSContext *ctx, int32_t val)
  552. {
  553. (void)&ctx;
  554. return JS_MKVAL(JS_TAG_CATCH_OFFSET, val);
  555. }
  556. static js_force_inline JSValue JS_NewInt64(JSContext *ctx, int64_t val)
  557. {
  558. JSValue v;
  559. if (val >= INT32_MIN && val <= INT32_MAX) {
  560. v = JS_NewInt32(ctx, (int32_t)val);
  561. } else {
  562. v = JS_NewFloat64(ctx, (double)val);
  563. }
  564. return v;
  565. }
  566. static js_force_inline JSValue JS_NewUint32(JSContext *ctx, uint32_t val)
  567. {
  568. JSValue v;
  569. if (val <= INT32_MAX) {
  570. v = JS_NewInt32(ctx, (int32_t)val);
  571. } else {
  572. v = JS_NewFloat64(ctx, (double)val);
  573. }
  574. return v;
  575. }
  576. JS_EXTERN JSValue JS_NewNumber(JSContext *ctx, double d);
  577. JS_EXTERN JSValue JS_NewBigInt64(JSContext *ctx, int64_t v);
  578. JS_EXTERN JSValue JS_NewBigUint64(JSContext *ctx, uint64_t v);
  579. static inline bool JS_IsNumber(JSValueConst v)
  580. {
  581. int tag = JS_VALUE_GET_TAG(v);
  582. return tag == JS_TAG_INT || JS_TAG_IS_FLOAT64(tag);
  583. }
  584. static inline bool JS_IsBigInt(JSContext *ctx, JSValueConst v)
  585. {
  586. (void)&ctx;
  587. return JS_VALUE_GET_TAG(v) == JS_TAG_BIG_INT;
  588. }
  589. static inline bool JS_IsBool(JSValueConst v)
  590. {
  591. return JS_VALUE_GET_TAG(v) == JS_TAG_BOOL;
  592. }
  593. static inline bool JS_IsNull(JSValueConst v)
  594. {
  595. return JS_VALUE_GET_TAG(v) == JS_TAG_NULL;
  596. }
  597. static inline bool JS_IsUndefined(JSValueConst v)
  598. {
  599. return JS_VALUE_GET_TAG(v) == JS_TAG_UNDEFINED;
  600. }
  601. static inline bool JS_IsException(JSValueConst v)
  602. {
  603. return JS_VALUE_GET_TAG(v) == JS_TAG_EXCEPTION;
  604. }
  605. static inline bool JS_IsUninitialized(JSValueConst v)
  606. {
  607. return JS_VALUE_GET_TAG(v) == JS_TAG_UNINITIALIZED;
  608. }
  609. static inline bool JS_IsString(JSValueConst v)
  610. {
  611. return JS_VALUE_GET_TAG(v) == JS_TAG_STRING;
  612. }
  613. static inline bool JS_IsSymbol(JSValueConst v)
  614. {
  615. return JS_VALUE_GET_TAG(v) == JS_TAG_SYMBOL;
  616. }
  617. static inline bool JS_IsObject(JSValueConst v)
  618. {
  619. return JS_VALUE_GET_TAG(v) == JS_TAG_OBJECT;
  620. }
  621. static inline bool JS_IsModule(JSValueConst v)
  622. {
  623. return JS_VALUE_GET_TAG(v) == JS_TAG_MODULE;
  624. }
  625. JS_EXTERN JSValue JS_Throw(JSContext *ctx, JSValue obj);
  626. JS_EXTERN JSValue JS_GetException(JSContext *ctx);
  627. JS_EXTERN bool JS_HasException(JSContext *ctx);
  628. JS_EXTERN bool JS_IsError(JSContext *ctx, JSValueConst val);
  629. JS_EXTERN bool JS_IsUncatchableError(JSContext* ctx, JSValueConst val);
  630. JS_EXTERN void JS_SetUncatchableError(JSContext *ctx, JSValueConst val);
  631. JS_EXTERN void JS_ClearUncatchableError(JSContext *ctx, JSValueConst val);
  632. // Shorthand for:
  633. // JSValue exc = JS_GetException(ctx);
  634. // JS_ClearUncatchableError(ctx, exc);
  635. // JS_Throw(ctx, exc);
  636. JS_EXTERN void JS_ResetUncatchableError(JSContext *ctx);
  637. JS_EXTERN JSValue JS_NewError(JSContext *ctx);
  638. JS_EXTERN JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_ThrowPlainError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...);
  639. JS_EXTERN JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_ThrowSyntaxError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...);
  640. JS_EXTERN JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_ThrowTypeError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...);
  641. JS_EXTERN JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_ThrowReferenceError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...);
  642. JS_EXTERN JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_ThrowRangeError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...);
  643. JS_EXTERN JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_ThrowInternalError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...);
  644. JS_EXTERN JSValue JS_ThrowOutOfMemory(JSContext *ctx);
  645. JS_EXTERN void JS_FreeValue(JSContext *ctx, JSValue v);
  646. JS_EXTERN void JS_FreeValueRT(JSRuntime *rt, JSValue v);
  647. JS_EXTERN JSValue JS_DupValue(JSContext *ctx, JSValueConst v);
  648. JS_EXTERN JSValue JS_DupValueRT(JSRuntime *rt, JSValueConst v);
  649. JS_EXTERN int JS_ToBool(JSContext *ctx, JSValueConst val); /* return -1 for JS_EXCEPTION */
  650. static inline JSValue JS_ToBoolean(JSContext *ctx, JSValueConst val)
  651. {
  652. return JS_NewBool(ctx, JS_ToBool(ctx, val));
  653. }
  654. JS_EXTERN JSValue JS_ToNumber(JSContext *ctx, JSValueConst val);
  655. JS_EXTERN int JS_ToInt32(JSContext *ctx, int32_t *pres, JSValueConst val);
  656. static inline int JS_ToUint32(JSContext *ctx, uint32_t *pres, JSValueConst val)
  657. {
  658. return JS_ToInt32(ctx, (int32_t*)pres, val);
  659. }
  660. JS_EXTERN int JS_ToInt64(JSContext *ctx, int64_t *pres, JSValueConst val);
  661. JS_EXTERN int JS_ToIndex(JSContext *ctx, uint64_t *plen, JSValueConst val);
  662. JS_EXTERN int JS_ToFloat64(JSContext *ctx, double *pres, JSValueConst val);
  663. /* return an exception if 'val' is a Number */
  664. JS_EXTERN int JS_ToBigInt64(JSContext *ctx, int64_t *pres, JSValueConst val);
  665. JS_EXTERN int JS_ToBigUint64(JSContext *ctx, uint64_t *pres, JSValueConst val);
  666. /* same as JS_ToInt64() but allow BigInt */
  667. JS_EXTERN int JS_ToInt64Ext(JSContext *ctx, int64_t *pres, JSValueConst val);
  668. JS_EXTERN JSValue JS_NewStringLen(JSContext *ctx, const char *str1, size_t len1);
  669. static inline JSValue JS_NewString(JSContext *ctx, const char *str) {
  670. return JS_NewStringLen(ctx, str, strlen(str));
  671. }
  672. JS_EXTERN JSValue JS_NewAtomString(JSContext *ctx, const char *str);
  673. JS_EXTERN JSValue JS_ToString(JSContext *ctx, JSValueConst val);
  674. JS_EXTERN JSValue JS_ToPropertyKey(JSContext *ctx, JSValueConst val);
  675. JS_EXTERN const char *JS_ToCStringLen2(JSContext *ctx, size_t *plen, JSValueConst val1, bool cesu8);
  676. static inline const char *JS_ToCStringLen(JSContext *ctx, size_t *plen, JSValueConst val1)
  677. {
  678. return JS_ToCStringLen2(ctx, plen, val1, 0);
  679. }
  680. static inline const char *JS_ToCString(JSContext *ctx, JSValueConst val1)
  681. {
  682. return JS_ToCStringLen2(ctx, NULL, val1, 0);
  683. }
  684. JS_EXTERN void JS_FreeCString(JSContext *ctx, const char *ptr);
  685. JS_EXTERN JSValue JS_NewObjectProtoClass(JSContext *ctx, JSValueConst proto,
  686. JSClassID class_id);
  687. JS_EXTERN JSValue JS_NewObjectClass(JSContext *ctx, int class_id);
  688. JS_EXTERN JSValue JS_NewObjectProto(JSContext *ctx, JSValueConst proto);
  689. JS_EXTERN JSValue JS_NewObject(JSContext *ctx);
  690. // takes ownership of the values
  691. JS_EXTERN JSValue JS_NewObjectFrom(JSContext *ctx, int count,
  692. const JSAtom *props,
  693. const JSValue *values);
  694. // takes ownership of the values
  695. JS_EXTERN JSValue JS_NewObjectFromStr(JSContext *ctx, int count,
  696. const char **props,
  697. const JSValue *values);
  698. JS_EXTERN JSValue JS_ToObject(JSContext *ctx, JSValueConst val);
  699. JS_EXTERN JSValue JS_ToObjectString(JSContext *ctx, JSValueConst val);
  700. JS_EXTERN bool JS_IsFunction(JSContext* ctx, JSValueConst val);
  701. JS_EXTERN bool JS_IsConstructor(JSContext* ctx, JSValueConst val);
  702. JS_EXTERN bool JS_SetConstructorBit(JSContext *ctx, JSValueConst func_obj, bool val);
  703. JS_EXTERN bool JS_IsRegExp(JSValueConst val);
  704. JS_EXTERN bool JS_IsMap(JSValueConst val);
  705. JS_EXTERN JSValue JS_NewArray(JSContext *ctx);
  706. // takes ownership of the values
  707. JS_EXTERN JSValue JS_NewArrayFrom(JSContext *ctx, int count,
  708. const JSValue *values);
  709. // reader beware: JS_IsArray used to "punch" through proxies and check
  710. // if the target object is an array but it no longer does; use JS_IsProxy
  711. // and JS_GetProxyTarget instead, and remember that the target itself can
  712. // also be a proxy, ad infinitum
  713. JS_EXTERN bool JS_IsArray(JSValueConst val);
  714. JS_EXTERN bool JS_IsProxy(JSValueConst val);
  715. JS_EXTERN JSValue JS_GetProxyTarget(JSContext *ctx, JSValueConst proxy);
  716. JS_EXTERN JSValue JS_GetProxyHandler(JSContext *ctx, JSValueConst proxy);
  717. JS_EXTERN JSValue JS_NewDate(JSContext *ctx, double epoch_ms);
  718. JS_EXTERN bool JS_IsDate(JSValueConst v);
  719. JS_EXTERN JSValue JS_GetProperty(JSContext *ctx, JSValueConst this_obj, JSAtom prop);
  720. JS_EXTERN JSValue JS_GetPropertyUint32(JSContext *ctx, JSValueConst this_obj,
  721. uint32_t idx);
  722. JS_EXTERN JSValue JS_GetPropertyInt64(JSContext *ctx, JSValueConst this_obj,
  723. int64_t idx);
  724. JS_EXTERN JSValue JS_GetPropertyStr(JSContext *ctx, JSValueConst this_obj,
  725. const char *prop);
  726. JS_EXTERN int JS_SetProperty(JSContext *ctx, JSValueConst this_obj,
  727. JSAtom prop, JSValue val);
  728. JS_EXTERN int JS_SetPropertyUint32(JSContext *ctx, JSValueConst this_obj,
  729. uint32_t idx, JSValue val);
  730. JS_EXTERN int JS_SetPropertyInt64(JSContext *ctx, JSValueConst this_obj,
  731. int64_t idx, JSValue val);
  732. JS_EXTERN int JS_SetPropertyStr(JSContext *ctx, JSValueConst this_obj,
  733. const char *prop, JSValue val);
  734. JS_EXTERN int JS_HasProperty(JSContext *ctx, JSValueConst this_obj, JSAtom prop);
  735. JS_EXTERN int JS_IsExtensible(JSContext *ctx, JSValueConst obj);
  736. JS_EXTERN int JS_PreventExtensions(JSContext *ctx, JSValueConst obj);
  737. JS_EXTERN int JS_DeleteProperty(JSContext *ctx, JSValueConst obj, JSAtom prop, int flags);
  738. JS_EXTERN int JS_SetPrototype(JSContext *ctx, JSValueConst obj, JSValue proto_val);
  739. JS_EXTERN JSValue JS_GetPrototype(JSContext *ctx, JSValueConst val);
  740. JS_EXTERN int JS_GetLength(JSContext *ctx, JSValueConst obj, int64_t *pres);
  741. JS_EXTERN int JS_SetLength(JSContext *ctx, JSValueConst obj, int64_t len);
  742. JS_EXTERN int JS_SealObject(JSContext *ctx, JSValueConst obj);
  743. JS_EXTERN int JS_FreezeObject(JSContext *ctx, JSValueConst obj);
  744. #define JS_GPN_STRING_MASK (1 << 0)
  745. #define JS_GPN_SYMBOL_MASK (1 << 1)
  746. #define JS_GPN_PRIVATE_MASK (1 << 2)
  747. /* only include the enumerable properties */
  748. #define JS_GPN_ENUM_ONLY (1 << 4)
  749. /* set theJSPropertyEnum.is_enumerable field */
  750. #define JS_GPN_SET_ENUM (1 << 5)
  751. JS_EXTERN int JS_GetOwnPropertyNames(JSContext *ctx, JSPropertyEnum **ptab,
  752. uint32_t *plen, JSValueConst obj,
  753. int flags);
  754. JS_EXTERN int JS_GetOwnProperty(JSContext *ctx, JSPropertyDescriptor *desc,
  755. JSValueConst obj, JSAtom prop);
  756. JS_EXTERN void JS_FreePropertyEnum(JSContext *ctx, JSPropertyEnum *tab,
  757. uint32_t len);
  758. JS_EXTERN JSValue JS_Call(JSContext *ctx, JSValueConst func_obj,
  759. JSValueConst this_obj, int argc, JSValueConst *argv);
  760. JS_EXTERN JSValue JS_Invoke(JSContext *ctx, JSValueConst this_val, JSAtom atom,
  761. int argc, JSValueConst *argv);
  762. JS_EXTERN JSValue JS_CallConstructor(JSContext *ctx, JSValueConst func_obj,
  763. int argc, JSValueConst *argv);
  764. JS_EXTERN JSValue JS_CallConstructor2(JSContext *ctx, JSValueConst func_obj,
  765. JSValueConst new_target,
  766. int argc, JSValueConst *argv);
  767. /* Try to detect if the input is a module. Returns true if parsing the input
  768. * as a module produces no syntax errors. It's a naive approach that is not
  769. * wholly infallible: non-strict classic scripts may _parse_ okay as a module
  770. * but not _execute_ as one (different runtime semantics.) Use with caution.
  771. * |input| can be either ASCII or UTF-8 encoded source code.
  772. */
  773. JS_EXTERN bool JS_DetectModule(const char *input, size_t input_len);
  774. /* 'input' must be zero terminated i.e. input[input_len] = '\0'. */
  775. JS_EXTERN JSValue JS_Eval(JSContext *ctx, const char *input, size_t input_len,
  776. const char *filename, int eval_flags);
  777. JS_EXTERN JSValue JS_Eval2(JSContext *ctx, const char *input, size_t input_len,
  778. JSEvalOptions *options);
  779. JS_EXTERN JSValue JS_EvalThis(JSContext *ctx, JSValueConst this_obj,
  780. const char *input, size_t input_len,
  781. const char *filename, int eval_flags);
  782. JS_EXTERN JSValue JS_EvalThis2(JSContext *ctx, JSValueConst this_obj,
  783. const char *input, size_t input_len,
  784. JSEvalOptions *options);
  785. JS_EXTERN JSValue JS_GetGlobalObject(JSContext *ctx);
  786. JS_EXTERN int JS_IsInstanceOf(JSContext *ctx, JSValueConst val, JSValueConst obj);
  787. JS_EXTERN int JS_DefineProperty(JSContext *ctx, JSValueConst this_obj,
  788. JSAtom prop, JSValueConst val,
  789. JSValueConst getter, JSValueConst setter,
  790. int flags);
  791. JS_EXTERN int JS_DefinePropertyValue(JSContext *ctx, JSValueConst this_obj,
  792. JSAtom prop, JSValue val, int flags);
  793. JS_EXTERN int JS_DefinePropertyValueUint32(JSContext *ctx, JSValueConst this_obj,
  794. uint32_t idx, JSValue val, int flags);
  795. JS_EXTERN int JS_DefinePropertyValueStr(JSContext *ctx, JSValueConst this_obj,
  796. const char *prop, JSValue val, int flags);
  797. JS_EXTERN int JS_DefinePropertyGetSet(JSContext *ctx, JSValueConst this_obj,
  798. JSAtom prop, JSValue getter, JSValue setter,
  799. int flags);
  800. /* Only supported for custom classes, returns 0 on success < 0 otherwise. */
  801. JS_EXTERN int JS_SetOpaque(JSValueConst obj, void *opaque);
  802. JS_EXTERN void *JS_GetOpaque(JSValueConst obj, JSClassID class_id);
  803. JS_EXTERN void *JS_GetOpaque2(JSContext *ctx, JSValueConst obj, JSClassID class_id);
  804. JS_EXTERN void *JS_GetAnyOpaque(JSValueConst obj, JSClassID *class_id);
  805. /* 'buf' must be zero terminated i.e. buf[buf_len] = '\0'. */
  806. JS_EXTERN JSValue JS_ParseJSON(JSContext *ctx, const char *buf, size_t buf_len,
  807. const char *filename);
  808. JS_EXTERN JSValue JS_JSONStringify(JSContext *ctx, JSValueConst obj,
  809. JSValueConst replacer, JSValueConst space0);
  810. typedef void JSFreeArrayBufferDataFunc(JSRuntime *rt, void *opaque, void *ptr);
  811. JS_EXTERN JSValue JS_NewArrayBuffer(JSContext *ctx, uint8_t *buf, size_t len,
  812. JSFreeArrayBufferDataFunc *free_func, void *opaque,
  813. bool is_shared);
  814. JS_EXTERN JSValue JS_NewArrayBufferCopy(JSContext *ctx, const uint8_t *buf, size_t len);
  815. JS_EXTERN void JS_DetachArrayBuffer(JSContext *ctx, JSValueConst obj);
  816. JS_EXTERN uint8_t *JS_GetArrayBuffer(JSContext *ctx, size_t *psize, JSValueConst obj);
  817. JS_EXTERN bool JS_IsArrayBuffer(JSValueConst obj);
  818. JS_EXTERN uint8_t *JS_GetUint8Array(JSContext *ctx, size_t *psize, JSValueConst obj);
  819. typedef enum JSTypedArrayEnum {
  820. JS_TYPED_ARRAY_UINT8C = 0,
  821. JS_TYPED_ARRAY_INT8,
  822. JS_TYPED_ARRAY_UINT8,
  823. JS_TYPED_ARRAY_INT16,
  824. JS_TYPED_ARRAY_UINT16,
  825. JS_TYPED_ARRAY_INT32,
  826. JS_TYPED_ARRAY_UINT32,
  827. JS_TYPED_ARRAY_BIG_INT64,
  828. JS_TYPED_ARRAY_BIG_UINT64,
  829. JS_TYPED_ARRAY_FLOAT16,
  830. JS_TYPED_ARRAY_FLOAT32,
  831. JS_TYPED_ARRAY_FLOAT64,
  832. } JSTypedArrayEnum;
  833. JS_EXTERN JSValue JS_NewTypedArray(JSContext *ctx, int argc, JSValueConst *argv,
  834. JSTypedArrayEnum array_type);
  835. JS_EXTERN JSValue JS_GetTypedArrayBuffer(JSContext *ctx, JSValueConst obj,
  836. size_t *pbyte_offset,
  837. size_t *pbyte_length,
  838. size_t *pbytes_per_element);
  839. JS_EXTERN JSValue JS_NewUint8Array(JSContext *ctx, uint8_t *buf, size_t len,
  840. JSFreeArrayBufferDataFunc *free_func, void *opaque,
  841. bool is_shared);
  842. /* returns -1 if not a typed array otherwise return a JSTypedArrayEnum value */
  843. JS_EXTERN int JS_GetTypedArrayType(JSValueConst obj);
  844. JS_EXTERN JSValue JS_NewUint8ArrayCopy(JSContext *ctx, const uint8_t *buf, size_t len);
  845. typedef struct {
  846. void *(*sab_alloc)(void *opaque, size_t size);
  847. void (*sab_free)(void *opaque, void *ptr);
  848. void (*sab_dup)(void *opaque, void *ptr);
  849. void *sab_opaque;
  850. } JSSharedArrayBufferFunctions;
  851. JS_EXTERN void JS_SetSharedArrayBufferFunctions(JSRuntime *rt, const JSSharedArrayBufferFunctions *sf);
  852. typedef enum JSPromiseStateEnum {
  853. JS_PROMISE_PENDING,
  854. JS_PROMISE_FULFILLED,
  855. JS_PROMISE_REJECTED,
  856. } JSPromiseStateEnum;
  857. JS_EXTERN JSValue JS_NewPromiseCapability(JSContext *ctx, JSValue *resolving_funcs);
  858. JS_EXTERN JSPromiseStateEnum JS_PromiseState(JSContext *ctx,
  859. JSValueConst promise);
  860. JS_EXTERN JSValue JS_PromiseResult(JSContext *ctx, JSValueConst promise);
  861. JS_EXTERN bool JS_IsPromise(JSValueConst val);
  862. JS_EXTERN JSValue JS_NewSymbol(JSContext *ctx, const char *description, bool is_global);
  863. /* is_handled = true means that the rejection is handled */
  864. typedef void JSHostPromiseRejectionTracker(JSContext *ctx, JSValueConst promise,
  865. JSValueConst reason,
  866. bool is_handled, void *opaque);
  867. JS_EXTERN void JS_SetHostPromiseRejectionTracker(JSRuntime *rt, JSHostPromiseRejectionTracker *cb, void *opaque);
  868. /* return != 0 if the JS code needs to be interrupted */
  869. typedef int JSInterruptHandler(JSRuntime *rt, void *opaque);
  870. JS_EXTERN void JS_SetInterruptHandler(JSRuntime *rt, JSInterruptHandler *cb, void *opaque);
  871. /* if can_block is true, Atomics.wait() can be used */
  872. JS_EXTERN void JS_SetCanBlock(JSRuntime *rt, bool can_block);
  873. /* set the [IsHTMLDDA] internal slot */
  874. JS_EXTERN void JS_SetIsHTMLDDA(JSContext *ctx, JSValueConst obj);
  875. typedef struct JSModuleDef JSModuleDef;
  876. /* return the module specifier (allocated with js_malloc()) or NULL if
  877. exception */
  878. typedef char *JSModuleNormalizeFunc(JSContext *ctx,
  879. const char *module_base_name,
  880. const char *module_name, void *opaque);
  881. typedef JSModuleDef *JSModuleLoaderFunc(JSContext *ctx,
  882. const char *module_name, void *opaque);
  883. /* module_normalize = NULL is allowed and invokes the default module
  884. filename normalizer */
  885. JS_EXTERN void JS_SetModuleLoaderFunc(JSRuntime *rt,
  886. JSModuleNormalizeFunc *module_normalize,
  887. JSModuleLoaderFunc *module_loader, void *opaque);
  888. /* return the import.meta object of a module */
  889. JS_EXTERN JSValue JS_GetImportMeta(JSContext *ctx, JSModuleDef *m);
  890. JS_EXTERN JSAtom JS_GetModuleName(JSContext *ctx, JSModuleDef *m);
  891. JS_EXTERN JSValue JS_GetModuleNamespace(JSContext *ctx, JSModuleDef *m);
  892. /* JS Job support */
  893. typedef JSValue JSJobFunc(JSContext *ctx, int argc, JSValueConst *argv);
  894. JS_EXTERN int JS_EnqueueJob(JSContext *ctx, JSJobFunc *job_func,
  895. int argc, JSValueConst *argv);
  896. JS_EXTERN bool JS_IsJobPending(JSRuntime *rt);
  897. JS_EXTERN int JS_ExecutePendingJob(JSRuntime *rt, JSContext **pctx);
  898. /* Structure to retrieve (de)serialized SharedArrayBuffer objects. */
  899. typedef struct JSSABTab {
  900. uint8_t **tab;
  901. size_t len;
  902. } JSSABTab;
  903. /* Object Writer/Reader (currently only used to handle precompiled code) */
  904. #define JS_WRITE_OBJ_BYTECODE (1 << 0) /* allow function/module */
  905. #define JS_WRITE_OBJ_BSWAP (0) /* byte swapped output (obsolete, handled transparently) */
  906. #define JS_WRITE_OBJ_SAB (1 << 2) /* allow SharedArrayBuffer */
  907. #define JS_WRITE_OBJ_REFERENCE (1 << 3) /* allow object references to encode arbitrary object graph */
  908. #define JS_WRITE_OBJ_STRIP_SOURCE (1 << 4) /* do not write source code information */
  909. #define JS_WRITE_OBJ_STRIP_DEBUG (1 << 5) /* do not write debug information */
  910. JS_EXTERN uint8_t *JS_WriteObject(JSContext *ctx, size_t *psize, JSValueConst obj, int flags);
  911. JS_EXTERN uint8_t *JS_WriteObject2(JSContext *ctx, size_t *psize, JSValueConst obj,
  912. int flags, JSSABTab *psab_tab);
  913. #define JS_READ_OBJ_BYTECODE (1 << 0) /* allow function/module */
  914. #define JS_READ_OBJ_ROM_DATA (0) /* avoid duplicating 'buf' data (obsolete, broken by ICs) */
  915. #define JS_READ_OBJ_SAB (1 << 2) /* allow SharedArrayBuffer */
  916. #define JS_READ_OBJ_REFERENCE (1 << 3) /* allow object references */
  917. JS_EXTERN JSValue JS_ReadObject(JSContext *ctx, const uint8_t *buf, size_t buf_len, int flags);
  918. JS_EXTERN JSValue JS_ReadObject2(JSContext *ctx, const uint8_t *buf, size_t buf_len,
  919. int flags, JSSABTab *psab_tab);
  920. /* instantiate and evaluate a bytecode function. Only used when
  921. reading a script or module with JS_ReadObject() */
  922. JS_EXTERN JSValue JS_EvalFunction(JSContext *ctx, JSValue fun_obj);
  923. /* load the dependencies of the module 'obj'. Useful when JS_ReadObject()
  924. returns a module. */
  925. JS_EXTERN int JS_ResolveModule(JSContext *ctx, JSValueConst obj);
  926. /* only exported for os.Worker() */
  927. JS_EXTERN JSAtom JS_GetScriptOrModuleName(JSContext *ctx, int n_stack_levels);
  928. /* only exported for os.Worker() */
  929. JS_EXTERN JSValue JS_LoadModule(JSContext *ctx, const char *basename,
  930. const char *filename);
  931. /* C function definition */
  932. typedef enum JSCFunctionEnum { /* XXX: should rename for namespace isolation */
  933. JS_CFUNC_generic,
  934. JS_CFUNC_generic_magic,
  935. JS_CFUNC_constructor,
  936. JS_CFUNC_constructor_magic,
  937. JS_CFUNC_constructor_or_func,
  938. JS_CFUNC_constructor_or_func_magic,
  939. JS_CFUNC_f_f,
  940. JS_CFUNC_f_f_f,
  941. JS_CFUNC_getter,
  942. JS_CFUNC_setter,
  943. JS_CFUNC_getter_magic,
  944. JS_CFUNC_setter_magic,
  945. JS_CFUNC_iterator_next,
  946. } JSCFunctionEnum;
  947. typedef union JSCFunctionType {
  948. JSCFunction *generic;
  949. JSValue (*generic_magic)(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv, int magic);
  950. JSCFunction *constructor;
  951. JSValue (*constructor_magic)(JSContext *ctx, JSValueConst new_target, int argc, JSValueConst *argv, int magic);
  952. JSCFunction *constructor_or_func;
  953. double (*f_f)(double);
  954. double (*f_f_f)(double, double);
  955. JSValue (*getter)(JSContext *ctx, JSValueConst this_val);
  956. JSValue (*setter)(JSContext *ctx, JSValueConst this_val, JSValueConst val);
  957. JSValue (*getter_magic)(JSContext *ctx, JSValueConst this_val, int magic);
  958. JSValue (*setter_magic)(JSContext *ctx, JSValueConst this_val, JSValueConst val, int magic);
  959. JSValue (*iterator_next)(JSContext *ctx, JSValueConst this_val,
  960. int argc, JSValueConst *argv, int *pdone, int magic);
  961. } JSCFunctionType;
  962. JS_EXTERN JSValue JS_NewCFunction2(JSContext *ctx, JSCFunction *func,
  963. const char *name,
  964. int length, JSCFunctionEnum cproto, int magic);
  965. JS_EXTERN JSValue JS_NewCFunction3(JSContext *ctx, JSCFunction *func,
  966. const char *name,
  967. int length, JSCFunctionEnum cproto, int magic,
  968. JSValue proto_val);
  969. JS_EXTERN JSValue JS_NewCFunctionData(JSContext *ctx, JSCFunctionData *func,
  970. int length, int magic, int data_len,
  971. JSValueConst *data);
  972. static inline JSValue JS_NewCFunction(JSContext *ctx, JSCFunction *func,
  973. const char *name, int length)
  974. {
  975. return JS_NewCFunction2(ctx, func, name, length, JS_CFUNC_generic, 0);
  976. }
  977. static inline JSValue JS_NewCFunctionMagic(JSContext *ctx, JSCFunctionMagic *func,
  978. const char *name, int length,
  979. JSCFunctionEnum cproto, int magic)
  980. {
  981. /* Used to squelch a -Wcast-function-type warning. */
  982. JSCFunctionType ft;
  983. ft.generic_magic = func;
  984. return JS_NewCFunction2(ctx, ft.generic, name, length, cproto, magic);
  985. }
  986. JS_EXTERN void JS_SetConstructor(JSContext *ctx, JSValueConst func_obj,
  987. JSValueConst proto);
  988. /* C property definition */
  989. typedef struct JSCFunctionListEntry {
  990. const char *name; /* pure ASCII or UTF-8 encoded */
  991. uint8_t prop_flags;
  992. uint8_t def_type;
  993. int16_t magic;
  994. union {
  995. struct {
  996. uint8_t length; /* XXX: should move outside union */
  997. uint8_t cproto; /* XXX: should move outside union */
  998. JSCFunctionType cfunc;
  999. } func;
  1000. struct {
  1001. JSCFunctionType get;
  1002. JSCFunctionType set;
  1003. } getset;
  1004. struct {
  1005. const char *name;
  1006. int base;
  1007. } alias;
  1008. struct {
  1009. const struct JSCFunctionListEntry *tab;
  1010. int len;
  1011. } prop_list;
  1012. const char *str; /* pure ASCII or UTF-8 encoded */
  1013. int32_t i32;
  1014. int64_t i64;
  1015. uint64_t u64;
  1016. double f64;
  1017. } u;
  1018. } JSCFunctionListEntry;
  1019. #define JS_DEF_CFUNC 0
  1020. #define JS_DEF_CGETSET 1
  1021. #define JS_DEF_CGETSET_MAGIC 2
  1022. #define JS_DEF_PROP_STRING 3
  1023. #define JS_DEF_PROP_INT32 4
  1024. #define JS_DEF_PROP_INT64 5
  1025. #define JS_DEF_PROP_DOUBLE 6
  1026. #define JS_DEF_PROP_UNDEFINED 7
  1027. #define JS_DEF_OBJECT 8
  1028. #define JS_DEF_ALIAS 9
  1029. /* Note: c++ does not like nested designators */
  1030. #define JS_CFUNC_DEF(name, length, func1) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE, JS_DEF_CFUNC, 0, { .func = { length, JS_CFUNC_generic, { .generic = func1 } } } }
  1031. #define JS_CFUNC_DEF2(name, length, func1, prop_flags) { name, prop_flags, JS_DEF_CFUNC, 0, { .func = { length, JS_CFUNC_generic, { .generic = func1 } } } }
  1032. #define JS_CFUNC_MAGIC_DEF(name, length, func1, magic) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE, JS_DEF_CFUNC, magic, { .func = { length, JS_CFUNC_generic_magic, { .generic_magic = func1 } } } }
  1033. #define JS_CFUNC_SPECIAL_DEF(name, length, cproto, func1) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE, JS_DEF_CFUNC, 0, { .func = { length, JS_CFUNC_ ## cproto, { .cproto = func1 } } } }
  1034. #define JS_ITERATOR_NEXT_DEF(name, length, func1, magic) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE, JS_DEF_CFUNC, magic, { .func = { length, JS_CFUNC_iterator_next, { .iterator_next = func1 } } } }
  1035. #define JS_CGETSET_DEF(name, fgetter, fsetter) { name, JS_PROP_CONFIGURABLE, JS_DEF_CGETSET, 0, { .getset = { .get = { .getter = fgetter }, .set = { .setter = fsetter } } } }
  1036. #define JS_CGETSET_DEF2(name, fgetter, fsetter, prop_flags) { name, prop_flags, JS_DEF_CGETSET, 0, { .getset = { .get = { .getter = fgetter }, .set = { .setter = fsetter } } } }
  1037. #define JS_CGETSET_MAGIC_DEF(name, fgetter, fsetter, magic) { name, JS_PROP_CONFIGURABLE, JS_DEF_CGETSET_MAGIC, magic, { .getset = { .get = { .getter_magic = fgetter }, .set = { .setter_magic = fsetter } } } }
  1038. #define JS_PROP_STRING_DEF(name, cstr, prop_flags) { name, prop_flags, JS_DEF_PROP_STRING, 0, { .str = cstr } }
  1039. #define JS_PROP_INT32_DEF(name, val, prop_flags) { name, prop_flags, JS_DEF_PROP_INT32, 0, { .i32 = val } }
  1040. #define JS_PROP_INT64_DEF(name, val, prop_flags) { name, prop_flags, JS_DEF_PROP_INT64, 0, { .i64 = val } }
  1041. #define JS_PROP_DOUBLE_DEF(name, val, prop_flags) { name, prop_flags, JS_DEF_PROP_DOUBLE, 0, { .f64 = val } }
  1042. #define JS_PROP_U2D_DEF(name, val, prop_flags) { name, prop_flags, JS_DEF_PROP_DOUBLE, 0, { .u64 = val } }
  1043. #define JS_PROP_UNDEFINED_DEF(name, prop_flags) { name, prop_flags, JS_DEF_PROP_UNDEFINED, 0, { .i32 = 0 } }
  1044. #define JS_OBJECT_DEF(name, tab, len, prop_flags) { name, prop_flags, JS_DEF_OBJECT, 0, { .prop_list = { tab, len } } }
  1045. #define JS_ALIAS_DEF(name, from) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE, JS_DEF_ALIAS, 0, { .alias = { from, -1 } } }
  1046. #define JS_ALIAS_BASE_DEF(name, from, base) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE, JS_DEF_ALIAS, 0, { .alias = { from, base } } }
  1047. JS_EXTERN void JS_SetPropertyFunctionList(JSContext *ctx, JSValue obj,
  1048. const JSCFunctionListEntry *tab,
  1049. int len);
  1050. /* C module definition */
  1051. typedef int JSModuleInitFunc(JSContext *ctx, JSModuleDef *m);
  1052. JS_EXTERN JSModuleDef *JS_NewCModule(JSContext *ctx, const char *name_str,
  1053. JSModuleInitFunc *func);
  1054. /* can only be called before the module is instantiated */
  1055. JS_EXTERN int JS_AddModuleExport(JSContext *ctx, JSModuleDef *m, const char *name_str);
  1056. JS_EXTERN int JS_AddModuleExportList(JSContext *ctx, JSModuleDef *m,
  1057. const JSCFunctionListEntry *tab, int len);
  1058. /* can only be called after the module is instantiated */
  1059. JS_EXTERN int JS_SetModuleExport(JSContext *ctx, JSModuleDef *m, const char *export_name,
  1060. JSValue val);
  1061. JS_EXTERN int JS_SetModuleExportList(JSContext *ctx, JSModuleDef *m,
  1062. const JSCFunctionListEntry *tab, int len);
  1063. /* Version */
  1064. #define QJS_VERSION_MAJOR 0
  1065. #define QJS_VERSION_MINOR 9
  1066. #define QJS_VERSION_PATCH 0
  1067. #define QJS_VERSION_SUFFIX ""
  1068. JS_EXTERN const char* JS_GetVersion(void);
  1069. /* Integration point for quickjs-libc.c, not for public use. */
  1070. JS_EXTERN uintptr_t js_std_cmd(int cmd, ...);
  1071. #undef JS_EXTERN
  1072. #undef js_force_inline
  1073. #ifdef __cplusplus
  1074. } /* extern "C" { */
  1075. #endif
  1076. #endif /* QUICKJS_H */