xbintsc 0.6.24 → 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 (472) hide show
  1. package/AGENTS.md +1 -1
  2. package/dist/src/binder/declarations.d.ts +1 -0
  3. package/dist/src/binder/declarations.js +37 -0
  4. package/dist/src/binder/declarations.js.map +1 -1
  5. package/dist/src/binder/references.js +12 -0
  6. package/dist/src/binder/references.js.map +1 -1
  7. package/dist/src/checker/globals.js +1 -0
  8. package/dist/src/checker/globals.js.map +1 -1
  9. package/dist/src/checker/type-system.d.ts +5 -0
  10. package/dist/src/checker/type-system.js +22 -3
  11. package/dist/src/checker/type-system.js.map +1 -1
  12. package/dist/src/cli/extensions.js +11 -3
  13. package/dist/src/cli/extensions.js.map +1 -1
  14. package/dist/src/cli/help.d.ts +1 -1
  15. package/dist/src/cli/help.js +1 -1
  16. package/dist/src/codegen/generator/calls/access.js +25 -0
  17. package/dist/src/codegen/generator/calls/access.js.map +1 -1
  18. package/dist/src/codegen/generator/calls/invocation.js +13 -0
  19. package/dist/src/codegen/generator/calls/invocation.js.map +1 -1
  20. package/dist/src/codegen/generator/calls/super.js +3 -4
  21. package/dist/src/codegen/generator/calls/super.js.map +1 -1
  22. package/dist/src/codegen/generator/calls/unwrap.d.ts +18 -0
  23. package/dist/src/codegen/generator/calls/unwrap.js +45 -0
  24. package/dist/src/codegen/generator/calls/unwrap.js.map +1 -0
  25. package/dist/src/codegen/generator/expressions/assignment.js +14 -0
  26. package/dist/src/codegen/generator/expressions/assignment.js.map +1 -1
  27. package/dist/src/codegen/generator/expressions/module-values.d.ts +6 -0
  28. package/dist/src/codegen/generator/expressions/module-values.js +51 -17
  29. package/dist/src/codegen/generator/expressions/module-values.js.map +1 -1
  30. package/dist/src/codegen/generator/expressions/primary.js +9 -5
  31. package/dist/src/codegen/generator/expressions/primary.js.map +1 -1
  32. package/dist/src/codegen/generator/generator.js +4 -0
  33. package/dist/src/codegen/generator/generator.js.map +1 -1
  34. package/dist/src/codegen/generator/lookup-tables.d.ts +22 -0
  35. package/dist/src/codegen/generator/lookup-tables.js +48 -0
  36. package/dist/src/codegen/generator/lookup-tables.js.map +1 -0
  37. package/dist/src/codegen/generator/module.js +38 -7
  38. package/dist/src/codegen/generator/module.js.map +1 -1
  39. package/dist/src/codegen/generator/state.js +24 -0
  40. package/dist/src/codegen/generator/state.js.map +1 -1
  41. package/dist/src/codegen/generator/tables.d.ts +2 -6
  42. package/dist/src/codegen/generator/tables.js +37 -13
  43. package/dist/src/codegen/generator/tables.js.map +1 -1
  44. package/dist/src/driver/bundler/commonjs.d.ts +9 -2
  45. package/dist/src/driver/bundler/commonjs.js +11 -4
  46. package/dist/src/driver/bundler/commonjs.js.map +1 -1
  47. package/dist/src/driver/bundler/concatenate.d.ts +20 -0
  48. package/dist/src/driver/bundler/concatenate.js +68 -4
  49. package/dist/src/driver/bundler/concatenate.js.map +1 -1
  50. package/dist/src/driver/bundler/deferred.d.ts +29 -1
  51. package/dist/src/driver/bundler/deferred.js +114 -5
  52. package/dist/src/driver/bundler/deferred.js.map +1 -1
  53. package/dist/src/driver/bundler/dynamic-import.d.ts +6 -0
  54. package/dist/src/driver/bundler/dynamic-import.js +59 -3
  55. package/dist/src/driver/bundler/dynamic-import.js.map +1 -1
  56. package/dist/src/driver/bundler/graph.d.ts +13 -0
  57. package/dist/src/driver/bundler/graph.js +11 -7
  58. package/dist/src/driver/bundler/graph.js.map +1 -1
  59. package/dist/src/driver/bundler/merge.js +32 -8
  60. package/dist/src/driver/bundler/merge.js.map +1 -1
  61. package/dist/src/driver/bundler/namespace-object.d.ts +11 -0
  62. package/dist/src/driver/bundler/namespace-object.js +32 -0
  63. package/dist/src/driver/bundler/namespace-object.js.map +1 -1
  64. package/dist/src/driver/bundler/reexport-resolution.js +8 -2
  65. package/dist/src/driver/bundler/reexport-resolution.js.map +1 -1
  66. package/dist/src/driver/bundler/symbols.d.ts +4 -0
  67. package/dist/src/driver/bundler/symbols.js +14 -1
  68. package/dist/src/driver/bundler/symbols.js.map +1 -1
  69. package/dist/src/driver/config.d.ts +1 -1
  70. package/dist/src/driver/fingerprint.d.ts +2 -2
  71. package/dist/src/driver/fingerprint.js +3 -3
  72. package/dist/src/driver/fingerprint.js.map +1 -1
  73. package/dist/src/driver/link-features.js +1 -1
  74. package/dist/src/driver/link-features.js.map +1 -1
  75. package/dist/src/driver/options.d.ts +1 -4
  76. package/dist/src/driver/paths.js +8 -4
  77. package/dist/src/driver/paths.js.map +1 -1
  78. package/dist/src/driver/runtime-lib.d.ts +27 -0
  79. package/dist/src/driver/runtime-lib.js +78 -2
  80. package/dist/src/driver/runtime-lib.js.map +1 -1
  81. package/dist/src/driver/runtime-objects.d.ts +1 -1
  82. package/dist/src/driver/runtime-objects.js +37 -6
  83. package/dist/src/driver/runtime-objects.js.map +1 -1
  84. package/dist/src/driver/version.d.ts +1 -1
  85. package/dist/src/driver/version.js +1 -1
  86. package/dist/src/driver/version.js.map +1 -1
  87. package/dist/src/extensions/catalog.js +5 -1
  88. package/dist/src/extensions/catalog.js.map +1 -1
  89. package/dist/src/extensions/gui/index.js +0 -1
  90. package/dist/src/extensions/gui/index.js.map +1 -1
  91. package/dist/src/extensions/link-feature.d.ts +8 -0
  92. package/dist/src/extensions/node/buffer/index.d.ts +5 -4
  93. package/dist/src/extensions/node/buffer/index.js +5 -4
  94. package/dist/src/extensions/node/buffer/index.js.map +1 -1
  95. package/dist/src/extensions/node/events/index.js +8 -1
  96. package/dist/src/extensions/node/events/index.js.map +1 -1
  97. package/dist/src/extensions/node/fs/callbacks.d.ts +9 -0
  98. package/dist/src/extensions/node/fs/callbacks.js +42 -0
  99. package/dist/src/extensions/node/fs/callbacks.js.map +1 -0
  100. package/dist/src/extensions/node/fs/index.d.ts +2 -3
  101. package/dist/src/extensions/node/fs/index.js +11 -4
  102. package/dist/src/extensions/node/fs/index.js.map +1 -1
  103. package/dist/src/extensions/node/process/index.d.ts +10 -3
  104. package/dist/src/extensions/node/process/index.js +30 -5
  105. package/dist/src/extensions/node/process/index.js.map +1 -1
  106. package/dist/src/extensions/node/stream-web/index.d.ts +6 -1
  107. package/dist/src/extensions/node/stream-web/index.js +59 -4
  108. package/dist/src/extensions/node/stream-web/index.js.map +1 -1
  109. package/dist/src/extensions/node/worker_threads/index.d.ts +14 -4
  110. package/dist/src/extensions/node/worker_threads/index.js +24 -11
  111. package/dist/src/extensions/node/worker_threads/index.js.map +1 -1
  112. package/dist/src/extensions/registry.d.ts +16 -1
  113. package/dist/src/extensions/registry.js +17 -5
  114. package/dist/src/extensions/registry.js.map +1 -1
  115. package/dist/src/extensions/wasm-asset.d.ts +16 -0
  116. package/dist/src/extensions/wasm-asset.js +28 -0
  117. package/dist/src/extensions/wasm-asset.js.map +1 -0
  118. package/dist/src/extensions/wasm.d.ts +17 -0
  119. package/dist/src/extensions/wasm.js +68 -0
  120. package/dist/src/extensions/wasm.js.map +1 -0
  121. package/dist/src/extensions/webworker/index.d.ts +24 -0
  122. package/dist/src/extensions/webworker/index.js +54 -0
  123. package/dist/src/extensions/webworker/index.js.map +1 -0
  124. package/dist/src/index.d.ts +1 -0
  125. package/dist/src/index.js +1 -0
  126. package/dist/src/index.js.map +1 -1
  127. package/dist/src/lexer/scanner.d.ts +7 -1
  128. package/dist/src/lexer/scanner.js +15 -0
  129. package/dist/src/lexer/scanner.js.map +1 -1
  130. package/dist/src/parser/context.d.ts +10 -0
  131. package/dist/src/parser/context.js +42 -3
  132. package/dist/src/parser/context.js.map +1 -1
  133. package/dist/src/parser/literals.js +6 -2
  134. package/dist/src/parser/literals.js.map +1 -1
  135. package/dist/tests/binder/binder.test.js +43 -0
  136. package/dist/tests/binder/binder.test.js.map +1 -1
  137. package/dist/tests/codegen/llvm.test.js +15 -0
  138. package/dist/tests/codegen/llvm.test.js.map +1 -1
  139. package/dist/tests/driver/bundler-resolve.test.js +12 -0
  140. package/dist/tests/driver/bundler-resolve.test.js.map +1 -1
  141. package/dist/tests/driver/commonjs-prelude.test.d.ts +11 -0
  142. package/dist/tests/driver/commonjs-prelude.test.js +69 -0
  143. package/dist/tests/driver/commonjs-prelude.test.js.map +1 -0
  144. package/dist/tests/driver/compiler.test.js +42 -1
  145. package/dist/tests/driver/compiler.test.js.map +1 -1
  146. package/dist/tests/driver/html-deferred-cycles.test.d.ts +10 -0
  147. package/dist/tests/driver/html-deferred-cycles.test.js +75 -0
  148. package/dist/tests/driver/html-deferred-cycles.test.js.map +1 -0
  149. package/dist/tests/driver/link-features.test.js +9 -0
  150. package/dist/tests/driver/link-features.test.js.map +1 -1
  151. package/dist/tests/driver/namespace-type-exports.test.d.ts +9 -0
  152. package/dist/tests/driver/namespace-type-exports.test.js +59 -0
  153. package/dist/tests/driver/namespace-type-exports.test.js.map +1 -0
  154. package/dist/tests/driver/runtime-lib.test.js +49 -1
  155. package/dist/tests/driver/runtime-lib.test.js.map +1 -1
  156. package/dist/tests/e2e/async-fs.test.js +44 -0
  157. package/dist/tests/e2e/async-fs.test.js.map +1 -1
  158. package/dist/tests/e2e/compile-basics.test.js +53 -0
  159. package/dist/tests/e2e/compile-basics.test.js.map +1 -1
  160. package/dist/tests/e2e/compile-deferred-marker.test.d.ts +14 -0
  161. package/dist/tests/e2e/compile-deferred-marker.test.js +68 -0
  162. package/dist/tests/e2e/compile-deferred-marker.test.js.map +1 -0
  163. package/dist/tests/e2e/compile-deferred-reexport.test.d.ts +12 -0
  164. package/dist/tests/e2e/compile-deferred-reexport.test.js +89 -0
  165. package/dist/tests/e2e/compile-deferred-reexport.test.js.map +1 -0
  166. package/dist/tests/e2e/compile-error-module.test.d.ts +9 -0
  167. package/dist/tests/e2e/compile-error-module.test.js +29 -0
  168. package/dist/tests/e2e/compile-error-module.test.js.map +1 -0
  169. package/dist/tests/e2e/compile-gc-promises.test.d.ts +11 -0
  170. package/dist/tests/e2e/compile-gc-promises.test.js +52 -0
  171. package/dist/tests/e2e/compile-gc-promises.test.js.map +1 -0
  172. package/dist/tests/e2e/compile-modules.test.js +40 -0
  173. package/dist/tests/e2e/compile-modules.test.js.map +1 -1
  174. package/dist/tests/e2e/compile-node-fs-callbacks.test.d.ts +6 -0
  175. package/dist/tests/e2e/compile-node-fs-callbacks.test.js +46 -0
  176. package/dist/tests/e2e/compile-node-fs-callbacks.test.js.map +1 -0
  177. package/dist/tests/e2e/compile-node-inheritance.test.d.ts +6 -0
  178. package/dist/tests/e2e/compile-node-inheritance.test.js +48 -0
  179. package/dist/tests/e2e/compile-node-inheritance.test.js.map +1 -0
  180. package/dist/tests/e2e/compile-node-require.test.d.ts +9 -0
  181. package/dist/tests/e2e/compile-node-require.test.js +29 -0
  182. package/dist/tests/e2e/compile-node-require.test.js.map +1 -0
  183. package/dist/tests/e2e/compile-node-stats.test.d.ts +8 -0
  184. package/dist/tests/e2e/compile-node-stats.test.js +26 -0
  185. package/dist/tests/e2e/compile-node-stats.test.js.map +1 -0
  186. package/dist/tests/e2e/compile-proxy.test.d.ts +6 -0
  187. package/dist/tests/e2e/compile-proxy.test.js +66 -0
  188. package/dist/tests/e2e/compile-proxy.test.js.map +1 -0
  189. package/dist/tests/e2e/compile-web-globals.test.d.ts +6 -0
  190. package/dist/tests/e2e/compile-web-globals.test.js +94 -0
  191. package/dist/tests/e2e/compile-web-globals.test.js.map +1 -0
  192. package/dist/tests/e2e/fetch.test.js +1 -1
  193. package/dist/tests/e2e/gui-script-import-order.test.js +24 -0
  194. package/dist/tests/e2e/gui-script-import-order.test.js.map +1 -1
  195. package/dist/tests/e2e/harness.d.ts +6 -0
  196. package/dist/tests/e2e/harness.js +13 -1
  197. package/dist/tests/e2e/harness.js.map +1 -1
  198. package/dist/tests/e2e/json-stringify-to-json.test.d.ts +10 -0
  199. package/dist/tests/e2e/json-stringify-to-json.test.js +61 -0
  200. package/dist/tests/e2e/json-stringify-to-json.test.js.map +1 -0
  201. package/dist/tests/e2e/node-crypto-random.test.d.ts +8 -0
  202. package/dist/tests/e2e/node-crypto-random.test.js +148 -0
  203. package/dist/tests/e2e/node-crypto-random.test.js.map +1 -0
  204. package/dist/tests/e2e/node-modules.test.js +1 -1
  205. package/dist/tests/e2e/node-modules.test.js.map +1 -1
  206. package/dist/tests/e2e/node-process.test.d.ts +7 -0
  207. package/dist/tests/e2e/node-process.test.js +50 -0
  208. package/dist/tests/e2e/node-process.test.js.map +1 -0
  209. package/dist/tests/e2e/runtime-coverage-buffer.test.js +31 -0
  210. package/dist/tests/e2e/runtime-coverage-buffer.test.js.map +1 -1
  211. package/dist/tests/e2e/web-streams.test.d.ts +8 -0
  212. package/dist/tests/e2e/web-streams.test.js +289 -0
  213. package/dist/tests/e2e/web-streams.test.js.map +1 -0
  214. package/dist/tests/e2e/web-worker.test.d.ts +7 -0
  215. package/dist/tests/e2e/web-worker.test.js +74 -0
  216. package/dist/tests/e2e/web-worker.test.js.map +1 -0
  217. package/dist/tests/e2e/webassembly.test.d.ts +8 -0
  218. package/dist/tests/e2e/webassembly.test.js +113 -0
  219. package/dist/tests/e2e/webassembly.test.js.map +1 -0
  220. package/dist/tests/e2e/worker-api.test.d.ts +14 -0
  221. package/dist/tests/e2e/worker-api.test.js +460 -0
  222. package/dist/tests/e2e/worker-api.test.js.map +1 -0
  223. package/dist/tests/e2e/worker-threads.test.d.ts +8 -0
  224. package/dist/tests/e2e/worker-threads.test.js +160 -0
  225. package/dist/tests/e2e/worker-threads.test.js.map +1 -0
  226. package/dist/tests/extensions/wasm.test.d.ts +1 -0
  227. package/dist/tests/extensions/wasm.test.js +53 -0
  228. package/dist/tests/extensions/wasm.test.js.map +1 -0
  229. package/dist/tests/parser/literals.test.js +19 -0
  230. package/dist/tests/parser/literals.test.js.map +1 -1
  231. package/dist/tests/parser/statements.test.js +13 -0
  232. package/dist/tests/parser/statements.test.js.map +1 -1
  233. package/doc/ai/cli.md +2 -1
  234. package/doc/ai/contributing.md +11 -4
  235. package/doc/ai/extensions.md +25 -2
  236. package/doc/ai/language-support.md +14 -2
  237. package/doc/ai/zh-CN/cli.md +2 -1
  238. package/doc/ai/zh-CN/contributing.md +8 -3
  239. package/doc/ai/zh-CN/extensions.md +23 -2
  240. package/doc/ai/zh-CN/language-support.md +13 -2
  241. package/doc/gui/api.md +1 -1
  242. package/doc/implemented/binder.md +2 -1
  243. package/doc/implemented/checker.md +5 -0
  244. package/doc/implemented/cli.md +1 -1
  245. package/doc/implemented/extensions.md +6 -5
  246. package/doc/implemented/lexer.md +1 -1
  247. package/doc/implemented/modules.md +23 -1
  248. package/doc/implemented/node/buffer.md +18 -5
  249. package/doc/implemented/node/fs.md +22 -1
  250. package/doc/implemented/node/process.md +65 -5
  251. package/doc/implemented/node/stream-web.md +31 -0
  252. package/doc/implemented/node/worker-threads.md +88 -5
  253. package/doc/implemented/node.md +9 -2
  254. package/doc/implemented/operators.md +3 -1
  255. package/doc/implemented/parser.md +1 -1
  256. package/doc/implemented/runtime-core.md +6 -2
  257. package/doc/implemented/stdlib.md +69 -2
  258. package/doc/implemented/streams.md +82 -0
  259. package/doc/implemented/wasm.md +80 -18
  260. package/doc/implemented/web-worker.md +84 -0
  261. package/doc/implemented.md +6 -2
  262. package/doc/unimplemented/classes.md +1 -0
  263. package/doc/unimplemented/deviations.md +1 -0
  264. package/doc/unimplemented/modules.md +1 -0
  265. package/doc/unimplemented/node/worker-threads.md +23 -0
  266. package/doc/unimplemented/node.md +1 -0
  267. package/doc/unimplemented/stdlib.md +3 -1
  268. package/doc/unimplemented/wasm.md +21 -9
  269. package/doc/unimplemented.md +1 -1
  270. package/doc/xbintsc.config.schema.json +5 -0
  271. package/doc/zh-CN/gui/api.md +1 -1
  272. package/doc/zh-CN/implemented/binder.md +2 -1
  273. package/doc/zh-CN/implemented/checker.md +3 -0
  274. package/doc/zh-CN/implemented/cli.md +1 -1
  275. package/doc/zh-CN/implemented/extensions.md +7 -6
  276. package/doc/zh-CN/implemented/lexer.md +1 -1
  277. package/doc/zh-CN/implemented/modules.md +17 -1
  278. package/doc/zh-CN/implemented/node/buffer.md +3 -1
  279. package/doc/zh-CN/implemented/node/fs.md +19 -1
  280. package/doc/zh-CN/implemented/node/process.md +59 -5
  281. package/doc/zh-CN/implemented/node/stream-web.md +24 -0
  282. package/doc/zh-CN/implemented/node/worker-threads.md +77 -4
  283. package/doc/zh-CN/implemented/node.md +3 -2
  284. package/doc/zh-CN/implemented/operators.md +2 -1
  285. package/doc/zh-CN/implemented/parser.md +1 -1
  286. package/doc/zh-CN/implemented/runtime-core.md +3 -2
  287. package/doc/zh-CN/implemented/stdlib.md +58 -2
  288. package/doc/zh-CN/implemented/streams.md +60 -0
  289. package/doc/zh-CN/implemented/wasm.md +73 -16
  290. package/doc/zh-CN/implemented/web-worker.md +79 -0
  291. package/doc/zh-CN/implemented.md +6 -2
  292. package/doc/zh-CN/unimplemented/classes.md +1 -0
  293. package/doc/zh-CN/unimplemented/deviations.md +1 -0
  294. package/doc/zh-CN/unimplemented/modules.md +1 -0
  295. package/doc/zh-CN/unimplemented/node/worker-threads.md +20 -0
  296. package/doc/zh-CN/unimplemented/node.md +1 -0
  297. package/doc/zh-CN/unimplemented/stdlib.md +3 -1
  298. package/doc/zh-CN/unimplemented/wasm.md +16 -8
  299. package/doc/zh-CN/unimplemented.md +1 -1
  300. package/llms.txt +2 -0
  301. package/package.json +8 -7
  302. package/runtime/ext_gui/browser.cpp +1 -1
  303. package/runtime/ext_node/buffer/buffer.c +5 -4
  304. package/runtime/ext_node/buffer/parts/byte-access.inc +115 -11
  305. package/runtime/ext_node/buffer/parts/prototype.inc +4 -1
  306. package/runtime/ext_node/events/events.c +9 -0
  307. package/runtime/ext_node/fs/callbacks.c +138 -0
  308. package/runtime/ext_node/fs/fs_ops.c +28 -15
  309. package/runtime/ext_node/module/module.c +18 -9
  310. package/runtime/ext_node/process/process.c +117 -32
  311. package/runtime/ext_node/process/streams.c +370 -0
  312. package/runtime/ext_node/process/streams.h +20 -0
  313. package/runtime/ext_node/stream_web/stream_web.c +20 -6
  314. package/runtime/rt.h +36 -0
  315. package/runtime/rt_internal.h +10 -10
  316. package/runtime/worker/worker_api.c +422 -0
  317. package/runtime/worker/worker_codec.c +489 -0
  318. package/runtime/worker/worker_core.c +445 -0
  319. package/runtime/worker/worker_internal.h +223 -0
  320. package/runtime/worker/worker_platform.c +344 -0
  321. package/runtime/worker/worker_ports.c +203 -0
  322. package/runtime/worker/worker_transport.c +464 -0
  323. package/runtime/worker/worker_wire.c +150 -0
  324. package/runtime/xt_alloc.c +67 -49
  325. package/runtime/xt_async_io.c +3 -0
  326. package/runtime/xt_builtins.c +41 -23
  327. package/runtime/xt_constructors.c +148 -2
  328. package/runtime/xt_containers/access.inc +29 -1
  329. package/runtime/xt_containers/function_prototypes.inc +58 -0
  330. package/runtime/xt_containers/objects.inc +18 -2
  331. package/runtime/xt_containers.c +1 -0
  332. package/runtime/xt_exit.c +34 -0
  333. package/runtime/xt_fetch/headers.inc +1 -0
  334. package/runtime/xt_fetch/response.inc +28 -8
  335. package/runtime/xt_fetch/webapi.inc +82 -0
  336. package/runtime/xt_fetch.c +5 -0
  337. package/runtime/xt_gc_verify.c +135 -0
  338. package/runtime/xt_intl.c +224 -0
  339. package/runtime/xt_io/exception.inc +40 -1
  340. package/runtime/xt_module_values.c +82 -0
  341. package/runtime/xt_proxy.c +140 -0
  342. package/runtime/xt_stdlib/dispatch.inc +98 -9
  343. package/runtime/xt_stdlib2/date.inc +7 -1
  344. package/runtime/xt_stdlib2/error.inc +4 -0
  345. package/runtime/xt_stdlib2/json.inc +28 -0
  346. package/runtime/xt_stdlib2/json_stringify.inc +70 -53
  347. package/runtime/xt_streams/common.inc +232 -0
  348. package/runtime/xt_streams/constructors.inc +232 -0
  349. package/runtime/xt_streams/encoding.inc +124 -0
  350. package/runtime/xt_streams/readable-extra.inc +372 -0
  351. package/runtime/xt_streams/readable.inc +415 -0
  352. package/runtime/xt_streams/transform.inc +195 -0
  353. package/runtime/xt_streams/writable.inc +542 -0
  354. package/runtime/xt_streams.c +30 -0
  355. package/runtime/xt_wasm/wasm.h +148 -0
  356. package/runtime/xt_wasm/wasm_api.c +357 -0
  357. package/runtime/xt_wasm/wasm_api.h +37 -0
  358. package/runtime/xt_wasm/wasm_bridge.c +30 -0
  359. package/runtime/xt_wasm/wasm_instance.c +591 -0
  360. package/runtime/xt_wasm/wasm_module.c +270 -0
  361. package/runtime/xt_wasm/wasm_values.c +177 -0
  362. package/runtime/xt_web.h +27 -0
  363. package/runtime/xt_web_abort.c +373 -0
  364. package/runtime/xt_webassembly.c +15 -4
  365. package/scripts/build-runtime.ts +8 -0
  366. package/scripts/fetch-vendored.ts +187 -20
  367. package/scripts/package-release.ts +3 -2
  368. package/src/binder/declarations.ts +34 -0
  369. package/src/binder/references.ts +12 -0
  370. package/src/checker/globals.ts +1 -0
  371. package/src/checker/type-system.ts +23 -3
  372. package/src/cli/extensions.ts +11 -3
  373. package/src/cli/help.ts +1 -1
  374. package/src/codegen/generator/calls/access.ts +23 -0
  375. package/src/codegen/generator/calls/invocation.ts +12 -0
  376. package/src/codegen/generator/calls/super.ts +5 -4
  377. package/src/codegen/generator/calls/unwrap.ts +47 -0
  378. package/src/codegen/generator/expressions/assignment.ts +15 -0
  379. package/src/codegen/generator/expressions/module-values.ts +57 -19
  380. package/src/codegen/generator/expressions/primary.ts +9 -9
  381. package/src/codegen/generator/generator.ts +4 -0
  382. package/src/codegen/generator/lookup-tables.ts +63 -0
  383. package/src/codegen/generator/module.ts +38 -7
  384. package/src/codegen/generator/state.ts +24 -0
  385. package/src/codegen/generator/tables.ts +37 -13
  386. package/src/driver/bundler/commonjs.ts +12 -5
  387. package/src/driver/bundler/concatenate.ts +84 -7
  388. package/src/driver/bundler/deferred.ts +122 -6
  389. package/src/driver/bundler/dynamic-import.ts +75 -2
  390. package/src/driver/bundler/graph.ts +11 -7
  391. package/src/driver/bundler/merge.ts +31 -5
  392. package/src/driver/bundler/namespace-object.ts +44 -0
  393. package/src/driver/bundler/reexport-resolution.ts +12 -2
  394. package/src/driver/bundler/symbols.ts +17 -0
  395. package/src/driver/config.ts +1 -1
  396. package/src/driver/fingerprint.ts +3 -3
  397. package/src/driver/link-features.ts +1 -1
  398. package/src/driver/options.ts +1 -4
  399. package/src/driver/paths.ts +8 -4
  400. package/src/driver/runtime-lib.ts +87 -2
  401. package/src/driver/runtime-objects.ts +37 -6
  402. package/src/driver/version.ts +1 -1
  403. package/src/extensions/catalog.ts +5 -1
  404. package/src/extensions/gui/index.ts +0 -1
  405. package/src/extensions/link-feature.ts +8 -0
  406. package/src/extensions/node/buffer/index.ts +5 -4
  407. package/src/extensions/node/events/index.ts +8 -1
  408. package/src/extensions/node/fs/callbacks.ts +45 -0
  409. package/src/extensions/node/fs/index.ts +11 -4
  410. package/src/extensions/node/process/index.ts +30 -5
  411. package/src/extensions/node/stream-web/index.ts +59 -4
  412. package/src/extensions/node/worker_threads/index.ts +24 -11
  413. package/src/extensions/registry.ts +34 -6
  414. package/src/extensions/wasm-asset.ts +32 -0
  415. package/src/extensions/wasm.ts +74 -0
  416. package/src/extensions/webworker/index.ts +58 -0
  417. package/src/index.ts +1 -0
  418. package/src/lexer/scanner.ts +16 -1
  419. package/src/parser/context.ts +39 -3
  420. package/src/parser/literals.ts +6 -2
  421. package/vendor/wasm3/.pin +1 -0
  422. package/vendor/wasm3/LICENSE +21 -0
  423. package/vendor/wasm3/m3_api_libc.c +265 -0
  424. package/vendor/wasm3/m3_api_libc.h +19 -0
  425. package/vendor/wasm3/m3_api_meta_wasi.c +1107 -0
  426. package/vendor/wasm3/m3_api_tracer.c +173 -0
  427. package/vendor/wasm3/m3_api_tracer.h +19 -0
  428. package/vendor/wasm3/m3_api_uvwasi.c +1290 -0
  429. package/vendor/wasm3/m3_api_wasi.c +1207 -0
  430. package/vendor/wasm3/m3_api_wasi.h +74 -0
  431. package/vendor/wasm3/m3_api_wasi_host.h +137 -0
  432. package/vendor/wasm3/m3_api_wasi_posix.h +979 -0
  433. package/vendor/wasm3/m3_api_wasi_win32.h +1093 -0
  434. package/vendor/wasm3/m3_atomic.h +114 -0
  435. package/vendor/wasm3/m3_bind.c +174 -0
  436. package/vendor/wasm3/m3_bind.h +20 -0
  437. package/vendor/wasm3/m3_code.c +232 -0
  438. package/vendor/wasm3/m3_code.h +74 -0
  439. package/vendor/wasm3/m3_compile.c +6603 -0
  440. package/vendor/wasm3/m3_compile.h +354 -0
  441. package/vendor/wasm3/m3_config.h +448 -0
  442. package/vendor/wasm3/m3_config_platforms.h +345 -0
  443. package/vendor/wasm3/m3_core.c +1034 -0
  444. package/vendor/wasm3/m3_core.h +526 -0
  445. package/vendor/wasm3/m3_deterministic.c +65 -0
  446. package/vendor/wasm3/m3_deterministic.h +59 -0
  447. package/vendor/wasm3/m3_env.c +3146 -0
  448. package/vendor/wasm3/m3_env.h +748 -0
  449. package/vendor/wasm3/m3_exception.h +33 -0
  450. package/vendor/wasm3/m3_exec.c +8 -0
  451. package/vendor/wasm3/m3_exec.h +4110 -0
  452. package/vendor/wasm3/m3_exec_atomic.h +146 -0
  453. package/vendor/wasm3/m3_exec_defs.h +76 -0
  454. package/vendor/wasm3/m3_function.c +278 -0
  455. package/vendor/wasm3/m3_function.h +244 -0
  456. package/vendor/wasm3/m3_host.h +226 -0
  457. package/vendor/wasm3/m3_host_none.h +76 -0
  458. package/vendor/wasm3/m3_host_posix.h +543 -0
  459. package/vendor/wasm3/m3_host_win32.h +253 -0
  460. package/vendor/wasm3/m3_info.c +616 -0
  461. package/vendor/wasm3/m3_info.h +38 -0
  462. package/vendor/wasm3/m3_math_utils.h +560 -0
  463. package/vendor/wasm3/m3_module.c +431 -0
  464. package/vendor/wasm3/m3_parse.c +1549 -0
  465. package/vendor/wasm3/m3_snapshot.c +3825 -0
  466. package/vendor/wasm3/m3_validate.c +1770 -0
  467. package/vendor/wasm3/m3_validate.h +25 -0
  468. package/vendor/wasm3/m3_wasi.h +10 -0
  469. package/vendor/wasm3/m3_xxh64.c +211 -0
  470. package/vendor/wasm3/wasm3.h +684 -0
  471. package/vendor/wasm3/wasm3_defs.h +350 -0
  472. package/runtime/ext_node/worker_threads/worker_threads.c +0 -214
