xbintsc 0.6.23 → 0.6.28

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 (249) hide show
  1. package/AGENTS.md +1 -1
  2. package/dist/src/cli/extensions.js +11 -3
  3. package/dist/src/cli/extensions.js.map +1 -1
  4. package/dist/src/cli/help.d.ts +1 -1
  5. package/dist/src/cli/help.js +1 -1
  6. package/dist/src/codegen/generator/state.js +7 -0
  7. package/dist/src/codegen/generator/state.js.map +1 -1
  8. package/dist/src/codegen/generator/tables.js +19 -1
  9. package/dist/src/codegen/generator/tables.js.map +1 -1
  10. package/dist/src/driver/bundler/concatenate.d.ts +23 -0
  11. package/dist/src/driver/bundler/concatenate.js +210 -0
  12. package/dist/src/driver/bundler/concatenate.js.map +1 -0
  13. package/dist/src/driver/bundler/graph.js +9 -5
  14. package/dist/src/driver/bundler/graph.js.map +1 -1
  15. package/dist/src/driver/bundler/merge.d.ts +4 -4
  16. package/dist/src/driver/bundler/merge.js +24 -207
  17. package/dist/src/driver/bundler/merge.js.map +1 -1
  18. package/dist/src/driver/bundler/{namespace-import.d.ts → namespace-object.d.ts} +5 -1
  19. package/dist/src/driver/bundler/{namespace-import.js → namespace-object.js} +6 -2
  20. package/dist/src/driver/bundler/namespace-object.js.map +1 -0
  21. package/dist/src/driver/bundler/{reexport-fixup.d.ts → reexport-resolution.d.ts} +9 -3
  22. package/dist/src/driver/bundler/{reexport-fixup.js → reexport-resolution.js} +16 -6
  23. package/dist/src/driver/bundler/reexport-resolution.js.map +1 -0
  24. package/dist/src/driver/fingerprint.d.ts +2 -2
  25. package/dist/src/driver/fingerprint.js +3 -3
  26. package/dist/src/driver/fingerprint.js.map +1 -1
  27. package/dist/src/driver/link-features.js +1 -1
  28. package/dist/src/driver/link-features.js.map +1 -1
  29. package/dist/src/driver/modules.d.ts +12 -5
  30. package/dist/src/driver/modules.js +12 -5
  31. package/dist/src/driver/modules.js.map +1 -1
  32. package/dist/src/driver/paths.js +8 -4
  33. package/dist/src/driver/paths.js.map +1 -1
  34. package/dist/src/driver/runtime-objects.d.ts +1 -1
  35. package/dist/src/driver/runtime-objects.js +13 -1
  36. package/dist/src/driver/runtime-objects.js.map +1 -1
  37. package/dist/src/driver/version.d.ts +1 -1
  38. package/dist/src/driver/version.js +1 -1
  39. package/dist/src/extensions/catalog.js +5 -1
  40. package/dist/src/extensions/catalog.js.map +1 -1
  41. package/dist/src/extensions/gui/index.js +0 -1
  42. package/dist/src/extensions/gui/index.js.map +1 -1
  43. package/dist/src/extensions/link-feature.d.ts +8 -0
  44. package/dist/src/extensions/node/stream-web/index.d.ts +6 -1
  45. package/dist/src/extensions/node/stream-web/index.js +59 -4
  46. package/dist/src/extensions/node/stream-web/index.js.map +1 -1
  47. package/dist/src/extensions/node/worker_threads/index.d.ts +14 -4
  48. package/dist/src/extensions/node/worker_threads/index.js +24 -11
  49. package/dist/src/extensions/node/worker_threads/index.js.map +1 -1
  50. package/dist/src/extensions/registry.d.ts +9 -1
  51. package/dist/src/extensions/registry.js +4 -1
  52. package/dist/src/extensions/registry.js.map +1 -1
  53. package/dist/src/extensions/wasm-asset.d.ts +16 -0
  54. package/dist/src/extensions/wasm-asset.js +28 -0
  55. package/dist/src/extensions/wasm-asset.js.map +1 -0
  56. package/dist/src/extensions/wasm.d.ts +17 -0
  57. package/dist/src/extensions/wasm.js +68 -0
  58. package/dist/src/extensions/wasm.js.map +1 -0
  59. package/dist/src/extensions/webworker/index.d.ts +24 -0
  60. package/dist/src/extensions/webworker/index.js +54 -0
  61. package/dist/src/extensions/webworker/index.js.map +1 -0
  62. package/dist/src/index.d.ts +1 -0
  63. package/dist/src/index.js +1 -0
  64. package/dist/src/index.js.map +1 -1
  65. package/dist/tests/driver/deferred-reexport.test.js +25 -0
  66. package/dist/tests/driver/deferred-reexport.test.js.map +1 -1
  67. package/dist/tests/driver/link-features.test.js +9 -0
  68. package/dist/tests/driver/link-features.test.js.map +1 -1
  69. package/dist/tests/e2e/async-fs.test.js +44 -0
  70. package/dist/tests/e2e/async-fs.test.js.map +1 -1
  71. package/dist/tests/e2e/harness.d.ts +6 -0
  72. package/dist/tests/e2e/harness.js +13 -1
  73. package/dist/tests/e2e/harness.js.map +1 -1
  74. package/dist/tests/e2e/node-crypto-random.test.d.ts +8 -0
  75. package/dist/tests/e2e/node-crypto-random.test.js +148 -0
  76. package/dist/tests/e2e/node-crypto-random.test.js.map +1 -0
  77. package/dist/tests/e2e/node-modules.test.js +1 -1
  78. package/dist/tests/e2e/node-modules.test.js.map +1 -1
  79. package/dist/tests/e2e/web-streams.test.d.ts +8 -0
  80. package/dist/tests/e2e/web-streams.test.js +274 -0
  81. package/dist/tests/e2e/web-streams.test.js.map +1 -0
  82. package/dist/tests/e2e/web-worker.test.d.ts +7 -0
  83. package/dist/tests/e2e/web-worker.test.js +74 -0
  84. package/dist/tests/e2e/web-worker.test.js.map +1 -0
  85. package/dist/tests/e2e/webassembly.test.d.ts +8 -0
  86. package/dist/tests/e2e/webassembly.test.js +113 -0
  87. package/dist/tests/e2e/webassembly.test.js.map +1 -0
  88. package/dist/tests/e2e/worker-api.test.d.ts +14 -0
  89. package/dist/tests/e2e/worker-api.test.js +460 -0
  90. package/dist/tests/e2e/worker-api.test.js.map +1 -0
  91. package/dist/tests/e2e/worker-threads.test.d.ts +8 -0
  92. package/dist/tests/e2e/worker-threads.test.js +160 -0
  93. package/dist/tests/e2e/worker-threads.test.js.map +1 -0
  94. package/dist/tests/extensions/wasm.test.d.ts +1 -0
  95. package/dist/tests/extensions/wasm.test.js +53 -0
  96. package/dist/tests/extensions/wasm.test.js.map +1 -0
  97. package/doc/ai/cli.md +1 -1
  98. package/doc/ai/contributing.md +11 -4
  99. package/doc/ai/extensions.md +25 -2
  100. package/doc/ai/language-support.md +14 -2
  101. package/doc/ai/zh-CN/cli.md +1 -1
  102. package/doc/ai/zh-CN/contributing.md +8 -3
  103. package/doc/ai/zh-CN/extensions.md +23 -2
  104. package/doc/ai/zh-CN/language-support.md +13 -2
  105. package/doc/gui/api.md +1 -1
  106. package/doc/implemented/cli.md +1 -1
  107. package/doc/implemented/extensions.md +6 -5
  108. package/doc/implemented/node/stream-web.md +31 -0
  109. package/doc/implemented/node/worker-threads.md +88 -5
  110. package/doc/implemented/node.md +2 -1
  111. package/doc/implemented/streams.md +82 -0
  112. package/doc/implemented/wasm.md +80 -18
  113. package/doc/implemented/web-worker.md +84 -0
  114. package/doc/implemented.md +6 -2
  115. package/doc/unimplemented/deviations.md +1 -0
  116. package/doc/unimplemented/node/worker-threads.md +23 -0
  117. package/doc/unimplemented/node.md +1 -0
  118. package/doc/unimplemented/stdlib.md +2 -1
  119. package/doc/unimplemented/wasm.md +21 -9
  120. package/doc/unimplemented.md +1 -1
  121. package/doc/zh-CN/gui/api.md +1 -1
  122. package/doc/zh-CN/implemented/cli.md +1 -1
  123. package/doc/zh-CN/implemented/extensions.md +7 -6
  124. package/doc/zh-CN/implemented/node/stream-web.md +24 -0
  125. package/doc/zh-CN/implemented/node/worker-threads.md +77 -4
  126. package/doc/zh-CN/implemented/node.md +2 -1
  127. package/doc/zh-CN/implemented/streams.md +60 -0
  128. package/doc/zh-CN/implemented/wasm.md +73 -16
  129. package/doc/zh-CN/implemented/web-worker.md +79 -0
  130. package/doc/zh-CN/implemented.md +6 -2
  131. package/doc/zh-CN/unimplemented/deviations.md +1 -0
  132. package/doc/zh-CN/unimplemented/node/worker-threads.md +20 -0
  133. package/doc/zh-CN/unimplemented/node.md +1 -0
  134. package/doc/zh-CN/unimplemented/stdlib.md +2 -1
  135. package/doc/zh-CN/unimplemented/wasm.md +16 -8
  136. package/doc/zh-CN/unimplemented.md +1 -1
  137. package/llms.txt +2 -0
  138. package/package.json +8 -7
  139. package/runtime/ext_gui/browser.cpp +1 -1
  140. package/runtime/ext_node/stream_web/stream_web.c +20 -6
  141. package/runtime/worker/worker_api.c +422 -0
  142. package/runtime/worker/worker_codec.c +489 -0
  143. package/runtime/worker/worker_core.c +445 -0
  144. package/runtime/worker/worker_internal.h +223 -0
  145. package/runtime/worker/worker_platform.c +344 -0
  146. package/runtime/worker/worker_ports.c +203 -0
  147. package/runtime/worker/worker_transport.c +464 -0
  148. package/runtime/worker/worker_wire.c +150 -0
  149. package/runtime/xt_builtins.c +15 -0
  150. package/runtime/xt_constructors.c +49 -2
  151. package/runtime/xt_stdlib2/error.inc +4 -0
  152. package/runtime/xt_streams/common.inc +232 -0
  153. package/runtime/xt_streams/constructors.inc +232 -0
  154. package/runtime/xt_streams/encoding.inc +124 -0
  155. package/runtime/xt_streams/readable-extra.inc +372 -0
  156. package/runtime/xt_streams/readable.inc +415 -0
  157. package/runtime/xt_streams/transform.inc +195 -0
  158. package/runtime/xt_streams/writable.inc +542 -0
  159. package/runtime/xt_streams.c +30 -0
  160. package/runtime/xt_wasm/wasm.h +148 -0
  161. package/runtime/xt_wasm/wasm_api.c +357 -0
  162. package/runtime/xt_wasm/wasm_api.h +37 -0
  163. package/runtime/xt_wasm/wasm_bridge.c +30 -0
  164. package/runtime/xt_wasm/wasm_instance.c +591 -0
  165. package/runtime/xt_wasm/wasm_module.c +270 -0
  166. package/runtime/xt_wasm/wasm_values.c +177 -0
  167. package/runtime/xt_webassembly.c +15 -4
  168. package/scripts/fetch-vendored.ts +187 -20
  169. package/scripts/package-release.ts +3 -2
  170. package/src/cli/extensions.ts +11 -3
  171. package/src/cli/help.ts +1 -1
  172. package/src/codegen/generator/state.ts +7 -0
  173. package/src/codegen/generator/tables.ts +19 -1
  174. package/src/driver/bundler/README.md +55 -0
  175. package/src/driver/bundler/concatenate.ts +244 -0
  176. package/src/driver/bundler/graph.ts +9 -5
  177. package/src/driver/bundler/merge.ts +24 -215
  178. package/src/driver/bundler/{namespace-import.ts → namespace-object.ts} +5 -1
  179. package/src/driver/bundler/{reexport-fixup.ts → reexport-resolution.ts} +15 -5
  180. package/src/driver/fingerprint.ts +3 -3
  181. package/src/driver/link-features.ts +1 -1
  182. package/src/driver/modules.ts +12 -5
  183. package/src/driver/paths.ts +8 -4
  184. package/src/driver/runtime-objects.ts +13 -1
  185. package/src/driver/version.ts +1 -1
  186. package/src/extensions/catalog.ts +5 -1
  187. package/src/extensions/gui/index.ts +0 -1
  188. package/src/extensions/link-feature.ts +8 -0
  189. package/src/extensions/node/stream-web/index.ts +59 -4
  190. package/src/extensions/node/worker_threads/index.ts +24 -11
  191. package/src/extensions/registry.ts +13 -2
  192. package/src/extensions/wasm-asset.ts +32 -0
  193. package/src/extensions/wasm.ts +74 -0
  194. package/src/extensions/webworker/index.ts +58 -0
  195. package/src/index.ts +1 -0
  196. package/vendor/wasm3/.pin +1 -0
  197. package/vendor/wasm3/LICENSE +21 -0
  198. package/vendor/wasm3/m3_api_libc.c +265 -0
  199. package/vendor/wasm3/m3_api_libc.h +19 -0
  200. package/vendor/wasm3/m3_api_meta_wasi.c +1107 -0
  201. package/vendor/wasm3/m3_api_tracer.c +173 -0
  202. package/vendor/wasm3/m3_api_tracer.h +19 -0
  203. package/vendor/wasm3/m3_api_uvwasi.c +1290 -0
  204. package/vendor/wasm3/m3_api_wasi.c +1207 -0
  205. package/vendor/wasm3/m3_api_wasi.h +74 -0
  206. package/vendor/wasm3/m3_api_wasi_host.h +137 -0
  207. package/vendor/wasm3/m3_api_wasi_posix.h +979 -0
  208. package/vendor/wasm3/m3_api_wasi_win32.h +1093 -0
  209. package/vendor/wasm3/m3_atomic.h +114 -0
  210. package/vendor/wasm3/m3_bind.c +174 -0
  211. package/vendor/wasm3/m3_bind.h +20 -0
  212. package/vendor/wasm3/m3_code.c +232 -0
  213. package/vendor/wasm3/m3_code.h +74 -0
  214. package/vendor/wasm3/m3_compile.c +6603 -0
  215. package/vendor/wasm3/m3_compile.h +354 -0
  216. package/vendor/wasm3/m3_config.h +448 -0
  217. package/vendor/wasm3/m3_config_platforms.h +345 -0
  218. package/vendor/wasm3/m3_core.c +1034 -0
  219. package/vendor/wasm3/m3_core.h +526 -0
  220. package/vendor/wasm3/m3_deterministic.c +65 -0
  221. package/vendor/wasm3/m3_deterministic.h +59 -0
  222. package/vendor/wasm3/m3_env.c +3146 -0
  223. package/vendor/wasm3/m3_env.h +748 -0
  224. package/vendor/wasm3/m3_exception.h +33 -0
  225. package/vendor/wasm3/m3_exec.c +8 -0
  226. package/vendor/wasm3/m3_exec.h +4110 -0
  227. package/vendor/wasm3/m3_exec_atomic.h +146 -0
  228. package/vendor/wasm3/m3_exec_defs.h +76 -0
  229. package/vendor/wasm3/m3_function.c +278 -0
  230. package/vendor/wasm3/m3_function.h +244 -0
  231. package/vendor/wasm3/m3_host.h +226 -0
  232. package/vendor/wasm3/m3_host_none.h +76 -0
  233. package/vendor/wasm3/m3_host_posix.h +543 -0
  234. package/vendor/wasm3/m3_host_win32.h +253 -0
  235. package/vendor/wasm3/m3_info.c +616 -0
  236. package/vendor/wasm3/m3_info.h +38 -0
  237. package/vendor/wasm3/m3_math_utils.h +560 -0
  238. package/vendor/wasm3/m3_module.c +431 -0
  239. package/vendor/wasm3/m3_parse.c +1549 -0
  240. package/vendor/wasm3/m3_snapshot.c +3825 -0
  241. package/vendor/wasm3/m3_validate.c +1770 -0
  242. package/vendor/wasm3/m3_validate.h +25 -0
  243. package/vendor/wasm3/m3_wasi.h +10 -0
  244. package/vendor/wasm3/m3_xxh64.c +211 -0
  245. package/vendor/wasm3/wasm3.h +684 -0
  246. package/vendor/wasm3/wasm3_defs.h +350 -0
  247. package/dist/src/driver/bundler/namespace-import.js.map +0 -1
  248. package/dist/src/driver/bundler/reexport-fixup.js.map +0 -1
  249. package/runtime/ext_node/worker_threads/worker_threads.c +0 -214
