xbintsc 0.6.28 → 0.7.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 (280) hide show
  1. package/dist/src/binder/declarations.d.ts +1 -0
  2. package/dist/src/binder/declarations.js +37 -0
  3. package/dist/src/binder/declarations.js.map +1 -1
  4. package/dist/src/binder/references.js +12 -0
  5. package/dist/src/binder/references.js.map +1 -1
  6. package/dist/src/checker/globals.js +1 -0
  7. package/dist/src/checker/globals.js.map +1 -1
  8. package/dist/src/checker/type-system.d.ts +5 -0
  9. package/dist/src/checker/type-system.js +22 -3
  10. package/dist/src/checker/type-system.js.map +1 -1
  11. package/dist/src/cli/help.d.ts +1 -1
  12. package/dist/src/codegen/generator/calls/access.js +25 -0
  13. package/dist/src/codegen/generator/calls/access.js.map +1 -1
  14. package/dist/src/codegen/generator/calls/invocation.js +13 -0
  15. package/dist/src/codegen/generator/calls/invocation.js.map +1 -1
  16. package/dist/src/codegen/generator/calls/super.js +3 -4
  17. package/dist/src/codegen/generator/calls/super.js.map +1 -1
  18. package/dist/src/codegen/generator/calls/unwrap.d.ts +18 -0
  19. package/dist/src/codegen/generator/calls/unwrap.js +45 -0
  20. package/dist/src/codegen/generator/calls/unwrap.js.map +1 -0
  21. package/dist/src/codegen/generator/expressions/assignment.js +14 -0
  22. package/dist/src/codegen/generator/expressions/assignment.js.map +1 -1
  23. package/dist/src/codegen/generator/expressions/module-values.d.ts +6 -0
  24. package/dist/src/codegen/generator/expressions/module-values.js +51 -17
  25. package/dist/src/codegen/generator/expressions/module-values.js.map +1 -1
  26. package/dist/src/codegen/generator/expressions/primary.js +9 -5
  27. package/dist/src/codegen/generator/expressions/primary.js.map +1 -1
  28. package/dist/src/codegen/generator/generator.js +4 -0
  29. package/dist/src/codegen/generator/generator.js.map +1 -1
  30. package/dist/src/codegen/generator/lookup-tables.d.ts +22 -0
  31. package/dist/src/codegen/generator/lookup-tables.js +48 -0
  32. package/dist/src/codegen/generator/lookup-tables.js.map +1 -0
  33. package/dist/src/codegen/generator/module.js +38 -7
  34. package/dist/src/codegen/generator/module.js.map +1 -1
  35. package/dist/src/codegen/generator/state.js +17 -0
  36. package/dist/src/codegen/generator/state.js.map +1 -1
  37. package/dist/src/codegen/generator/tables.d.ts +2 -6
  38. package/dist/src/codegen/generator/tables.js +18 -12
  39. package/dist/src/codegen/generator/tables.js.map +1 -1
  40. package/dist/src/driver/bundler/commonjs.d.ts +9 -2
  41. package/dist/src/driver/bundler/commonjs.js +11 -4
  42. package/dist/src/driver/bundler/commonjs.js.map +1 -1
  43. package/dist/src/driver/bundler/concatenate.d.ts +20 -0
  44. package/dist/src/driver/bundler/concatenate.js +68 -4
  45. package/dist/src/driver/bundler/concatenate.js.map +1 -1
  46. package/dist/src/driver/bundler/deferred.d.ts +29 -1
  47. package/dist/src/driver/bundler/deferred.js +114 -5
  48. package/dist/src/driver/bundler/deferred.js.map +1 -1
  49. package/dist/src/driver/bundler/dynamic-import.d.ts +6 -0
  50. package/dist/src/driver/bundler/dynamic-import.js +59 -3
  51. package/dist/src/driver/bundler/dynamic-import.js.map +1 -1
  52. package/dist/src/driver/bundler/graph.d.ts +13 -0
  53. package/dist/src/driver/bundler/graph.js +2 -2
  54. package/dist/src/driver/bundler/graph.js.map +1 -1
  55. package/dist/src/driver/bundler/merge.js +32 -8
  56. package/dist/src/driver/bundler/merge.js.map +1 -1
  57. package/dist/src/driver/bundler/namespace-object.d.ts +11 -0
  58. package/dist/src/driver/bundler/namespace-object.js +32 -0
  59. package/dist/src/driver/bundler/namespace-object.js.map +1 -1
  60. package/dist/src/driver/bundler/reexport-resolution.js +8 -2
  61. package/dist/src/driver/bundler/reexport-resolution.js.map +1 -1
  62. package/dist/src/driver/bundler/symbols.d.ts +4 -0
  63. package/dist/src/driver/bundler/symbols.js +14 -1
  64. package/dist/src/driver/bundler/symbols.js.map +1 -1
  65. package/dist/src/driver/config.d.ts +1 -1
  66. package/dist/src/driver/options.d.ts +1 -4
  67. package/dist/src/driver/runtime-lib.d.ts +27 -0
  68. package/dist/src/driver/runtime-lib.js +78 -2
  69. package/dist/src/driver/runtime-lib.js.map +1 -1
  70. package/dist/src/driver/runtime-objects.d.ts +1 -1
  71. package/dist/src/driver/runtime-objects.js +24 -5
  72. package/dist/src/driver/runtime-objects.js.map +1 -1
  73. package/dist/src/driver/version.d.ts +1 -1
  74. package/dist/src/driver/version.js +1 -1
  75. package/dist/src/driver/version.js.map +1 -1
  76. package/dist/src/extensions/node/buffer/index.d.ts +5 -4
  77. package/dist/src/extensions/node/buffer/index.js +5 -4
  78. package/dist/src/extensions/node/buffer/index.js.map +1 -1
  79. package/dist/src/extensions/node/events/index.js +8 -1
  80. package/dist/src/extensions/node/events/index.js.map +1 -1
  81. package/dist/src/extensions/node/fs/callbacks.d.ts +9 -0
  82. package/dist/src/extensions/node/fs/callbacks.js +42 -0
  83. package/dist/src/extensions/node/fs/callbacks.js.map +1 -0
  84. package/dist/src/extensions/node/fs/index.d.ts +2 -3
  85. package/dist/src/extensions/node/fs/index.js +11 -4
  86. package/dist/src/extensions/node/fs/index.js.map +1 -1
  87. package/dist/src/extensions/node/process/index.d.ts +10 -3
  88. package/dist/src/extensions/node/process/index.js +30 -5
  89. package/dist/src/extensions/node/process/index.js.map +1 -1
  90. package/dist/src/extensions/registry.d.ts +7 -0
  91. package/dist/src/extensions/registry.js +13 -4
  92. package/dist/src/extensions/registry.js.map +1 -1
  93. package/dist/src/lexer/scanner.d.ts +7 -1
  94. package/dist/src/lexer/scanner.js +15 -0
  95. package/dist/src/lexer/scanner.js.map +1 -1
  96. package/dist/src/parser/context.d.ts +10 -0
  97. package/dist/src/parser/context.js +42 -3
  98. package/dist/src/parser/context.js.map +1 -1
  99. package/dist/src/parser/literals.js +6 -2
  100. package/dist/src/parser/literals.js.map +1 -1
  101. package/dist/tests/binder/binder.test.js +43 -0
  102. package/dist/tests/binder/binder.test.js.map +1 -1
  103. package/dist/tests/codegen/llvm.test.js +15 -0
  104. package/dist/tests/codegen/llvm.test.js.map +1 -1
  105. package/dist/tests/driver/bundler-resolve.test.js +12 -0
  106. package/dist/tests/driver/bundler-resolve.test.js.map +1 -1
  107. package/dist/tests/driver/commonjs-prelude.test.d.ts +11 -0
  108. package/dist/tests/driver/commonjs-prelude.test.js +69 -0
  109. package/dist/tests/driver/commonjs-prelude.test.js.map +1 -0
  110. package/dist/tests/driver/compiler.test.js +42 -1
  111. package/dist/tests/driver/compiler.test.js.map +1 -1
  112. package/dist/tests/driver/html-deferred-cycles.test.d.ts +10 -0
  113. package/dist/tests/driver/html-deferred-cycles.test.js +75 -0
  114. package/dist/tests/driver/html-deferred-cycles.test.js.map +1 -0
  115. package/dist/tests/driver/namespace-type-exports.test.d.ts +9 -0
  116. package/dist/tests/driver/namespace-type-exports.test.js +59 -0
  117. package/dist/tests/driver/namespace-type-exports.test.js.map +1 -0
  118. package/dist/tests/driver/runtime-lib.test.js +49 -1
  119. package/dist/tests/driver/runtime-lib.test.js.map +1 -1
  120. package/dist/tests/e2e/compile-basics.test.js +53 -0
  121. package/dist/tests/e2e/compile-basics.test.js.map +1 -1
  122. package/dist/tests/e2e/compile-deferred-marker.test.d.ts +14 -0
  123. package/dist/tests/e2e/compile-deferred-marker.test.js +68 -0
  124. package/dist/tests/e2e/compile-deferred-marker.test.js.map +1 -0
  125. package/dist/tests/e2e/compile-deferred-reexport.test.d.ts +12 -0
  126. package/dist/tests/e2e/compile-deferred-reexport.test.js +89 -0
  127. package/dist/tests/e2e/compile-deferred-reexport.test.js.map +1 -0
  128. package/dist/tests/e2e/compile-error-module.test.d.ts +9 -0
  129. package/dist/tests/e2e/compile-error-module.test.js +29 -0
  130. package/dist/tests/e2e/compile-error-module.test.js.map +1 -0
  131. package/dist/tests/e2e/compile-gc-promises.test.d.ts +11 -0
  132. package/dist/tests/e2e/compile-gc-promises.test.js +52 -0
  133. package/dist/tests/e2e/compile-gc-promises.test.js.map +1 -0
  134. package/dist/tests/e2e/compile-modules.test.js +40 -0
  135. package/dist/tests/e2e/compile-modules.test.js.map +1 -1
  136. package/dist/tests/e2e/compile-node-fs-callbacks.test.d.ts +6 -0
  137. package/dist/tests/e2e/compile-node-fs-callbacks.test.js +46 -0
  138. package/dist/tests/e2e/compile-node-fs-callbacks.test.js.map +1 -0
  139. package/dist/tests/e2e/compile-node-inheritance.test.d.ts +6 -0
  140. package/dist/tests/e2e/compile-node-inheritance.test.js +48 -0
  141. package/dist/tests/e2e/compile-node-inheritance.test.js.map +1 -0
  142. package/dist/tests/e2e/compile-node-require.test.d.ts +9 -0
  143. package/dist/tests/e2e/compile-node-require.test.js +29 -0
  144. package/dist/tests/e2e/compile-node-require.test.js.map +1 -0
  145. package/dist/tests/e2e/compile-node-stats.test.d.ts +8 -0
  146. package/dist/tests/e2e/compile-node-stats.test.js +26 -0
  147. package/dist/tests/e2e/compile-node-stats.test.js.map +1 -0
  148. package/dist/tests/e2e/compile-proxy.test.d.ts +6 -0
  149. package/dist/tests/e2e/compile-proxy.test.js +66 -0
  150. package/dist/tests/e2e/compile-proxy.test.js.map +1 -0
  151. package/dist/tests/e2e/compile-web-globals.test.d.ts +6 -0
  152. package/dist/tests/e2e/compile-web-globals.test.js +94 -0
  153. package/dist/tests/e2e/compile-web-globals.test.js.map +1 -0
  154. package/dist/tests/e2e/fetch.test.js +1 -1
  155. package/dist/tests/e2e/gui-script-import-order.test.js +24 -0
  156. package/dist/tests/e2e/gui-script-import-order.test.js.map +1 -1
  157. package/dist/tests/e2e/json-stringify-to-json.test.d.ts +10 -0
  158. package/dist/tests/e2e/json-stringify-to-json.test.js +61 -0
  159. package/dist/tests/e2e/json-stringify-to-json.test.js.map +1 -0
  160. package/dist/tests/e2e/node-process.test.d.ts +7 -0
  161. package/dist/tests/e2e/node-process.test.js +50 -0
  162. package/dist/tests/e2e/node-process.test.js.map +1 -0
  163. package/dist/tests/e2e/runtime-coverage-buffer.test.js +31 -0
  164. package/dist/tests/e2e/runtime-coverage-buffer.test.js.map +1 -1
  165. package/dist/tests/e2e/web-streams.test.js +44 -29
  166. package/dist/tests/e2e/web-streams.test.js.map +1 -1
  167. package/dist/tests/parser/literals.test.js +19 -0
  168. package/dist/tests/parser/literals.test.js.map +1 -1
  169. package/dist/tests/parser/statements.test.js +13 -0
  170. package/dist/tests/parser/statements.test.js.map +1 -1
  171. package/doc/ai/cli.md +1 -0
  172. package/doc/ai/zh-CN/cli.md +1 -0
  173. package/doc/implemented/binder.md +2 -1
  174. package/doc/implemented/checker.md +5 -0
  175. package/doc/implemented/lexer.md +1 -1
  176. package/doc/implemented/modules.md +23 -1
  177. package/doc/implemented/node/buffer.md +18 -5
  178. package/doc/implemented/node/fs.md +22 -1
  179. package/doc/implemented/node/process.md +65 -5
  180. package/doc/implemented/node.md +7 -1
  181. package/doc/implemented/operators.md +3 -1
  182. package/doc/implemented/parser.md +1 -1
  183. package/doc/implemented/runtime-core.md +6 -2
  184. package/doc/implemented/stdlib.md +69 -2
  185. package/doc/unimplemented/classes.md +1 -0
  186. package/doc/unimplemented/modules.md +1 -0
  187. package/doc/unimplemented/stdlib.md +2 -1
  188. package/doc/xbintsc.config.schema.json +5 -0
  189. package/doc/zh-CN/implemented/binder.md +2 -1
  190. package/doc/zh-CN/implemented/checker.md +3 -0
  191. package/doc/zh-CN/implemented/lexer.md +1 -1
  192. package/doc/zh-CN/implemented/modules.md +17 -1
  193. package/doc/zh-CN/implemented/node/buffer.md +3 -1
  194. package/doc/zh-CN/implemented/node/fs.md +19 -1
  195. package/doc/zh-CN/implemented/node/process.md +59 -5
  196. package/doc/zh-CN/implemented/node.md +1 -1
  197. package/doc/zh-CN/implemented/operators.md +2 -1
  198. package/doc/zh-CN/implemented/parser.md +1 -1
  199. package/doc/zh-CN/implemented/runtime-core.md +3 -2
  200. package/doc/zh-CN/implemented/stdlib.md +58 -2
  201. package/doc/zh-CN/unimplemented/classes.md +1 -0
  202. package/doc/zh-CN/unimplemented/modules.md +1 -0
  203. package/doc/zh-CN/unimplemented/stdlib.md +2 -1
  204. package/package.json +7 -7
  205. package/runtime/ext_node/buffer/buffer.c +5 -4
  206. package/runtime/ext_node/buffer/parts/byte-access.inc +115 -11
  207. package/runtime/ext_node/buffer/parts/prototype.inc +4 -1
  208. package/runtime/ext_node/events/events.c +9 -0
  209. package/runtime/ext_node/fs/callbacks.c +138 -0
  210. package/runtime/ext_node/fs/fs_ops.c +28 -15
  211. package/runtime/ext_node/module/module.c +18 -9
  212. package/runtime/ext_node/process/process.c +117 -32
  213. package/runtime/ext_node/process/streams.c +370 -0
  214. package/runtime/ext_node/process/streams.h +20 -0
  215. package/runtime/rt.h +36 -0
  216. package/runtime/rt_internal.h +10 -10
  217. package/runtime/xt_alloc.c +67 -49
  218. package/runtime/xt_async_io.c +3 -0
  219. package/runtime/xt_builtins.c +26 -23
  220. package/runtime/xt_constructors.c +101 -2
  221. package/runtime/xt_containers/access.inc +29 -1
  222. package/runtime/xt_containers/function_prototypes.inc +58 -0
  223. package/runtime/xt_containers/objects.inc +18 -2
  224. package/runtime/xt_containers.c +1 -0
  225. package/runtime/xt_exit.c +34 -0
  226. package/runtime/xt_fetch/headers.inc +1 -0
  227. package/runtime/xt_fetch/response.inc +28 -8
  228. package/runtime/xt_fetch/webapi.inc +82 -0
  229. package/runtime/xt_fetch.c +5 -0
  230. package/runtime/xt_gc_verify.c +135 -0
  231. package/runtime/xt_intl.c +224 -0
  232. package/runtime/xt_io/exception.inc +40 -1
  233. package/runtime/xt_module_values.c +82 -0
  234. package/runtime/xt_proxy.c +140 -0
  235. package/runtime/xt_stdlib/dispatch.inc +98 -9
  236. package/runtime/xt_stdlib2/date.inc +7 -1
  237. package/runtime/xt_stdlib2/json.inc +28 -0
  238. package/runtime/xt_stdlib2/json_stringify.inc +70 -53
  239. package/runtime/xt_web.h +27 -0
  240. package/runtime/xt_web_abort.c +373 -0
  241. package/scripts/build-runtime.ts +8 -0
  242. package/src/binder/declarations.ts +34 -0
  243. package/src/binder/references.ts +12 -0
  244. package/src/checker/globals.ts +1 -0
  245. package/src/checker/type-system.ts +23 -3
  246. package/src/codegen/generator/calls/access.ts +23 -0
  247. package/src/codegen/generator/calls/invocation.ts +12 -0
  248. package/src/codegen/generator/calls/super.ts +5 -4
  249. package/src/codegen/generator/calls/unwrap.ts +47 -0
  250. package/src/codegen/generator/expressions/assignment.ts +15 -0
  251. package/src/codegen/generator/expressions/module-values.ts +57 -19
  252. package/src/codegen/generator/expressions/primary.ts +9 -9
  253. package/src/codegen/generator/generator.ts +4 -0
  254. package/src/codegen/generator/lookup-tables.ts +63 -0
  255. package/src/codegen/generator/module.ts +38 -7
  256. package/src/codegen/generator/state.ts +17 -0
  257. package/src/codegen/generator/tables.ts +18 -12
  258. package/src/driver/bundler/commonjs.ts +12 -5
  259. package/src/driver/bundler/concatenate.ts +84 -7
  260. package/src/driver/bundler/deferred.ts +122 -6
  261. package/src/driver/bundler/dynamic-import.ts +75 -2
  262. package/src/driver/bundler/graph.ts +2 -2
  263. package/src/driver/bundler/merge.ts +31 -5
  264. package/src/driver/bundler/namespace-object.ts +44 -0
  265. package/src/driver/bundler/reexport-resolution.ts +12 -2
  266. package/src/driver/bundler/symbols.ts +17 -0
  267. package/src/driver/config.ts +1 -1
  268. package/src/driver/options.ts +1 -4
  269. package/src/driver/runtime-lib.ts +87 -2
  270. package/src/driver/runtime-objects.ts +24 -5
  271. package/src/driver/version.ts +1 -1
  272. package/src/extensions/node/buffer/index.ts +5 -4
  273. package/src/extensions/node/events/index.ts +8 -1
  274. package/src/extensions/node/fs/callbacks.ts +45 -0
  275. package/src/extensions/node/fs/index.ts +11 -4
  276. package/src/extensions/node/process/index.ts +30 -5
  277. package/src/extensions/registry.ts +21 -4
  278. package/src/lexer/scanner.ts +16 -1
  279. package/src/parser/context.ts +39 -3
  280. package/src/parser/literals.ts +6 -2
