@zappdev/cli 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (209) hide show
  1. package/README.md +55 -0
  2. package/dist/zapp-cli.js +9471 -0
  3. package/native/src/app/app.zc +490 -0
  4. package/native/src/event/event.zc +24 -0
  5. package/native/src/event/events.zc +70 -0
  6. package/native/src/platform/darwin/backend.zc +923 -0
  7. package/native/src/platform/darwin/backend_bootstrap.zc +9 -0
  8. package/native/src/platform/darwin/bootstrap.zc +9 -0
  9. package/native/src/platform/darwin/engine_jsc.zc +86 -0
  10. package/native/src/platform/darwin/engine_qjs.zc +92 -0
  11. package/native/src/platform/darwin/platform.zc +156 -0
  12. package/native/src/platform/darwin/webview.zc +550 -0
  13. package/native/src/platform/darwin/webview_bootstrap.zc +9 -0
  14. package/native/src/platform/darwin/window.zc +1223 -0
  15. package/native/src/platform/darwin/worker/common.zc +223 -0
  16. package/native/src/platform/darwin/worker/core/base64_core.zc +29 -0
  17. package/native/src/platform/darwin/worker/core/crypto_core.zc +19 -0
  18. package/native/src/platform/darwin/worker/core/encoding_core.zc +32 -0
  19. package/native/src/platform/darwin/worker/core/fetch_core.zc +145 -0
  20. package/native/src/platform/darwin/worker/core/url_core.zc +69 -0
  21. package/native/src/platform/darwin/worker/core/websocket_core.zc +179 -0
  22. package/native/src/platform/darwin/worker/dispatch.zc +55 -0
  23. package/native/src/platform/darwin/worker/jsc/base64_jsc.zc +39 -0
  24. package/native/src/platform/darwin/worker/jsc/crypto_jsc.zc +49 -0
  25. package/native/src/platform/darwin/worker/jsc/encoding_jsc.zc +86 -0
  26. package/native/src/platform/darwin/worker/jsc/fetch_jsc.zc +149 -0
  27. package/native/src/platform/darwin/worker/jsc/url_jsc.zc +54 -0
  28. package/native/src/platform/darwin/worker/jsc/websocket_jsc.zc +127 -0
  29. package/native/src/platform/darwin/worker/jsc.zc +670 -0
  30. package/native/src/platform/darwin/worker/mod.zc +30 -0
  31. package/native/src/platform/darwin/worker/qjs/fetch_qjs.zc +233 -0
  32. package/native/src/platform/darwin/worker/qjs/qjs_macros.zc +23 -0
  33. package/native/src/platform/darwin/worker/qjs/websocket_qjs.zc +223 -0
  34. package/native/src/platform/darwin/worker/qjs.zc +1053 -0
  35. package/native/src/platform/darwin/worker/timers.zc +149 -0
  36. package/native/src/platform/darwin/worker/timers_qjs.zc +209 -0
  37. package/native/src/platform/platform.zc +64 -0
  38. package/native/src/platform/shared/log.zc +156 -0
  39. package/native/src/platform/shared/worker/qjs/base64_qjs.zc +38 -0
  40. package/native/src/platform/shared/worker/qjs/crypto_qjs.zc +44 -0
  41. package/native/src/platform/shared/worker/qjs/encoding_qjs.zc +95 -0
  42. package/native/src/platform/shared/worker/qjs/url_qjs.zc +65 -0
  43. package/native/src/platform/shared/worker_registry.zc +206 -0
  44. package/native/src/platform/window.zc +446 -0
  45. package/native/src/platform/windows/backend.zc +452 -0
  46. package/native/src/platform/windows/backend_bootstrap.zc +9 -0
  47. package/native/src/platform/windows/bootstrap.zc +9 -0
  48. package/native/src/platform/windows/engine_qjs.zc +60 -0
  49. package/native/src/platform/windows/platform.zc +387 -0
  50. package/native/src/platform/windows/webview.zc +1175 -0
  51. package/native/src/platform/windows/webview_bootstrap.zc +9 -0
  52. package/native/src/platform/windows/window.zc +1271 -0
  53. package/native/src/platform/windows/worker/common.zc +409 -0
  54. package/native/src/platform/windows/worker/core/base64_core.zc +52 -0
  55. package/native/src/platform/windows/worker/core/crypto_core.zc +34 -0
  56. package/native/src/platform/windows/worker/core/encoding_core.zc +60 -0
  57. package/native/src/platform/windows/worker/core/fetch_core.zc +274 -0
  58. package/native/src/platform/windows/worker/core/url_core.zc +216 -0
  59. package/native/src/platform/windows/worker/core/websocket_core.zc +343 -0
  60. package/native/src/platform/windows/worker/dispatch.zc +34 -0
  61. package/native/src/platform/windows/worker/mod.zc +46 -0
  62. package/native/src/platform/windows/worker/qjs/fetch_qjs.zc +255 -0
  63. package/native/src/platform/windows/worker/qjs/websocket_qjs.zc +263 -0
  64. package/native/src/platform/windows/worker/qjs.zc +1049 -0
  65. package/native/src/platform/windows/worker/timers_qjs.zc +288 -0
  66. package/native/src/platform/worker.zc +8 -0
  67. package/native/src/service/service.zc +228 -0
  68. package/native/vendor/quickjs-ng/.gitattributes +4 -0
  69. package/native/vendor/quickjs-ng/.github/dependabot.yml +7 -0
  70. package/native/vendor/quickjs-ng/.github/workflows/ci.yml +812 -0
  71. package/native/vendor/quickjs-ng/.github/workflows/docs.yml +49 -0
  72. package/native/vendor/quickjs-ng/.github/workflows/release.yml +162 -0
  73. package/native/vendor/quickjs-ng/.github/workflows/test-docs.yml +23 -0
  74. package/native/vendor/quickjs-ng/.github/workflows/tsan.yml +32 -0
  75. package/native/vendor/quickjs-ng/.github/workflows/valgrind.yml +33 -0
  76. package/native/vendor/quickjs-ng/.gitmodules +5 -0
  77. package/native/vendor/quickjs-ng/CMakeLists.txt +553 -0
  78. package/native/vendor/quickjs-ng/LICENSE +24 -0
  79. package/native/vendor/quickjs-ng/Makefile +149 -0
  80. package/native/vendor/quickjs-ng/amalgam.js +53 -0
  81. package/native/vendor/quickjs-ng/api-test.c +927 -0
  82. package/native/vendor/quickjs-ng/builtin-array-fromasync.h +119 -0
  83. package/native/vendor/quickjs-ng/builtin-array-fromasync.js +36 -0
  84. package/native/vendor/quickjs-ng/builtin-iterator-zip-keyed.h +332 -0
  85. package/native/vendor/quickjs-ng/builtin-iterator-zip-keyed.js +194 -0
  86. package/native/vendor/quickjs-ng/builtin-iterator-zip.h +337 -0
  87. package/native/vendor/quickjs-ng/builtin-iterator-zip.js +210 -0
  88. package/native/vendor/quickjs-ng/ctest.c +17 -0
  89. package/native/vendor/quickjs-ng/cutils.h +2013 -0
  90. package/native/vendor/quickjs-ng/cxxtest.cc +2 -0
  91. package/native/vendor/quickjs-ng/dtoa.c +1619 -0
  92. package/native/vendor/quickjs-ng/dtoa.h +87 -0
  93. package/native/vendor/quickjs-ng/examples/fib.c +67 -0
  94. package/native/vendor/quickjs-ng/examples/fib_module.js +10 -0
  95. package/native/vendor/quickjs-ng/examples/hello.js +1 -0
  96. package/native/vendor/quickjs-ng/examples/hello_module.js +6 -0
  97. package/native/vendor/quickjs-ng/examples/meson.build +17 -0
  98. package/native/vendor/quickjs-ng/examples/pi_bigint.js +118 -0
  99. package/native/vendor/quickjs-ng/examples/point.c +154 -0
  100. package/native/vendor/quickjs-ng/examples/test_fib.js +8 -0
  101. package/native/vendor/quickjs-ng/examples/test_point.js +43 -0
  102. package/native/vendor/quickjs-ng/fuzz.c +51 -0
  103. package/native/vendor/quickjs-ng/gen/function_source.c +81 -0
  104. package/native/vendor/quickjs-ng/gen/hello.c +53 -0
  105. package/native/vendor/quickjs-ng/gen/hello_module.c +106 -0
  106. package/native/vendor/quickjs-ng/gen/repl.c +3053 -0
  107. package/native/vendor/quickjs-ng/gen/standalone.c +324 -0
  108. package/native/vendor/quickjs-ng/gen/test_fib.c +81 -0
  109. package/native/vendor/quickjs-ng/libregexp-opcode.h +58 -0
  110. package/native/vendor/quickjs-ng/libregexp.c +2687 -0
  111. package/native/vendor/quickjs-ng/libregexp.h +98 -0
  112. package/native/vendor/quickjs-ng/libunicode-table.h +4707 -0
  113. package/native/vendor/quickjs-ng/libunicode.c +1746 -0
  114. package/native/vendor/quickjs-ng/libunicode.h +126 -0
  115. package/native/vendor/quickjs-ng/list.h +107 -0
  116. package/native/vendor/quickjs-ng/lre-test.c +73 -0
  117. package/native/vendor/quickjs-ng/meson.build +684 -0
  118. package/native/vendor/quickjs-ng/meson_options.txt +6 -0
  119. package/native/vendor/quickjs-ng/qjs-wasi-reactor.c +208 -0
  120. package/native/vendor/quickjs-ng/qjs.c +748 -0
  121. package/native/vendor/quickjs-ng/qjsc.c +673 -0
  122. package/native/vendor/quickjs-ng/quickjs-atom.h +267 -0
  123. package/native/vendor/quickjs-ng/quickjs-c-atomics.h +54 -0
  124. package/native/vendor/quickjs-ng/quickjs-libc.c +4986 -0
  125. package/native/vendor/quickjs-ng/quickjs-libc.h +79 -0
  126. package/native/vendor/quickjs-ng/quickjs-opcode.h +369 -0
  127. package/native/vendor/quickjs-ng/quickjs.c +60259 -0
  128. package/native/vendor/quickjs-ng/quickjs.h +1419 -0
  129. package/native/vendor/quickjs-ng/repl.js +1927 -0
  130. package/native/vendor/quickjs-ng/run-test262.c +2417 -0
  131. package/native/vendor/quickjs-ng/standalone.js +129 -0
  132. package/native/vendor/quickjs-ng/tests/assert.js +49 -0
  133. package/native/vendor/quickjs-ng/tests/bug1221.js +16 -0
  134. package/native/vendor/quickjs-ng/tests/bug1296.js +12 -0
  135. package/native/vendor/quickjs-ng/tests/bug1297.js +22 -0
  136. package/native/vendor/quickjs-ng/tests/bug1301.js +21 -0
  137. package/native/vendor/quickjs-ng/tests/bug1302.js +24 -0
  138. package/native/vendor/quickjs-ng/tests/bug1305.js +26 -0
  139. package/native/vendor/quickjs-ng/tests/bug1318.js +54 -0
  140. package/native/vendor/quickjs-ng/tests/bug1352.js +8 -0
  141. package/native/vendor/quickjs-ng/tests/bug1354.js +6 -0
  142. package/native/vendor/quickjs-ng/tests/bug1355.js +58 -0
  143. package/native/vendor/quickjs-ng/tests/bug1368.js +9 -0
  144. package/native/vendor/quickjs-ng/tests/bug39/1.js +6 -0
  145. package/native/vendor/quickjs-ng/tests/bug39/2.js +6 -0
  146. package/native/vendor/quickjs-ng/tests/bug39/3.js +7 -0
  147. package/native/vendor/quickjs-ng/tests/bug488-upstream.js +7 -0
  148. package/native/vendor/quickjs-ng/tests/bug633/0.js +7 -0
  149. package/native/vendor/quickjs-ng/tests/bug633/1.js +4 -0
  150. package/native/vendor/quickjs-ng/tests/bug633/2.js +4 -0
  151. package/native/vendor/quickjs-ng/tests/bug633/3.js +4 -0
  152. package/native/vendor/quickjs-ng/tests/bug645/0.js +4 -0
  153. package/native/vendor/quickjs-ng/tests/bug645/1.js +9 -0
  154. package/native/vendor/quickjs-ng/tests/bug645/2.js +7 -0
  155. package/native/vendor/quickjs-ng/tests/bug648.js +13 -0
  156. package/native/vendor/quickjs-ng/tests/bug652.js +4 -0
  157. package/native/vendor/quickjs-ng/tests/bug741.js +19 -0
  158. package/native/vendor/quickjs-ng/tests/bug775.js +7 -0
  159. package/native/vendor/quickjs-ng/tests/bug776.js +7 -0
  160. package/native/vendor/quickjs-ng/tests/bug832.js +2 -0
  161. package/native/vendor/quickjs-ng/tests/bug858.js +26 -0
  162. package/native/vendor/quickjs-ng/tests/bug904.js +6 -0
  163. package/native/vendor/quickjs-ng/tests/bug988.js +7 -0
  164. package/native/vendor/quickjs-ng/tests/bug999.js +3 -0
  165. package/native/vendor/quickjs-ng/tests/destructured-export.js +8 -0
  166. package/native/vendor/quickjs-ng/tests/detect_module/0.js +1 -0
  167. package/native/vendor/quickjs-ng/tests/detect_module/1.js +2 -0
  168. package/native/vendor/quickjs-ng/tests/detect_module/2.js +1 -0
  169. package/native/vendor/quickjs-ng/tests/detect_module/3.js +8 -0
  170. package/native/vendor/quickjs-ng/tests/detect_module/4.js +3 -0
  171. package/native/vendor/quickjs-ng/tests/empty.js +0 -0
  172. package/native/vendor/quickjs-ng/tests/fixture_cyclic_import.js +2 -0
  173. package/native/vendor/quickjs-ng/tests/fixture_string_exports.js +12 -0
  174. package/native/vendor/quickjs-ng/tests/function_source.js +14 -0
  175. package/native/vendor/quickjs-ng/tests/microbench.js +1267 -0
  176. package/native/vendor/quickjs-ng/tests/null_or_undefined.js +38 -0
  177. package/native/vendor/quickjs-ng/tests/str-pad-leak.js +5 -0
  178. package/native/vendor/quickjs-ng/tests/test_bigint.js +107 -0
  179. package/native/vendor/quickjs-ng/tests/test_bjson.js +366 -0
  180. package/native/vendor/quickjs-ng/tests/test_builtin.js +1314 -0
  181. package/native/vendor/quickjs-ng/tests/test_closure.js +220 -0
  182. package/native/vendor/quickjs-ng/tests/test_cyclic_import.js +12 -0
  183. package/native/vendor/quickjs-ng/tests/test_domexception.js +35 -0
  184. package/native/vendor/quickjs-ng/tests/test_language.js +755 -0
  185. package/native/vendor/quickjs-ng/tests/test_loop.js +367 -0
  186. package/native/vendor/quickjs-ng/tests/test_queue_microtask.js +39 -0
  187. package/native/vendor/quickjs-ng/tests/test_std.js +340 -0
  188. package/native/vendor/quickjs-ng/tests/test_string_exports.js +25 -0
  189. package/native/vendor/quickjs-ng/tests/test_worker.js +43 -0
  190. package/native/vendor/quickjs-ng/tests/test_worker_module.js +30 -0
  191. package/native/vendor/quickjs-ng/tests.conf +14 -0
  192. package/native/vendor/quickjs-ng/unicode_download.sh +19 -0
  193. package/native/vendor/quickjs-ng/unicode_gen.c +3108 -0
  194. package/native/vendor/quickjs-ng/unicode_gen_def.h +310 -0
  195. package/native/vendor/quickjs-ng/update-version.sh +32 -0
  196. package/native/vendor/webview2/include/WebView2.h +60636 -0
  197. package/native/vendor/webview2/include/WebView2EnvironmentOptions.h +406 -0
  198. package/package.json +33 -0
  199. package/src/backend.ts +139 -0
  200. package/src/build-config.ts +87 -0
  201. package/src/build.ts +276 -0
  202. package/src/common.ts +195 -0
  203. package/src/config.ts +89 -0
  204. package/src/dev.ts +164 -0
  205. package/src/generate.ts +200 -0
  206. package/src/icons.ts +116 -0
  207. package/src/init.ts +190 -0
  208. package/src/package.ts +150 -0
  209. package/src/zapp-cli.ts +263 -0