@@ -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.
@@ -1,54 +1,47 @@
1
1
  /**
2
- * Merge every reachable module's statements into a single source file.
2
+ * Bundling pass orchestrator (phases 1-3).
3
3
  *
4
4
  * Dependencies come first (post-order over the import graph), the entry module
5
- * last. Top-level bindings are renamed to globally unique names and imports are
6
- * rewritten to the (renamed) exported binding; namespace imports are lowered
7
- * to a synthetic object literal.
5
+ * last. These passes record each module's exports, pick globally unique final
6
+ * names, rewrite imported names and lower deferred imports. `concatenate.ts`
7
+ * then emits the statement list and applies the final renames (phases 4-5).
8
8
  */
9
9
 
10
10
  import { dirname } from "node:path";
11
11
  import {
12
12
  ModifierKind,
13
13
  SyntaxKind,
14
- type ExportAssignment,
15
14
  type ExportDeclaration,
16
15
  type Identifier,
17
16
  type ImportDeclaration,
18
- type Modifier,
19
17
  type Node,
20
- type SourceFileNode,
21
18
  type Statement,
22
- type VariableDeclaration,
23
- type VariableDeclarationList,
24
19
  type VariableStatement,
25
20
  } from "../../ast/nodes.js";
26
- import { namespaceImportToStatement } from "./namespace-import.js";
27
- import { resolveReexports } from "./reexport-fixup.js";
28
21
  import type { BindingName } from "../../ast/declarations.js";