@@ -0,0 +1,373 @@
1
+ /*
2
+ * xbintsc runtime — `AbortController` / `AbortSignal`.
3
+ *
4
+ * A signal is an ordinary object carrying its state under `__`-prefixed own
5
+ * properties (aborted flag, reason, listener array, timeout handle); the
6
+ * public surface lives on the prototype, so `signal instanceof AbortSignal`
7
+ * holds and methods are shared. `aborted` / `reason` / `onabort` are accessors
8
+ * over that state.
9
+ *
10
+ * Covered: `abort(reason?)`, `addEventListener("abort", fn[, { once }])`,
11
+ * `removeEventListener`, `onabort`, `throwIfAborted()`, and the statics
12
+ * `AbortSignal.abort()`, `AbortSignal.timeout(ms)`, `AbortSignal.any([...])`.
13
+ */
14
+
15
+ #include "rt_internal.h"
16
+
17
+ #include <stdlib.h>
18
+ #include <string.h>
19
+
20
+ /* Prototypes are built once and kept alive for the life of the program. */
21
+ static xt_value xt_web_abort_signal_proto_value = XT_UNDEFINED;
22
+ static xt_value xt_web_abort_controller_proto_value = XT_UNDEFINED;
23
+
24
+ /* Method bodies, defined below and installed on the prototypes. */
25
+ static xt_value xt_web_signal_add_listener(xt_value thisValue, xt_value env, int32_t argc,
26
+ xt_value *argv);
27
+ static xt_value xt_web_signal_remove_listener(xt_value thisValue, xt_value env, int32_t argc,
28
+ xt_value *argv);
29
+ static xt_value xt_web_signal_throw_if_aborted(xt_value thisValue, xt_value env, int32_t argc,
30
+ xt_value *argv);
31
+ static xt_value xt_web_controller_abort(xt_value thisValue, xt_value env, int32_t argc,
32
+ xt_value *argv);
33
+ static xt_value xt_web_timeout_fire(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv);
34
+ static xt_value xt_web_chain_fire(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv);
35
+
36
+ static void xt_web_abort_define(xt_value object, const char *name, xt_code_fn fn,
37
+ int32_t arity) {
38
+ xt_value key = xt_string_from_cstr(name);
39
+ xt_value closure = xt_closure_new((void *)fn, 0, NULL);
40
+ xt_function_set_metadata(closure, key, arity);
41
+ xt_object_set(object, key, closure);
42
+ }
43
+
44
+ static void xt_web_abort_define_accessor(xt_value object, const char *name, xt_code_fn getter,
45
+ xt_code_fn setter) {
46
+ xt_value key = xt_string_from_cstr(name);
47
+ xt_value getterValue = xt_closure_new((void *)getter, 0, NULL);
48
+ xt_function_set_metadata(getterValue, key, 0);
49
+ xt_object_define_getter(object, key, getterValue);
50
+ if (!setter) return;
51
+ xt_value setterValue = xt_closure_new((void *)setter, 0, NULL);
52
+ xt_function_set_metadata(setterValue, key, 1);
53
+ xt_object_define_setter(object, key, setterValue);
54
+ }
55
+
56
+ /* -- state ---------------------------------------------------------------- */
57
+
58
+ static int xt_web_signal_is_aborted(xt_value signal) {
59
+ return xt_truthy(xt_object_get_cstr(signal, "__aborted"));
60
+ }
61
+
62
+ static xt_value xt_web_signal_listeners(xt_value signal) {
63
+ xt_value listeners = xt_object_get_cstr(signal, "__listeners");
64
+ if (!XT_IS_ARRAY(listeners)) {
65
+ listeners = xt_array_new(0, NULL);
66
+ xt_object_set(signal, xt_string_from_cstr("__listeners"), listeners);
67
+ }
68
+ return listeners;
69
+ }
70
+
71
+ static int32_t xt_web_count(xt_value array) {
72
+ if (!XT_IS_ARRAY(array)) return 0;
73
+ return (int32_t)xt_to_number(xt_array_length(array));
74
+ }
75
+
76
+ /* `<name>Error` with a message, shaped like the platform's DOMException
77
+ * (`error.name` is what programs branch on). */
78
+ static xt_value xt_web_named_error(const char *name, const char *message) {
79
+ xt_value arguments[1];
80
+ arguments[0] = xt_string_from_cstr(message);
81
+ xt_value error = xt_error_ctor_by_name(xt_string_from_cstr("Error"), 1, arguments);
82
+ if (XT_IS_OBJECT(error)) {
83
+ xt_object_set(error, xt_string_from_cstr("name"), xt_string_from_cstr(name));
84
+ }
85
+ return error;
86
+ }
87
+
88
+ static xt_value xt_web_abort_reason_default(void) {
89
+ return xt_web_named_error("AbortError", "This operation was aborted");
90
+ }
91
+
92
+ static xt_value xt_web_timeout_reason(void) {
93
+ return xt_web_named_error("TimeoutError", "The operation was aborted due to timeout");
94
+ }
95
+
96
+ /* -- aborting ------------------------------------------------------------- */
97
+
98
+ /* Fire the `abort` event: `onabort` first, then every registered listener in
99
+ * registration order; `{ once: true }` entries are dropped after the call. */
100
+ static void xt_web_signal_fire(xt_value signal) {
101
+ xt_value event = xt_object_new();
102
+ xt_object_set(event, xt_string_from_cstr("type"), xt_string_from_cstr("abort"));
103
+ xt_object_set(event, xt_string_from_cstr("target"), signal);
104
+ xt_object_set(event, xt_string_from_cstr("currentTarget"), signal);
105
+
106
+ xt_value handler = xt_object_get_cstr(signal, "__onabort");
107
+ if (XT_IS_FUNCTION(handler)) xt_call_with_this(handler, signal, 1, &event);
108
+
109
+ xt_value listeners = xt_web_signal_listeners(signal);
110
+ int32_t count = xt_web_count(listeners);
111
+ if (count == 0) return;
112
+ /* Snapshot first: a listener may add or remove listeners. */
113
+ xt_value snapshot = xt_array_new(0, NULL);
114
+ for (int32_t index = 0; index < count; index++) {
115
+ xt_array_push(snapshot, xt_array_get(listeners, xt_number((double)index)));
116
+ }
117
+ int32_t snapshotCount = xt_web_count(snapshot);
118
+ for (int32_t index = 0; index < snapshotCount; index++) {
119
+ xt_value entry = xt_array_get(snapshot, xt_number((double)index));
120
+ if (!XT_IS_OBJECT(entry)) continue;
121
+ xt_value fn = xt_object_get_cstr(entry, "fn");
122
+ if (XT_IS_FUNCTION(fn)) xt_call_with_this(fn, signal, 1, &event);
123
+ }
124
+ /* Drop every `once` entry that fired. */
125
+ int32_t remaining = 0;
126
+ for (int32_t index = 0; index < count; index++) {
127
+ xt_value entry = xt_array_get(listeners, xt_number((double)index));
128
+ if (XT_IS_OBJECT(entry) && xt_truthy(xt_object_get_cstr(entry, "once"))) continue;
129
+ if (remaining != index) {
130
+ xt_array_set(listeners, xt_number((double)remaining), entry);
131
+ }
132
+ remaining++;
133
+ }
134
+ xt_as_array(listeners)->length = remaining;
135
+ }
136
+
137
+ static void xt_web_signal_abort(xt_value signal, xt_value reason) {
138
+ if (xt_web_signal_is_aborted(signal)) return;
139
+ xt_object_set(signal, xt_string_from_cstr("__aborted"), XT_TRUE);
140
+ xt_object_set(signal, xt_string_from_cstr("__reason"), reason);
141
+ xt_web_signal_fire(signal);
142
+ }
143
+
144
+ /* A signal that is not aborted yet, chained onto the signal prototype. */
145
+ static xt_value xt_web_signal_new(void) {
146
+ xt_value signal = xt_object_new();
147
+ xt_object_set_prototype(signal, xt_web_abort_signal_proto());
148
+ xt_object_set(signal, xt_string_from_cstr("__aborted"), XT_FALSE);
149
+ xt_object_set(signal, xt_string_from_cstr("__reason"), XT_UNDEFINED);
150
+ xt_object_set(signal, xt_string_from_cstr("__listeners"), xt_array_new(0, NULL));
151
+ return signal;
152
+ }
153
+
154
+ /* -- AbortSignal methods -------------------------------------------------- */
155
+
156
+ static xt_value xt_web_signal_add_listener(xt_value thisValue, xt_value env, int32_t argc,
157
+ xt_value *argv) {
158
+ (void)env;
159
+ xt_value type = xt_arg(argc, argv, 0);
160
+ xt_value listener = xt_arg(argc, argv, 1);
161
+ xt_value options = xt_arg(argc, argv, 2);
162
+ const char *name = XT_IS_STRING(type) ? xt_string_data(type) : NULL;
163
+ if (!name || strcmp(name, "abort") != 0) return XT_UNDEFINED;
164
+ if (!XT_IS_FUNCTION(listener)) return XT_UNDEFINED;
165
+ /* Registering after the signal aborted never fires (same as the platform). */
166
+ if (xt_web_signal_is_aborted(thisValue)) return XT_UNDEFINED;
167
+ int once = 0;
168
+ if (XT_IS_OBJECT(options)) once = xt_truthy(xt_object_get_cstr(options, "once"));
169
+ xt_value entry = xt_object_new();
170
+ xt_object_set(entry, xt_string_from_cstr("fn"), listener);
171
+ xt_object_set(entry, xt_string_from_cstr("once"), once ? XT_TRUE : XT_FALSE);
172
+ xt_array_push(xt_web_signal_listeners(thisValue), entry);
173
+ return XT_UNDEFINED;
174
+ }
175
+
176
+ static xt_value xt_web_signal_remove_listener(xt_value thisValue, xt_value env, int32_t argc,
177
+ xt_value *argv) {
178
+ (void)env;
179
+ xt_value type = xt_arg(argc, argv, 0);
180
+ xt_value listener = xt_arg(argc, argv, 1);
181
+ const char *name = XT_IS_STRING(type) ? xt_string_data(type) : NULL;
182
+ if (!name || strcmp(name, "abort") != 0 || !XT_IS_FUNCTION(listener)) return XT_UNDEFINED;
183
+ xt_value listeners = xt_web_signal_listeners(thisValue);
184
+ int32_t count = xt_web_count(listeners);
185
+ int32_t remaining = 0;
186
+ for (int32_t index = 0; index < count; index++) {
187
+ xt_value entry = xt_array_get(listeners, xt_number((double)index));
188
+ xt_value fn = XT_IS_OBJECT(entry) ? xt_object_get_cstr(entry, "fn") : XT_UNDEFINED;
189
+ if (fn == listener) continue;
190
+ if (remaining != index) xt_array_set(listeners, xt_number((double)remaining), entry);
191
+ remaining++;
192
+ }
193
+ xt_as_array(listeners)->length = remaining;
194
+ return XT_UNDEFINED;
195
+ }
196
+
197
+ static xt_value xt_web_signal_throw_if_aborted(xt_value thisValue, xt_value env, int32_t argc,
198
+ xt_value *argv) {
199
+ (void)env;
200
+ (void)argc;
201
+ (void)argv;
202
+ if (!xt_web_signal_is_aborted(thisValue)) return XT_UNDEFINED;
203
+ xt_throw(xt_object_get_cstr(thisValue, "__reason"));
204
+ return XT_UNDEFINED;
205
+ }
206
+
207
+ static xt_value xt_web_signal_get_aborted(xt_value thisValue, xt_value env, int32_t argc,
208
+ xt_value *argv) {
209
+ (void)env;
210
+ (void)argc;
211
+ (void)argv;
212
+ return xt_web_signal_is_aborted(thisValue) ? XT_TRUE : XT_FALSE;
213
+ }
214
+
215
+ static xt_value xt_web_signal_get_reason(xt_value thisValue, xt_value env, int32_t argc,
216
+ xt_value *argv) {
217
+ (void)env;
218
+ (void)argc;
219
+ (void)argv;
220
+ return xt_object_get_cstr(thisValue, "__reason");
221
+ }
222
+
223
+ static xt_value xt_web_signal_get_onabort(xt_value thisValue, xt_value env, int32_t argc,
224
+ xt_value *argv) {
225
+ (void)env;
226
+ (void)argc;
227
+ (void)argv;
228
+ return xt_object_get_cstr(thisValue, "__onabort");
229
+ }
230
+
231
+ static xt_value xt_web_signal_set_onabort(xt_value thisValue, xt_value env, int32_t argc,
232
+ xt_value *argv) {
233
+ (void)env;
234
+ xt_value handler = xt_arg(argc, argv, 0);
235
+ if (!XT_IS_FUNCTION(handler) && handler != XT_NULL && handler != XT_UNDEFINED) return XT_UNDEFINED;
236
+ xt_object_set(thisValue, xt_string_from_cstr("__onabort"),
237
+ XT_IS_FUNCTION(handler) ? handler : XT_UNDEFINED);
238
+ return XT_UNDEFINED;
239
+ }
240
+
241
+ /* -- AbortController methods ---------------------------------------------- */
242
+
243
+ static xt_value xt_web_controller_abort(xt_value thisValue, xt_value env, int32_t argc,
244
+ xt_value *argv) {
245
+ (void)env;
246
+ xt_value signal = xt_object_get_cstr(thisValue, "signal");
247
+ if (!XT_IS_OBJECT(signal)) return XT_UNDEFINED;
248
+ xt_value reason = xt_arg(argc, argv, 0);
249
+ if (reason == XT_UNDEFINED) reason = xt_web_abort_reason_default();
250
+ xt_web_signal_abort(signal, reason);
251
+ return XT_UNDEFINED;
252
+ }
253
+
254
+ /* -- prototypes ----------------------------------------------------------- */
255
+
256
+ xt_value xt_web_abort_signal_proto(void) {
257
+ if (xt_web_abort_signal_proto_value != XT_UNDEFINED) return xt_web_abort_signal_proto_value;
258
+ xt_value proto = xt_object_new();
259
+ xt_web_abort_signal_proto_value = proto;
260
+ xt_gc_add_root(&xt_web_abort_signal_proto_value);
261
+ xt_web_abort_define(proto, "addEventListener", xt_web_signal_add_listener, 2);
262
+ xt_web_abort_define(proto, "removeEventListener", xt_web_signal_remove_listener, 2);
263
+ xt_web_abort_define(proto, "throwIfAborted", xt_web_signal_throw_if_aborted, 0);
264
+ xt_web_abort_define_accessor(proto, "aborted", xt_web_signal_get_aborted, NULL);
265
+ xt_web_abort_define_accessor(proto, "reason", xt_web_signal_get_reason, NULL);
266
+ xt_web_abort_define_accessor(proto, "onabort", xt_web_signal_get_onabort,
267
+ xt_web_signal_set_onabort);
268
+ return proto;
269
+ }
270
+
271
+ xt_value xt_web_abort_controller_proto(void) {
272
+ if (xt_web_abort_controller_proto_value != XT_UNDEFINED) {
273
+ return xt_web_abort_controller_proto_value;
274
+ }
275
+ xt_value proto = xt_object_new();
276
+ xt_web_abort_controller_proto_value = proto;
277
+ xt_gc_add_root(&xt_web_abort_controller_proto_value);
278
+ xt_web_abort_define(proto, "abort", xt_web_controller_abort, 1);
279
+ return proto;
280
+ }
281
+
282
+ /* -- constructors --------------------------------------------------------- */
283
+
284
+ xt_value xt_abort_signal_ctor(int32_t argc, xt_value *argv) {
285
+ (void)argc;
286
+ (void)argv;
287
+ /* `new AbortSignal()` is illegal in the platform API. */
288
+ xt_throw(xt_error_from_message("TypeError: Illegal constructor"));
289
+ return XT_UNDEFINED;
290
+ }
291
+
292
+ xt_value xt_abort_controller_ctor(int32_t argc, xt_value *argv) {
293
+ (void)argc;
294
+ (void)argv;
295
+ xt_value controller = xt_object_new();
296
+ xt_object_set_prototype(controller, xt_web_abort_controller_proto());
297
+ xt_object_set(controller, xt_string_from_cstr("signal"), xt_web_signal_new());
298
+ return controller;
299
+ }
300
+
301
+ /* -- statics -------------------------------------------------------------- */
302
+
303
+ static xt_value xt_web_signal_aborted(int32_t argc, xt_value *argv) {
304
+ xt_value signal = xt_web_signal_new();
305
+ xt_value reason = xt_arg(argc, argv, 0);
306
+ if (reason == XT_UNDEFINED) reason = xt_web_abort_reason_default();
307
+ xt_web_signal_abort(signal, reason);
308
+ return signal;
309
+ }
310
+
311
+ static xt_value xt_web_signal_timeout(int32_t argc, xt_value *argv) {
312
+ double milliseconds = xt_to_number(xt_arg(argc, argv, 0));
313
+ if (!(milliseconds >= 0)) milliseconds = 0;
314
+ xt_value signal = xt_web_signal_new();
315
+ xt_value environment[1] = {signal};
316
+ xt_value callback = xt_closure_new((void *)xt_web_timeout_fire, 1, environment);
317
+ xt_value arguments[2] = {callback, xt_number(milliseconds)};
318
+ xt_value handle = xt_set_timeout(2, arguments);
319
+ xt_object_set(signal, xt_string_from_cstr("__timer"), handle);
320
+ return signal;
321
+ }
322
+
323
+ static xt_value xt_web_timeout_fire(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
324
+ (void)thisValue;
325
+ (void)argc;
326
+ (void)argv;
327
+ xt_value signal = xt_closure_env(env, 0);
328
+ xt_web_signal_abort(signal, xt_web_timeout_reason());
329
+ return XT_UNDEFINED;
330
+ }
331
+
332
+ /* `AbortSignal.any(signals)`: abort with the first input's reason. */
333
+ static xt_value xt_web_signal_any(int32_t argc, xt_value *argv) {
334
+ xt_value inputs = xt_arg(argc, argv, 0);
335
+ xt_value combined = xt_web_signal_new();
336
+ if (!XT_IS_ARRAY(inputs)) return combined;
337
+ int32_t count = xt_web_count(inputs);
338
+ for (int32_t index = 0; index < count; index++) {
339
+ xt_value input = xt_array_get(inputs, xt_number((double)index));
340
+ if (!XT_IS_OBJECT(input)) continue;
341
+ if (xt_web_signal_is_aborted(input)) {
342
+ xt_web_signal_abort(combined, xt_object_get_cstr(input, "__reason"));
343
+ return combined;
344
+ }
345
+ xt_value environment[2] = {combined, input};
346
+ xt_value closure = xt_closure_new((void *)xt_web_chain_fire, 2, environment);
347
+ xt_value entry = xt_object_new();
348
+ xt_object_set(entry, xt_string_from_cstr("fn"), closure);
349
+ xt_object_set(entry, xt_string_from_cstr("once"), XT_TRUE);
350
+ xt_array_push(xt_web_signal_listeners(input), entry);
351
+ }
352
+ return combined;
353
+ }
354
+
355
+ static xt_value xt_web_chain_fire(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
356
+ (void)thisValue;
357
+ (void)argc;
358
+ (void)argv;
359
+ xt_value combined = xt_closure_env(env, 0);
360
+ xt_value input = xt_closure_env(env, 1);
361
+ xt_web_signal_abort(combined, xt_object_get_cstr(input, "__reason"));
362
+ return XT_UNDEFINED;
363
+ }
364
+
365
+ /* `AbortSignal.<method>(...)`: the code generator's static dispatcher. */
366
+ xt_value xt_abort_signal_static(xt_value method, int32_t argc, xt_value *argv) {
367
+ const char *name = XT_IS_STRING(method) ? xt_string_data(method) : NULL;
368
+ if (!name) return XT_UNDEFINED;
369
+ if (strcmp(name, "abort") == 0) return xt_web_signal_aborted(argc, argv);
370
+ if (strcmp(name, "timeout") == 0) return xt_web_signal_timeout(argc, argv);
371
+ if (strcmp(name, "any") == 0) return xt_web_signal_any(argc, argv);
372
+ return XT_UNDEFINED;
373
+ }
@@ -26,9 +26,11 @@ import { basename, dirname, join, resolve } from "node:path";
26
26
  import { fileURLToPath } from "node:url";