@@ -24,9 +24,18 @@ import {
24
24
  type VariableDeclarationList,
25
25
  } from "../../ast/nodes.js";
26
26
  import { SymbolKind } from "../../binder/binder.js";
27
- import { deferredModuleFunctions, eagerModuleFunctions, type DeferredAlias } from "./deferred.js";
27
+ import {
28
+ assignmentStatement,
29
+ deferredAliasDeclarations,
30
+ deferredModuleFunctions,
31
+ deferredModuleSplit,
32
+ eagerModuleFunctions,
33
+ letDeclaration,
34
+ simpleVariableDeclarations,
35
+ type DeferredAlias,
36
+ } from "./deferred.js";
28
37
  import { classifyDependency } from "./resolve.js";
29
- import { renameSymbol, topLevelSymbols } from "./symbols.js";
38
+ import { moduleScope, renameSymbol, topLevelSymbols } from "./symbols.js";
30
39
  import type { ModuleRecord } from "./types.js";
31
40
 
32
41
  export interface ConcatenateOptions {
@@ -38,6 +47,13 @@ export interface ConcatenateOptions {
38
47
  readonly namespaceStatements: ReadonlyMap<Statement, Statement>;
39
48
  readonly externalSpecifiers: ReadonlySet<string>;
40
49
  readonly moduleOverrides?: Readonly<Record<string, string>>;
50
+ /**
51
+ * `module`/`exports` state for every CommonJS module, emitted above the first
52
+ * module body. Node creates the pair when a module starts loading, so a
53
+ * `require` cycle reads the earlier module's partly filled object instead of
54
+ * one that is declared later in the concatenated program.
55
+ */
56
+ readonly cjsPrelude?: readonly { readonly record: ModuleRecord; readonly statement: Statement }[];
41
57
  }
42
58
 
43
59
  export function concatenateModules(options: ConcatenateOptions): SourceFileNode {
@@ -50,6 +66,7 @@ export function concatenateModules(options: ConcatenateOptions): SourceFileNode
50
66
  namespaceStatements,
51
67
  externalSpecifiers,
52
68
  moduleOverrides,
69
+ cjsPrelude,
53
70
  } = options;
54
71
  const merged: Statement[] = [];
55
72
  /* Every node keeps the module it was parsed from, so a later codegen
@@ -91,6 +108,11 @@ export function concatenateModules(options: ConcatenateOptions): SourceFileNode
91
108
  trackOrigin(record, statement);
92
109
  merged.push(statement);
93
110
  };
111
+ /* A module's `module`/`exports` pair exists as soon as the module starts
112
+ * loading, so hoist every pair above the first module body. A `require`
113
+ * cycle then reads the earlier module's object (partly filled, like Node)
114
+ * instead of a variable declared later in the concatenated program. */
115
+ for (const entry of cjsPrelude ?? []) push(entry.record, entry.statement);
94
116
  const first = records[records.length - 1]!;
95
117
  for (const record of records) {
96
118
  if (deferredModules.has(record.path)) {
@@ -98,7 +120,15 @@ export function concatenateModules(options: ConcatenateOptions): SourceFileNode
98
120
  * external imports stay top-level (codegen resolves them there); eager
99
121
  * namespace objects likewise. */
100
122
  const topLevel: Statement[] = [];
123
+ const hoisted: Statement[] = [];
124
+ const hoistedVariables: { name: string; start: number; end: number }[] = [];
101
125
  const body: Statement[] = [];
126
+ /* A module cycle calls a dependency's *function declarations* before its
127
+ * body has run — that is what hoisting means in JavaScript. Keeping them
128
+ * inside the wrapper makes them `undefined` at that moment, so the
129
+ * declarations move to the top level and their module-level variable
130
+ * bindings become top-level `let`s whose values the wrapper assigns. */
131
+ const split = deferredModuleSplit(record);
102
132
  for (const statement of record.sourceFile.statements) {
103
133
  if (statement.kind === SyntaxKind.ImportDeclaration) {
104
134
  const declaration = statement as ImportDeclaration;
@@ -112,6 +142,14 @@ export function concatenateModules(options: ConcatenateOptions): SourceFileNode
112
142
  }
113
143
  if (statement.kind === SyntaxKind.ExportDeclaration) {
114
144
  const declaration = statement as ExportDeclaration;
145
+ /* `export * as ns from "..."` needs its synthetic namespace binding
146
+ * inside the wrapper: it reads the dependency's exports, which only
147
+ * exist once that dependency's wrapper has run. */
148
+ const synthetic = namespaceStatements.get(statement);
149
+ if (synthetic) {
150
+ body.push(synthetic);
151
+ continue;
152
+ }
115
153
  const inner = (declaration as unknown as { declaration?: Statement }).declaration;
116
154
  if (inner) body.push(inner);
117
155
  continue;
@@ -121,14 +159,42 @@ export function concatenateModules(options: ConcatenateOptions): SourceFileNode
121
159
  continue;
122
160
  }
123
161
  stripExportModifiers(statement);
162
+ if (split) {
163
+ if (statement.kind === SyntaxKind.FunctionDeclaration) {
164
+ hoisted.push(statement);
165
+ continue;
166
+ }
167
+ const declarations = simpleVariableDeclarations(statement);
168
+ if (declarations) {
169
+ const scope = moduleScope(record);
170
+ for (const declaration of declarations) {
171
+ /* The rename pass rewrites references to the final name, so the
172
+ * synthetic binding and its assignment must use it too. */
173
+ const symbol = scope?.symbols.get(declaration.name);
174
+ const finalName =
175
+ (symbol ? record.finalNames.get(symbol.id) : undefined) ?? declaration.name;
176
+ hoistedVariables.push({
177
+ name: finalName,
178
+ start: declaration.start,
179
+ end: declaration.end,
180
+ });
181
+ if (declaration.initializer) {
182
+ body.push(
183
+ assignmentStatement(finalName, declaration.initializer, statement.start, statement.end),
184
+ );
185
+ }
186
+ }
187
+ continue;
188
+ }
189
+ }
124
190
  body.push(statement);
125
191
  }
192
+ const aliases = deferredAliases.get(record.path) ?? [];
126
193
  for (const statement of topLevel) push(record, statement);
127
- for (const statement of deferredModuleFunctions(
128
- record,
129
- deferredAliases.get(record.path) ?? [],
130
- body,
131
- )) {
194
+ for (const statement of deferredAliasDeclarations(aliases)) push(record, statement);
195
+ if (hoistedVariables.length > 0) push(record, letDeclaration(hoistedVariables));
196
+ for (const statement of hoisted) push(record, statement);
197
+ for (const statement of deferredModuleFunctions(record, aliases, body)) {
132
198
  push(record, statement);
133
199
  }
134
200
  continue;
@@ -242,3 +308,14 @@ function exportAssignmentToStatement(record: ModuleRecord, assignment: ExportAss
242
308
  end: assignment.end,
243
309
  };
244
310
  }
311
+
312
+ /**
313
+ * Whether a deferred module's top-level declarations can be split into
314
+ * top-level bindings plus in-wrapper initialisation.
315
+ *
316
+ * Function declarations are hoisted (a cycle may call them before any body
317
+ * runs), so the bindings they reference must be top level too. That works for
318
+ * plain `const`/`let`/`var x = ...`; a destructuring pattern, a class or an
319
+ * enum needs code we do not synthesise, so those modules keep everything inside
320
+ * the wrapper (and behave as before).
321
+ */
@@ -24,6 +24,7 @@ import {
24
24
  type Expression,
25
25
  type FunctionDeclaration,
26
26
  type Identifier,
27
+ type ExportDeclaration,
27
28
  type ImportDeclaration,
28
29
  type ObjectLiteralElementLike,
29
30
  type PropertyAssignment,
@@ -35,6 +36,9 @@ import {
35
36
  type VariableStatement,
36
37
  } from "../../ast/nodes.js";
37
38
  import { SymbolKind } from "../../binder/types.js";
39
+ import { AssignmentOperator } from "../../ast/operators.js";
40
+ import { TokenKind } from "../../lexer/token.js";
41
+ import { isTypeOnlyExport, isTypeOnlyImport } from "./graph.js";
38
42
  import { classifyDependency } from "./resolve.js";
39
43
  import { topLevelSymbols } from "./symbols.js";
40
44
  import type { ModuleRecord } from "./types.js";
@@ -105,6 +109,7 @@ function constDeclaration(
105
109
  initializer: Expression | undefined,
106
110
  start: number,
107
111
  end: number,
112
+ declarationKind: "const" | "let" = "const",
108
113
  ): VariableStatement {
109
114
  const declaration: VariableDeclaration = {
110
115
  kind: SyntaxKind.VariableDeclaration,
@@ -117,22 +122,80 @@ function constDeclaration(
117
122
  const list: VariableDeclarationList = {
118
123
  kind: SyntaxKind.VariableDeclarationList,
119
124
  declarations: [declaration],
120
- declarationKind: "const",
125
+ declarationKind,
121
126
  start,
122
127
  end,
123
128
  };
124
129
  return { kind: SyntaxKind.VariableStatement, declarationList: list, modifiers: [], start, end };
125
130
  }
126
131
 
127
- /** `const <alias> = <wrapper>()[.<importedName>];` */
128
- function aliasStatement(alias: DeferredAlias): VariableStatement {
132
+ /**
133
+ * `<alias> = <wrapper>()[.<importedName>];`
134
+ *
135
+ * The alias itself is declared at top level (see
136
+ * {@link deferredAliasDeclarations}) so that a function declaration hoisted
137
+ * out of the wrapper can reference it: a module cycle calls that function
138
+ * before the wrapper body has run.
139
+ */
140
+ function aliasStatement(alias: DeferredAlias): Statement {
129
141
  const callee = identifier(alias.wrapper, alias.start, alias.end);
130
142
  const namespace = call(callee, [], alias.start, alias.end);
131
143
  const value =
132
144
  alias.importedName === undefined || alias.namespace
133
145
  ? namespace
134
146
  : property(namespace, alias.importedName, alias.start, alias.end);
135
- return constDeclaration(alias.name, value, alias.start, alias.end);
147
+ return assignmentStatement(alias.name, value, alias.start, alias.end);
148
+ }
149
+
150
+ /** `<name> = <value>;` — used to assign a top-level `let` from inside a wrapper. */
151
+ export function assignmentStatement(name: string, value: Expression, start: number, end: number): Statement {
152
+ const assignment = {
153
+ kind: SyntaxKind.BinaryExpression,
154
+ left: identifier(name, start, end),
155
+ operator: AssignmentOperator.Assign,
156
+ right: value,
157
+ operatorToken: { kind: TokenKind.Equals, text: "=", precededByLineBreak: false, start, end },
158
+ start,
159
+ end,
160
+ } as unknown as Expression;
161
+ return {
162
+ kind: SyntaxKind.ExpressionStatement,
163
+ expression: assignment,
164
+ start,
165
+ end,
166
+ };
167
+ }
168
+
169
+ /** `let a, b;` for bindings a deferred module declares at its top level. */
170
+ export function letDeclaration(
171
+ names: readonly { readonly name: string; readonly start: number; readonly end: number }[],
172
+ ): Statement {
173
+ const declarations: VariableDeclaration[] = names.map((entry) => ({
174
+ kind: SyntaxKind.VariableDeclaration,
175
+ name: identifier(entry.name, entry.start, entry.end),
176
+ exclamation: false,
177
+ initializer: undefined,
178
+ start: entry.start,
179
+ end: entry.end,
180
+ }));
181
+ const list: VariableDeclarationList = {
182
+ kind: SyntaxKind.VariableDeclarationList,
183
+ declarations,
184
+ declarationKind: "let",
185
+ start: declarations[0]!.start,
186
+ end: declarations[declarations.length - 1]!.end,
187
+ };
188
+ return { kind: SyntaxKind.VariableStatement, declarationList: list, modifiers: [], start: list.start, end: list.end };
189
+ }
190
+
191
+ /** `let <alias>;` for every alias that binds a name, emitted at top level. */
192
+ export function deferredAliasDeclarations(aliases: readonly DeferredAlias[]): Statement[] {
193
+ const statements: Statement[] = [];
194
+ for (const alias of aliases) {
195
+ if (alias.sideEffect || alias.name.length === 0) continue;
196
+ statements.push(constDeclaration(alias.name, undefined, alias.start, alias.end, "let"));
197
+ }
198
+ return statements;
136
199
  }
137
200
 
138
201
  /** A wrapper call used for a side-effect-only import. */
@@ -299,14 +362,18 @@ export function computeDeferredModules(
299
362
  const resolution = classifyDependency(dirname(record.path), specifier, externalSpecifiers, moduleOverrides);
300
363
  return resolution.kind === "file" ? byPath.get(resolution.path) : undefined;
301
364
  };
365
+ /* A type-only import or re-export is erased, so it is not a module edge: it
366
+ * must not make its target eager (`export type { T } from "pkg"` used to
367
+ * pull a whole package into the startup path). */
302
368
  const specifierOf = (statement: Statement): string | undefined => {
303
369
  if (statement.kind === SyntaxKind.ImportDeclaration) {
304
370
  const declaration = statement as ImportDeclaration;
305
- if (declaration.importClause?.isTypeOnly) return undefined;
371
+ if (isTypeOnlyImport(declaration)) return undefined;
306
372
  return declaration.moduleSpecifier.value;
307
373
  }
308
374
  if (statement.kind === SyntaxKind.ExportDeclaration) {
309
- const declaration = statement as { moduleSpecifier?: { value: string } };
375
+ const declaration = statement as ExportDeclaration;
376
+ if (isTypeOnlyExport(declaration)) return undefined;
310
377
  return declaration.moduleSpecifier?.value;
311
378
  }
312
379
  return undefined;
@@ -350,3 +417,52 @@ export function computeDeferredModules(
350
417
  }
351
418
  return deferred;
352
419
  }
420
+ /** The declarations of a `const`/`let`/`var` statement with plain identifiers. */
421
+ export function simpleVariableDeclarations(
422
+ statement: Statement,
423
+ ): { name: string; initializer?: Expression; start: number; end: number }[] | undefined {
424
+ if (statement.kind !== SyntaxKind.VariableStatement) return undefined;
425
+ const list = (statement as VariableStatement).declarationList;
426
+ const declarations: { name: string; initializer?: Expression; start: number; end: number }[] = [];
427
+ for (const declaration of list.declarations) {
428
+ if (declaration.name.kind !== SyntaxKind.Identifier) return undefined;
429
+ declarations.push({
430
+ name: (declaration.name as Identifier).text,
431
+ initializer: declaration.initializer,
432
+ start: declaration.start,
433
+ end: declaration.end,
434
+ });
435
+ }
436
+ return declarations;
437
+ }
438
+
439
+ /**
440
+ * Whether a deferred module's top-level declarations can be split into
441
+ * top-level bindings plus in-wrapper initialisation.
442
+ *
443
+ * Function declarations are hoisted (a cycle may call them before any body
444
+ * runs), so the bindings they reference must be top level too. That works for
445
+ * plain `const`/`let`/`var x = ...`; a destructuring pattern, a class or an
446
+ * enum needs code we do not synthesise, so those modules keep everything inside
447
+ * the wrapper (and behave as before).
448
+ */
449
+ export function deferredModuleSplit(record: ModuleRecord): boolean {
450
+ for (const statement of record.sourceFile.statements) {
451
+ if (
452
+ statement.kind === SyntaxKind.ImportDeclaration ||
453
+ statement.kind === SyntaxKind.ExportDeclaration ||
454
+ statement.kind === SyntaxKind.ExportAssignment ||
455
+ statement.kind === SyntaxKind.FunctionDeclaration
456
+ ) {
457
+ continue;
458
+ }
459
+ if (statement.kind === SyntaxKind.VariableStatement) {
460
+ if (simpleVariableDeclarations(statement) === undefined) return false;
461
+ continue;
462
+ }
463
+ if (statement.kind === SyntaxKind.ClassDeclaration || statement.kind === SyntaxKind.EnumDeclaration) {
464
+ return false;
465
+ }
466
+ }
467
+ return true;
468
+ }
@@ -9,7 +9,21 @@
9
9
  * and is left in place for the code generator to reject.
10
10
  */
11
11
 
12
- import { SyntaxKind, type CallExpression, type Expression, type Identifier, type ImportExpression, type Node, type PropertyAccessExpression, type SourceFileNode, type StringLiteral } from "../../ast/nodes.js";
12
+ import {
13
+ SyntaxKind,
14
+ type CallExpression,
15
+ type Expression,
16
+ type Identifier,
17
+ type ImportDeclaration,
18
+ type ImportExpression,
19
+ type ImportSpecifier,
20
+ type NamedImports,
21
+ type Node,
22
+ type PropertyAccessExpression,
23
+ type SourceFileNode,
24
+ type Statement,
25
+ type StringLiteral,
26
+ } from "../../ast/nodes.js";
13
27
  import { deferredWrapperName } from "./deferred.js";
14
28
  import type { ModuleRecord } from "./types.js";
15
29
 
@@ -65,6 +79,47 @@ function member(object: string, name: string, anchor: number): PropertyAccessExp
65
79
  };
66
80
  }
67
81
 
82
+ /** `import { __xt_deferred_module as <name> } from "<specifier>"`. */
83
+ function markerImport(name: string, specifier: string, anchor: number): ImportDeclaration {
84
+ const moduleSpecifier: StringLiteral = {
85
+ kind: SyntaxKind.StringLiteral,
86
+ text: JSON.stringify(specifier),
87
+ value: specifier,
88
+ raw: JSON.stringify(specifier),
89
+ start: anchor,
90
+ end: anchor,
91
+ };
92
+ const element: ImportSpecifier = {
93
+ kind: SyntaxKind.ImportSpecifier,
94
+ isTypeOnly: false,
95
+ propertyName: identifier("__xt_deferred_module", anchor),
96
+ name: identifier(name, anchor),
97
+ start: anchor,
98
+ end: anchor,
99
+ };
100
+ const namedBindings: NamedImports = {
101
+ kind: SyntaxKind.NamedImports,
102
+ elements: [element],
103
+ start: anchor,
104
+ end: anchor,
105
+ };
106
+ return {
107
+ kind: SyntaxKind.ImportDeclaration,
108
+ importClause: {
109
+ kind: SyntaxKind.ImportClause,
110
+ name: undefined,
111
+ isTypeOnly: false,
112
+ namedBindings,
113
+ start: anchor,
114
+ end: anchor,
115
+ },
116
+ moduleSpecifier,
117
+ attributes: [],
118
+ start: anchor,
119
+ end: anchor,
120
+ };
121
+ }
122
+
68
123
  function call(expression: Expression, args: Expression[], anchor: number): CallExpression {
69
124
  return { kind: SyntaxKind.CallExpression, expression, typeArguments: [], arguments: args, optional: false, start: anchor, end: anchor };
70
125
  }
@@ -115,6 +170,12 @@ export function rewriteDynamicImports(
115
170
  * Rewrite every module's dynamic imports in place and return the set of module
116
171
  * paths that are reached only through a dynamic import (so the merge can emit a
117
172
  * namespace wrapper for them).
173
+ *
174
+ * A literal dynamic import must not evaluate its target at startup: it is
175
+ * deferred by marking the target with the same synthetic import the GUI
176
+ * `<script src>` loader uses (see `deferred.ts`). The marker edge is skipped by
177
+ * the eager-reachability walk, so a module that *only* appears in an
178
+ * `import(...)` is wrapped and its body runs on the first call.
118
179
  */
119
180
  export function prepareDynamicImports(
120
181
  records: readonly ModuleRecord[],
@@ -122,14 +183,26 @@ export function prepareDynamicImports(
122
183
  ): Set<string> {
123
184
  const targets = new Set<string>();
124
185
  for (const record of records) {
186
+ const markers: Statement[] = [];
187
+ const marked = new Set<string>();
125
188
  const resolve = (specifier: string): string | undefined => {
126
189
  const dependency = dependencyOf(record, specifier);
127
- if (dependency) targets.add(dependency.path);
190
+ if (!dependency) return undefined;
191
+ targets.add(dependency.path);
192
+ if (!marked.has(dependency.path)) {
193
+ marked.add(dependency.path);
194
+ markers.push(
195
+ markerImport(`__xt_dynamic_${markers.length}`, specifier, record.sourceFile.start),
196
+ );
197
+ }
128
198
  return dependency ? deferredWrapperName(dependency) : undefined;
129
199
  };
130
200
  for (const statement of record.sourceFile.statements) {
131
201
  rewriteDynamicImports(statement, resolve, record.sourceFile.start);
132
202
  }
203
+ if (markers.length > 0) {
204
+ (record.sourceFile.statements as Statement[]).unshift(...markers);
205
+ }
133
206
  }
134
207
  return targets;
135
208
  }
@@ -26,7 +26,7 @@ import type { ModuleRecord, OriginRange } from "./types.js";
26
26
  * and, in particular, must not turn the mutual `import type` references
27
27
  * between source files into a circular-import error.
28
28
  */
29
- function isTypeOnlyImport(declaration: ImportDeclaration): boolean {
29
+ export function isTypeOnlyImport(declaration: ImportDeclaration): boolean {
30
30
  const clause = declaration.importClause;
31
31
  if (!clause) return false;
32
32
  if (clause.isTypeOnly) return true;
@@ -39,7 +39,7 @@ function isTypeOnlyImport(declaration: ImportDeclaration): boolean {
39
39
  }
40
40
 
41
41
  /** True when an export declaration only re-exports types. */
42
- function isTypeOnlyExport(declaration: ExportDeclaration): boolean {
42
+ export function isTypeOnlyExport(declaration: ExportDeclaration): boolean {
43
43
  if (declaration.isTypeOnly) return true;
44
44
  const clause = declaration.exportClause;
45
45
  if (!clause || clause.kind !== SyntaxKind.NamedExports) return false;
@@ -96,10 +96,14 @@ export function loadGraph(
96
96
  return undefined;
97
97
  }
98
98
  stack.push(path);
99
- const raw = sourceTextBytes(readFileSync(path, "utf8"));
100
- /* An extension may rewrite an imported asset (e.g. `.html`) into a TS
101
- * module before it is parsed. The core only sees the transformed text. */
102
- const loader = assetLoaders[extname(path).toLowerCase()];
99
+ const extension = extname(path).toLowerCase();
100
+ const loader = assetLoaders[extension];
101
+ /* A text asset is normalised from UTF-8; a binary asset loader (`.wasm`)
102
+ * receives one Latin-1 code unit per byte so no byte can be replaced. */
103
+ const fileBytes = readFileSync(path);
104
+ const raw = loader?.binary === true
105
+ ? fileBytes.toString("latin1")
106
+ : sourceTextBytes(fileBytes.toString("utf8"));
103
107
  let text = raw;
104
108
  let originRanges: readonly OriginRange[] | undefined;
105
109
  if (loader) {
@@ -116,7 +120,7 @@ export function loadGraph(
116
120
  stack.pop();
117
121
  return undefined;
118
122
  }
119
- } else if (extname(path).toLowerCase() === ".json") {
123
+ } else if (extension === ".json") {
120
124
  // `resolveJsonModule`: a JSON file imports as a module whose default
121
125
  // export is the parsed value. JSON is a subset of expression syntax, so
122
126
  // `export default <json>;` is always valid TypeScript.
@@ -27,6 +27,7 @@ import { lowerCommonJSRequires, prepareCommonJS } from "./commonjs.js";
27
27
  import {
28
28
  computeDeferredModules,
29
29
  deferredAliasName,
30
+ deferredModuleSplit,
30
31
  deferredWrapperName,
31
32
  isDeferredMarkerImport,
32
33
  type DeferredAlias,
@@ -39,6 +40,7 @@ import { classifyDependency } from "./resolve.js";
39
40
  import {
40
41
  declarationName,
41
42
  hasModifier,
43
+ isTypeOnlySymbol,
42
44
  moduleScope,
43
45
  renameImport,
44
46
  renameReferences,
@@ -75,8 +77,12 @@ export function bundleModules(
75
77
  }
76
78
 
77
79
  // Phase 1b: give every CommonJS module its `module`/`exports` state and
78
- // record its export surface, so later phases can re-export it.
79
- for (const record of records) prepareCommonJS(record);
80
+ // record its export surface, so later phases can re-export it. The state
81
+ // itself is hoisted above every module body (see `cjsPrelude` below).
82
+ const cjsPrelude: Array<{ record: ModuleRecord; statement: Statement }> = [];
83
+ for (const record of records) {
84
+ for (const statement of prepareCommonJS(record)) cjsPrelude.push({ record, statement });
85
+ }
80
86
 
81
87
  // Phase 2: collect each module's exported names.
82
88
  const namespaceStatements = new Map<Statement, Statement>();
@@ -99,7 +105,10 @@ export function bundleModules(
99
105
  if (target) record.exports.set(exportedName, target);
100
106
  } else {
101
107
  const symbol = symbolByName.get(localName);
102
- if (symbol) {
108
+ /* A type-only local (`export { FooOptions }`) has no runtime
109
+ * binding; exporting its name would make every namespace import
110
+ * of this module read a slot that does not exist. */
111
+ if (symbol && !isTypeOnlySymbol(symbol)) {
103
112
  const finalName = record.finalNames.get(symbol.id) ?? symbol.name;
104
113
  record.exports.set(exportedName, finalName);
105
114
  }
@@ -129,7 +138,7 @@ export function bundleModules(
129
138
  const name = declarationName(statement) ?? declarationName((statement as { declaration?: Node }).declaration ?? statement);
130
139
  if (name) {
131
140
  const symbol = symbolByName.get(name.text);
132
- if (symbol) record.exports.set("default", record.finalNames.get(symbol.id) ?? symbol.name);
141
+ if (symbol && !isTypeOnlySymbol(symbol)) record.exports.set("default", record.finalNames.get(symbol.id) ?? symbol.name);
133
142
  }
134
143
  continue;
135
144
  }
@@ -137,7 +146,7 @@ export function bundleModules(
137
146
  const names = exportedNames(statement);
138
147
  for (const name of names) {
139
148
  const symbol = symbolByName.get(name);
140
- if (symbol) record.exports.set(name, record.finalNames.get(symbol.id) ?? symbol.name);
149
+ if (symbol && !isTypeOnlySymbol(symbol)) record.exports.set(name, record.finalNames.get(symbol.id) ?? symbol.name);
141
150
  }
142
151
  }
143
152
  }
@@ -269,6 +278,22 @@ export function bundleModules(
269
278
  if (specifier.isTypeOnly) continue;
270
279
  const importedName = specifier.propertyName?.text ?? specifier.name.text;
271
280
  const local = scope.symbols.get(specifier.name.text);
281
+ /* A function declaration is hoisted out of the dependency's wrapper
282
+ * (see `concatenateModules`), so referring to its final name works
283
+ * even inside a module cycle — which is exactly when the alias
284
+ * would still be `undefined`. The dependency is still initialised
285
+ * once, through a side-effect alias. */
286
+ const target = dependency.exports.get(importedName);
287
+ const dependencyScope = moduleScope(dependency);
288
+ const hoisted =
289
+ target !== undefined &&
290
+ deferredModuleSplit(dependency) &&
291
+ dependencyScope?.symbols.get(importedName)?.kind === SymbolKind.Function;
292
+ if (hoisted) {
293
+ if (local) renameReferences(local, target);
294
+ aliases.push({ name: "", wrapper, sideEffect: true, start, end });
295
+ continue;
296
+ }
272
297
  const alias = deferredAliasName(record, aliases.length, importedName);
273
298
  if (local) renameReferences(local, alias);
274
299
  aliases.push({
@@ -351,6 +376,7 @@ export function bundleModules(
351
376
  namespaceStatements,
352
377
  externalSpecifiers,
353
378
  moduleOverrides,
379
+ cjsPrelude,
354
380
  });
355
381
  return { sourceFile, text: sourceFile.text, moduleCount: records.length };
356
382
  }
@@ -8,6 +8,8 @@
8
8
 
9
9
  import {
10
10
  SyntaxKind,
11
+ type CallExpression,
12
+ type Identifier,
11
13
  type ObjectLiteralExpression,
12
14
  type PropertyAssignment,
13
15
  type Statement,
@@ -16,6 +18,39 @@ import {
16
18
  } from "../../ast/nodes.js";
17
19
  import type { ModuleRecord } from "./types.js";
18
20
 
21
+ /**
22
+ * Lower a *deferred* module's `export * as ns from "./dep"` to
23
+ * `const <final> = __xt_deferred_<dep>()`.
24
+ *
25
+ * The dependency's export names only exist inside its own wrapper function, so
26
+ * an object literal of final names would reference names that are not in scope
27
+ * (TS3002) or be `undefined` at run time. The wrapper returns the dependency's
28
+ * exports object, which is what the namespace export needs, and calling it also
29
+ * initialises the dependency first.
30
+ */
31
+ export function deferredNamespaceToStatement(
32
+ statement: Statement,
33
+ finalName: string,
34
+ wrapperName: string,
35
+ ): Statement {
36
+ const callee: Identifier = {
37
+ kind: SyntaxKind.Identifier,
38
+ text: wrapperName,
39
+ start: statement.start,
40
+ end: statement.start,
41
+ };
42
+ const initializer: CallExpression = {
43
+ kind: SyntaxKind.CallExpression,
44
+ expression: callee,
45
+ arguments: [],
46
+ typeArguments: [],
47
+ optional: false,
48
+ start: statement.start,
49
+ end: statement.end,
50
+ };
51
+ return variableStatement(statement, finalName, initializer);
52
+ }
53
+
19
54
  /** Lower `import * as ns from "./mod"` to `const m0_ns = { ...exports }`. */
20
55
  export function namespaceImportToStatement(
21
56
  statement: Statement,
@@ -38,6 +73,15 @@ export function namespaceImportToStatement(
38
73
  start: statement.start,
39
74
  end: statement.end,
40
75
  };
76
+ return variableStatement(statement, finalName, initializer);
77
+ }
78
+
79
+ /** `const <finalName> = <initializer>` at the statement's offsets. */
80
+ function variableStatement(
81
+ statement: Statement,
82
+ finalName: string,
83
+ initializer: ObjectLiteralExpression | CallExpression,
84
+ ): Statement {
41
85
  const declaration: VariableDeclaration = {
42
86
  kind: SyntaxKind.VariableDeclaration,
43
87
  name: { kind: SyntaxKind.Identifier, text: finalName, start: statement.start, end: statement.start },
@@ -24,7 +24,8 @@ import {
24
24
  type Statement,
25
25
  } from "../../ast/nodes.js";
26
26
  import type { ModuleRecord } from "./types.js";
27
- import { namespaceImportToStatement } from "./namespace-object.js";
27
+ import { deferredNamespaceToStatement, namespaceImportToStatement } from "./namespace-object.js";
28
+ import { deferredWrapperName } from "./deferred.js";
28
29
 
29
30
  export function resolveReexports(
30
31
  records: ModuleRecord[],
@@ -82,9 +83,18 @@ export function resolveReexports(
82
83
  const nsName = (declaration.exportClause as { name: Identifier }).name.text;
83
84
  const dependency = dependencyOf(record, declaration.moduleSpecifier.value);
84
85
  if (dependency) {
86
+ /* Both deferred: the dependency's final names live inside its
87
+ * wrapper, so read its exports object instead of building one. */
88
+ const deferred = deferredModules.has(record.path) && deferredModules.has(dependency.path);
85
89
  namespaceStatements.set(
86
90
  statement,
87
- namespaceImportToStatement(statement, `${record.prefix}${nsName}`, dependency),
91
+ deferred
92
+ ? deferredNamespaceToStatement(
93
+ statement,
94
+ `${record.prefix}${nsName}`,
95
+ deferredWrapperName(dependency),
96
+ )
97
+ : namespaceImportToStatement(statement, `${record.prefix}${nsName}`, dependency),
88
98
  );
89
99
  }
90
100
  }