29
22
  import { SymbolKind } from "../../binder/binder.js";
30
23
  import type { DiagnosticBag } from "../../diagnostics/diagnostic.js";
31
24
  import type { AssetLoader } from "../../extensions/registry.js";
32
- import { loadGraph } from "./graph.js";
25
+ import { concatenateModules } from "./concatenate.js";
33
26
  import { lowerCommonJSRequires, prepareCommonJS } from "./commonjs.js";
34
- import { prepareDynamicImports } from "./dynamic-import.js";
35
- import { classifyDependency } from "./resolve.js";
36
27
  import {
37
28
  computeDeferredModules,
38
29
  deferredAliasName,
39
- deferredModuleFunctions,
40
30
  deferredWrapperName,
41
- eagerModuleFunctions,
42
31
  isDeferredMarkerImport,
43
32
  type DeferredAlias,
44
33
  } from "./deferred.js";
34
+ import { prepareDynamicImports } from "./dynamic-import.js";
35
+ import { loadGraph } from "./graph.js";
36
+ import { namespaceImportToStatement } from "./namespace-object.js";
37
+ import { resolveReexports } from "./reexport-resolution.js";
38
+ import { classifyDependency } from "./resolve.js";
45
39
  import {
46
40
  declarationName,
47
41
  hasModifier,
48
42
  moduleScope,
49
43
  renameImport,
50
44
  renameReferences,
51
- renameSymbol,
52
45
  topLevelSymbols,
53
46
  } from "./symbols.js";