27
27
  import { RUNTIME_SOURCES } from "../src/driver/compiler.js";
28
28
  import { platformSlug } from "../src/driver/paths.js";
29
+ import { runtimeSourceFingerprint, writeRuntimeStamp } from "../src/driver/runtime-lib.js";
29
30
  import { applyMacosDeploymentTarget } from "../src/driver/platform-support.js";
30
31
  import { resolveToolchain } from "../src/driver/toolchain-provider.js";
31
32
  import { RUNTIME_CFLAGS, sysrootFlags } from "../src/driver/toolchain.js";
33
+ import { COMPILER_VERSION } from "../src/driver/version.js";
32
34
  import { nodeExtension } from "../src/extensions/node/index.js";
33
35
 
34
36
  const root = resolve(dirname(fileURLToPath(import.meta.url)), "..");
@@ -117,3 +119,9 @@ const nodeSources = nodeExtension.runtimeSources?.() ?? [];
117
119
  if (nodeSources.length > 0) {
118
120
  archive(`ext_node${archiveSuffix}`, compile(nodeSources, "node"), ar);
119
121
  }
122
+
123
+ /* Record which sources these archives came from: the driver only links them
124
+ * while the runtime sources on disk still match, and otherwise compiles from
125
+ * source (a stale archive would silently run an old runtime). */
126
+ writeRuntimeStamp(outDir, runtimeSourceFingerprint(runtimeDir, COMPILER_VERSION, RUNTIME_CFLAGS), COMPILER_VERSION);
127
+ console.log(`xbintsc: stamped ${join(outDir, "runtime-stamp.json")}`);
@@ -92,6 +92,7 @@ export interface DeclarationMethods {
92
92
  predeclareVariableList(this: Binder, list: VariableDeclarationList, scope: Scope): void;
93
93
  predeclareImport(this: Binder, node: ImportDeclaration, scope: Scope): void;
94
94
  collectFunctionScoped(this: Binder, statements: readonly Statement[], fn: MutableFunction): void;
95
+ reuseHoistedVar(this: Binder, name: BindingName, scope: Scope): boolean;
95
96
  }
