xbintsc 0.6.22 → 0.6.24

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 (191) hide show
  1. package/AGENTS.md +5 -0
  2. package/dist/src/cli/help.d.ts +1 -1
  3. package/dist/src/driver/build-key.js +2 -1
  4. package/dist/src/driver/build-key.js.map +1 -1
  5. package/dist/src/driver/bundler/concatenate.d.ts +23 -0
  6. package/dist/src/driver/bundler/concatenate.js +210 -0
  7. package/dist/src/driver/bundler/concatenate.js.map +1 -0
  8. package/dist/src/driver/bundler/merge.d.ts +4 -4
  9. package/dist/src/driver/bundler/merge.js +24 -207
  10. package/dist/src/driver/bundler/merge.js.map +1 -1
  11. package/dist/src/driver/bundler/{namespace-import.d.ts → namespace-object.d.ts} +5 -1
  12. package/dist/src/driver/bundler/{namespace-import.js → namespace-object.js} +6 -2
  13. package/dist/src/driver/bundler/namespace-object.js.map +1 -0
  14. package/dist/src/driver/bundler/{reexport-fixup.d.ts → reexport-resolution.d.ts} +9 -3
  15. package/dist/src/driver/bundler/{reexport-fixup.js → reexport-resolution.js} +16 -6
  16. package/dist/src/driver/bundler/reexport-resolution.js.map +1 -0
  17. package/dist/src/driver/fingerprint.d.ts +11 -0
  18. package/dist/src/driver/fingerprint.js +25 -0
  19. package/dist/src/driver/fingerprint.js.map +1 -1
  20. package/dist/src/driver/link-features.js +36 -11
  21. package/dist/src/driver/link-features.js.map +1 -1
  22. package/dist/src/driver/modules.d.ts +12 -5
  23. package/dist/src/driver/modules.js +12 -5
  24. package/dist/src/driver/modules.js.map +1 -1
  25. package/dist/src/driver/paths.d.ts +10 -0
  26. package/dist/src/driver/paths.js +18 -0
  27. package/dist/src/driver/paths.js.map +1 -1
  28. package/dist/src/driver/runtime-objects.d.ts +1 -1
  29. package/dist/src/driver/runtime-objects.js +22 -6
  30. package/dist/src/driver/runtime-objects.js.map +1 -1
  31. package/dist/src/driver/toolchain.d.ts +1 -1
  32. package/dist/src/driver/toolchain.js +10 -1
  33. package/dist/src/driver/toolchain.js.map +1 -1
  34. package/dist/src/driver/version.d.ts +1 -1
  35. package/dist/src/driver/version.js +1 -1
  36. package/dist/src/extensions/node/http2/feature.d.ts +14 -0
  37. package/dist/src/extensions/node/http2/feature.js +28 -0
  38. package/dist/src/extensions/node/http2/feature.js.map +1 -0
  39. package/dist/src/extensions/node/http2/index.d.ts +5 -1
  40. package/dist/src/extensions/node/http2/index.js +6 -1
  41. package/dist/src/extensions/node/http2/index.js.map +1 -1
  42. package/dist/src/extensions/node/index.js +3 -1
  43. package/dist/src/extensions/node/index.js.map +1 -1
  44. package/dist/src/extensions/node/tls/feature.d.ts +12 -0
  45. package/dist/src/extensions/node/tls/feature.js +24 -0
  46. package/dist/src/extensions/node/tls/feature.js.map +1 -0
  47. package/dist/src/extensions/node/tls/index.d.ts +5 -1
  48. package/dist/src/extensions/node/tls/index.js +5 -1
  49. package/dist/src/extensions/node/tls/index.js.map +1 -1
  50. package/dist/src/extensions/node/zlib/compression.js +11 -6
  51. package/dist/src/extensions/node/zlib/compression.js.map +1 -1
  52. package/dist/src/extensions/registry.js +2 -1
  53. package/dist/src/extensions/registry.js.map +1 -1
  54. package/dist/src/extensions/storage.js +4 -3
  55. package/dist/src/extensions/storage.js.map +1 -1
  56. package/dist/src/extensions/tls.d.ts +26 -0
  57. package/dist/src/extensions/tls.js +46 -0
  58. package/dist/src/extensions/tls.js.map +1 -0
  59. package/dist/tests/driver/deferred-reexport.test.js +25 -0
  60. package/dist/tests/driver/deferred-reexport.test.js.map +1 -1
  61. package/dist/tests/driver/link-features.test.js +9 -0
  62. package/dist/tests/driver/link-features.test.js.map +1 -1
  63. package/dist/tests/e2e/fetch.test.js +3 -1
  64. package/dist/tests/e2e/fetch.test.js.map +1 -1
  65. package/dist/tests/e2e/http2.test.d.ts +7 -0
  66. package/dist/tests/e2e/http2.test.js +201 -0
  67. package/dist/tests/e2e/http2.test.js.map +1 -0
  68. package/dist/tests/e2e/net-helpers.d.ts +28 -0
  69. package/dist/tests/e2e/net-helpers.js +59 -0
  70. package/dist/tests/e2e/net-helpers.js.map +1 -0
  71. package/dist/tests/e2e/node-modules.test.js +3 -5
  72. package/dist/tests/e2e/node-modules.test.js.map +1 -1
  73. package/dist/tests/e2e/tls.test.d.ts +10 -0
  74. package/dist/tests/e2e/tls.test.js +197 -0
  75. package/dist/tests/e2e/tls.test.js.map +1 -0
  76. package/dist/tests/extensions/storage.test.js +3 -2
  77. package/dist/tests/extensions/storage.test.js.map +1 -1
  78. package/doc/ai/contributing.md +14 -0
  79. package/doc/ai/extensions.md +5 -3
  80. package/doc/ai/language-support.md +1 -1
  81. package/doc/ai/zh-CN/contributing.md +12 -0
  82. package/doc/ai/zh-CN/extensions.md +4 -2
  83. package/doc/ai/zh-CN/language-support.md +1 -1
  84. package/doc/gui/api.md +1 -1
  85. package/doc/gui/build.md +5 -3
  86. package/doc/gui/rendering.md +1 -1
  87. package/doc/gui.md +1 -1
  88. package/doc/implemented/async.md +1 -1
  89. package/doc/implemented/extensions.md +1 -0
  90. package/doc/implemented/fetch.md +19 -4
  91. package/doc/implemented/node/http2.md +51 -0
  92. package/doc/implemented/node/tls.md +56 -0
  93. package/doc/implemented/node.md +8 -0
  94. package/doc/implemented/storage.md +2 -1
  95. package/doc/implemented.md +2 -2
  96. package/doc/unimplemented/node/modules.md +6 -6
  97. package/doc/unimplemented/toolchain.md +1 -1
  98. package/doc/zh-CN/gui/api.md +1 -1
  99. package/doc/zh-CN/gui/build.md +4 -2
  100. package/doc/zh-CN/gui/rendering.md +1 -1
  101. package/doc/zh-CN/gui.md +1 -1
  102. package/doc/zh-CN/implemented/async.md +1 -1
  103. package/doc/zh-CN/implemented/extensions.md +1 -0
  104. package/doc/zh-CN/implemented/fetch.md +15 -3
  105. package/doc/zh-CN/implemented/node/http2.md +44 -0
  106. package/doc/zh-CN/implemented/node/tls.md +47 -0
  107. package/doc/zh-CN/implemented/node.md +7 -0
  108. package/doc/zh-CN/implemented/storage.md +2 -1
  109. package/doc/zh-CN/implemented.md +2 -2
  110. package/doc/zh-CN/unimplemented/node/modules.md +3 -3
  111. package/doc/zh-CN/unimplemented/toolchain.md +1 -1
  112. package/package.json +16 -8
  113. package/runtime/ext_node/http2/http2.c +25 -11
  114. package/runtime/ext_node/http2/http2_backend.c +48 -0
  115. package/runtime/ext_node/http2/http2_bridge.c +25 -0
  116. package/runtime/ext_node/http2/parts/api.inc +40 -0
  117. package/runtime/ext_node/http2/parts/client.inc +215 -0
  118. package/runtime/ext_node/http2/parts/common.inc +330 -0
  119. package/runtime/ext_node/http2/parts/events.inc +139 -0
  120. package/runtime/ext_node/http2/parts/server.inc +301 -0
  121. package/runtime/ext_node/http2/parts/streams.inc +254 -0
  122. package/runtime/ext_node/tls/parts/api.inc +38 -0
  123. package/runtime/ext_node/tls/parts/connect.inc +107 -0
  124. package/runtime/ext_node/tls/parts/helpers.inc +121 -0
  125. package/runtime/ext_node/tls/parts/server.inc +101 -0
  126. package/runtime/ext_node/tls/parts/socket.inc +171 -0
  127. package/runtime/ext_node/tls/tls.c +21 -13
  128. package/runtime/ext_node/tls/tls_backend.c +50 -0
  129. package/runtime/ext_node/zlib/{miniz/compression_bridge.c → compression_bridge.c} +1 -1
  130. package/runtime/xt_fetch/async.inc +19 -12
  131. package/runtime/xt_fetch/socket.inc +55 -0
  132. package/runtime/xt_fetch.c +3 -0
  133. package/runtime/xt_http2/nghttp2_compat.h +19 -0
  134. package/runtime/xt_http2/nghttp2_static.h +18 -0
  135. package/runtime/xt_http2_api.h +38 -0
  136. package/runtime/xt_node_tls_api.h +37 -0
  137. package/runtime/xt_protocol_registry.c +22 -0
  138. package/runtime/xt_storage/storage_internal.h +3 -3
  139. package/runtime/xt_tls/parts/api.inc +21 -0
  140. package/runtime/xt_tls/parts/client.inc +112 -0
  141. package/runtime/xt_tls/parts/io.inc +157 -0
  142. package/runtime/xt_tls/parts/server.inc +102 -0
  143. package/runtime/xt_tls/parts/setup.inc +146 -0
  144. package/runtime/xt_tls/tls_bridge.c +27 -0
  145. package/runtime/xt_tls/tls_core.c +39 -0
  146. package/runtime/xt_tls/tls_internal.h +73 -0
  147. package/runtime/xt_tls_api.h +73 -0
  148. package/scripts/build-gui.ts +8 -7
  149. package/scripts/build-tls.ts +248 -0
  150. package/scripts/check-file-length.ts +4 -25
  151. package/scripts/fetch-vendored.ts +244 -0
  152. package/scripts/package-release.ts +13 -3
  153. package/src/driver/build-key.ts +7 -1
  154. package/src/driver/bundler/README.md +55 -0
  155. package/src/driver/bundler/concatenate.ts +244 -0
  156. package/src/driver/bundler/merge.ts +24 -215
  157. package/src/driver/bundler/{namespace-import.ts → namespace-object.ts} +5 -1
  158. package/src/driver/bundler/{reexport-fixup.ts → reexport-resolution.ts} +15 -5
  159. package/src/driver/fingerprint.ts +24 -0
  160. package/src/driver/link-features.ts +33 -11
  161. package/src/driver/modules.ts +12 -5
  162. package/src/driver/paths.ts +20 -0
  163. package/src/driver/runtime-objects.ts +25 -6
  164. package/src/driver/toolchain.ts +11 -2
  165. package/src/driver/version.ts +1 -1
  166. package/src/extensions/node/http2/feature.ts +33 -0
  167. package/src/extensions/node/http2/index.ts +6 -1
  168. package/src/extensions/node/index.ts +3 -1
  169. package/src/extensions/node/tls/feature.ts +29 -0
  170. package/src/extensions/node/tls/index.ts +5 -1
  171. package/src/extensions/node/zlib/compression.ts +11 -6
  172. package/src/extensions/registry.ts +2 -1
  173. package/src/extensions/storage.ts +4 -3
  174. package/src/extensions/tls.ts +52 -0
  175. package/{runtime/ext_gui → vendor/stb}/stb_image.h +17 -1
  176. package/dist/src/driver/bundler/namespace-import.js.map +0 -1
  177. package/dist/src/driver/bundler/reexport-fixup.js.map +0 -1
  178. /package/{runtime/ext_node/zlib → vendor}/miniz/miniz.c +0 -0
  179. /package/{runtime/ext_node/zlib → vendor}/miniz/miniz.h +0 -0
  180. /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_common.h +0 -0
  181. /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_export.h +0 -0
  182. /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_tdef.c +0 -0
  183. /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_tdef.h +0 -0
  184. /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_tinfl.c +0 -0
  185. /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_tinfl.h +0 -0
  186. /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_zip.h +0 -0
  187. /package/{runtime/ext_gui → vendor/nanosvg}/nanosvg.h +0 -0
  188. /package/{runtime/ext_gui → vendor/nanosvg}/nanosvgrast.h +0 -0
  189. /package/{runtime/xt_storage → vendor}/sqlite3/sqlite3.c +0 -0
  190. /package/{runtime/xt_storage → vendor}/sqlite3/sqlite3.h +0 -0
  191. /package/{runtime/ext_gui → vendor/stb}/stb_truetype.h +0 -0