54
47
  import type { BundleResult, ModuleRecord } from "./types.js";
@@ -304,7 +297,7 @@ export function bundleModules(
304
297
  }
305
298
 
306
299
  if (clause.name) {
307
- const target = dependency.exports.get("default") ?? dependency.exports.get("default");
300
+ const target = dependency.exports.get("default");
308
301
  const local = scope.symbols.get(clause.name.text);
309
302
  if (target && local) renameImport(record, local, target);
310
303
  }
@@ -347,152 +340,18 @@ export function bundleModules(
347
340
  // the final renamed names (multi-level re-export chains and cycle back-edges).
348
341
  resolveReexports(records, dependencyOf, namespaceStatements, deferredModules);
349
342
 
350
- // Phase 4: concatenate statements, dropping imports and pure export wrappers.
351
- const merged: Statement[] = [];
352
- /* Every node keeps the module it was parsed from, so a later codegen
353
- * diagnostic can name the right file and line instead of pointing into the
354
- * entry module (whose text happens to start at the same offsets). */
355
- const origins = new Map<object, string>();
356
- /* A synthesized module (an extension asset loader such as the `gui` `.html`
357
- * transform) may contain spans copied from another file — a `<script
358
- * src="app.js">` body, say. Nodes inside such a span keep that file, so a
359
- * diagnostic points at real source and a plain-`.js` script stays exempt
360
- * from the type checker. */
361
- const originOf = (record: ModuleRecord, node: Node): string => {
362
- const ranges = record.originRanges;
363
- if (ranges === undefined) return record.path;
364
- for (const range of ranges) {
365
- if (node.start >= range.start && node.end <= range.end) return range.file;
366
- }
367
- return record.path;
368
- };
369
- const trackOrigin = (record: ModuleRecord, root: Node): void => {
370
- const visit = (node: Node): void => {
371
- origins.set(node, originOf(record, node));
372
- const fields = node as unknown as Record<string, unknown>;
373
- for (const key of Object.keys(fields)) {
374
- if (key === "kind" || key === "start" || key === "end" || key === "text" || key === "value" || key === "raw") {
375
- continue;
376
- }
377
- const value = fields[key];
378
- if (Array.isArray(value)) {
379
- for (const child of value) if (isNode(child)) visit(child);
380
- } else if (isNode(value)) {
381
- visit(value);
382
- }
383
- }
384
- };
385
- visit(root);
386
- };
387
- const push = (record: ModuleRecord, statement: Statement): void => {
388
- trackOrigin(record, statement);
389
- merged.push(statement);
390
- };
391
- const first = records[records.length - 1]!;
392
- for (const record of records) {
393
- if (deferredModules.has(record.path)) {
394
- /* A deferred module is evaluated by the wrapper when a script runs. Its
395
- * external imports stay top-level (codegen resolves them there); eager
396
- * namespace objects likewise. */
397
- const topLevel: Statement[] = [];
398
- const body: Statement[] = [];
399
- for (const statement of record.sourceFile.statements) {
400
- if (statement.kind === SyntaxKind.ImportDeclaration) {
401
- const declaration = statement as ImportDeclaration;
402
- const resolution = classifyDependency(
403
- dirname(record.path), declaration.moduleSpecifier.value, externalSpecifiers, moduleOverrides,
404
- );
405
- if (resolution.kind === "external") topLevel.push(statement);
406
- const synthetic = namespaceStatements.get(statement);
407
- if (synthetic) topLevel.push(synthetic);
408
- continue;
409
- }
410
- if (statement.kind === SyntaxKind.ExportDeclaration) {
411
- const declaration = statement as ExportDeclaration;
412
- const inner = (declaration as unknown as { declaration?: Statement }).declaration;
413
- if (inner) body.push(inner);
414
- continue;
415
- }
416
- if (statement.kind === SyntaxKind.ExportAssignment) {
417
- body.push(exportAssignmentToStatement(record, statement as ExportAssignment));
418
- continue;
419
- }
420
- stripExportModifiers(statement);
421
- body.push(statement);
422
- }
423
- for (const statement of topLevel) push(record, statement);
424
- for (const statement of deferredModuleFunctions(
425
- record,
426
- deferredAliases.get(record.path) ?? [],
427
- body,
428
- )) {
429
- push(record, statement);
430
- }
431
- continue;
432
- }
433
- for (const statement of record.sourceFile.statements) {
434
- if (statement.kind === SyntaxKind.ImportDeclaration) {
435
- // External (extension / built-in) imports stay in place for codegen.
436
- const declaration = statement as ImportDeclaration;
437
- const resolution = classifyDependency(
438
- dirname(record.path), declaration.moduleSpecifier.value, externalSpecifiers, moduleOverrides,
439
- );
440
- if (resolution.kind === "external") {
441
- push(record, statement);
442
- } else {
443
- const synthetic = namespaceStatements.get(statement);
444
- if (synthetic) push(record, synthetic);
445
- }
446
- continue;
447
- }
448
- if (statement.kind === SyntaxKind.ExportDeclaration) {
449
- const declaration = statement as ExportDeclaration;
450
- const synthetic = namespaceStatements.get(statement);
451
- if (synthetic) {
452
- push(record, synthetic);
453
- continue;
454
- }
455
- const inner = (declaration as unknown as { declaration?: Statement }).declaration;
456
- if (inner) push(record, inner);
457
- continue;
458
- }
459
- if (statement.kind === SyntaxKind.ExportAssignment) {
460
- const assignment = statement as ExportAssignment;
461
- push(record, exportAssignmentToStatement(record, assignment));
462
- continue;
463
- }
464
- push(record, statement);
465
- }
466
- }
467
-
468
- /* A marker or dynamic-import target that stayed eager still needs a wrapper:
469
- * its exports are already initialised. */
470
- for (const record of records) {
471
- if (deferredModules.has(record.path) || (!markerTargets.has(record.path) && !dynamicTargets.has(record.path))) continue;
472
- for (const statement of eagerModuleFunctions(record)) push(record, statement);
473
- }
474
-
475
- // Phase 5: rename every top-level binding (declaration + references) to its
476
- // unique final name, now that import references have been rewritten.
477
- for (const record of records) {
478
- for (const symbol of topLevelSymbols(record)) {
479
- if (symbol.kind === SymbolKind.Import) continue;
480
- const finalName = record.finalNames.get(symbol.id);
481
- if (finalName) renameSymbol(symbol, finalName);
482
- }
483
- }
484
-
485
- const sourceFile: SourceFileNode = {
486
- kind: SyntaxKind.SourceFile,
487
- statements: merged,
488
- fileName: first.path,
489
- origins,
490
- moduleTexts: new Map(records.map((record) => [record.path, record.text])),
491
- modulePrefixes: records.map((record) => record.prefix),
492
- text: records.map((record) => record.text).join("\n"),
493
- start: 0,
494
- end: 0,
495
- };
343
+ // Phases 4-5: concatenate the modules into one file and give every top-level
344
+ // binding its final name.
345
+ const sourceFile = concatenateModules({
346
+ records,
347
+ deferredModules,
348
+ deferredAliases,
349
+ markerTargets,
350
+ dynamicTargets,
351
+ namespaceStatements,
352
+ externalSpecifiers,
353
+ moduleOverrides,
354
+ });
496
355
  return { sourceFile, text: sourceFile.text, moduleCount: records.length };
497
356
  }