96
97
 
97
98
  export const declarationMethods: DeclarationMethods = {
@@ -215,10 +216,43 @@ export const declarationMethods: DeclarationMethods = {
215
216
  ? SymbolKind.Var
216
217
  : SymbolKind.Let;
217
218
  for (const declaration of list.declarations) {
219
+ if (kind === SymbolKind.Var) {
220
+ if (this.reuseHoistedVar(declaration.name, scope)) continue;
221
+ /* `var` is function scoped even when the declaration sits in a block
222
+ * or in a `for` head, so it always lands in the function scope. */
223
+ this.declareBinding(declaration.name, kind, declaration, scope.fn.scope ?? scope, true);
224
+ continue;
225
+ }
218
226
  this.declareBinding(declaration.name, kind, declaration, scope, kind !== SymbolKind.Const);
219
227
  }
220
228
  },
221
229
 
230
+ /**
231
+ * Point a `var` declaration that sits in a nested block (or in a `for` head)
232
+ * at the symbol `collectFunctionScoped` already hoisted into the function
233
+ * scope. Declaring it again in the block would *shadow* the hoisted name, so
234
+ * `{ var x = 1 }` would write a block-local `x` the rest of the function
235
+ * never sees — the bug behind `if (c) { var label = "big" }` losing its
236
+ * value in minified bundles.
237
+ */
238
+ reuseHoistedVar(name: BindingName, scope: Scope): boolean {
239
+ const functionScope = scope.fn.scope;
240
+ if (functionScope === undefined || scope === functionScope) return false;
241
+ if (name.kind === SyntaxKind.Identifier) {
242
+ const hoisted = functionScope.symbols.get(name.text);
243
+ if (!hoisted || hoisted.kind !== SymbolKind.Var) return false;
244
+ this.symbolOfDeclaration.set(name, hoisted);
245
+ return true;
246
+ }
247
+ const pattern = name as ArrayBindingPattern | ObjectBindingPattern;
248
+ let reused = true;
249
+ for (const element of pattern.elements) {
250
+ if (!element) continue;
251
+ if (!this.reuseHoistedVar(element.name, scope)) reused = false;
252
+ }
253
+ return reused;
254
+ },
255
+
222
256
  predeclareImport(node: ImportDeclaration, scope: Scope): void {
223
257
  const clause = node.importClause;
224
258
  if (!clause) return;
@@ -339,6 +339,12 @@ export const referenceMethods: ReferenceMethods = {
339
339
  const paramSymbol = this.declareBinding(parameter.name, SymbolKind.Parameter, parameter, scope, true);
340
340
  if (paramSymbol) params.push(paramSymbol);
341
341
  if (parameter.initializer) this.bindNode(parameter.initializer, scope);
342
+ /* A destructuring parameter binds its names through `declareBinding`
343
+ * above, but the *default values* inside the pattern are expressions:
344
+ * `({ noNewArrows = !(e => e) })` contains an arrow that has to get a
345
+ * FunctionInfo of its own, or codegen reports "does not yet support this
346
+ * closure". */
347
+ this.bindBindingPattern(parameter.name, scope);
342
348
  }
343
349
  (fn as { params: SymbolInfo[] }).params = params;
344
350
 
@@ -402,6 +408,12 @@ export const referenceMethods: ReferenceMethods = {
402
408
 
403
409
  const classScope = this.createScope(ScopeKind.Block, node, scope);
404
410
  this.scopes.set(node, classScope);
411
+ /* A class *expression* binds its own name inside the class body only: the
412
+ * enclosing scope never sees it (`const A = class Inner { m() { return Inner } }`).
413
+ * A declaration's name already lives in the enclosing scope. */
414
+ if (node.kind === SyntaxKind.ClassExpression && node.name && !classScope.symbols.has(node.name.text)) {
415
+ this.declare(node.name.text, SymbolKind.Class, node, classScope, false);
416
+ }
405
417
 
406
418
  let constructor: FunctionInfo | undefined;
407
419
  /* `static { ... }` functions, so the field pass below can interleave them
@@ -32,6 +32,7 @@ const KNOWN_GLOBALS: ReadonlySet<string> = new Set<string>([
32
32
  ...WEB_GLOBALS,
33
33
  // Handled directly in `emitIdentifier` rather than through a table.
34
34
  "console",
35
+ "global",
35
36
  "undefined",
36
37
  "NaN",
37
38
  "Infinity",
@@ -68,7 +68,10 @@ type TypeDeclaration =
68
68
 
69
69
  /** Resolve type syntax and symbol annotations into the small model above. */
70
70
  export class TypeResolver {
71
+ /** Module-scope declarations, visible from every file in the bundle. */
71
72
  private readonly declarations = new Map<string, TypeDeclaration | undefined>();
73
+ /** Declarations nested inside a function body, visible only in their file. */
74
+ private readonly localDeclarations = new Map<string, Map<string, TypeDeclaration | undefined>>();
72
75
  private readonly symbolTypes = new Map<number, Type>();
73
76
  private readonly resolvingSymbols = new Set<number>();
74
77
  private readonly resolvingAliases = new Set<string>();
@@ -116,11 +119,28 @@ export class TypeResolver {
116
119
  }
117
120
 
118
121
  private addDeclaration(name: string, declaration: TypeDeclaration): void {
119
- // A duplicate means bundling or shadowing made the name ambiguous; treating
120
- // it as `any` is the only safe answer without a full binder for types.
122
+ /* A class declared inside a function body is scoped to that function: a
123
+ * bundled dependency is full of them (`babel.cjs` has its own `class
124
+ * Token`), and treating those as program-wide would hijack a type the
125
+ * importing file meant (`import type { Token } from "marked"`). */
126
+ const symbol = this.binding.symbolOfDeclaration.get(declaration.node);
127
+ if (symbol && !symbol.fn.isModule) {
128
+ const file = this.sourceFile.origins?.get(declaration.node) ?? this.sourceFile.fileName;
129
+ const local = this.localDeclarations.get(file) ?? new Map<string, TypeDeclaration | undefined>();
130
+ this.localDeclarations.set(file, local);
131
+ local.set(name, local.has(name) ? undefined : declaration);
132
+ return;
133
+ }
134
+ // A duplicate means bundling made the name ambiguous; `any` is the only safe answer.
121
135
  this.declarations.set(name, this.declarations.has(name) ? undefined : declaration);
122
136
  }
123
137
 
138
+ /** The declaration a type reference in `node` names, if one is in scope. */
139
+ private declarationFor(name: string, node: Node): TypeDeclaration | undefined {
140
+ const file = this.sourceFile.origins?.get(node) ?? this.sourceFile.fileName;
141
+ return this.localDeclarations.get(file)?.get(name) ?? this.declarations.get(name);
142
+ }
143
+
124
144
  private collectDynamicClasses(): void {
125
145
  walk(this.sourceFile, (node) => {
126
146
  if (node.kind === SyntaxKind.BinaryExpression) {
@@ -334,7 +354,7 @@ export class TypeResolver {
334
354
  if (typeParams.has(name)) return typeParams.get(name)!;
335
355
  const builtin = this.builtinType(name, node.typeArguments.map((argument) => this.resolve(argument, typeParams)));
336
356
  if (builtin) return builtin;
337
- const declaration = this.declarations.get(name);
357
+ const declaration = this.declarationFor(name, node);
338
358
  if (!declaration) return { kind: "typeParam", name };
339
359
  if (declaration.kind === "interface") return this.interfaceType(declaration.node, node.typeArguments.map((argument) => this.resolve(argument, typeParams)));
340
360
  if (declaration.kind === "alias") return this.aliasType(declaration.node, node.typeArguments.map((argument) => this.resolve(argument, typeParams)));
@@ -16,6 +16,7 @@ import { i64, numberLiteral, XT_TRUE, XT_UNDEFINED } from "../../values.js";
16
16
  import { MATH_CONSTANTS, NAMESPACE_PROPERTIES, NUMBER_CONSTANTS, SYMBOL_WELL_KNOWN, TYPED_ARRAYS } from "../tables.js";
17
17
  import { isSuperKeyword } from "./super.js";
18
18
  import type { Generator } from "../generator.js";
19
+ import { globalObjectReceiver } from "./unwrap.js";
19
20
 
20
21
  export interface AccessCallMethods {
21
22
  emitDelete(this: Generator, node: DeleteExpression): string;
@@ -58,6 +59,28 @@ export const accessCallMethods: AccessCallMethods = {
58
59
  this.emit(` ${value} = call i64 @xt_get(i64 ${parentProto}, i64 ${key})`);
59
60
  return value;
60
61
  }
62
+ /* `globalThis.fetch` / `global.Deno`: the global object *is* the global
63
+ * scope, so a *known* global reads as the bare global name. Without this the
64
+ * receiver would be the global bag and every feature probe
65
+ * (`globalThis.Deno ? ... : ...`) would miss the real global. `global` is
66
+ * Node's alias for the same object. Parentheses and `as`/`!` wrappers are
67
+ * transparent here.
68
+ *
69
+ * A name the compiler does not know is an ordinary property read on the
70
+ * global bag instead: `global.gracefulQueue = q` stores there, so
71
+ * `global.gracefulQueue` must read it back (libraries publish state on
72
+ * `global` this way). */
73
+ {
74
+ const receiver = globalObjectReceiver(node.expression);
75
+ if (receiver && !this.binding.symbolOfIdentifier.get(receiver)) {
76
+ /* `globalThis.require` is how bundled CommonJS detects the loader. It is
77
+ * a probe, not a use: an absent global property reads as `undefined`.
78
+ * Only invoking it (`globalThis.require("x")`) is a CommonJS call, and
79
+ * the invocation path reports that. */
80
+ if (node.name.text === "require") return i64(XT_UNDEFINED);
81
+ if (this.globalTypeOf(node.name.text) !== undefined) return this.emitIdentifier(node.name);
82
+ }
83
+ }
61
84
  if (
62
85
  node.name.text in MATH_CONSTANTS &&
63
86
  node.expression.kind === SyntaxKind.Identifier &&
@@ -25,6 +25,7 @@ import {
25
25
  NAMESPACE_STATICS,
26
26
  } from "../tables.js";
27
27
  import { isSuperKeyword } from "./super.js";
28
+ import { globalObjectReceiver } from "./unwrap.js";
28
29
  import type { Generator } from "../generator.js";
29
30
 
30
31
  export interface InvocationCallMethods {
@@ -71,6 +72,14 @@ export const invocationCallMethods: InvocationCallMethods = {
71
72
  }
72
73
  if (callee.kind === SyntaxKind.PropertyAccessExpression) {
73
74
  const access = callee as PropertyAccessExpression;
75
+ /* Reading `globalThis.require` is a CommonJS loader probe and reads as
76
+ * `undefined`; calling it is a real CommonJS use, which xbintsc has no
77
+ * runtime for. */
78
+ const globalReceiver = globalObjectReceiver(access.expression);
79
+ if (globalReceiver && !this.binding.symbolOfIdentifier.get(globalReceiver) && access.name.text === "require") {
80
+ this.reportRequireUse(node);
81
+ return i64(XT_UNDEFINED);
82
+ }
74
83
  const special = this.tryEmitBuiltinCall(node, access);
75
84
  if (special) return special;
76
85
  if (isSuperKeyword(access.expression)) return this.emitSuperCall(node, access);
@@ -244,6 +253,9 @@ export const invocationCallMethods: InvocationCallMethods = {
244
253
  /** Lower a call to an imported module binding to its runtime symbol. */
245
254
  emitModuleExport(node: CallExpression, exported: ModuleExport): string {
246
255
  const args = this.emitArguments(node.arguments);
256
+ /* `createRequire(url)` hands back a `require` that answers from the table
257
+ * of modules linked into this binary, so it has to exist first. */
258
+ if (exported.symbol === "xt_module_create_require") this.ensureModuleRegistry();
247
259
  if (exported.symbol) {
248
260
  this.extraDeclarations.add(
249
261
  exported.returnVoid
@@ -55,12 +55,13 @@ export const superCallMethods: SuperCallMethods = {
55
55
  this.emit(` ${proto} = call i64 @xt_function_get_prototype(i64 ${owner})`);
56
56
  const parentProto = this.reg();
57
57
  this.emit(` ${parentProto} = call i64 @xt_object_get_prototype(i64 ${proto})`);
58
- const key = this.stringValue("__ctor");
59
- const ctor = this.reg();
60
- this.emit(` ${ctor} = call i64 @xt_get(i64 ${parentProto}, i64 ${key})`);
61
58
  const args = this.emitArguments(node.arguments);
62
59
  const result = this.reg();
63
- this.emit(` ${result} = call i64 @xt_call_with_this(i64 ${ctor}, i64 ${thisValue}, i32 ${args.argc}, i64* ${args.ptr})`);
60
+ /* The runtime resolves the parent's `__ctor` hook, and falls back to
61
+ * calling a native/extension parent constructor (which has none). */
62
+ this.emit(
63
+ ` ${result} = call i64 @xt_super_construct(i64 ${thisValue}, i64 ${owner}, i64 ${parentProto}, i32 ${args.argc}, i64* ${args.ptr})`,
64
+ );
64
65
  return result;
65
66
  },
66
67
 
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Small AST helpers shared by the call-access and call-invocation lowering.
3
+ */
4
+
5
+ import { SyntaxKind, type Expression, type Identifier } from "../../../ast/nodes.js";
6
+
7
+ /** Strip `(...)`, `x as T`, `x satisfies T` and `x!` to the underlying expression. */
8
+ export function unwrapExpression(expression: Expression): Expression {
9
+ let current = expression;
10
+ for (;;) {
11
+ if (
12
+ current.kind === SyntaxKind.ParenthesizedExpression ||
13
+ current.kind === SyntaxKind.AsExpression ||
14
+ current.kind === SyntaxKind.SatisfiesExpression ||
15
+ current.kind === SyntaxKind.NonNullExpression
16
+ ) {
17
+ current = (current as { expression: Expression }).expression;
18
+ continue;
19
+ }
20
+ return current;
21
+ }
22
+ }
23
+
24
+ /**
25
+ * The identifier naming the global object (`globalThis`, or Node's alias
26
+ * `global`) when `expression` is one, ignoring `(...)`/`as`/`!` wrappers.
27
+ * Callers still have to check that the name is not shadowed by a binding.
28
+ */
29
+ export function globalObjectReceiver(expression: Expression): Identifier | undefined {
30
+ const receiver = unwrapExpression(expression);
31
+ if (receiver.kind !== SyntaxKind.Identifier) return undefined;
32
+ const name = (receiver as Identifier).text;
33
+ return name === "globalThis" || name === "global" ? (receiver as Identifier) : undefined;
34
+ }
35
+
36
+ /**
37
+ * Source text of a `new` callee (`Foo`, `ns.Bar`, `expr.Baz`), for the runtime's
38
+ * "new X is not a constructor" message. `undefined` when the callee is an
39
+ * arbitrary expression.
40
+ */
41
+ export function newCalleeName(callee: Expression): string | undefined {
42
+ const name = unwrapExpression(callee);
43
+ if (name.kind === SyntaxKind.Identifier) return (name as Identifier).text;
44
+ if (name.kind !== SyntaxKind.PropertyAccessExpression) return undefined;
45
+ const access = name as { name: { text: string } };
46
+ return access.name.text;
47
+ }