@@ -0,0 +1,1419 @@
1
+ /*
2
+ * QuickJS Javascript Engine
3
+ *
4
+ * Copyright (c) 2017-2026 Fabrice Bellard
5
+ * Copyright (c) 2017-2024 Charlie Gordon
6
+ * Copyright (c) 2023-2026 Ben Noordhuis
7
+ * Copyright (c) 2023-2026 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
+
30
+ #include <stdbool.h>
31
+ #include <stdio.h>
32
+ #include <stdint.h>
33
+ #include <string.h>
34
+ #include <math.h>
35
+
36
+ #ifdef __cplusplus
37
+ extern "C" {
38
+ #endif
39
+
40
+ #define QUICKJS_NG 1
41
+
42
+ /* Helpers. */
43
+ #if defined(_WIN32) || defined(__CYGWIN__)
44
+ # define QUICKJS_NG_PLAT_WIN32 1
45
+ #endif /* defined(_WIN32) || defined(__CYGWIN__) */
46
+
47
+ #if defined(__GNUC__) || defined(__clang__)
48
+ # define QUICKJS_NG_CC_GNULIKE 1
49
+ #endif /* defined(__GNUC__) || defined(__clang__) */
50
+
51
+ /*
52
+ * `JS_EXTERN` -- helper macro that must be used to mark the external
53
+ * interfaces of libqjs.
54
+ *
55
+ * Define BUILDING_QJS_SHARED when building and USING_QJS_SHARED when using
56
+ * shared libqjs.
57
+ *
58
+ * Windows note: The `__declspec` syntax is supported by both Clang and GCC.
59
+ * If building qjs, the BUILDING_QJS_SHARED macro must be defined for libqjs
60
+ * (and only for it) to properly export symbols.
61
+ */
62
+ #ifdef QUICKJS_NG_PLAT_WIN32
63
+ # if defined(BUILDING_QJS_SHARED)
64
+ # define JS_EXTERN __declspec(dllexport)
65
+ # elif defined(USING_QJS_SHARED)
66
+ # define JS_EXTERN __declspec(dllimport)
67
+ # else
68
+ # define JS_EXTERN /* nothing */
69
+ # endif
70
+ #else
71
+ # ifdef QUICKJS_NG_CC_GNULIKE
72
+ # define JS_EXTERN __attribute__((visibility("default")))
73
+ # else
74
+ # define JS_EXTERN /* nothing */
75
+ # endif
76
+ #endif /* QUICKJS_NG_PLAT_WIN32 */
77
+
78
+ /*
79
+ * `JS_LIBC_EXTERN` -- helper macro that must be used to mark the extern
80
+ * interfaces of quickjs-libc specifically.
81
+ */
82
+ #if defined(QUICKJS_NG_BUILD) && !defined(QJS_BUILD_LIBC) && defined(QUICKJS_NG_PLAT_WIN32)
83
+ /*
84
+ * We are building QuickJS-NG, quickjs-libc is a static library and we are on
85
+ * Windows. Then, make sure to not export any interfaces.
86
+ */
87
+ # define JS_LIBC_EXTERN /* nothing */
88
+ #else
89
+ /*
90
+ * Otherwise, if we are either (1) not building QuickJS-NG, (2) libc is built as
91
+ * a part of libqjs, or (3) we are not on Windows, define JS_LIBC_EXTERN to
92
+ * JS_EXTERN.
93
+ */
94
+ # define JS_LIBC_EXTERN JS_EXTERN
95
+ #endif
96
+
97
+ /*
98
+ * `JS_MODULE_EXTERN` -- helper macro that must be used to mark `js_init_module`
99
+ * and other public functions of the binary modules. See examples/ for examples
100
+ * of the usage.
101
+ *
102
+ * Windows note: -DQUICKJS_NG_MODULE_BUILD must be set when building a binary
103
+ * module to properly set __declspec.
104
+ */
105
+ #ifdef QUICKJS_NG_PLAT_WIN32
106
+ # ifdef QUICKJS_NG_MODULE_BUILD
107
+ # define JS_MODULE_EXTERN __declspec(dllexport)
108
+ # else
109
+ # define JS_MODULE_EXTERN __declspec(dllimport)
110
+ # endif
111
+ #else
112
+ # ifdef QUICKJS_NG_CC_GNULIKE
113
+ # define JS_MODULE_EXTERN __attribute__((visibility("default")))
114
+ # else
115
+ # define JS_MODULE_EXTERN /* nothing */
116
+ # endif
117
+ #endif /* QUICKJS_NG_PLAT_WIN32 */
118
+
119
+ /* Borrowed from Folly */
120
+ #ifndef JS_PRINTF_FORMAT
121
+ #ifdef _MSC_VER
122
+ #include <sal.h>
123
+ #define JS_PRINTF_FORMAT _Printf_format_string_
124
+ #define JS_PRINTF_FORMAT_ATTR(format_param, dots_param)
125
+ #else
126
+ #define JS_PRINTF_FORMAT
127
+ #if !defined(__clang__) && defined(__GNUC__)
128
+ #define JS_PRINTF_FORMAT_ATTR(format_param, dots_param) \
129
+ __attribute__((format(gnu_printf, format_param, dots_param)))
130
+ #else
131
+ #define JS_PRINTF_FORMAT_ATTR(format_param, dots_param) \
132
+ __attribute__((format(printf, format_param, dots_param)))
133
+ #endif
134
+ #endif
135
+ #endif
136
+
137
+ #undef QUICKJS_NG_CC_GNULIKE
138
+ #undef QUICKJS_NG_PLAT_WIN32
139
+
140
+ typedef struct JSRuntime JSRuntime;
141
+ typedef struct JSContext JSContext;
142
+ typedef struct JSObject JSObject;
143
+ typedef struct JSClass JSClass;
144
+ typedef uint32_t JSClassID;
145
+ typedef uint32_t JSAtom;
146
+
147
+ /* Unless documented otherwise, C string pointers (`char *` or `const char *`)
148
+ are assumed to verify these constraints:
149
+ - unless a length is passed separately, the string has a null terminator
150
+ - string contents is either pure ASCII or is UTF-8 encoded.
151
+ */
152
+
153
+ /* Overridable purely for testing purposes; don't touch. */
154
+ #ifndef JS_NAN_BOXING
155
+ #if INTPTR_MAX < INT64_MAX
156
+ #define JS_NAN_BOXING 1 /* Use NAN boxing for 32bit builds. */
157
+ #endif
158
+ #endif
159
+
160
+ enum {
161
+ /* all tags with a reference count are negative */
162
+ JS_TAG_FIRST = -9, /* first negative tag */
163
+ JS_TAG_BIG_INT = -9,
164
+ JS_TAG_SYMBOL = -8,
165
+ JS_TAG_STRING = -7,
166
+ JS_TAG_STRING_ROPE = -6,
167
+ JS_TAG_MODULE = -3, /* used internally */
168
+ JS_TAG_FUNCTION_BYTECODE = -2, /* used internally */
169
+ JS_TAG_OBJECT = -1,
170
+
171
+ JS_TAG_INT = 0,
172
+ JS_TAG_BOOL = 1,
173
+ JS_TAG_NULL = 2,
174
+ JS_TAG_UNDEFINED = 3,
175
+ JS_TAG_UNINITIALIZED = 4,
176
+ JS_TAG_CATCH_OFFSET = 5,
177
+ JS_TAG_EXCEPTION = 6,
178
+ JS_TAG_SHORT_BIG_INT = 7,
179
+ JS_TAG_FLOAT64 = 8,
180
+ /* any larger tag is FLOAT64 if JS_NAN_BOXING */
181
+ };
182
+
183
+ #if !defined(JS_CHECK_JSVALUE)
184
+ #define JSValueConst JSValue
185
+ #endif
186
+
187
+ // JS_CHECK_JSVALUE build mode does not produce working code but is here to
188
+ // help catch reference counting bugs at compile time, by making it harder
189
+ // to mix up JSValue and JSValueConst
190
+ //
191
+ // rules:
192
+ //
193
+ // - a function with a JSValue parameter takes ownership;
194
+ // caller must *not* call JS_FreeValue
195
+ //
196
+ // - a function with a JSValueConst parameter does not take ownership;
197
+ // caller *must* call JS_FreeValue
198
+ //
199
+ // - a function returning a JSValue transfers ownership to caller;
200
+ // caller *must* call JS_FreeValue
201
+ //
202
+ // - a function returning a JSValueConst does *not* transfer ownership;
203
+ // caller must *not* call JS_FreeValue
204
+ #if defined(JS_CHECK_JSVALUE)
205
+
206
+ typedef struct JSValue *JSValue;
207
+ typedef const struct JSValue *JSValueConst;
208
+
209
+ #define JS_MKVAL(tag, val) ((JSValue)((tag) | (intptr_t)(val) << 4))
210
+ #define JS_MKPTR(tag, ptr) ((JSValue)((tag) | (intptr_t)(ptr)))
211
+ #define JS_VALUE_GET_NORM_TAG(v) ((int)((intptr_t)(v) & 15))
212
+ #define JS_VALUE_GET_TAG(v) ((int)((intptr_t)(v) & 15))
213
+ #define JS_VALUE_GET_SHORT_BIG_INT(v) JS_VALUE_GET_INT(v)
214
+ #define JS_VALUE_GET_PTR(v) ((void *)((intptr_t)(v) & ~15))
215
+ #define JS_VALUE_GET_INT(v) ((int)((intptr_t)(v) >> 4))
216
+ #define JS_VALUE_GET_BOOL(v) ((int)((intptr_t)(v) >> 4))
217
+ #define JS_VALUE_GET_FLOAT64(v) ((double)((intptr_t)(v) >> 4))
218
+ #define JS_TAG_IS_FLOAT64(tag) ((int)(tag) == JS_TAG_FLOAT64)
219
+ #define JS_NAN JS_MKVAL(JS_TAG_FLOAT64, 0)
220
+
221
+ static inline JSValue __JS_NewFloat64(double d)
222
+ {
223
+ return JS_MKVAL(JS_TAG_FLOAT64, (int)d);
224
+ }
225
+
226
+ static inline JSValue __JS_NewShortBigInt(JSContext *ctx, int32_t d)
227
+ {
228
+ (void)&ctx;
229
+ return JS_MKVAL(JS_TAG_SHORT_BIG_INT, d);
230
+ }
231
+
232
+ static inline bool JS_VALUE_IS_NAN(JSValue v)
233
+ {
234
+ (void)&v;
235
+ return false;
236
+ }
237
+
238
+ #elif defined(JS_NAN_BOXING) && JS_NAN_BOXING
239
+
240
+ typedef uint64_t JSValue;
241
+
242
+ #define JS_VALUE_GET_TAG(v) (int)((v) >> 32)
243
+ #define JS_VALUE_GET_INT(v) (int)(v)
244
+ #define JS_VALUE_GET_BOOL(v) (int)(v)
245
+ #define JS_VALUE_GET_SHORT_BIG_INT(v) (int)(v)
246
+ #define JS_VALUE_GET_PTR(v) (void *)(intptr_t)(v)
247
+
248
+ #define JS_MKVAL(tag, val) (((uint64_t)(tag) << 32) | (uint32_t)(val))
249
+ #define JS_MKPTR(tag, ptr) (((uint64_t)(tag) << 32) | (uintptr_t)(ptr))
250
+
251
+ #define JS_FLOAT64_TAG_ADDEND (0x7ff80000 - JS_TAG_FIRST + 1) /* quiet NaN encoding */
252
+
253
+ static inline double JS_VALUE_GET_FLOAT64(JSValue v)
254
+ {
255
+ union {
256
+ JSValue v;
257
+ double d;
258
+ } u;
259
+ u.v = v;
260
+ u.v += (uint64_t)JS_FLOAT64_TAG_ADDEND << 32;
261
+ return u.d;
262
+ }
263
+
264
+ #define JS_NAN (0x7ff8000000000000 - ((uint64_t)JS_FLOAT64_TAG_ADDEND << 32))
265
+
266
+ static inline JSValue __JS_NewFloat64(double d)
267
+ {
268
+ union {
269
+ double d;
270
+ uint64_t u64;
271
+ } u;
272
+ JSValue v;
273
+ u.d = d;
274
+ /* normalize NaN */
275
+ if ((u.u64 & 0x7fffffffffffffff) > 0x7ff0000000000000)
276
+ v = JS_NAN;
277
+ else
278
+ v = u.u64 - ((uint64_t)JS_FLOAT64_TAG_ADDEND << 32);
279
+ return v;
280
+ }
281
+
282
+ static inline JSValue __JS_NewShortBigInt(JSContext *ctx, int32_t d)
283
+ {
284
+ (void)&ctx;
285
+ return JS_MKVAL(JS_TAG_SHORT_BIG_INT, d);
286
+ }
287
+
288
+ #define JS_TAG_IS_FLOAT64(tag) ((unsigned)((tag) - JS_TAG_FIRST) >= (JS_TAG_FLOAT64 - JS_TAG_FIRST))
289
+
290
+ /* same as JS_VALUE_GET_TAG, but return JS_TAG_FLOAT64 with NaN boxing */
291
+ static inline int JS_VALUE_GET_NORM_TAG(JSValue v)
292
+ {
293
+ uint32_t tag;
294
+ tag = JS_VALUE_GET_TAG(v);
295
+ if (JS_TAG_IS_FLOAT64(tag))
296
+ return JS_TAG_FLOAT64;
297
+ else
298
+ return tag;
299
+ }
300
+
301
+ static inline bool JS_VALUE_IS_NAN(JSValue v)
302
+ {
303
+ uint32_t tag;
304
+ tag = JS_VALUE_GET_TAG(v);
305
+ return tag == (JS_NAN >> 32);
306
+ }
307
+
308
+ #else /* !JS_NAN_BOXING */
309
+
310
+ typedef union JSValueUnion {
311
+ int32_t int32;
312
+ double float64;
313
+ void *ptr;
314
+ int32_t short_big_int;
315
+ } JSValueUnion;
316
+
317
+ typedef struct JSValue {
318
+ JSValueUnion u;
319
+ int64_t tag;
320
+ } JSValue;
321
+
322
+ #define JS_VALUE_GET_TAG(v) ((int32_t)(v).tag)
323
+ /* same as JS_VALUE_GET_TAG, but return JS_TAG_FLOAT64 with NaN boxing */
324
+ #define JS_VALUE_GET_NORM_TAG(v) JS_VALUE_GET_TAG(v)
325
+ #define JS_VALUE_GET_INT(v) ((v).u.int32)
326
+ #define JS_VALUE_GET_BOOL(v) ((v).u.int32)
327
+ #define JS_VALUE_GET_FLOAT64(v) ((v).u.float64)
328
+ #define JS_VALUE_GET_SHORT_BIG_INT(v) ((v).u.short_big_int)
329
+ #define JS_VALUE_GET_PTR(v) ((v).u.ptr)
330
+
331
+ /* msvc doesn't understand designated initializers without /std:c++20 */
332
+ #ifdef __cplusplus
333
+ static inline JSValue JS_MKPTR(int64_t tag, void *ptr)
334
+ {
335
+ JSValue v;
336
+ v.u.ptr = ptr;
337
+ v.tag = tag;
338
+ return v;
339
+ }
340
+ static inline JSValue JS_MKVAL(int64_t tag, int32_t int32)
341
+ {
342
+ JSValue v;
343
+ v.u.int32 = int32;
344
+ v.tag = tag;
345
+ return v;
346
+ }
347
+ static inline JSValue JS_MKNAN(void)
348
+ {
349
+ JSValue v;
350
+ v.u.float64 = NAN;
351
+ v.tag = JS_TAG_FLOAT64;
352
+ return v;
353
+ }
354
+ /* provide as macros for consistency and backward compat reasons */
355
+ #define JS_MKPTR(tag, ptr) JS_MKPTR(tag, ptr)
356
+ #define JS_MKVAL(tag, val) JS_MKVAL(tag, val)
357
+ #define JS_NAN JS_MKNAN() /* alas, not a constant expression */
358
+ #else
359
+ #define JS_MKPTR(tag, p) (JSValue){ (JSValueUnion){ .ptr = p }, tag }
360
+ #define JS_MKVAL(tag, val) (JSValue){ (JSValueUnion){ .int32 = val }, tag }
361
+ #define JS_NAN (JSValue){ (JSValueUnion){ .float64 = NAN }, JS_TAG_FLOAT64 }
362
+ #endif
363
+
364
+ #define JS_TAG_IS_FLOAT64(tag) ((unsigned)(tag) == JS_TAG_FLOAT64)
365
+
366
+ static inline JSValue __JS_NewFloat64(double d)
367
+ {
368
+ JSValue v;
369
+ v.tag = JS_TAG_FLOAT64;
370
+ v.u.float64 = d;
371
+ return v;
372
+ }
373
+
374
+ static inline JSValue __JS_NewShortBigInt(JSContext *ctx, int64_t d)
375
+ {
376
+ (void)&ctx;
377
+ JSValue v;
378
+ v.tag = JS_TAG_SHORT_BIG_INT;
379
+ v.u.short_big_int = d;
380
+ return v;
381
+ }
382
+
383
+ static inline bool JS_VALUE_IS_NAN(JSValue v)
384
+ {
385
+ union {
386
+ double d;
387
+ uint64_t u64;
388
+ } u;
389
+ if (v.tag != JS_TAG_FLOAT64)
390
+ return 0;
391
+ u.d = v.u.float64;
392
+ return (u.u64 & 0x7fffffffffffffff) > 0x7ff0000000000000;
393
+ }
394
+
395
+ #endif /* !JS_NAN_BOXING */
396
+
397
+ #define JS_VALUE_IS_BOTH_INT(v1, v2) ((JS_VALUE_GET_TAG(v1) | JS_VALUE_GET_TAG(v2)) == 0)
398
+ #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)))
399
+
400
+ #define JS_VALUE_HAS_REF_COUNT(v) ((unsigned)JS_VALUE_GET_TAG(v) >= (unsigned)JS_TAG_FIRST)
401
+
402
+ /* special values */
403
+ #define JS_NULL JS_MKVAL(JS_TAG_NULL, 0)
404
+ #define JS_UNDEFINED JS_MKVAL(JS_TAG_UNDEFINED, 0)
405
+ #define JS_FALSE JS_MKVAL(JS_TAG_BOOL, 0)
406
+ #define JS_TRUE JS_MKVAL(JS_TAG_BOOL, 1)
407
+ #define JS_EXCEPTION JS_MKVAL(JS_TAG_EXCEPTION, 0)
408
+ #define JS_UNINITIALIZED JS_MKVAL(JS_TAG_UNINITIALIZED, 0)
409
+
410
+ /* flags for object properties */
411
+ #define JS_PROP_CONFIGURABLE (1 << 0)
412
+ #define JS_PROP_WRITABLE (1 << 1)
413
+ #define JS_PROP_ENUMERABLE (1 << 2)
414
+ #define JS_PROP_C_W_E (JS_PROP_CONFIGURABLE | JS_PROP_WRITABLE | JS_PROP_ENUMERABLE)
415
+ #define JS_PROP_LENGTH (1 << 3) /* used internally in Arrays */
416
+ #define JS_PROP_TMASK (3 << 4) /* mask for NORMAL, GETSET, VARREF, AUTOINIT */
417
+ #define JS_PROP_NORMAL (0 << 4)
418
+ #define JS_PROP_GETSET (1 << 4)
419
+ #define JS_PROP_VARREF (2 << 4) /* used internally */
420
+ #define JS_PROP_AUTOINIT (3 << 4) /* used internally */
421
+
422
+ /* flags for JS_DefineProperty */
423
+ #define JS_PROP_HAS_SHIFT 8
424
+ #define JS_PROP_HAS_CONFIGURABLE (1 << 8)
425
+ #define JS_PROP_HAS_WRITABLE (1 << 9)
426
+ #define JS_PROP_HAS_ENUMERABLE (1 << 10)
427
+ #define JS_PROP_HAS_GET (1 << 11)
428
+ #define JS_PROP_HAS_SET (1 << 12)
429
+ #define JS_PROP_HAS_VALUE (1 << 13)
430
+
431
+ /* throw an exception if false would be returned
432
+ (JS_DefineProperty/JS_SetProperty) */
433
+ #define JS_PROP_THROW (1 << 14)
434
+ /* throw an exception if false would be returned in strict mode
435
+ (JS_SetProperty) */
436
+ #define JS_PROP_THROW_STRICT (1 << 15)
437
+
438
+ #define JS_PROP_NO_ADD (1 << 16) /* internal use */
439
+ #define JS_PROP_NO_EXOTIC (1 << 17) /* internal use */
440
+ #define JS_PROP_DEFINE_PROPERTY (1 << 18) /* internal use */
441
+ #define JS_PROP_REFLECT_DEFINE_PROPERTY (1 << 19) /* internal use */
442
+
443
+ #ifndef JS_DEFAULT_STACK_SIZE
444
+ #define JS_DEFAULT_STACK_SIZE (1024 * 1024)
445
+ #endif
446
+
447
+ /* JS_Eval() flags */
448
+ #define JS_EVAL_TYPE_GLOBAL (0 << 0) /* global code (default) */
449
+ #define JS_EVAL_TYPE_MODULE (1 << 0) /* module code */
450
+ #define JS_EVAL_TYPE_DIRECT (2 << 0) /* direct call (internal use) */
451
+ #define JS_EVAL_TYPE_INDIRECT (3 << 0) /* indirect call (internal use) */
452
+ #define JS_EVAL_TYPE_MASK (3 << 0)
453
+
454
+ #define JS_EVAL_FLAG_STRICT (1 << 3) /* force 'strict' mode */
455
+ #define JS_EVAL_FLAG_UNUSED (1 << 4) /* unused */
456
+ /* compile but do not run. The result is an object with a
457
+ JS_TAG_FUNCTION_BYTECODE or JS_TAG_MODULE tag. It can be executed
458
+ with JS_EvalFunction(). */
459
+ #define JS_EVAL_FLAG_COMPILE_ONLY (1 << 5)
460
+ /* don't include the stack frames before this eval in the Error() backtraces */
461
+ #define JS_EVAL_FLAG_BACKTRACE_BARRIER (1 << 6)
462
+ /* allow top-level await in normal script. JS_Eval() returns a
463
+ promise. Only allowed with JS_EVAL_TYPE_GLOBAL */
464
+ #define JS_EVAL_FLAG_ASYNC (1 << 7)
465
+
466
+ typedef JSValue JSCFunction(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv);
467
+ typedef JSValue JSCFunctionMagic(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv, int magic);
468
+ typedef JSValue JSCFunctionData(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv, int magic, JSValueConst *func_data);
469
+ typedef JSValue JSCClosure(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv, int magic, void *opaque);
470
+
471
+ typedef struct JSMallocFunctions {
472
+ void *(*js_calloc)(void *opaque, size_t count, size_t size);
473
+ void *(*js_malloc)(void *opaque, size_t size);
474
+ void (*js_free)(void *opaque, void *ptr);
475
+ void *(*js_realloc)(void *opaque, void *ptr, size_t size);
476
+ size_t (*js_malloc_usable_size)(const void *ptr);
477
+ } JSMallocFunctions;
478
+
479
+ // Debug trace system: the debug output will be produced to the dump stream (currently
480
+ // stdout) if dumps are enabled and JS_SetDumpFlags is invoked with the corresponding
481
+ // bit set.
482
+ #define JS_DUMP_BYTECODE_FINAL 0x01 /* dump pass 3 final byte code */
483
+ #define JS_DUMP_BYTECODE_PASS2 0x02 /* dump pass 2 code */
484
+ #define JS_DUMP_BYTECODE_PASS1 0x04 /* dump pass 1 code */
485
+ #define JS_DUMP_BYTECODE_HEX 0x10 /* dump bytecode in hex */
486
+ #define JS_DUMP_BYTECODE_PC2LINE 0x20 /* dump line number table */
487
+ #define JS_DUMP_BYTECODE_STACK 0x40 /* dump compute_stack_size */
488
+ #define JS_DUMP_BYTECODE_STEP 0x80 /* dump executed bytecode */
489
+ #define JS_DUMP_READ_OBJECT 0x100 /* dump the marshalled objects at load time */
490
+ #define JS_DUMP_FREE 0x200 /* dump every object free */
491
+ #define JS_DUMP_GC 0x400 /* dump the occurrence of the automatic GC */
492
+ #define JS_DUMP_GC_FREE 0x800 /* dump objects freed by the GC */
493
+ #define JS_DUMP_MODULE_RESOLVE 0x1000 /* dump module resolution steps */
494
+ #define JS_DUMP_PROMISE 0x2000 /* dump promise steps */
495
+ #define JS_DUMP_LEAKS 0x4000 /* dump leaked objects and strings in JS_FreeRuntime */
496
+ #define JS_DUMP_ATOM_LEAKS 0x8000 /* dump leaked atoms in JS_FreeRuntime */
497
+ #define JS_DUMP_MEM 0x10000 /* dump memory usage in JS_FreeRuntime */
498
+ #define JS_DUMP_OBJECTS 0x20000 /* dump objects in JS_FreeRuntime */
499
+ #define JS_DUMP_ATOMS 0x40000 /* dump atoms in JS_FreeRuntime */
500
+ #define JS_DUMP_SHAPES 0x80000 /* dump shapes in JS_FreeRuntime */
501
+
502
+ // Finalizers run in LIFO order at the very end of JS_FreeRuntime.
503
+ // Intended for cleanup of associated resources; the runtime itself
504
+ // is no longer usable.
505
+ typedef void JSRuntimeFinalizer(JSRuntime *rt, void *arg);
506
+
507
+ typedef struct JSGCObjectHeader JSGCObjectHeader;
508
+
509
+ JS_EXTERN JSRuntime *JS_NewRuntime(void);
510
+ /* info lifetime must exceed that of rt */
511
+ JS_EXTERN void JS_SetRuntimeInfo(JSRuntime *rt, const char *info);
512
+ /* use 0 to disable memory limit */
513
+ JS_EXTERN void JS_SetMemoryLimit(JSRuntime *rt, size_t limit);
514
+ JS_EXTERN void JS_SetDumpFlags(JSRuntime *rt, uint64_t flags);
515
+ JS_EXTERN uint64_t JS_GetDumpFlags(JSRuntime *rt);
516
+ JS_EXTERN size_t JS_GetGCThreshold(JSRuntime *rt);
517
+ JS_EXTERN void JS_SetGCThreshold(JSRuntime *rt, size_t gc_threshold);
518
+ /* use 0 to disable maximum stack size check */
519
+ JS_EXTERN void JS_SetMaxStackSize(JSRuntime *rt, size_t stack_size);
520
+ /* should be called when changing thread to update the stack top value
521
+ used to check stack overflow. */
522
+ JS_EXTERN void JS_UpdateStackTop(JSRuntime *rt);
523
+ JS_EXTERN JSRuntime *JS_NewRuntime2(const JSMallocFunctions *mf, void *opaque);
524
+ JS_EXTERN void JS_FreeRuntime(JSRuntime *rt);
525
+ JS_EXTERN void *JS_GetRuntimeOpaque(JSRuntime *rt);
526
+ JS_EXTERN void JS_SetRuntimeOpaque(JSRuntime *rt, void *opaque);
527
+ JS_EXTERN int JS_AddRuntimeFinalizer(JSRuntime *rt,
528
+ JSRuntimeFinalizer *finalizer, void *arg);
529
+ typedef void JS_MarkFunc(JSRuntime *rt, JSGCObjectHeader *gp);
530
+ JS_EXTERN void JS_MarkValue(JSRuntime *rt, JSValueConst val,
531
+ JS_MarkFunc *mark_func);
532
+ JS_EXTERN void JS_RunGC(JSRuntime *rt);
533
+ JS_EXTERN bool JS_IsLiveObject(JSRuntime *rt, JSValueConst obj);
534
+
535
+ JS_EXTERN JSContext *JS_NewContext(JSRuntime *rt);
536
+ JS_EXTERN void JS_FreeContext(JSContext *s);
537
+ JS_EXTERN JSContext *JS_DupContext(JSContext *ctx);
538
+ JS_EXTERN void *JS_GetContextOpaque(JSContext *ctx);
539
+ JS_EXTERN void JS_SetContextOpaque(JSContext *ctx, void *opaque);
540
+ JS_EXTERN JSRuntime *JS_GetRuntime(JSContext *ctx);
541
+ JS_EXTERN void JS_SetClassProto(JSContext *ctx, JSClassID class_id, JSValue obj);
542
+ JS_EXTERN JSValue JS_GetClassProto(JSContext *ctx, JSClassID class_id);
543
+ JS_EXTERN JSValue JS_GetFunctionProto(JSContext *ctx);
544
+
545
+ /* the following functions are used to select the intrinsic object to
546
+ save memory */
547
+ JS_EXTERN JSContext *JS_NewContextRaw(JSRuntime *rt);
548
+ JS_EXTERN void JS_AddIntrinsicBaseObjects(JSContext *ctx);
549
+ JS_EXTERN void JS_AddIntrinsicDate(JSContext *ctx);
550
+ JS_EXTERN void JS_AddIntrinsicEval(JSContext *ctx);
551
+ JS_EXTERN void JS_AddIntrinsicRegExpCompiler(JSContext *ctx);
552
+ JS_EXTERN int JS_AddIntrinsicRegExp(JSContext *ctx);
553
+ JS_EXTERN void JS_AddIntrinsicJSON(JSContext *ctx);
554
+ JS_EXTERN void JS_AddIntrinsicProxy(JSContext *ctx);
555
+ JS_EXTERN void JS_AddIntrinsicMapSet(JSContext *ctx);
556
+ JS_EXTERN void JS_AddIntrinsicTypedArrays(JSContext *ctx);
557
+ JS_EXTERN void JS_AddIntrinsicPromise(JSContext *ctx);
558
+ JS_EXTERN void JS_AddIntrinsicBigInt(JSContext *ctx);
559
+ JS_EXTERN void JS_AddIntrinsicWeakRef(JSContext *ctx);
560
+ JS_EXTERN void JS_AddPerformance(JSContext *ctx);
561
+ JS_EXTERN void JS_AddIntrinsicDOMException(JSContext *ctx);
562
+
563
+ /* for equality comparisons and sameness */
564
+ JS_EXTERN int JS_IsEqual(JSContext *ctx, JSValueConst op1, JSValueConst op2);
565
+ JS_EXTERN bool JS_IsStrictEqual(JSContext *ctx, JSValueConst op1, JSValueConst op2);
566
+ JS_EXTERN bool JS_IsSameValue(JSContext *ctx, JSValueConst op1, JSValueConst op2);
567
+ /* Similar to same-value equality, but +0 and -0 are considered equal. */
568
+ JS_EXTERN bool JS_IsSameValueZero(JSContext *ctx, JSValueConst op1, JSValueConst op2);
569
+
570
+ /* Only used for running 262 tests. TODO(saghul) add build time flag. */
571
+ JS_EXTERN JSValue js_string_codePointRange(JSContext *ctx, JSValueConst this_val,
572
+ int argc, JSValueConst *argv);
573
+
574
+ JS_EXTERN void *js_calloc_rt(JSRuntime *rt, size_t count, size_t size);
575
+ JS_EXTERN void *js_malloc_rt(JSRuntime *rt, size_t size);
576
+ JS_EXTERN void js_free_rt(JSRuntime *rt, void *ptr);
577
+ JS_EXTERN void *js_realloc_rt(JSRuntime *rt, void *ptr, size_t size);
578
+ JS_EXTERN size_t js_malloc_usable_size_rt(JSRuntime *rt, const void *ptr);
579
+ JS_EXTERN void *js_mallocz_rt(JSRuntime *rt, size_t size);
580
+
581
+ JS_EXTERN void *js_calloc(JSContext *ctx, size_t count, size_t size);
582
+ JS_EXTERN void *js_malloc(JSContext *ctx, size_t size);
583
+ JS_EXTERN void js_free(JSContext *ctx, void *ptr);
584
+ JS_EXTERN void *js_realloc(JSContext *ctx, void *ptr, size_t size);
585
+ JS_EXTERN size_t js_malloc_usable_size(JSContext *ctx, const void *ptr);
586
+ JS_EXTERN void *js_realloc2(JSContext *ctx, void *ptr, size_t size, size_t *pslack);
587
+ JS_EXTERN void *js_mallocz(JSContext *ctx, size_t size);
588
+ JS_EXTERN char *js_strdup(JSContext *ctx, const char *str);
589
+ JS_EXTERN char *js_strndup(JSContext *ctx, const char *s, size_t n);
590
+
591
+ typedef struct JSMemoryUsage {
592
+ int64_t malloc_size, malloc_limit, memory_used_size;
593
+ int64_t malloc_count;
594
+ int64_t memory_used_count;
595
+ int64_t atom_count, atom_size;
596
+ int64_t str_count, str_size;
597
+ int64_t obj_count, obj_size;
598
+ int64_t prop_count, prop_size;
599
+ int64_t shape_count, shape_size;
600
+ int64_t js_func_count, js_func_size, js_func_code_size;
601
+ int64_t js_func_pc2line_count, js_func_pc2line_size;
602
+ int64_t c_func_count, array_count;
603
+ int64_t fast_array_count, fast_array_elements;
604
+ int64_t binary_object_count, binary_object_size;
605
+ } JSMemoryUsage;
606
+
607
+ JS_EXTERN void JS_ComputeMemoryUsage(JSRuntime *rt, JSMemoryUsage *s);
608
+ JS_EXTERN void JS_DumpMemoryUsage(FILE *fp, const JSMemoryUsage *s, JSRuntime *rt);
609
+
610
+ /* atom support */
611
+ #define JS_ATOM_NULL 0
612
+
613
+ JS_EXTERN JSAtom JS_NewAtomLen(JSContext *ctx, const char *str, size_t len);
614
+ JS_EXTERN JSAtom JS_NewAtom(JSContext *ctx, const char *str);
615
+ JS_EXTERN JSAtom JS_NewAtomUInt32(JSContext *ctx, uint32_t n);
616
+ JS_EXTERN JSAtom JS_DupAtom(JSContext *ctx, JSAtom v);
617
+ JS_EXTERN JSAtom JS_DupAtomRT(JSRuntime *rt, JSAtom v);
618
+ JS_EXTERN void JS_FreeAtom(JSContext *ctx, JSAtom v);
619
+ JS_EXTERN void JS_FreeAtomRT(JSRuntime *rt, JSAtom v);
620
+ JS_EXTERN JSValue JS_AtomToValue(JSContext *ctx, JSAtom atom);
621
+ JS_EXTERN JSValue JS_AtomToString(JSContext *ctx, JSAtom atom);
622
+ JS_EXTERN const char *JS_AtomToCStringLen(JSContext *ctx, size_t *plen, JSAtom atom);
623
+ static inline const char *JS_AtomToCString(JSContext *ctx, JSAtom atom)
624
+ {
625
+ return JS_AtomToCStringLen(ctx, NULL, atom);
626
+ }
627
+ JS_EXTERN JSAtom JS_ValueToAtom(JSContext *ctx, JSValueConst val);
628
+
629
+ /* object class support */
630
+
631
+ typedef struct JSPropertyEnum {
632
+ bool is_enumerable;
633
+ JSAtom atom;
634
+ } JSPropertyEnum;
635
+
636
+ typedef struct JSPropertyDescriptor {
637
+ int flags;
638
+ JSValue value;
639
+ JSValue getter;
640
+ JSValue setter;
641
+ } JSPropertyDescriptor;
642
+
643
+ typedef struct JSClassExoticMethods {
644
+ /* Return -1 if exception (can only happen in case of Proxy object),
645
+ false if the property does not exists, true if it exists. If 1 is
646
+ returned, the property descriptor 'desc' is filled if != NULL. */
647
+ int (*get_own_property)(JSContext *ctx, JSPropertyDescriptor *desc,
648
+ JSValueConst obj, JSAtom prop);
649
+ /* '*ptab' should hold the '*plen' property keys. Return 0 if OK,
650
+ -1 if exception. The 'is_enumerable' field is ignored.
651
+ */
652
+ int (*get_own_property_names)(JSContext *ctx, JSPropertyEnum **ptab,
653
+ uint32_t *plen, JSValueConst obj);
654
+ /* return < 0 if exception, or true/false */
655
+ int (*delete_property)(JSContext *ctx, JSValueConst obj, JSAtom prop);
656
+ /* return < 0 if exception or true/false */
657
+ int (*define_own_property)(JSContext *ctx, JSValueConst this_obj,
658
+ JSAtom prop, JSValueConst val,
659
+ JSValueConst getter, JSValueConst setter,
660
+ int flags);
661
+ /* The following methods can be emulated with the previous ones,
662
+ so they are usually not needed */
663
+ /* return < 0 if exception or true/false */
664
+ int (*has_property)(JSContext *ctx, JSValueConst obj, JSAtom atom);
665
+ JSValue (*get_property)(JSContext *ctx, JSValueConst obj, JSAtom atom,
666
+ JSValueConst receiver);
667
+ /* return < 0 if exception or true/false */
668
+ int (*set_property)(JSContext *ctx, JSValueConst obj, JSAtom atom,
669
+ JSValueConst value, JSValueConst receiver, int flags);
670
+ } JSClassExoticMethods;
671
+
672
+ typedef void JSClassFinalizer(JSRuntime *rt, JSValueConst val);
673
+ typedef void JSClassGCMark(JSRuntime *rt, JSValueConst val,
674
+ JS_MarkFunc *mark_func);
675
+ #define JS_CALL_FLAG_CONSTRUCTOR (1 << 0)
676
+ typedef JSValue JSClassCall(JSContext *ctx, JSValueConst func_obj,
677
+ JSValueConst this_val, int argc,
678
+ JSValueConst *argv, int flags);
679
+
680
+ typedef struct JSClassDef {
681
+ const char *class_name; /* pure ASCII only! */
682
+ JSClassFinalizer *finalizer;
683
+ JSClassGCMark *gc_mark;
684
+ /* if call != NULL, the object is a function. If (flags &
685
+ JS_CALL_FLAG_CONSTRUCTOR) != 0, the function is called as a
686
+ constructor. In this case, 'this_val' is new.target. A
687
+ constructor call only happens if the object constructor bit is
688
+ set (see JS_SetConstructorBit()). */
689
+ JSClassCall *call;
690
+ /* XXX: suppress this indirection ? It is here only to save memory
691
+ because only a few classes need these methods */
692
+ JSClassExoticMethods *exotic;
693
+ } JSClassDef;
694
+
695
+ #define JS_EVAL_OPTIONS_VERSION 1
696
+
697
+ typedef struct JSEvalOptions {
698
+ int version;
699
+ int eval_flags;
700
+ const char *filename;
701
+ int line_num;
702
+ // can add new fields in ABI-compatible manner by incrementing JS_EVAL_OPTIONS_VERSION
703
+ } JSEvalOptions;
704
+
705
+ #define JS_INVALID_CLASS_ID 0
706
+ JS_EXTERN JSClassID JS_NewClassID(JSRuntime *rt, JSClassID *pclass_id);
707
+ /* Returns the class ID if `v` is an object, otherwise returns JS_INVALID_CLASS_ID. */
708
+ JS_EXTERN JSClassID JS_GetClassID(JSValueConst v);
709
+ JS_EXTERN int JS_NewClass(JSRuntime *rt, JSClassID class_id, const JSClassDef *class_def);
710
+ JS_EXTERN bool JS_IsRegisteredClass(JSRuntime *rt, JSClassID class_id);
711
+ /* Returns the class name or JS_ATOM_NULL if `id` is not a registered class. Must be freed with JS_FreeAtom. */
712
+ JS_EXTERN JSAtom JS_GetClassName(JSRuntime *rt, JSClassID class_id);
713
+
714
+ /* value handling */
715
+
716
+ static inline JSValue JS_NewBool(JSContext *ctx, bool val)
717
+ {
718
+ (void)&ctx;
719
+ return JS_MKVAL(JS_TAG_BOOL, (val != 0));
720
+ }
721
+
722
+ static inline JSValue JS_NewInt32(JSContext *ctx, int32_t val)
723
+ {
724
+ (void)&ctx;
725
+ return JS_MKVAL(JS_TAG_INT, val);
726
+ }
727
+
728
+ static inline JSValue JS_NewFloat64(JSContext *ctx, double val)
729
+ {
730
+ (void)&ctx;
731
+ return __JS_NewFloat64(val);
732
+ }
733
+
734
+ static inline JSValue JS_NewCatchOffset(JSContext *ctx, int32_t val)
735
+ {
736
+ (void)&ctx;
737
+ return JS_MKVAL(JS_TAG_CATCH_OFFSET, val);
738
+ }
739
+
740
+ static inline JSValue JS_NewInt64(JSContext *ctx, int64_t val)
741
+ {
742
+ JSValue v;
743
+ if (val >= INT32_MIN && val <= INT32_MAX) {
744
+ v = JS_NewInt32(ctx, (int32_t)val);
745
+ } else {
746
+ v = JS_NewFloat64(ctx, (double)val);
747
+ }
748
+ return v;
749
+ }
750
+
751
+ static inline JSValue JS_NewUint32(JSContext *ctx, uint32_t val)
752
+ {
753
+ JSValue v;
754
+ if (val <= INT32_MAX) {
755
+ v = JS_NewInt32(ctx, (int32_t)val);
756
+ } else {
757
+ v = JS_NewFloat64(ctx, (double)val);
758
+ }
759
+ return v;
760
+ }
761
+
762
+ JS_EXTERN JSValue JS_NewNumber(JSContext *ctx, double d);
763
+ JS_EXTERN JSValue JS_NewBigInt64(JSContext *ctx, int64_t v);
764
+ JS_EXTERN JSValue JS_NewBigUint64(JSContext *ctx, uint64_t v);
765
+
766
+ static inline bool JS_IsNumber(JSValueConst v)
767
+ {
768
+ int tag = JS_VALUE_GET_TAG(v);
769
+ return tag == JS_TAG_INT || JS_TAG_IS_FLOAT64(tag);
770
+ }
771
+
772
+ static inline bool JS_IsBigInt(JSValueConst v)
773
+ {
774
+ int tag = JS_VALUE_GET_TAG(v);
775
+ return tag == JS_TAG_BIG_INT || tag == JS_TAG_SHORT_BIG_INT;
776
+ }
777
+
778
+ static inline bool JS_IsBool(JSValueConst v)
779
+ {
780
+ return JS_VALUE_GET_TAG(v) == JS_TAG_BOOL;
781
+ }
782
+
783
+ static inline bool JS_IsNull(JSValueConst v)
784
+ {
785
+ return JS_VALUE_GET_TAG(v) == JS_TAG_NULL;
786
+ }
787
+
788
+ static inline bool JS_IsUndefined(JSValueConst v)
789
+ {
790
+ return JS_VALUE_GET_TAG(v) == JS_TAG_UNDEFINED;
791
+ }
792
+
793
+ static inline bool JS_IsException(JSValueConst v)
794
+ {
795
+ return JS_VALUE_GET_TAG(v) == JS_TAG_EXCEPTION;
796
+ }
797
+
798
+ static inline bool JS_IsUninitialized(JSValueConst v)
799
+ {
800
+ return JS_VALUE_GET_TAG(v) == JS_TAG_UNINITIALIZED;
801
+ }
802
+
803
+ static inline bool JS_IsString(JSValueConst v)
804
+ {
805
+ int tag = JS_VALUE_GET_TAG(v);
806
+ return tag == JS_TAG_STRING || tag == JS_TAG_STRING_ROPE;
807
+ }
808
+
809
+ static inline bool JS_IsSymbol(JSValueConst v)
810
+ {
811
+ return JS_VALUE_GET_TAG(v) == JS_TAG_SYMBOL;
812
+ }
813
+
814
+ static inline bool JS_IsObject(JSValueConst v)
815
+ {
816
+ return JS_VALUE_GET_TAG(v) == JS_TAG_OBJECT;
817
+ }
818
+
819
+ static inline bool JS_IsModule(JSValueConst v)
820
+ {
821
+ return JS_VALUE_GET_TAG(v) == JS_TAG_MODULE;
822
+ }
823
+
824
+ JS_EXTERN JSValue JS_Throw(JSContext *ctx, JSValue obj);
825
+ JS_EXTERN JSValue JS_GetException(JSContext *ctx);
826
+ JS_EXTERN bool JS_HasException(JSContext *ctx);
827
+ JS_EXTERN bool JS_IsError(JSValueConst val);
828
+ JS_EXTERN bool JS_IsUncatchableError(JSValueConst val);
829
+ JS_EXTERN void JS_SetUncatchableError(JSContext *ctx, JSValueConst val);
830
+ JS_EXTERN void JS_ClearUncatchableError(JSContext *ctx, JSValueConst val);
831
+ // Shorthand for:
832
+ // JSValue exc = JS_GetException(ctx);
833
+ // JS_ClearUncatchableError(ctx, exc);
834
+ // JS_Throw(ctx, exc);
835
+ JS_EXTERN void JS_ResetUncatchableError(JSContext *ctx);
836
+ JS_EXTERN JSValue JS_NewError(JSContext *ctx);
837
+ JS_EXTERN JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_NewInternalError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...);
838
+ JS_EXTERN JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_NewPlainError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...);
839
+ JS_EXTERN JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_NewRangeError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...);
840
+ JS_EXTERN JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_NewReferenceError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...);
841
+ JS_EXTERN JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_NewSyntaxError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...);
842
+ JS_EXTERN JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_NewTypeError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...);
843
+ JS_EXTERN JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_ThrowInternalError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...);
844
+ JS_EXTERN JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_ThrowPlainError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...);
845
+ JS_EXTERN JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_ThrowRangeError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...);
846
+ JS_EXTERN JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_ThrowReferenceError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...);
847
+ JS_EXTERN JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_ThrowSyntaxError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...);
848
+ JS_EXTERN JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_ThrowTypeError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...);
849
+ JS_EXTERN JSValue JS_PRINTF_FORMAT_ATTR(3, 4) JS_ThrowDOMException(JSContext *ctx, const char *name, JS_PRINTF_FORMAT const char *fmt, ...);
850
+ JS_EXTERN JSValue JS_ThrowOutOfMemory(JSContext *ctx);
851
+ JS_EXTERN void JS_FreeValue(JSContext *ctx, JSValue v);
852
+ JS_EXTERN void JS_FreeValueRT(JSRuntime *rt, JSValue v);
853
+ JS_EXTERN JSValue JS_DupValue(JSContext *ctx, JSValueConst v);
854
+ JS_EXTERN JSValue JS_DupValueRT(JSRuntime *rt, JSValueConst v);
855
+ JS_EXTERN int JS_ToBool(JSContext *ctx, JSValueConst val); /* return -1 for JS_EXCEPTION */
856
+ static inline JSValue JS_ToBoolean(JSContext *ctx, JSValueConst val)
857
+ {
858
+ return JS_NewBool(ctx, JS_ToBool(ctx, val));
859
+ }
860
+ JS_EXTERN JSValue JS_ToNumber(JSContext *ctx, JSValueConst val);
861
+ JS_EXTERN int JS_ToInt32(JSContext *ctx, int32_t *pres, JSValueConst val);
862
+ static inline int JS_ToUint32(JSContext *ctx, uint32_t *pres, JSValueConst val)
863
+ {
864
+ return JS_ToInt32(ctx, (int32_t*)pres, val);
865
+ }
866
+ JS_EXTERN int JS_ToInt64(JSContext *ctx, int64_t *pres, JSValueConst val);
867
+ JS_EXTERN int JS_ToIndex(JSContext *ctx, uint64_t *plen, JSValueConst val);
868
+ JS_EXTERN int JS_ToFloat64(JSContext *ctx, double *pres, JSValueConst val);
869
+ /* return an exception if 'val' is a Number */
870
+ JS_EXTERN int JS_ToBigInt64(JSContext *ctx, int64_t *pres, JSValueConst val);
871
+ JS_EXTERN int JS_ToBigUint64(JSContext *ctx, uint64_t *pres, JSValueConst val);
872
+ /* same as JS_ToInt64() but allow BigInt */
873
+ JS_EXTERN int JS_ToInt64Ext(JSContext *ctx, int64_t *pres, JSValueConst val);
874
+
875
+ JS_EXTERN JSValue JS_NewStringLen(JSContext *ctx, const char *str1, size_t len1);
876
+ static inline JSValue JS_NewString(JSContext *ctx, const char *str) {
877
+ return JS_NewStringLen(ctx, str, strlen(str));
878
+ }
879
+ // makes a copy of the input; does not check if the input is valid UTF-16,
880
+ // that is the responsibility of the caller
881
+ JS_EXTERN JSValue JS_NewStringUTF16(JSContext *ctx, const uint16_t *buf,
882
+ size_t len);
883
+ JS_EXTERN JSValue JS_NewAtomString(JSContext *ctx, const char *str);
884
+ JS_EXTERN JSValue JS_ToString(JSContext *ctx, JSValueConst val);
885
+ JS_EXTERN JSValue JS_ToPropertyKey(JSContext *ctx, JSValueConst val);
886
+ JS_EXTERN const char *JS_ToCStringLen2(JSContext *ctx, size_t *plen, JSValueConst val1, bool cesu8);
887
+ static inline const char *JS_ToCStringLen(JSContext *ctx, size_t *plen, JSValueConst val1)
888
+ {
889
+ return JS_ToCStringLen2(ctx, plen, val1, 0);
890
+ }
891
+ static inline const char *JS_ToCString(JSContext *ctx, JSValueConst val1)
892
+ {
893
+ return JS_ToCStringLen2(ctx, NULL, val1, 0);
894
+ }
895
+ // returns a utf-16 version of the string in native endianness; the
896
+ // string is not nul terminated and can contain unmatched surrogates
897
+ // |*plen| is in uint16s, not code points; a surrogate pair such as
898
+ // U+D834 U+DF06 has len=2; an unmatched surrogate has len=1
899
+ JS_EXTERN const uint16_t *JS_ToCStringLenUTF16(JSContext *ctx, size_t *plen,
900
+ JSValueConst val1);
901
+ static inline const uint16_t *JS_ToCStringUTF16(JSContext *ctx,
902
+ JSValueConst val1)
903
+ {
904
+ return JS_ToCStringLenUTF16(ctx, NULL, val1);
905
+ }
906
+ JS_EXTERN void JS_FreeCString(JSContext *ctx, const char *ptr);
907
+ JS_EXTERN void JS_FreeCStringRT(JSRuntime *rt, const char *ptr);
908
+ JS_EXTERN void JS_FreeCStringUTF16(JSContext *ctx, const uint16_t *ptr);
909
+ JS_EXTERN void JS_FreeCStringRT_UTF16(JSRuntime *rt, const uint16_t *ptr);
910
+
911
+ JS_EXTERN JSValue JS_NewObjectProtoClass(JSContext *ctx, JSValueConst proto,
912
+ JSClassID class_id);
913
+ JS_EXTERN JSValue JS_NewObjectClass(JSContext *ctx, JSClassID class_id);
914
+ JS_EXTERN JSValue JS_NewObjectProto(JSContext *ctx, JSValueConst proto);
915
+ JS_EXTERN JSValue JS_NewObject(JSContext *ctx);
916
+ // takes ownership of the values
917
+ JS_EXTERN JSValue JS_NewObjectFrom(JSContext *ctx, int count,
918
+ const JSAtom *props,
919
+ const JSValue *values);
920
+ // takes ownership of the values
921
+ JS_EXTERN JSValue JS_NewObjectFromStr(JSContext *ctx, int count,
922
+ const char **props,
923
+ const JSValue *values);
924
+ JS_EXTERN JSValue JS_ToObject(JSContext *ctx, JSValueConst val);
925
+ JS_EXTERN JSValue JS_ToObjectString(JSContext *ctx, JSValueConst val);
926
+
927
+ JS_EXTERN bool JS_IsFunction(JSContext* ctx, JSValueConst val);
928
+ JS_EXTERN bool JS_IsConstructor(JSContext* ctx, JSValueConst val);
929
+ JS_EXTERN bool JS_SetConstructorBit(JSContext *ctx, JSValueConst func_obj, bool val);
930
+
931
+ JS_EXTERN bool JS_IsRegExp(JSValueConst val);
932
+ JS_EXTERN bool JS_IsMap(JSValueConst val);
933
+ JS_EXTERN bool JS_IsSet(JSValueConst val);
934
+ JS_EXTERN bool JS_IsWeakRef(JSValueConst val);
935
+ JS_EXTERN bool JS_IsWeakSet(JSValueConst val);
936
+ JS_EXTERN bool JS_IsWeakMap(JSValueConst val);
937
+ JS_EXTERN bool JS_IsDataView(JSValueConst val);
938
+
939
+ JS_EXTERN JSValue JS_NewArray(JSContext *ctx);
940
+ // takes ownership of the values
941
+ JS_EXTERN JSValue JS_NewArrayFrom(JSContext *ctx, int count,
942
+ const JSValue *values);
943
+ // reader beware: JS_IsArray used to "punch" through proxies and check
944
+ // if the target object is an array but it no longer does; use JS_IsProxy
945
+ // and JS_GetProxyTarget instead, and remember that the target itself can
946
+ // also be a proxy, ad infinitum
947
+ JS_EXTERN bool JS_IsArray(JSValueConst val);
948
+
949
+ JS_EXTERN bool JS_IsProxy(JSValueConst val);
950
+ JS_EXTERN JSValue JS_GetProxyTarget(JSContext *ctx, JSValueConst proxy);
951
+ JS_EXTERN JSValue JS_GetProxyHandler(JSContext *ctx, JSValueConst proxy);
952
+ JS_EXTERN JSValue JS_NewProxy(JSContext *ctx, JSValueConst target,
953
+ JSValueConst handler);
954
+
955
+ JS_EXTERN JSValue JS_NewDate(JSContext *ctx, double epoch_ms);
956
+ JS_EXTERN bool JS_IsDate(JSValueConst v);
957
+
958
+ JS_EXTERN JSValue JS_GetProperty(JSContext *ctx, JSValueConst this_obj, JSAtom prop);
959
+ JS_EXTERN JSValue JS_GetPropertyUint32(JSContext *ctx, JSValueConst this_obj,
960
+ uint32_t idx);
961
+ JS_EXTERN JSValue JS_GetPropertyInt64(JSContext *ctx, JSValueConst this_obj,
962
+ int64_t idx);
963
+ JS_EXTERN JSValue JS_GetPropertyStr(JSContext *ctx, JSValueConst this_obj,
964
+ const char *prop);
965
+
966
+ JS_EXTERN int JS_SetProperty(JSContext *ctx, JSValueConst this_obj,
967
+ JSAtom prop, JSValue val);
968
+ JS_EXTERN int JS_SetPropertyUint32(JSContext *ctx, JSValueConst this_obj,
969
+ uint32_t idx, JSValue val);
970
+ JS_EXTERN int JS_SetPropertyInt64(JSContext *ctx, JSValueConst this_obj,
971
+ int64_t idx, JSValue val);
972
+ JS_EXTERN int JS_SetPropertyStr(JSContext *ctx, JSValueConst this_obj,
973
+ const char *prop, JSValue val);
974
+ JS_EXTERN int JS_HasProperty(JSContext *ctx, JSValueConst this_obj, JSAtom prop);
975
+ JS_EXTERN int JS_IsExtensible(JSContext *ctx, JSValueConst obj);
976
+ JS_EXTERN int JS_PreventExtensions(JSContext *ctx, JSValueConst obj);
977
+ JS_EXTERN int JS_DeleteProperty(JSContext *ctx, JSValueConst obj, JSAtom prop, int flags);
978
+ JS_EXTERN int JS_SetPrototype(JSContext *ctx, JSValueConst obj, JSValueConst proto_val);
979
+ JS_EXTERN JSValue JS_GetPrototype(JSContext *ctx, JSValueConst val);
980
+ JS_EXTERN int JS_GetLength(JSContext *ctx, JSValueConst obj, int64_t *pres);
981
+ JS_EXTERN int JS_SetLength(JSContext *ctx, JSValueConst obj, int64_t len);
982
+ JS_EXTERN int JS_SealObject(JSContext *ctx, JSValueConst obj);
983
+ JS_EXTERN int JS_FreezeObject(JSContext *ctx, JSValueConst obj);
984
+
985
+ #define JS_GPN_STRING_MASK (1 << 0)
986
+ #define JS_GPN_SYMBOL_MASK (1 << 1)
987
+ #define JS_GPN_PRIVATE_MASK (1 << 2)
988
+ /* only include the enumerable properties */
989
+ #define JS_GPN_ENUM_ONLY (1 << 4)
990
+ /* set theJSPropertyEnum.is_enumerable field */
991
+ #define JS_GPN_SET_ENUM (1 << 5)
992
+
993
+ JS_EXTERN int JS_GetOwnPropertyNames(JSContext *ctx, JSPropertyEnum **ptab,
994
+ uint32_t *plen, JSValueConst obj,
995
+ int flags);
996
+ JS_EXTERN int JS_GetOwnProperty(JSContext *ctx, JSPropertyDescriptor *desc,
997
+ JSValueConst obj, JSAtom prop);
998
+ JS_EXTERN void JS_FreePropertyEnum(JSContext *ctx, JSPropertyEnum *tab,
999
+ uint32_t len);
1000
+
1001
+ JS_EXTERN JSValue JS_Call(JSContext *ctx, JSValueConst func_obj,
1002
+ JSValueConst this_obj, int argc, JSValueConst *argv);
1003
+ JS_EXTERN JSValue JS_Invoke(JSContext *ctx, JSValueConst this_val, JSAtom atom,
1004
+ int argc, JSValueConst *argv);
1005
+ JS_EXTERN JSValue JS_CallConstructor(JSContext *ctx, JSValueConst func_obj,
1006
+ int argc, JSValueConst *argv);
1007
+ JS_EXTERN JSValue JS_CallConstructor2(JSContext *ctx, JSValueConst func_obj,
1008
+ JSValueConst new_target,
1009
+ int argc, JSValueConst *argv);
1010
+ /* Try to detect if the input is a module. Returns true if parsing the input
1011
+ * as a module produces no syntax errors. It's a naive approach that is not
1012
+ * wholly infallible: non-strict classic scripts may _parse_ okay as a module
1013
+ * but not _execute_ as one (different runtime semantics.) Use with caution.
1014
+ * |input| can be either ASCII or UTF-8 encoded source code.
1015
+ * Returns false if QuickJS was built with -DQJS_DISABLE_PARSER.
1016
+ */
1017
+ JS_EXTERN bool JS_DetectModule(const char *input, size_t input_len);
1018
+ /* 'input' must be zero terminated i.e. input[input_len] = '\0'. */
1019
+ JS_EXTERN JSValue JS_Eval(JSContext *ctx, const char *input, size_t input_len,
1020
+ const char *filename, int eval_flags);
1021
+ JS_EXTERN JSValue JS_Eval2(JSContext *ctx, const char *input, size_t input_len,
1022
+ JSEvalOptions *options);
1023
+ JS_EXTERN JSValue JS_EvalThis(JSContext *ctx, JSValueConst this_obj,
1024
+ const char *input, size_t input_len,
1025
+ const char *filename, int eval_flags);
1026
+ JS_EXTERN JSValue JS_EvalThis2(JSContext *ctx, JSValueConst this_obj,
1027
+ const char *input, size_t input_len,
1028
+ JSEvalOptions *options);
1029
+ JS_EXTERN JSValue JS_GetGlobalObject(JSContext *ctx);
1030
+ JS_EXTERN int JS_IsInstanceOf(JSContext *ctx, JSValueConst val, JSValueConst obj);
1031
+ JS_EXTERN int JS_DefineProperty(JSContext *ctx, JSValueConst this_obj,
1032
+ JSAtom prop, JSValueConst val,
1033
+ JSValueConst getter, JSValueConst setter,
1034
+ int flags);
1035
+ JS_EXTERN int JS_DefinePropertyValue(JSContext *ctx, JSValueConst this_obj,
1036
+ JSAtom prop, JSValue val, int flags);
1037
+ JS_EXTERN int JS_DefinePropertyValueUint32(JSContext *ctx, JSValueConst this_obj,
1038
+ uint32_t idx, JSValue val, int flags);
1039
+ JS_EXTERN int JS_DefinePropertyValueStr(JSContext *ctx, JSValueConst this_obj,
1040
+ const char *prop, JSValue val, int flags);
1041
+ JS_EXTERN int JS_DefinePropertyGetSet(JSContext *ctx, JSValueConst this_obj,
1042
+ JSAtom prop, JSValue getter, JSValue setter,
1043
+ int flags);
1044
+ /* Only supported for custom classes, returns 0 on success < 0 otherwise. */
1045
+ JS_EXTERN int JS_SetOpaque(JSValueConst obj, void *opaque);
1046
+ JS_EXTERN void *JS_GetOpaque(JSValueConst obj, JSClassID class_id);
1047
+ JS_EXTERN void *JS_GetOpaque2(JSContext *ctx, JSValueConst obj, JSClassID class_id);
1048
+ JS_EXTERN void *JS_GetAnyOpaque(JSValueConst obj, JSClassID *class_id);
1049
+
1050
+ /* 'buf' must be zero terminated i.e. buf[buf_len] = '\0'. */
1051
+ JS_EXTERN JSValue JS_ParseJSON(JSContext *ctx, const char *buf, size_t buf_len,
1052
+ const char *filename);
1053
+ JS_EXTERN JSValue JS_JSONStringify(JSContext *ctx, JSValueConst obj,
1054
+ JSValueConst replacer, JSValueConst space0);
1055
+
1056
+ typedef void JSFreeArrayBufferDataFunc(JSRuntime *rt, void *opaque, void *ptr);
1057
+ JS_EXTERN JSValue JS_NewArrayBuffer(JSContext *ctx, uint8_t *buf, size_t len,
1058
+ JSFreeArrayBufferDataFunc *free_func, void *opaque,
1059
+ bool is_shared);
1060
+ JS_EXTERN JSValue JS_NewArrayBufferCopy(JSContext *ctx, const uint8_t *buf, size_t len);
1061
+ JS_EXTERN void JS_DetachArrayBuffer(JSContext *ctx, JSValueConst obj);
1062
+ JS_EXTERN uint8_t *JS_GetArrayBuffer(JSContext *ctx, size_t *psize, JSValueConst obj);
1063
+ JS_EXTERN bool JS_IsArrayBuffer(JSValueConst obj);
1064
+ // returns true or false if obj is an ArrayBuffer, -1 otherwise
1065
+ JS_EXTERN int JS_IsImmutableArrayBuffer(JSValueConst obj);
1066
+ // returns 0 if obj is an ArrayBuffer, -1 otherwise
1067
+ JS_EXTERN int JS_SetImmutableArrayBuffer(JSValueConst obj, bool immutable);
1068
+ JS_EXTERN uint8_t *JS_GetUint8Array(JSContext *ctx, size_t *psize, JSValueConst obj);
1069
+
1070
+ typedef enum JSTypedArrayEnum {
1071
+ JS_TYPED_ARRAY_UINT8C = 0,
1072
+ JS_TYPED_ARRAY_INT8,
1073
+ JS_TYPED_ARRAY_UINT8,
1074
+ JS_TYPED_ARRAY_INT16,
1075
+ JS_TYPED_ARRAY_UINT16,
1076
+ JS_TYPED_ARRAY_INT32,
1077
+ JS_TYPED_ARRAY_UINT32,
1078
+ JS_TYPED_ARRAY_BIG_INT64,
1079
+ JS_TYPED_ARRAY_BIG_UINT64,
1080
+ JS_TYPED_ARRAY_FLOAT16,
1081
+ JS_TYPED_ARRAY_FLOAT32,
1082
+ JS_TYPED_ARRAY_FLOAT64,
1083
+ } JSTypedArrayEnum;
1084
+
1085
+ JS_EXTERN JSValue JS_NewTypedArray(JSContext *ctx, int argc, JSValueConst *argv,
1086
+ JSTypedArrayEnum array_type);
1087
+ JS_EXTERN JSValue JS_GetTypedArrayBuffer(JSContext *ctx, JSValueConst obj,
1088
+ size_t *pbyte_offset,
1089
+ size_t *pbyte_length,
1090
+ size_t *pbytes_per_element);
1091
+ JS_EXTERN JSValue JS_NewUint8Array(JSContext *ctx, uint8_t *buf, size_t len,
1092
+ JSFreeArrayBufferDataFunc *free_func, void *opaque,
1093
+ bool is_shared);
1094
+ /* returns -1 if not a typed array otherwise return a JSTypedArrayEnum value */
1095
+ JS_EXTERN int JS_GetTypedArrayType(JSValueConst obj);
1096
+ JS_EXTERN JSValue JS_NewUint8ArrayCopy(JSContext *ctx, const uint8_t *buf, size_t len);
1097
+ typedef struct {
1098
+ void *(*sab_alloc)(void *opaque, size_t size);
1099
+ void (*sab_free)(void *opaque, void *ptr);
1100
+ void (*sab_dup)(void *opaque, void *ptr);
1101
+ void *sab_opaque;
1102
+ } JSSharedArrayBufferFunctions;
1103
+ JS_EXTERN void JS_SetSharedArrayBufferFunctions(JSRuntime *rt, const JSSharedArrayBufferFunctions *sf);
1104
+
1105
+ typedef enum JSPromiseStateEnum {
1106
+ // argument to JS_PromiseState() was not in fact a promise
1107
+ JS_PROMISE_NOT_A_PROMISE = -1,
1108
+ JS_PROMISE_PENDING = 0,
1109
+ JS_PROMISE_FULFILLED,
1110
+ JS_PROMISE_REJECTED,
1111
+ } JSPromiseStateEnum;
1112
+
1113
+ JS_EXTERN JSValue JS_NewPromiseCapability(JSContext *ctx, JSValue *resolving_funcs);
1114
+ JS_EXTERN JSPromiseStateEnum JS_PromiseState(JSContext *ctx,
1115
+ JSValueConst promise);
1116
+ JS_EXTERN JSValue JS_PromiseResult(JSContext *ctx, JSValueConst promise);
1117
+ JS_EXTERN bool JS_IsPromise(JSValueConst val);
1118
+ JS_EXTERN JSValue JS_NewSettledPromise(JSContext *ctx, bool is_reject, JSValueConst value);
1119
+
1120
+ JS_EXTERN JSValue JS_NewSymbol(JSContext *ctx, const char *description, bool is_global);
1121
+
1122
+ typedef enum JSPromiseHookType {
1123
+ JS_PROMISE_HOOK_INIT, // emitted when a new promise is created
1124
+ JS_PROMISE_HOOK_BEFORE, // runs right before promise.then is invoked
1125
+ JS_PROMISE_HOOK_AFTER, // runs right after promise.then is invoked
1126
+ JS_PROMISE_HOOK_RESOLVE, // not emitted for rejected promises
1127
+ } JSPromiseHookType;
1128
+
1129
+ // parent_promise is only passed in when type == JS_PROMISE_HOOK_INIT and
1130
+ // is then either a promise object or JS_UNDEFINED if the new promise does
1131
+ // not have a parent promise; only promises created with promise.then have
1132
+ // a parent promise
1133
+ typedef void JSPromiseHook(JSContext *ctx, JSPromiseHookType type,
1134
+ JSValueConst promise, JSValueConst parent_promise,
1135
+ void *opaque);
1136
+ JS_EXTERN void JS_SetPromiseHook(JSRuntime *rt, JSPromiseHook promise_hook,
1137
+ void *opaque);
1138
+
1139
+ /* is_handled = true means that the rejection is handled */
1140
+ typedef void JSHostPromiseRejectionTracker(JSContext *ctx, JSValueConst promise,
1141
+ JSValueConst reason,
1142
+ bool is_handled, void *opaque);
1143
+ JS_EXTERN void JS_SetHostPromiseRejectionTracker(JSRuntime *rt, JSHostPromiseRejectionTracker *cb, void *opaque);
1144
+
1145
+ /* return != 0 if the JS code needs to be interrupted */
1146
+ typedef int JSInterruptHandler(JSRuntime *rt, void *opaque);
1147
+ JS_EXTERN void JS_SetInterruptHandler(JSRuntime *rt, JSInterruptHandler *cb, void *opaque);
1148
+ /* if can_block is true, Atomics.wait() can be used */
1149
+ JS_EXTERN void JS_SetCanBlock(JSRuntime *rt, bool can_block);
1150
+ /* set the [IsHTMLDDA] internal slot */
1151
+ JS_EXTERN void JS_SetIsHTMLDDA(JSContext *ctx, JSValueConst obj);
1152
+
1153
+ typedef struct JSModuleDef JSModuleDef;
1154
+
1155
+ /* return the module specifier (allocated with js_malloc()) or NULL if
1156
+ exception */
1157
+ typedef char *JSModuleNormalizeFunc(JSContext *ctx,
1158
+ const char *module_base_name,
1159
+ const char *module_name, void *opaque);
1160
+ typedef char *JSModuleNormalizeFunc2(JSContext *ctx,
1161
+ const char *module_base_name,
1162
+ const char *module_name,
1163
+ JSValueConst attributes,
1164
+ void *opaque);
1165
+ typedef JSModuleDef *JSModuleLoaderFunc(JSContext *ctx,
1166
+ const char *module_name, void *opaque);
1167
+
1168
+ /* module loader with import attributes support */
1169
+ typedef JSModuleDef *JSModuleLoaderFunc2(JSContext *ctx,
1170
+ const char *module_name, void *opaque,
1171
+ JSValueConst attributes);
1172
+
1173
+ /* return -1 if exception, 0 if OK */
1174
+ typedef int JSModuleCheckSupportedImportAttributes(JSContext *ctx, void *opaque,
1175
+ JSValueConst attributes);
1176
+
1177
+ /* module_normalize = NULL is allowed and invokes the default module
1178
+ filename normalizer */
1179
+ JS_EXTERN void JS_SetModuleLoaderFunc(JSRuntime *rt,
1180
+ JSModuleNormalizeFunc *module_normalize,
1181
+ JSModuleLoaderFunc *module_loader, void *opaque);
1182
+
1183
+ /* same as JS_SetModuleLoaderFunc but with import attributes support */
1184
+ JS_EXTERN void JS_SetModuleLoaderFunc2(JSRuntime *rt,
1185
+ JSModuleNormalizeFunc *module_normalize,
1186
+ JSModuleLoaderFunc2 *module_loader,
1187
+ JSModuleCheckSupportedImportAttributes *module_check_attrs,
1188
+ void *opaque);
1189
+
1190
+ /* Set an attributes-aware module normalizer. Call after JS_SetModuleLoaderFunc2. */
1191
+ JS_EXTERN void JS_SetModuleNormalizeFunc2(JSRuntime *rt,
1192
+ JSModuleNormalizeFunc2 *module_normalize);
1193
+
1194
+ /* return the import.meta object of a module */
1195
+ JS_EXTERN JSValue JS_GetImportMeta(JSContext *ctx, JSModuleDef *m);
1196
+ JS_EXTERN JSAtom JS_GetModuleName(JSContext *ctx, JSModuleDef *m);
1197
+ JS_EXTERN JSValue JS_GetModuleNamespace(JSContext *ctx, JSModuleDef *m);
1198
+
1199
+ /* associate a JSValue to a C module */
1200
+ JS_EXTERN int JS_SetModulePrivateValue(JSContext *ctx, JSModuleDef *m, JSValue val);
1201
+ JS_EXTERN JSValue JS_GetModulePrivateValue(JSContext *ctx, JSModuleDef *m);
1202
+
1203
+ /* JS Job support */
1204
+
1205
+ typedef JSValue JSJobFunc(JSContext *ctx, int argc, JSValueConst *argv);
1206
+ JS_EXTERN int JS_EnqueueJob(JSContext *ctx, JSJobFunc *job_func,
1207
+ int argc, JSValueConst *argv);
1208
+
1209
+ JS_EXTERN bool JS_IsJobPending(JSRuntime *rt);
1210
+ JS_EXTERN int JS_ExecutePendingJob(JSRuntime *rt, JSContext **pctx);
1211
+
1212
+ /* Structure to retrieve (de)serialized SharedArrayBuffer objects. */
1213
+ typedef struct JSSABTab {
1214
+ uint8_t **tab;
1215
+ size_t len;
1216
+ } JSSABTab;
1217
+
1218
+ /* Object Writer/Reader (currently only used to handle precompiled code) */
1219
+ #define JS_WRITE_OBJ_BYTECODE (1 << 0) /* allow function/module */
1220
+ #define JS_WRITE_OBJ_BSWAP (0) /* byte swapped output (obsolete, handled transparently) */
1221
+ #define JS_WRITE_OBJ_SAB (1 << 2) /* allow SharedArrayBuffer */
1222
+ #define JS_WRITE_OBJ_REFERENCE (1 << 3) /* allow object references to encode arbitrary object graph */
1223
+ #define JS_WRITE_OBJ_STRIP_SOURCE (1 << 4) /* do not write source code information */
1224
+ #define JS_WRITE_OBJ_STRIP_DEBUG (1 << 5) /* do not write debug information */
1225
+ JS_EXTERN uint8_t *JS_WriteObject(JSContext *ctx, size_t *psize, JSValueConst obj, int flags);
1226
+ JS_EXTERN uint8_t *JS_WriteObject2(JSContext *ctx, size_t *psize, JSValueConst obj,
1227
+ int flags, JSSABTab *psab_tab);
1228
+
1229
+ #define JS_READ_OBJ_BYTECODE (1 << 0) /* allow function/module */
1230
+ #define JS_READ_OBJ_ROM_DATA (0) /* avoid duplicating 'buf' data (obsolete, broken by ICs) */
1231
+ #define JS_READ_OBJ_SAB (1 << 2) /* allow SharedArrayBuffer */
1232
+ #define JS_READ_OBJ_REFERENCE (1 << 3) /* allow object references */
1233
+ JS_EXTERN JSValue JS_ReadObject(JSContext *ctx, const uint8_t *buf, size_t buf_len, int flags);
1234
+ JS_EXTERN JSValue JS_ReadObject2(JSContext *ctx, const uint8_t *buf, size_t buf_len,
1235
+ int flags, JSSABTab *psab_tab);
1236
+ /* instantiate and evaluate a bytecode function. Only used when
1237
+ reading a script or module with JS_ReadObject() */
1238
+ JS_EXTERN JSValue JS_EvalFunction(JSContext *ctx, JSValue fun_obj);
1239
+ /* load the dependencies of the module 'obj'. Useful when JS_ReadObject()
1240
+ returns a module. */
1241
+ JS_EXTERN int JS_ResolveModule(JSContext *ctx, JSValueConst obj);
1242
+
1243
+ /* only exported for os.Worker() */
1244
+ JS_EXTERN JSAtom JS_GetScriptOrModuleName(JSContext *ctx, int n_stack_levels);
1245
+ /* only exported for os.Worker() */
1246
+ JS_EXTERN JSValue JS_LoadModule(JSContext *ctx, const char *basename,
1247
+ const char *filename);
1248
+
1249
+ /* C function definition */
1250
+ typedef enum JSCFunctionEnum { /* XXX: should rename for namespace isolation */
1251
+ JS_CFUNC_generic,
1252
+ JS_CFUNC_generic_magic,
1253
+ JS_CFUNC_constructor,
1254
+ JS_CFUNC_constructor_magic,
1255
+ JS_CFUNC_constructor_or_func,
1256
+ JS_CFUNC_constructor_or_func_magic,
1257
+ JS_CFUNC_f_f,
1258
+ JS_CFUNC_f_f_f,
1259
+ JS_CFUNC_getter,
1260
+ JS_CFUNC_setter,
1261
+ JS_CFUNC_getter_magic,
1262
+ JS_CFUNC_setter_magic,
1263
+ JS_CFUNC_iterator_next,
1264
+ } JSCFunctionEnum;
1265
+
1266
+ typedef union JSCFunctionType {
1267
+ JSCFunction *generic;
1268
+ JSValue (*generic_magic)(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv, int magic);
1269
+ JSCFunction *constructor;
1270
+ JSValue (*constructor_magic)(JSContext *ctx, JSValueConst new_target, int argc, JSValueConst *argv, int magic);
1271
+ JSCFunction *constructor_or_func;
1272
+ double (*f_f)(double);
1273
+ double (*f_f_f)(double, double);
1274
+ JSValue (*getter)(JSContext *ctx, JSValueConst this_val);
1275
+ JSValue (*setter)(JSContext *ctx, JSValueConst this_val, JSValueConst val);
1276
+ JSValue (*getter_magic)(JSContext *ctx, JSValueConst this_val, int magic);
1277
+ JSValue (*setter_magic)(JSContext *ctx, JSValueConst this_val, JSValueConst val, int magic);
1278
+ JSValue (*iterator_next)(JSContext *ctx, JSValueConst this_val,
1279
+ int argc, JSValueConst *argv, int *pdone, int magic);
1280
+ } JSCFunctionType;
1281
+
1282
+ JS_EXTERN JSValue JS_NewCFunction2(JSContext *ctx, JSCFunction *func,
1283
+ const char *name,
1284
+ int length, JSCFunctionEnum cproto, int magic);
1285
+ JS_EXTERN JSValue JS_NewCFunction3(JSContext *ctx, JSCFunction *func,
1286
+ const char *name,
1287
+ int length, JSCFunctionEnum cproto, int magic,
1288
+ JSValueConst proto_val);
1289
+ JS_EXTERN JSValue JS_NewCFunctionData(JSContext *ctx, JSCFunctionData *func,
1290
+ int length, int magic, int data_len,
1291
+ JSValueConst *data);
1292
+ JS_EXTERN JSValue JS_NewCFunctionData2(JSContext *ctx, JSCFunctionData *func,
1293
+ const char *name,
1294
+ int length, int magic, int data_len,
1295
+ JSValueConst *data);
1296
+ typedef void JSCClosureFinalizerFunc(void*);
1297
+ JS_EXTERN JSValue JS_NewCClosure(JSContext *ctx, JSCClosure *func,
1298
+ const char *name,
1299
+ JSCClosureFinalizerFunc *opaque_finalize,
1300
+ int length, int magic, void *opaque);
1301
+
1302
+ static inline JSValue JS_NewCFunction(JSContext *ctx, JSCFunction *func,
1303
+ const char *name, int length)
1304
+ {
1305
+ return JS_NewCFunction2(ctx, func, name, length, JS_CFUNC_generic, 0);
1306
+ }
1307
+
1308
+ static inline JSValue JS_NewCFunctionMagic(JSContext *ctx, JSCFunctionMagic *func,
1309
+ const char *name, int length,
1310
+ JSCFunctionEnum cproto, int magic)
1311
+ {
1312
+ /* Used to squelch a -Wcast-function-type warning. */
1313
+ JSCFunctionType ft;
1314
+ ft.generic_magic = func;
1315
+ return JS_NewCFunction2(ctx, ft.generic, name, length, cproto, magic);
1316
+ }
1317
+ JS_EXTERN void JS_SetConstructor(JSContext *ctx, JSValueConst func_obj,
1318
+ JSValueConst proto);
1319
+
1320
+ /* C property definition */
1321
+
1322
+ typedef struct JSCFunctionListEntry {
1323
+ const char *name; /* pure ASCII or UTF-8 encoded */
1324
+ uint8_t prop_flags;
1325
+ uint8_t def_type;
1326
+ int16_t magic;
1327
+ union {
1328
+ struct {
1329
+ uint8_t length; /* XXX: should move outside union */
1330
+ uint8_t cproto; /* XXX: should move outside union */
1331
+ JSCFunctionType cfunc;
1332
+ } func;
1333
+ struct {
1334
+ JSCFunctionType get;
1335
+ JSCFunctionType set;
1336
+ } getset;
1337
+ struct {
1338
+ const char *name;
1339
+ int base;
1340
+ } alias;
1341
+ struct {
1342
+ const struct JSCFunctionListEntry *tab;
1343
+ int len;
1344
+ } prop_list;
1345
+ const char *str; /* pure ASCII or UTF-8 encoded */
1346
+ int32_t i32;
1347
+ int64_t i64;
1348
+ uint64_t u64;
1349
+ double f64;
1350
+ } u;
1351
+ } JSCFunctionListEntry;
1352
+
1353
+ #define JS_DEF_CFUNC 0
1354
+ #define JS_DEF_CGETSET 1
1355
+ #define JS_DEF_CGETSET_MAGIC 2
1356
+ #define JS_DEF_PROP_STRING 3
1357
+ #define JS_DEF_PROP_INT32 4
1358
+ #define JS_DEF_PROP_INT64 5
1359
+ #define JS_DEF_PROP_DOUBLE 6
1360
+ #define JS_DEF_PROP_UNDEFINED 7
1361
+ #define JS_DEF_OBJECT 8
1362
+ #define JS_DEF_ALIAS 9
1363
+
1364
+ /* Note: c++ does not like nested designators */
1365
+ #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 } } } }
1366
+ #define JS_CFUNC_DEF2(name, length, func1, prop_flags) { name, prop_flags, JS_DEF_CFUNC, 0, { .func = { length, JS_CFUNC_generic, { .generic = func1 } } } }
1367
+ #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 } } } }
1368
+ #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 } } } }
1369
+ #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 } } } }
1370
+ #define JS_CGETSET_DEF(name, fgetter, fsetter) { name, JS_PROP_CONFIGURABLE, JS_DEF_CGETSET, 0, { .getset = { .get = { .getter = fgetter }, .set = { .setter = fsetter } } } }
1371
+ #define JS_CGETSET_DEF2(name, fgetter, fsetter, prop_flags) { name, prop_flags, JS_DEF_CGETSET, 0, { .getset = { .get = { .getter = fgetter }, .set = { .setter = fsetter } } } }
1372
+ #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 } } } }
1373
+ #define JS_PROP_STRING_DEF(name, cstr, prop_flags) { name, prop_flags, JS_DEF_PROP_STRING, 0, { .str = cstr } }
1374
+ #define JS_PROP_INT32_DEF(name, val, prop_flags) { name, prop_flags, JS_DEF_PROP_INT32, 0, { .i32 = val } }
1375
+ #define JS_PROP_INT64_DEF(name, val, prop_flags) { name, prop_flags, JS_DEF_PROP_INT64, 0, { .i64 = val } }
1376
+ #define JS_PROP_DOUBLE_DEF(name, val, prop_flags) { name, prop_flags, JS_DEF_PROP_DOUBLE, 0, { .f64 = val } }
1377
+ #define JS_PROP_U2D_DEF(name, val, prop_flags) { name, prop_flags, JS_DEF_PROP_DOUBLE, 0, { .u64 = val } }
1378
+ #define JS_PROP_UNDEFINED_DEF(name, prop_flags) { name, prop_flags, JS_DEF_PROP_UNDEFINED, 0, { .i32 = 0 } }
1379
+ #define JS_OBJECT_DEF(name, tab, len, prop_flags) { name, prop_flags, JS_DEF_OBJECT, 0, { .prop_list = { tab, len } } }
1380
+ #define JS_ALIAS_DEF(name, from) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE, JS_DEF_ALIAS, 0, { .alias = { from, -1 } } }
1381
+ #define JS_ALIAS_BASE_DEF(name, from, base) { name, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE, JS_DEF_ALIAS, 0, { .alias = { from, base } } }
1382
+
1383
+ JS_EXTERN int JS_SetPropertyFunctionList(JSContext *ctx, JSValueConst obj,
1384
+ const JSCFunctionListEntry *tab,
1385
+ int len);
1386
+
1387
+ /* C module definition */
1388
+
1389
+ typedef int JSModuleInitFunc(JSContext *ctx, JSModuleDef *m);
1390
+
1391
+ JS_EXTERN JSModuleDef *JS_NewCModule(JSContext *ctx, const char *name_str,
1392
+ JSModuleInitFunc *func);
1393
+ /* can only be called before the module is instantiated */
1394
+ JS_EXTERN int JS_AddModuleExport(JSContext *ctx, JSModuleDef *m, const char *name_str);
1395
+ JS_EXTERN int JS_AddModuleExportList(JSContext *ctx, JSModuleDef *m,
1396
+ const JSCFunctionListEntry *tab, int len);
1397
+ /* can only be called after the module is instantiated */
1398
+ JS_EXTERN int JS_SetModuleExport(JSContext *ctx, JSModuleDef *m, const char *export_name,
1399
+ JSValue val);
1400
+ JS_EXTERN int JS_SetModuleExportList(JSContext *ctx, JSModuleDef *m,
1401
+ const JSCFunctionListEntry *tab, int len);
1402
+
1403
+ /* Version */
1404
+
1405
+ #define QJS_VERSION_MAJOR 0
1406
+ #define QJS_VERSION_MINOR 12
1407
+ #define QJS_VERSION_PATCH 1
1408
+ #define QJS_VERSION_SUFFIX ""
1409
+
1410
+ JS_EXTERN const char* JS_GetVersion(void);
1411
+
1412
+ /* Integration point for quickjs-libc.c, not for public use. */
1413
+ JS_EXTERN uintptr_t js_std_cmd(int cmd, ...);
1414
+
1415
+ #ifdef __cplusplus
1416
+ } /* extern "C" { */
1417
+ #endif
1418
+
1419
+ #endif /* QUICKJS_H */