498
357
 
@@ -514,57 +373,7 @@ function collectBindingIdentifiers(name: BindingName, out: string[]): void {
514
373
  out.push(name.text);
515
374
  return;
516
375
  }
517
- const elements = name.kind === SyntaxKind.ArrayBindingPattern ? name.elements : name.elements;
518
- for (const element of elements) {
376
+ for (const element of name.elements) {
519
377
  if (element) collectBindingIdentifiers(element.name, out);
520
378
  }
521
379
  }
522
-
523
-
524
- /** Drop `export` / `export default` from a statement moved into a wrapper. */
525
- function stripExportModifiers(statement: Statement): void {
526
- const modifiers = (statement as { modifiers?: Modifier[] }).modifiers;
527
- if (!modifiers) return;
528
- (statement as { modifiers: Modifier[] }).modifiers = modifiers.filter(
529
- (modifier) => modifier.modifierKind !== ModifierKind.Export && modifier.modifierKind !== ModifierKind.Default,
530
- );
531
- }
532
-
533
- /** True for anything shaped like an AST node (constructed nodes carry no trivia). */
534
- function isNode(value: unknown): value is Node {
535
- return !!value && typeof value === "object" && "kind" in (value as object);
536
- }
537
-
538
- /** Turn `export default expr` into a synthetic `const` declaration. */
539
- function exportAssignmentToStatement(record: ModuleRecord, assignment: ExportAssignment): Statement {
540
- const name: Identifier = {
541
- kind: SyntaxKind.Identifier,
542
- text: `${record.prefix}default`,
543
- start: assignment.start,
544
- end: assignment.start,
545
- };
546
- record.exports.set("default", name.text);
547
- const declaration: VariableDeclaration = {
548
- kind: SyntaxKind.VariableDeclaration,
549
- name,
550
- exclamation: false,
551
- initializer: assignment.expression,
552
- start: assignment.start,
553
- end: assignment.end,
554
- };
555
- const list: VariableDeclarationList = {
556
- kind: SyntaxKind.VariableDeclarationList,
557
- declarationKind: "const" as const,
558
- declarations: [declaration],
559
- start: assignment.start,
560
- end: assignment.end,
561
- };
562
- const statement: VariableStatement = {
563
- kind: SyntaxKind.VariableStatement,
564
- declarationList: list,
565
- modifiers: [],
566
- start: assignment.start,
567
- end: assignment.end,
568
- };
569
- return statement;
570
- }
@@ -1,5 +1,9 @@
1
1
  /**
2
- * Synthetic AST statement builders used by the module merge.
2
+ * Build the synthetic object literal that stands in for a namespace binding.
3
+ *
4
+ * `import * as ns from "./mod"` and `export * as ns from "./mod"` are lowered
5
+ * to `const <final> = { exportName: finalName, ... }`, because the merged
6
+ * module has no runtime namespace object to point at.
3
7
  */