@@ -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),
@@ -34,6 +34,30 @@ export function runtimeFingerprint(runtimeDir: string): string {
34
34
  return hashParts(parts);
35
35
  }
36
36
 
37
+ /**
38
+ * Content hash of the vendored C sources a link feature compiles into the
39
+ * program (`vendor/miniz` for `compression`, `vendor/sqlite3` for `storage`).
40
+ *
41
+ * They live outside `runtime/` — the tree is fetched, not committed — so
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
45
+ * fingerprint already covers.
46
+ */
47
+ export function vendoredRuntimeFingerprint(runtimeDir: string): string {
48
+ const vendorDir = resolve(runtimeDir, "..", "vendor");
49
+ const parts: string[] = [];
50
+ for (const name of ["miniz", "sqlite3"]) {
51
+ const dir = join(vendorDir, name);
52
+ if (!existsSync(dir)) continue;
53
+ for (const entry of readdirSync(dir, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) {
54
+ if (!entry.isFile()) continue;
55
+ parts.push(name, entry.name, readFileSync(join(dir, entry.name)).toString("base64"));
56
+ }
57
+ }
58
+ return hashParts(parts);
59
+ }
60
+
37
61
  /**
38
62
  * Content hash of the compiler's own TypeScript sources, when they are present.
39
63
  *
@@ -94,33 +94,55 @@ export function resolveLinkFeatures(
94
94
  }
95
95
  if (specificallyForced || (forceAll && !specificallyBlocked)) forced.add(feature.name);
96
96
  }
97
- let changed = true;
98
- while (changed) {
99
- changed = false;
100
- for (const { feature } of declarations) {
101
- if (!forced.has(feature.name)) continue;
97
+ /* `strict` records the features whose absence is an error: everything the
98
+ * user asked for, plus its `requires` closure. An auto-detected feature is
99
+ * allowed to be unavailable (the driver warns), but a feature the user named
100
+ * must either link or fail the build. */
101
+ const strict = new Set(forced);
102
+ const expand = (target: Set<string>, roots: readonly string[]): void => {
103
+ const queue = [...roots];
104
+ while (queue.length > 0) {
105
+ const name = queue.pop()!;
106
+ const feature = byName.get(name);
107
+ if (feature === undefined) continue;
102
108
  for (const dependency of feature.requires ?? []) {
103
109
  if (!byName.has(dependency)) {
104
110
  diagnostics.error(
105
111
  DiagnosticCode.LinkFeatureUnavailable,
106
- `link feature '${feature.name}' requires unknown feature '${dependency}'`,
112
+ `link feature '${name}' requires unknown feature '${dependency}'`,
107
113
  );
108
114
  continue;
109
115
  }
110
116
  if (disabled.has(dependency)) {
111
117
  diagnostics.error(
112
118
  DiagnosticCode.LinkFeatureUnavailable,
113
- `link feature '${feature.name}' requires '${dependency}', which --no-feature disables`,
119
+ `link feature '${name}' requires '${dependency}', which --no-feature disables`,
114
120
  );
115
121
  continue;
116
122
  }
117
- if (!forced.has(dependency)) {
118
- forced.add(dependency);
119
- changed = true;
123
+ if (!target.has(dependency)) {
124
+ target.add(dependency);
125
+ queue.push(dependency);
120
126
  }
121
127
  }
122
128
  }
129
+ };
130
+ expand(strict, [...strict]);
131
+ for (const name of strict) forced.add(name);
132
+
133
+ /* Select the program's auto-detected features and pull in `requires`
134
+ * transitively. Dependencies are resolved for every selected feature, not
135
+ * just the explicitly forced ones: `node:tls` is auto-detected from the
136
+ * import, and the transport it builds on (`tls`) must be linked with it. */
137
+ const autoRoots: string[] = [];
138
+ for (const { feature } of declarations) {
139
+ if (forced.has(feature.name) || disabled.has(feature.name)) continue;
140
+ if (disableAll && !requested.has(feature.name)) continue;
141
+ if (!feature.used(usage)) continue;
142
+ forced.add(feature.name);
143
+ autoRoots.push(feature.name);
123
144
  }
145
+ expand(forced, autoRoots);
124
146
  if (diagnostics.hasErrors) return [];
125
147
 
126
148
  const resolved: ResolvedLinkFeature[] = [];
@@ -139,7 +161,7 @@ export function resolveLinkFeatures(
139
161
  nativeObjects = feature.nativeObjects?.() ?? [];
140
162
  } catch (error) {
141
163
  const message = error instanceof Error ? error.message : String(error);
142
- if (forcedHere) {
164
+ if (strict.has(feature.name)) {
143
165
  diagnostics.error(DiagnosticCode.LinkFeatureUnavailable, message);
144
166
  } else {
145
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
  */
@@ -22,6 +22,26 @@ export function findRuntimeDir(): string {
22
22
  throw new Error("Unable to locate the xbintsc runtime directory (missing runtime/rt.h)");
23
23
  }
24
24
 
25
+ /**
26
+ * Directory holding the third-party sources fetched by `npm run vendor`
27
+ * (`vendor/` next to `runtime/`).
28
+ *
29
+ * They are not committed: the tree is rebuilt from pinned upstream releases by
30
+ * `scripts/fetch-vendored.ts`. `runtime/xt_storage` and the `compression`
31
+ * feature resolve their C sources here, so a build that needs them fails with
32
+ * the command to run when the directory is missing.
33
+ */
34
+ export function findVendorDir(): string {
35
+ const candidate = resolve(findRuntimeDir(), "..", "vendor");
36
+ const markers = [join("sqlite3", "sqlite3.c"), join("miniz", "miniz.c")];
37
+ if (markers.some((marker) => existsSync(join(candidate, marker)))) return candidate;
38
+ 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.",
42
+ );
43
+ }
44
+
25
45
  /** Platform-architecture slug, e.g. `darwin-arm64`, `linux-x64`, `win32-x64`. */
26
46
  export function platformSlug(): string {
27
47
  return `${process.platform}-${process.arch}`;
@@ -7,7 +7,7 @@
7
7
  */
8
8
 
9
9
  import { existsSync, readFileSync, readdirSync } from "node:fs";
10
- import { basename, join, resolve } from "node:path";
10
+ import { basename, join, resolve, sep } from "node:path";
11
11
  import type { ExtensionRegistry } from "../extensions/registry.js";
12
12
  import type { ResolvedLinkFeature } from "./link-features.js";
13
13
  import { hashParts } from "./cache.js";
@@ -46,6 +46,7 @@ export const RUNTIME_SOURCES = [
46
46
  "xt_url_object.c",
47
47
  "xt_array_buffer.c",
48
48
  "xt_io.c",
49
+ "xt_protocol_registry.c",
49
50
  ] as const;
50
51
 
51
52
  export interface RuntimeObjects {
@@ -109,15 +110,24 @@ export function ensureRuntimeObjects(options: RuntimeObjectsOptions): RuntimeObj
109
110
  }
110
111
  };
111
112
  collectHeaders(runtimeDir);
113
+ /* The link features compile vendors from `vendor/`, so their headers must
114
+ * invalidate objects just like the runtime's own (the packages are sibling to
115
+ * `runtime/`, and absent in a checkout that has not run `npm run vendor`). */
116
+ const vendorDir = resolve(runtimeDir, "..", "vendor");
117
+ for (const name of ["miniz", "sqlite3"]) {
118
+ const dir = join(vendorDir, name);
119
+ if (existsSync(dir)) collectHeaders(dir);
120
+ }
112
121
  const headerStamp = hashParts(headerParts);
113
122
 
114
- const compileOne = (sourcePath: string, tag: string): string => {
123
+ const compileOne = (sourcePath: string, tag: string, includeDirs: readonly string[]): string => {
115
124
  const body = readFileSync(sourcePath, "utf8");
116
125
  const key = hashParts([
117
126
  "runtime",
118
127
  COMPILER_VERSION,
119
128
  RUNTIME_CFLAGS.join(" "),
120
129
  compileFlags.join(" "),
130
+ includeDirs.join(";"),
121
131
  tag,
122
132
  headerStamp,
123
133
  body,
@@ -126,15 +136,16 @@ export function ensureRuntimeObjects(options: RuntimeObjectsOptions): RuntimeObj
126
136
  ]);
127
137
  const objectPath = join(cacheDir, `${tag}-${key}.o`);
128
138
  if (!existsSync(objectPath)) {
129
- compileC(runner, clang, sourcePath, objectPath, runtimeDir, env, compileFlags);
139
+ compileC(runner, clang, sourcePath, objectPath, includeDirs, env, compileFlags);
130
140
  }
131
141
  return objectPath;
132
142
  };
133
143
 
144
+ const runtimeInclude = [runtimeDir];
134
145
  const coreArchive = preferPrebuilt ? findRuntimeLibrary("core", options.runtimeLibDirs) : undefined;
135
146
  const runtimeObjects = coreArchive
136
147
  ? [coreArchive]
137
- : RUNTIME_SOURCES.map((source) => compileOne(join(runtimeDir, source), basename(source, ".c")));
148
+ : RUNTIME_SOURCES.map((source) => compileOne(join(runtimeDir, source), basename(source, ".c"), runtimeInclude));
138
149
 
139
150
  /** Prebuilt archives this link uses; the driver retries without them. */
140
151
  const prebuiltArchives: string[] = coreArchive ? [coreArchive] : [];
@@ -156,12 +167,20 @@ export function ensureRuntimeObjects(options: RuntimeObjectsOptions): RuntimeObj
156
167
  const dedupeKey = resolve(sourcePath);
157
168
  if (seen.has(dedupeKey)) continue;
158
169
  seen.add(dedupeKey);
159
- extensionObjects.push(compileOne(sourcePath, `ext_${extensionObjects.length}_${basename(sourcePath, ".c")}`));
170
+ extensionObjects.push(
171
+ compileOne(sourcePath, `ext_${extensionObjects.length}_${basename(sourcePath, ".c")}`, runtimeInclude),
172
+ );
160
173
  }
161
174
  }
162
175
  // A native archive may be shared by several manifests; link each once.
163
176
  const featureObjects: string[] = [];
164
177
  for (const feature of options.features ?? []) {
178
+ /* A feature that compiles a vendored library needs `vendor/` on the include
179
+ * path for its bridge (`sqlite3/sqlite3.h`, `miniz/miniz.h`). */
180
+ const usesVendor = feature.runtimeSources.some((sourcePath) =>
181
+ resolve(sourcePath).startsWith(`${vendorDir}${sep}`),
182
+ );
183
+ const featureInclude = usesVendor ? [runtimeDir, vendorDir] : runtimeInclude;
165
184
  /* Feature bridges are always compiled: they are not part of any prebuilt
166
185
  * archive, and their references are what make the linker extract the
167
186
  * feature's implementation. */
@@ -170,7 +189,7 @@ export function ensureRuntimeObjects(options: RuntimeObjectsOptions): RuntimeObj
170
189
  if (seen.has(dedupeKey)) continue;
171
190
  seen.add(dedupeKey);
172
191
  extensionObjects.push(
173
- compileOne(sourcePath, `feature_${feature.name}_${basename(sourcePath, ".c")}`),
192
+ compileOne(sourcePath, `feature_${feature.name}_${basename(sourcePath, ".c")}`, featureInclude),
174
193
  );
175
194
  }
176
195
  featureObjects.push(...feature.nativeObjects);
@@ -495,13 +495,22 @@ export function compileC(
495
495
  clang: string,
496
496
  sourcePath: string,
497
497
  objectPath: string,
498
- includeDir: string,
498
+ includeDir: string | readonly string[],
499
499
  env?: Record<string, string>,
500
500
  extraFlags: readonly string[] = [],
501
501
  ): void {
502
+ const includeDirs = typeof includeDir === "string" ? [includeDir] : includeDir;
502
503
  const result = runner.run(
503
504
  clang,
504
- [...RUNTIME_CFLAGS, ...extraFlags, "-c", sourcePath, "-o", objectPath, `-I${includeDir}`],
505
+ [
506
+ ...RUNTIME_CFLAGS,
507
+ ...extraFlags,
508
+ "-c",
509
+ sourcePath,
510
+ "-o",
511
+ objectPath,
512
+ ...includeDirs.map((dir) => `-I${dir}`),
513
+ ],
505
514
  { env },
506
515
  );
507
516
  if (result.status !== 0) throw new ToolchainError(`${clang} ${sourcePath}`, result.status, result.stderr);
@@ -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.22";
12
+ export const COMPILER_VERSION = "0.6.24";