4
8
 
5
9
  import {
@@ -1,13 +1,19 @@
1
1
  /**
2
- * Re-export fixpoint pass.
2
+ * Resolve re-export chains and rebuild namespace objects from final names.
3
3
  *
4
4
  * Phase 2 records `export { x } from "./dep"` by copying the dependency's
5
5
  * export *value* at that moment; if `dep` re-exports an import, that value is
6
6
  * the pre-rename local name and is only corrected by Phase 3's import rewrite.
7
7
  * A multi-level chain (`index -> sdk -> tools`) therefore needs a second pass,
8
8
  * and a namespace import reaching back across a cycle needs the final names.
9
- * This function re-resolves module re-exports until the export maps stop
10
- * changing, then rebuilds every namespace object from those final names.
9
+ * This pass re-resolves module re-exports until the export maps stop changing,
10
+ * then rebuilds every eager namespace object from those final names.
11
+ *
12
+ * Deferred modules (`<script src>`) are the exception: their bindings live in a
13
+ * wrapper function, so Phase 3 already lowered any namespace binding between
14
+ * two deferred modules to an alias that calls `__xt_deferred_<dep>()`. Such a
15
+ * binding must be left alone; rebuilding it here would reference wrapper-local
16
+ * names from the top level (TS3002 `Cannot find name`).
11
17
  */
12
18
 
13
19
  import {
@@ -18,7 +24,7 @@ import {
18
24
  type Statement,
19
25
  } from "../../ast/nodes.js";
20
26
  import type { ModuleRecord } from "./types.js";
21
- import { namespaceImportToStatement } from "./namespace-import.js";
27
+ import { namespaceImportToStatement } from "./namespace-object.js";
22
28
 
23
29
  export function resolveReexports(
24
30
  records: ModuleRecord[],
@@ -89,7 +95,11 @@ export function resolveReexports(
89
95
  const bindings = clause?.namedBindings;
90
96
  if (bindings?.kind === SyntaxKind.NamespaceImport) {
91
97
  const dependency = dependencyOf(record, declaration.moduleSpecifier.value);
92
- if (dependency) {
98
+ /* Phase 3 already lowered a deferred module's namespace import of
99
+ * another deferred module to an alias read from the dependency's
100
+ * wrapper. Rebuilding the object here would reference the
101
+ * dependency's final names, which are wrapper-local (TS3002). */
102
+ if (dependency && !(deferredModules.has(record.path) && deferredModules.has(dependency.path))) {
93
103
  namespaceStatements.set(
94
104
  statement,
95
105
  namespaceImportToStatement(statement, `${record.prefix}${bindings.name.text}`, dependency),
@@ -40,14 +40,14 @@ export function runtimeFingerprint(runtimeDir: string): string {
40
40
  *
41
41
  * They live outside `runtime/` — the tree is fetched, not committed — so
42
42
  * {@link runtimeFingerprint} cannot see them, yet changing one changes the
43
- * linked binary. Only the two packages compiled at program-build time are
44
- * hashed: `stb`/`nanosvg` are baked into `gui.a`, whose content the registry
43
+ * linked binary. Only the packages compiled at program-build time are hashed:
44
+ * `stb`/`nanosvg` are baked into `gui.a`, whose content the registry
45
45
  * fingerprint already covers.
46
46
  */
47
47
  export function vendoredRuntimeFingerprint(runtimeDir: string): string {
48
48
  const vendorDir = resolve(runtimeDir, "..", "vendor");
49
49
  const parts: string[] = [];
50
- for (const name of ["miniz", "sqlite3"]) {
50
+ for (const name of ["miniz", "sqlite3", "wasm3"]) {
51
51
  const dir = join(vendorDir, name);
52
52
  if (!existsSync(dir)) continue;
53
53
  for (const entry of readdirSync(dir, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) {
@@ -161,7 +161,7 @@ export function resolveLinkFeatures(
161
161
  nativeObjects = feature.nativeObjects?.() ?? [];
162
162
  } catch (error) {
163
163
  const message = error instanceof Error ? error.message : String(error);
164
- if (strict.has(feature.name)) {
164
+ if (strict.has(feature.name) || feature.required === true) {
165
165
  diagnostics.error(DiagnosticCode.LinkFeatureUnavailable, message);
166
166
  } else {
167
167
  diagnostics.warning(
@@ -6,11 +6,18 @@
6
6
  * Each module's top-level bindings are renamed to a globally unique name and
7
7
  * imported names are rewritten to the (renamed) exported binding.
8
8
  *
9
- * The implementation lives under `bundler/` split by concern:
10
- * resolve.ts specifier/path/package resolution
11
- * graph.ts import-graph loading (parse + bind)
12
- * symbols.ts top-level symbol renaming
13
- * merge.ts the bundling passes themselves
9
+ * The implementation lives under `bundler/` (see `bundler/README.md`):
10
+ * resolve.ts specifier/path/package resolution
11
+ * graph.ts import-graph loading (parse + bind)
12
+ * merge.ts bundling pass orchestrator (phases 1-3)
13
+ * reexport-resolution.ts re-export chain fixpoint + namespace objects
14
+ * namespace-object.ts namespace object literal builder
15
+ * deferred.ts `<script src>` deferred module wrappers
16
+ * dynamic-import.ts `import()` lowering
17
+ * commonjs.ts CommonJS compatibility in `node_modules`
18
+ * concatenate.ts final concatenation (phases 4-5)
19
+ * symbols.ts top-level symbol renaming
20
+ * types.ts shared types
14
21
  *
15
22
  * This file remains the public entry point for the driver and tests.
16
23
  */
@@ -33,12 +33,16 @@ export function findRuntimeDir(): string {
33
33
  */
34
34
  export function findVendorDir(): string {
35
35
  const candidate = resolve(findRuntimeDir(), "..", "vendor");
36
- const markers = [join("sqlite3", "sqlite3.c"), join("miniz", "miniz.c")];
36
+ const markers = [
37
+ join("sqlite3", "sqlite3.c"),
38
+ join("miniz", "miniz.c"),
39
+ join("wasm3", "wasm3.h"),
40
+ ];
37
41
  if (markers.some((marker) => existsSync(join(candidate, marker)))) return candidate;
38
42
  throw new Error(
39
- "Unable to locate the xbintsc vendor directory (missing vendor/miniz/miniz.c or " +
40
- "vendor/sqlite3/sqlite3.c); run `npm run vendor` in a source checkout, or reinstall " +
41
- "xbintsc so the package's vendored sources are present.",
43
+ "Unable to locate the xbintsc vendor directory (missing vendor/miniz/miniz.c, " +
44
+ "vendor/sqlite3/sqlite3.c or vendor/wasm3/wasm3.h); run `npm run vendor` in a " +
45
+ "source checkout, or reinstall xbintsc so the package's vendored sources are present.",
42
46
  );
43
47
  }
44
48
 
@@ -42,11 +42,23 @@ export const RUNTIME_SOURCES = [
42
42
  "xt_random.c",
43
43
  "xt_crypto.c",
44
44
  "xt_text_codec.c",
45
+ "xt_streams.c",
45
46
  "xt_url.c",
46
47
  "xt_url_object.c",
47
48
  "xt_array_buffer.c",
48
49
  "xt_io.c",
49
50
  "xt_protocol_registry.c",
51
+ /* Worker transport. It lives in the core runtime (not in an extension's
52
+ * sources) because both `node:worker_threads` and the `webworker` extension
53
+ * link it; a single definition avoids duplicate symbols when both are
54
+ * enabled. Unused members are dropped by the linker's section GC. */
55
+ "worker/worker_platform.c",
56
+ "worker/worker_wire.c",
57
+ "worker/worker_codec.c",
58
+ "worker/worker_core.c",
59
+ "worker/worker_transport.c",
60
+ "worker/worker_api.c",
61
+ "worker/worker_ports.c",
50
62
  ] as const;
51
63
 
52
64
  export interface RuntimeObjects {
@@ -114,7 +126,7 @@ export function ensureRuntimeObjects(options: RuntimeObjectsOptions): RuntimeObj
114
126
  * invalidate objects just like the runtime's own (the packages are sibling to
115
127
  * `runtime/`, and absent in a checkout that has not run `npm run vendor`). */
116
128
  const vendorDir = resolve(runtimeDir, "..", "vendor");
117
- for (const name of ["miniz", "sqlite3"]) {
129
+ for (const name of ["miniz", "sqlite3", "wasm3"]) {
118
130
  const dir = join(vendorDir, name);
119
131
  if (existsSync(dir)) collectHeaders(dir);
120
132
  }
@@ -9,4 +9,4 @@
9
9
  * `main`/`master` only (see `.githooks/pre-commit`), so feature branches never
10
10
  * change this line and merging two of them cannot conflict here.
11
11
  */
12
- export const COMPILER_VERSION = "0.6.23";
12
+ export const COMPILER_VERSION = "0.6.28";
@@ -13,8 +13,12 @@
13
13
  import type { Extension } from "./registry.js";
14
14
  import { nodeExtension } from "./node/index.js";
15
15
  import { guiExtension } from "./gui/index.js";
16
+ import { webworkerExtension } from "./webworker/index.js";
16
17
 
17
18
  /** Every extension bundled with xbintsc, in registration order. */
18
19
  export function bundledExtensions(): readonly Extension[] {
19
- return [nodeExtension, guiExtension];
20
+ /* `webworker` is registered after `gui`: both define `self`, and the worker
21
+ * extension's `self` must resolve to the worker scope (a page still reaches
22
+ * the window through `window`). */
23
+ return [nodeExtension, guiExtension, webworkerExtension];
20
24
  }
@@ -87,7 +87,6 @@ const hostTypes = [
87
87
  "Request",
88
88
  "Headers",
89
89
  "Response",
90
- "ReadableStream",
91
90
  "WebGLRenderingContext",
92
91
  "WebGL2RenderingContext",
93
92
  "WebGLBuffer",
@@ -67,6 +67,14 @@ export interface LinkFeature {
67
67
  linkerFlags?(): readonly string[];
68
68
  /** Other link features this one needs (activated with it). */
69
69
  readonly requires?: readonly string[];
70
+ /**
71
+ * A required feature has no graceful degradation: if its sources are
72
+ * unavailable the build fails with the feature's own message even when the
73
+ * feature was selected by the usage scan rather than `--feature`. Optional
74
+ * features (GUI media, image decoders, ...) warn and let the program fall
75
+ * back at run time instead.
76
+ */
77
+ readonly required?: boolean;
70
78
  /** Whether the program may use this feature. */
71
79
  used(usage: ProgramUsage): boolean;
72
80
  }
@@ -1,5 +1,10 @@
1
1
  /**
2
- * Node's `stream/web` subpath module (WHATWG streams, not implemented).
2
+ * Node's `stream/web` subpath module.
3
+ *
4
+ * The WHATWG stream classes are implemented by the core runtime (`xt_streams.c`)
5
+ * and are also available as browser globals. This module re-exports the same
6
+ * constructors; only `ReadableStreamTee` and the compression streams stay
7
+ * Node-specific.
3
8
  */
4
9
 
5
10
  import type { NodeModule } from "../module.js";
@@ -7,10 +12,60 @@ import { resolveFrom } from "../module.js";
7
12
  import type { ModuleExports } from "../../registry.js";
8
13
 
9
14
  const exports: ModuleExports = {
15
+ ReadableStream: {
16
+ symbol: "xt_readable_stream_ctor",
17
+ valueSymbol: "xt_readable_stream_constructor_value",
18
+ isConstructor: true,
19
+ },
20
+ ReadableStreamDefaultReader: { symbol: "xt_readable_stream_default_reader_ctor", isConstructor: true },
21
+ ReadableStreamBYOBReader: { symbol: "xt_readable_stream_byob_reader_ctor", isConstructor: true },
22
+ ReadableStreamBYOBRequest: { symbol: "xt_readable_stream_byob_request_ctor", isConstructor: true },
23
+ ReadableStreamDefaultController: {
24
+ symbol: "xt_readable_stream_default_controller_ctor",
25
+ isConstructor: true,
26
+ },
27
+ WritableStream: {
28
+ symbol: "xt_writable_stream_ctor",
29
+ valueSymbol: "xt_writable_stream_constructor_value",
30
+ isConstructor: true,
31
+ },
32
+ WritableStreamDefaultWriter: { symbol: "xt_writable_stream_default_writer_ctor", isConstructor: true },
33
+ WritableStreamDefaultController: {
34
+ symbol: "xt_writable_stream_default_controller_ctor",
35
+ isConstructor: true,
36
+ },
37
+ TransformStream: {
38
+ symbol: "xt_transform_stream_ctor",
39
+ valueSymbol: "xt_transform_stream_constructor_value",
40
+ isConstructor: true,
41
+ },
42
+ TransformStreamDefaultController: {
43
+ symbol: "xt_transform_stream_default_controller_ctor",
44
+ isConstructor: true,
45
+ },
46
+ ByteLengthQueuingStrategy: {
47
+ symbol: "xt_byte_length_queuing_strategy_ctor",
48
+ valueSymbol: "xt_byte_strategy_constructor_value",
49
+ isConstructor: true,
50
+ },
51
+ CountQueuingStrategy: {
52
+ symbol: "xt_count_queuing_strategy_ctor",
53
+ valueSymbol: "xt_count_strategy_constructor_value",
54
+ isConstructor: true,
55
+ },
56
+ TextEncoderStream: {
57
+ symbol: "xt_text_encoder_stream_ctor",
58
+ valueSymbol: "xt_text_encoder_stream_constructor_value",
59
+ isConstructor: true,
60
+ },
61
+ TextDecoderStream: {
62
+ symbol: "xt_text_decoder_stream_ctor",
63
+ valueSymbol: "xt_text_decoder_stream_constructor_value",
64
+ isConstructor: true,
65
+ },
66
+ CompressionStream: { symbol: "xt_compression_stream_ctor", isConstructor: true },
67
+ DecompressionStream: { symbol: "xt_decompression_stream_ctor", isConstructor: true },
10
68
  ReadableStreamTee: { valueSymbol: "xt_stream_web_tee" },
11
- ReadableStream: { symbol: "xt_stream_web_unsupported_ctor", isConstructor: true },
12
- WritableStream: { symbol: "xt_stream_web_unsupported_ctor", isConstructor: true },
13
- TransformStream: { symbol: "xt_stream_web_unsupported_ctor", isConstructor: true },
14
69
  };
15
70
 
16
71
  export const streamWebModule: NodeModule = {