xbintsc 0.6.28 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (280) hide show
  1. package/dist/src/binder/declarations.d.ts +1 -0
  2. package/dist/src/binder/declarations.js +37 -0
  3. package/dist/src/binder/declarations.js.map +1 -1
  4. package/dist/src/binder/references.js +12 -0
  5. package/dist/src/binder/references.js.map +1 -1
  6. package/dist/src/checker/globals.js +1 -0
  7. package/dist/src/checker/globals.js.map +1 -1
  8. package/dist/src/checker/type-system.d.ts +5 -0
  9. package/dist/src/checker/type-system.js +22 -3
  10. package/dist/src/checker/type-system.js.map +1 -1
  11. package/dist/src/cli/help.d.ts +1 -1
  12. package/dist/src/codegen/generator/calls/access.js +25 -0
  13. package/dist/src/codegen/generator/calls/access.js.map +1 -1
  14. package/dist/src/codegen/generator/calls/invocation.js +13 -0
  15. package/dist/src/codegen/generator/calls/invocation.js.map +1 -1
  16. package/dist/src/codegen/generator/calls/super.js +3 -4
  17. package/dist/src/codegen/generator/calls/super.js.map +1 -1
  18. package/dist/src/codegen/generator/calls/unwrap.d.ts +18 -0
  19. package/dist/src/codegen/generator/calls/unwrap.js +45 -0
  20. package/dist/src/codegen/generator/calls/unwrap.js.map +1 -0
  21. package/dist/src/codegen/generator/expressions/assignment.js +14 -0
  22. package/dist/src/codegen/generator/expressions/assignment.js.map +1 -1
  23. package/dist/src/codegen/generator/expressions/module-values.d.ts +6 -0
  24. package/dist/src/codegen/generator/expressions/module-values.js +51 -17
  25. package/dist/src/codegen/generator/expressions/module-values.js.map +1 -1
  26. package/dist/src/codegen/generator/expressions/primary.js +9 -5
  27. package/dist/src/codegen/generator/expressions/primary.js.map +1 -1
  28. package/dist/src/codegen/generator/generator.js +4 -0
  29. package/dist/src/codegen/generator/generator.js.map +1 -1
  30. package/dist/src/codegen/generator/lookup-tables.d.ts +22 -0
  31. package/dist/src/codegen/generator/lookup-tables.js +48 -0
  32. package/dist/src/codegen/generator/lookup-tables.js.map +1 -0
  33. package/dist/src/codegen/generator/module.js +38 -7
  34. package/dist/src/codegen/generator/module.js.map +1 -1
  35. package/dist/src/codegen/generator/state.js +17 -0
  36. package/dist/src/codegen/generator/state.js.map +1 -1
  37. package/dist/src/codegen/generator/tables.d.ts +2 -6
  38. package/dist/src/codegen/generator/tables.js +18 -12
  39. package/dist/src/codegen/generator/tables.js.map +1 -1
  40. package/dist/src/driver/bundler/commonjs.d.ts +9 -2
  41. package/dist/src/driver/bundler/commonjs.js +11 -4
  42. package/dist/src/driver/bundler/commonjs.js.map +1 -1
  43. package/dist/src/driver/bundler/concatenate.d.ts +20 -0
  44. package/dist/src/driver/bundler/concatenate.js +68 -4
  45. package/dist/src/driver/bundler/concatenate.js.map +1 -1
  46. package/dist/src/driver/bundler/deferred.d.ts +29 -1
  47. package/dist/src/driver/bundler/deferred.js +114 -5
  48. package/dist/src/driver/bundler/deferred.js.map +1 -1
  49. package/dist/src/driver/bundler/dynamic-import.d.ts +6 -0
  50. package/dist/src/driver/bundler/dynamic-import.js +59 -3
  51. package/dist/src/driver/bundler/dynamic-import.js.map +1 -1
  52. package/dist/src/driver/bundler/graph.d.ts +13 -0
  53. package/dist/src/driver/bundler/graph.js +2 -2
  54. package/dist/src/driver/bundler/graph.js.map +1 -1
  55. package/dist/src/driver/bundler/merge.js +32 -8
  56. package/dist/src/driver/bundler/merge.js.map +1 -1
  57. package/dist/src/driver/bundler/namespace-object.d.ts +11 -0
  58. package/dist/src/driver/bundler/namespace-object.js +32 -0
  59. package/dist/src/driver/bundler/namespace-object.js.map +1 -1
  60. package/dist/src/driver/bundler/reexport-resolution.js +8 -2
  61. package/dist/src/driver/bundler/reexport-resolution.js.map +1 -1
  62. package/dist/src/driver/bundler/symbols.d.ts +4 -0
  63. package/dist/src/driver/bundler/symbols.js +14 -1
  64. package/dist/src/driver/bundler/symbols.js.map +1 -1
  65. package/dist/src/driver/config.d.ts +1 -1
  66. package/dist/src/driver/options.d.ts +1 -4
  67. package/dist/src/driver/runtime-lib.d.ts +27 -0
  68. package/dist/src/driver/runtime-lib.js +78 -2
  69. package/dist/src/driver/runtime-lib.js.map +1 -1
  70. package/dist/src/driver/runtime-objects.d.ts +1 -1
  71. package/dist/src/driver/runtime-objects.js +24 -5
  72. package/dist/src/driver/runtime-objects.js.map +1 -1
  73. package/dist/src/driver/version.d.ts +1 -1
  74. package/dist/src/driver/version.js +1 -1
  75. package/dist/src/driver/version.js.map +1 -1
  76. package/dist/src/extensions/node/buffer/index.d.ts +5 -4
  77. package/dist/src/extensions/node/buffer/index.js +5 -4
  78. package/dist/src/extensions/node/buffer/index.js.map +1 -1
  79. package/dist/src/extensions/node/events/index.js +8 -1
  80. package/dist/src/extensions/node/events/index.js.map +1 -1
  81. package/dist/src/extensions/node/fs/callbacks.d.ts +9 -0
  82. package/dist/src/extensions/node/fs/callbacks.js +42 -0
  83. package/dist/src/extensions/node/fs/callbacks.js.map +1 -0
  84. package/dist/src/extensions/node/fs/index.d.ts +2 -3
  85. package/dist/src/extensions/node/fs/index.js +11 -4
  86. package/dist/src/extensions/node/fs/index.js.map +1 -1
  87. package/dist/src/extensions/node/process/index.d.ts +10 -3
  88. package/dist/src/extensions/node/process/index.js +30 -5
  89. package/dist/src/extensions/node/process/index.js.map +1 -1
  90. package/dist/src/extensions/registry.d.ts +7 -0
  91. package/dist/src/extensions/registry.js +13 -4
  92. package/dist/src/extensions/registry.js.map +1 -1
  93. package/dist/src/lexer/scanner.d.ts +7 -1
  94. package/dist/src/lexer/scanner.js +15 -0
  95. package/dist/src/lexer/scanner.js.map +1 -1
  96. package/dist/src/parser/context.d.ts +10 -0
  97. package/dist/src/parser/context.js +42 -3
  98. package/dist/src/parser/context.js.map +1 -1
  99. package/dist/src/parser/literals.js +6 -2
  100. package/dist/src/parser/literals.js.map +1 -1
  101. package/dist/tests/binder/binder.test.js +43 -0
  102. package/dist/tests/binder/binder.test.js.map +1 -1
  103. package/dist/tests/codegen/llvm.test.js +15 -0
  104. package/dist/tests/codegen/llvm.test.js.map +1 -1
  105. package/dist/tests/driver/bundler-resolve.test.js +12 -0
  106. package/dist/tests/driver/bundler-resolve.test.js.map +1 -1
  107. package/dist/tests/driver/commonjs-prelude.test.d.ts +11 -0
  108. package/dist/tests/driver/commonjs-prelude.test.js +69 -0
  109. package/dist/tests/driver/commonjs-prelude.test.js.map +1 -0
  110. package/dist/tests/driver/compiler.test.js +42 -1
  111. package/dist/tests/driver/compiler.test.js.map +1 -1
  112. package/dist/tests/driver/html-deferred-cycles.test.d.ts +10 -0
  113. package/dist/tests/driver/html-deferred-cycles.test.js +75 -0
  114. package/dist/tests/driver/html-deferred-cycles.test.js.map +1 -0
  115. package/dist/tests/driver/namespace-type-exports.test.d.ts +9 -0
  116. package/dist/tests/driver/namespace-type-exports.test.js +59 -0
  117. package/dist/tests/driver/namespace-type-exports.test.js.map +1 -0
  118. package/dist/tests/driver/runtime-lib.test.js +49 -1
  119. package/dist/tests/driver/runtime-lib.test.js.map +1 -1
  120. package/dist/tests/e2e/compile-basics.test.js +53 -0
  121. package/dist/tests/e2e/compile-basics.test.js.map +1 -1
  122. package/dist/tests/e2e/compile-deferred-marker.test.d.ts +14 -0
  123. package/dist/tests/e2e/compile-deferred-marker.test.js +68 -0
  124. package/dist/tests/e2e/compile-deferred-marker.test.js.map +1 -0
  125. package/dist/tests/e2e/compile-deferred-reexport.test.d.ts +12 -0
  126. package/dist/tests/e2e/compile-deferred-reexport.test.js +89 -0
  127. package/dist/tests/e2e/compile-deferred-reexport.test.js.map +1 -0
  128. package/dist/tests/e2e/compile-error-module.test.d.ts +9 -0
  129. package/dist/tests/e2e/compile-error-module.test.js +29 -0
  130. package/dist/tests/e2e/compile-error-module.test.js.map +1 -0
  131. package/dist/tests/e2e/compile-gc-promises.test.d.ts +11 -0
  132. package/dist/tests/e2e/compile-gc-promises.test.js +52 -0
  133. package/dist/tests/e2e/compile-gc-promises.test.js.map +1 -0
  134. package/dist/tests/e2e/compile-modules.test.js +40 -0
  135. package/dist/tests/e2e/compile-modules.test.js.map +1 -1
  136. package/dist/tests/e2e/compile-node-fs-callbacks.test.d.ts +6 -0
  137. package/dist/tests/e2e/compile-node-fs-callbacks.test.js +46 -0
  138. package/dist/tests/e2e/compile-node-fs-callbacks.test.js.map +1 -0
  139. package/dist/tests/e2e/compile-node-inheritance.test.d.ts +6 -0
  140. package/dist/tests/e2e/compile-node-inheritance.test.js +48 -0
  141. package/dist/tests/e2e/compile-node-inheritance.test.js.map +1 -0
  142. package/dist/tests/e2e/compile-node-require.test.d.ts +9 -0
  143. package/dist/tests/e2e/compile-node-require.test.js +29 -0
  144. package/dist/tests/e2e/compile-node-require.test.js.map +1 -0
  145. package/dist/tests/e2e/compile-node-stats.test.d.ts +8 -0
  146. package/dist/tests/e2e/compile-node-stats.test.js +26 -0
  147. package/dist/tests/e2e/compile-node-stats.test.js.map +1 -0
  148. package/dist/tests/e2e/compile-proxy.test.d.ts +6 -0
  149. package/dist/tests/e2e/compile-proxy.test.js +66 -0
  150. package/dist/tests/e2e/compile-proxy.test.js.map +1 -0
  151. package/dist/tests/e2e/compile-web-globals.test.d.ts +6 -0
  152. package/dist/tests/e2e/compile-web-globals.test.js +94 -0
  153. package/dist/tests/e2e/compile-web-globals.test.js.map +1 -0
  154. package/dist/tests/e2e/fetch.test.js +1 -1
  155. package/dist/tests/e2e/gui-script-import-order.test.js +24 -0
  156. package/dist/tests/e2e/gui-script-import-order.test.js.map +1 -1
  157. package/dist/tests/e2e/json-stringify-to-json.test.d.ts +10 -0
  158. package/dist/tests/e2e/json-stringify-to-json.test.js +61 -0
  159. package/dist/tests/e2e/json-stringify-to-json.test.js.map +1 -0
  160. package/dist/tests/e2e/node-process.test.d.ts +7 -0
  161. package/dist/tests/e2e/node-process.test.js +50 -0
  162. package/dist/tests/e2e/node-process.test.js.map +1 -0
  163. package/dist/tests/e2e/runtime-coverage-buffer.test.js +31 -0
  164. package/dist/tests/e2e/runtime-coverage-buffer.test.js.map +1 -1
  165. package/dist/tests/e2e/web-streams.test.js +44 -29
  166. package/dist/tests/e2e/web-streams.test.js.map +1 -1
  167. package/dist/tests/parser/literals.test.js +19 -0
  168. package/dist/tests/parser/literals.test.js.map +1 -1
  169. package/dist/tests/parser/statements.test.js +13 -0
  170. package/dist/tests/parser/statements.test.js.map +1 -1
  171. package/doc/ai/cli.md +1 -0
  172. package/doc/ai/zh-CN/cli.md +1 -0
  173. package/doc/implemented/binder.md +2 -1
  174. package/doc/implemented/checker.md +5 -0
  175. package/doc/implemented/lexer.md +1 -1
  176. package/doc/implemented/modules.md +23 -1
  177. package/doc/implemented/node/buffer.md +18 -5
  178. package/doc/implemented/node/fs.md +22 -1
  179. package/doc/implemented/node/process.md +65 -5
  180. package/doc/implemented/node.md +7 -1
  181. package/doc/implemented/operators.md +3 -1
  182. package/doc/implemented/parser.md +1 -1
  183. package/doc/implemented/runtime-core.md +6 -2
  184. package/doc/implemented/stdlib.md +69 -2
  185. package/doc/unimplemented/classes.md +1 -0
  186. package/doc/unimplemented/modules.md +1 -0
  187. package/doc/unimplemented/stdlib.md +2 -1
  188. package/doc/xbintsc.config.schema.json +5 -0
  189. package/doc/zh-CN/implemented/binder.md +2 -1
  190. package/doc/zh-CN/implemented/checker.md +3 -0
  191. package/doc/zh-CN/implemented/lexer.md +1 -1
  192. package/doc/zh-CN/implemented/modules.md +17 -1
  193. package/doc/zh-CN/implemented/node/buffer.md +3 -1
  194. package/doc/zh-CN/implemented/node/fs.md +19 -1
  195. package/doc/zh-CN/implemented/node/process.md +59 -5
  196. package/doc/zh-CN/implemented/node.md +1 -1
  197. package/doc/zh-CN/implemented/operators.md +2 -1
  198. package/doc/zh-CN/implemented/parser.md +1 -1
  199. package/doc/zh-CN/implemented/runtime-core.md +3 -2
  200. package/doc/zh-CN/implemented/stdlib.md +58 -2
  201. package/doc/zh-CN/unimplemented/classes.md +1 -0
  202. package/doc/zh-CN/unimplemented/modules.md +1 -0
  203. package/doc/zh-CN/unimplemented/stdlib.md +2 -1
  204. package/package.json +7 -7
  205. package/runtime/ext_node/buffer/buffer.c +5 -4
  206. package/runtime/ext_node/buffer/parts/byte-access.inc +115 -11
  207. package/runtime/ext_node/buffer/parts/prototype.inc +4 -1
  208. package/runtime/ext_node/events/events.c +9 -0
  209. package/runtime/ext_node/fs/callbacks.c +138 -0
  210. package/runtime/ext_node/fs/fs_ops.c +28 -15
  211. package/runtime/ext_node/module/module.c +18 -9
  212. package/runtime/ext_node/process/process.c +117 -32
  213. package/runtime/ext_node/process/streams.c +370 -0
  214. package/runtime/ext_node/process/streams.h +20 -0
  215. package/runtime/rt.h +36 -0
  216. package/runtime/rt_internal.h +10 -10
  217. package/runtime/xt_alloc.c +67 -49
  218. package/runtime/xt_async_io.c +3 -0
  219. package/runtime/xt_builtins.c +26 -23
  220. package/runtime/xt_constructors.c +101 -2
  221. package/runtime/xt_containers/access.inc +29 -1
  222. package/runtime/xt_containers/function_prototypes.inc +58 -0
  223. package/runtime/xt_containers/objects.inc +18 -2
  224. package/runtime/xt_containers.c +1 -0
  225. package/runtime/xt_exit.c +34 -0
  226. package/runtime/xt_fetch/headers.inc +1 -0
  227. package/runtime/xt_fetch/response.inc +28 -8
  228. package/runtime/xt_fetch/webapi.inc +82 -0
  229. package/runtime/xt_fetch.c +5 -0
  230. package/runtime/xt_gc_verify.c +135 -0
  231. package/runtime/xt_intl.c +224 -0
  232. package/runtime/xt_io/exception.inc +40 -1
  233. package/runtime/xt_module_values.c +82 -0
  234. package/runtime/xt_proxy.c +140 -0
  235. package/runtime/xt_stdlib/dispatch.inc +98 -9
  236. package/runtime/xt_stdlib2/date.inc +7 -1
  237. package/runtime/xt_stdlib2/json.inc +28 -0
  238. package/runtime/xt_stdlib2/json_stringify.inc +70 -53
  239. package/runtime/xt_web.h +27 -0
  240. package/runtime/xt_web_abort.c +373 -0
  241. package/scripts/build-runtime.ts +8 -0
  242. package/src/binder/declarations.ts +34 -0
  243. package/src/binder/references.ts +12 -0
  244. package/src/checker/globals.ts +1 -0
  245. package/src/checker/type-system.ts +23 -3
  246. package/src/codegen/generator/calls/access.ts +23 -0
  247. package/src/codegen/generator/calls/invocation.ts +12 -0
  248. package/src/codegen/generator/calls/super.ts +5 -4
  249. package/src/codegen/generator/calls/unwrap.ts +47 -0
  250. package/src/codegen/generator/expressions/assignment.ts +15 -0
  251. package/src/codegen/generator/expressions/module-values.ts +57 -19
  252. package/src/codegen/generator/expressions/primary.ts +9 -9
  253. package/src/codegen/generator/generator.ts +4 -0
  254. package/src/codegen/generator/lookup-tables.ts +63 -0
  255. package/src/codegen/generator/module.ts +38 -7
  256. package/src/codegen/generator/state.ts +17 -0
  257. package/src/codegen/generator/tables.ts +18 -12
  258. package/src/driver/bundler/commonjs.ts +12 -5
  259. package/src/driver/bundler/concatenate.ts +84 -7
  260. package/src/driver/bundler/deferred.ts +122 -6
  261. package/src/driver/bundler/dynamic-import.ts +75 -2
  262. package/src/driver/bundler/graph.ts +2 -2
  263. package/src/driver/bundler/merge.ts +31 -5
  264. package/src/driver/bundler/namespace-object.ts +44 -0
  265. package/src/driver/bundler/reexport-resolution.ts +12 -2
  266. package/src/driver/bundler/symbols.ts +17 -0
  267. package/src/driver/config.ts +1 -1
  268. package/src/driver/options.ts +1 -4
  269. package/src/driver/runtime-lib.ts +87 -2
  270. package/src/driver/runtime-objects.ts +24 -5
  271. package/src/driver/version.ts +1 -1
  272. package/src/extensions/node/buffer/index.ts +5 -4
  273. package/src/extensions/node/events/index.ts +8 -1
  274. package/src/extensions/node/fs/callbacks.ts +45 -0
  275. package/src/extensions/node/fs/index.ts +11 -4
  276. package/src/extensions/node/process/index.ts +30 -5
  277. package/src/extensions/registry.ts +21 -4
  278. package/src/lexer/scanner.ts +16 -1
  279. package/src/parser/context.ts +39 -3
  280. package/src/parser/literals.ts +6 -2
@@ -71,10 +71,7 @@ export interface BuildOptions {
71
71
  readonly features?: readonly string[];
72
72
  /** Optional link features to keep out even when the scan detects a use. */
73
73
  readonly noFeatures?: readonly string[];
74
- /**
75
- * Bare module specifiers mapped to local replacement files. A specifier in
76
- * this map resolves to its file (an absolute path) before `node_modules`.
77
- */
74
+ /** Specifiers mapped to local replacement files, checked before `node_modules`. */
78
75
  readonly moduleOverrides?: Readonly<Record<string, string>>;
79
76
  }
80
77
 
@@ -19,8 +19,9 @@
19
19
  * dependency.
20
20
  */
21
21
 
22
- import { existsSync, readdirSync } from "node:fs";
23
- import { dirname, join } from "node:path";
22
+ import { existsSync, readFileSync, readdirSync, statSync, writeFileSync } from "node:fs";
23
+ import { dirname, join, relative, sep } from "node:path";
24
+ import { hashParts } from "./cache.js";
24
25
  import { findRuntimeDir, platformSlug } from "./paths.js";
25
26
 
26
27
  /** Archive suffixes, in the order a name is probed. */
@@ -94,3 +95,87 @@ export function findRuntimeLibrary(
94
95
  }
95
96
  return undefined;
96
97
  }
98
+
99
+
100
+ /** Name of the file recording which sources an archive was built from. */
101
+ export const RUNTIME_STAMP_FILE = "runtime-stamp.json";
102
+
103
+ /** Source files that make up the runtime archives (the archives' own folder is
104
+ * skipped: it holds the `.a`/`.lib` outputs and the stamp). */
105
+ function runtimeSourceFiles(runtimeDir: string): string[] {
106
+ const files: string[] = [];
107
+ const walk = (dir: string): void => {
108
+ let entries: string[];
109
+ try {
110
+ entries = readdirSync(dir);
111
+ } catch {
112
+ return;
113
+ }
114
+ for (const name of entries.sort()) {
115
+ const full = join(dir, name);
116
+ if (statSync(full).isDirectory()) {
117
+ if (name === "lib") continue;
118
+ walk(full);
119
+ } else if (/\.(c|h|inc)$/.test(name)) {
120
+ files.push(full);
121
+ }
122
+ }
123
+ };
124
+ walk(runtimeDir);
125
+ return files;
126
+ }
127
+
128
+ /**
129
+ * Fingerprint of the C runtime sources an archive is compiled from.
130
+ *
131
+ * A prebuilt `core`/`ext_<name>` archive is only usable when it was built from
132
+ * exactly these sources: the driver compares the archive's stamp with this
133
+ * value and compiles from source when they differ. Without the stamp a stale
134
+ * archive links silently whenever every symbol it needs still exists — the
135
+ * program then runs the *old* runtime, which is very hard to diagnose.
136
+ */
137
+ export function runtimeSourceFingerprint(runtimeDir: string, compilerVersion: string, flags: readonly string[]): string {
138
+ const parts: string[] = ["runtime-archive", compilerVersion, process.platform, process.arch, [...flags].join(" ")];
139
+ for (const file of runtimeSourceFiles(runtimeDir)) {
140
+ parts.push(relative(runtimeDir, file).split(sep).join("/"), readFileSync(file, "utf8"));
141
+ }
142
+ return hashParts(parts);
143
+ }
144
+
145
+ /** The recorded fingerprint of the archives in `dir`, if any. */
146
+ export function readRuntimeStamp(dir: string): string | undefined {
147
+ try {
148
+ const parsed: unknown = JSON.parse(readFileSync(join(dir, RUNTIME_STAMP_FILE), "utf8"));
149
+ if (typeof parsed !== "object" || parsed === null) return undefined;
150
+ const fingerprint = (parsed as { fingerprint?: unknown }).fingerprint;
151
+ return typeof fingerprint === "string" ? fingerprint : undefined;
152
+ } catch {
153
+ return undefined;
154
+ }
155
+ }
156
+
157
+ /** Record the fingerprint next to the archives a build just produced. */
158
+ export function writeRuntimeStamp(dir: string, fingerprint: string, compilerVersion: string): void {
159
+ writeFileSync(
160
+ join(dir, RUNTIME_STAMP_FILE),
161
+ `${JSON.stringify({ compilerVersion, fingerprint }, null, "\t")}\n`,
162
+ "utf8",
163
+ );
164
+ }
165
+
166
+ /**
167
+ * A prebuilt archive, but only when it was built from the sources now on disk.
168
+ *
169
+ * Returns `undefined` for a missing archive *and* for a stale one, so the caller
170
+ * falls back to compiling the runtime; `stale` tells the two apart for the
171
+ * message.
172
+ */
173
+ export function freshRuntimeLibrary(
174
+ name: string,
175
+ dirs: readonly string[],
176
+ fingerprint: string,
177
+ ): { path?: string; stale?: string } {
178
+ const path = findRuntimeLibrary(name, dirs);
179
+ if (!path) return {};
180
+ return readRuntimeStamp(dirname(path)) === fingerprint ? { path } : { stale: path };
181
+ }
@@ -11,7 +11,7 @@ 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";
14
- import { findRuntimeLibrary } from "./runtime-lib.js";
14
+ import { freshRuntimeLibrary, runtimeLibDirs, runtimeSourceFingerprint } from "./runtime-lib.js";
15
15
  import { compileC, optimizationFlags, RUNTIME_CFLAGS, sysrootFlags, type PgoMode, type Runner } from "./toolchain.js";
16
16
  import { COMPILER_VERSION } from "./version.js";
17
17
 
@@ -31,9 +31,14 @@ export const RUNTIME_SOURCES = [
31
31
  "xt_async.c",
32
32
  "xt_loop.c",
33
33
  "xt_async_io.c",
34
+ "xt_exit.c",
35
+ "xt_intl.c",
34
36
  "xt_generator.c",
35
37
  "xt_coroutine.c",
36
38
  "xt_fetch.c",
39
+ "xt_web_abort.c",
40
+ "xt_proxy.c",
41
+ "xt_gc_verify.c",
37
42
  "xt_symbol.c",
38
43
  "xt_builtins.c",
39
44
  "xt_constructors.c",
@@ -154,7 +159,17 @@ export function ensureRuntimeObjects(options: RuntimeObjectsOptions): RuntimeObj
154
159
  };
155
160
 
156
161
  const runtimeInclude = [runtimeDir];
157
- const coreArchive = preferPrebuilt ? findRuntimeLibrary("core", options.runtimeLibDirs) : undefined;
162
+ /* A shipped archive is only used when it was built from the sources on disk;
163
+ * a stale one links silently and the program runs an old runtime. */
164
+ const fingerprint = runtimeSourceFingerprint(runtimeDir, COMPILER_VERSION, RUNTIME_CFLAGS);
165
+ const staleArchives: string[] = [];
166
+ const prebuilt = (name: string): string | undefined => {
167
+ if (!preferPrebuilt) return undefined;
168
+ const found = freshRuntimeLibrary(name, options.runtimeLibDirs ?? runtimeLibDirs(), fingerprint);
169
+ if (found.stale !== undefined) staleArchives.push(found.stale);
170
+ return found.path;
171
+ };
172
+ const coreArchive = prebuilt("core");
158
173
  const runtimeObjects = coreArchive
159
174
  ? [coreArchive]
160
175
  : RUNTIME_SOURCES.map((source) => compileOne(join(runtimeDir, source), basename(source, ".c"), runtimeInclude));
@@ -167,9 +182,7 @@ export function ensureRuntimeObjects(options: RuntimeObjectsOptions): RuntimeObj
167
182
  for (const extension of registry.all()) {
168
183
  const sources = extension.runtimeSources ? extension.runtimeSources() : [];
169
184
  if (sources.length === 0) continue;
170
- const archive = preferPrebuilt
171
- ? findRuntimeLibrary(`ext_${extension.name}`, options.runtimeLibDirs)
172
- : undefined;
185
+ const archive = prebuilt(`ext_${extension.name}`);
173
186
  if (archive) {
174
187
  extensionObjects.push(archive);
175
188
  prebuiltArchives.push(archive);
@@ -184,6 +197,12 @@ export function ensureRuntimeObjects(options: RuntimeObjectsOptions): RuntimeObj
184
197
  );
185
198
  }
186
199
  }
200
+ if (staleArchives.length > 0) {
201
+ /* Once per process: the archives stay stale until `npm run runtime` . */
202
+ process.stderr.write(
203
+ "xbintsc: prebuilt runtime archive is stale (the runtime sources changed); compiling the C runtime from source\n",
204
+ );
205
+ }
187
206
  // A native archive may be shared by several manifests; link each once.
188
207
  const featureObjects: string[] = [];
189
208
  for (const feature of options.features ?? []) {
@@ -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.28";
12
+ export const COMPILER_VERSION = "0.7.0";
@@ -1,10 +1,11 @@
1
1
  /**
2
2
  * Node's `Buffer` module.
3
3
  *
4
- * Buffers are represented as plain xbintsc objects (indexed byte properties
5
- * plus `length`) with methods supplied by the C runtime, so `Buffer.from`,
6
- * `Buffer.alloc` and the instance methods (`toString`, `slice`, ...) work
7
- * without a dedicated binary value type in the compiler.
4
+ * Buffers are ordinary xbintsc objects whose bytes live in an `ArrayBuffer`
5
+ * store behind dynamic-property hooks (plus a `length` property) with methods
6
+ * supplied by the C runtime, so `Buffer.from`, `Buffer.alloc` and the instance
7
+ * methods (`toString`, `slice`, ...) work without a dedicated binary value type
8
+ * in the compiler, and `buf[i]` still reads a byte.
8
9
  */
9
10
 
10
11
  import type { NodeModule } from "../module.js";
@@ -14,7 +14,14 @@ import type { ModuleExports } from "../../registry.js";
14
14
  const methods = ["listenerCount", "getEventListeners", "getMaxListeners", "setMaxListeners", "once", "addAbortListener"] as const;
15
15
 
16
16
  const exports: ModuleExports = {
17
- EventEmitter: { symbol: "xt_event_emitter_ctor", isConstructor: true },
17
+ EventEmitter: {
18
+ symbol: "xt_event_emitter_ctor",
19
+ isConstructor: true,
20
+ // `class X extends EventEmitter` chains onto this prototype, so the
21
+ // inherited listener API is reachable (and `x instanceof EventEmitter`
22
+ // holds when the same constructor value is compared).
23
+ prototypeSymbol: "xt_events_prototype",
24
+ },
18
25
  defaultMaxListeners: { valueSymbol: "xt_events_default_max_listeners" },
19
26
  ...Object.fromEntries(methods.map((method) => [method, { namespace: "events", method }])),
20
27
  };
@@ -0,0 +1,45 @@
1
+ /**
2
+ * The callback forms of Node's `fs` API.
3
+ *
4
+ * Each name forwards to the promise implementation in `callbacks.c`, which
5
+ * attaches `callback(null, value)` / `callback(error)` handlers. `exists` keeps
6
+ * Node's odd shape: one boolean argument and no error.
7
+ */
8
+
9
+ import type { BuiltinFunction } from "../../registry.js";
10
+
11
+ const callback = (name: string): BuiltinFunction => ({ symbol: `xt_node_${name}_cb` });
12
+
13
+ export const fsCallbackBuiltins: Readonly<Record<string, BuiltinFunction>> = {
14
+ readFile: callback("read_file"),
15
+ writeFile: callback("write_file"),
16
+ appendFile: callback("append_file"),
17
+ mkdir: callback("mkdir"),
18
+ mkdtemp: callback("mkdtemp"),
19
+ readdir: callback("readdir"),
20
+ rm: callback("rm"),
21
+ rmdir: callback("rmdir"),
22
+ unlink: callback("unlink"),
23
+ rename: callback("rename"),
24
+ copyFile: callback("copy_file"),
25
+ cp: callback("cp"),
26
+ realpath: callback("realpath"),
27
+ stat: callback("stat"),
28
+ lstat: callback("lstat"),
29
+ statfs: callback("statfs"),
30
+ access: callback("access"),
31
+ open: callback("open"),
32
+ chmod: callback("chmod"),
33
+ lchmod: callback("lchmod"),
34
+ chown: callback("chown"),
35
+ lchown: callback("lchown"),
36
+ truncate: callback("truncate"),
37
+ utimes: callback("utimes"),
38
+ lutimes: callback("lutimes"),
39
+ link: callback("link"),
40
+ symlink: callback("symlink"),
41
+ readlink: callback("readlink"),
42
+ opendir: callback("opendir"),
43
+ glob: callback("glob"),
44
+ exists: callback("exists"),
45
+ };
@@ -1,8 +1,7 @@
1
1
  /**
2
2
  * Node's `fs` module: synchronous file and directory operations backed by
3
- * `runtime/ext_node/fs`, plus the `fs.promises` namespace (implemented in
4
- * `async_*.c` on top of the asynchronous I/O pool). The callback-style
5
- * `readFile`/`writeFile` forms are not supported; `*Sync` and `fs.promises` are.
3
+ * `runtime/ext_node/fs`, the callback forms (`readFile`, `writeFile`, `stat`,
4
+ * …) built on the asynchronous I/O pool, and the `fs.promises` namespace.
6
5
  * `watch`/`watchFile` return API-shaped watchers that never emit.
7
6
  *
8
7
  * `fs.constants` and `fs.promises` are exposed as value getters, so both
@@ -16,9 +15,16 @@ import type { ModuleExports } from "../../registry.js";
16
15
  import { readFileBuiltins } from "./read-file.js";
17
16
  import { writeFileBuiltins } from "./write-file.js";
18
17
  import { fsOpsBuiltins } from "./fs-ops.js";
18
+ import { fsCallbackBuiltins } from "./callbacks.js";
19
19
  import { fsStreamBuiltins } from "./streams.js";
20
20
 
21
- const builtins = { ...readFileBuiltins, ...writeFileBuiltins, ...fsOpsBuiltins, ...fsStreamBuiltins };
21
+ const builtins = {
22
+ ...readFileBuiltins,
23
+ ...writeFileBuiltins,
24
+ ...fsOpsBuiltins,
25
+ ...fsCallbackBuiltins,
26
+ ...fsStreamBuiltins,
27
+ };
22
28
 
23
29
  const exports: ModuleExports = {
24
30
  ...builtins,
@@ -31,6 +37,7 @@ export const fsModule: NodeModule = {
31
37
  runtimeSources: () => [
32
38
  resolveFrom(import.meta.url, "../../../../runtime/ext_node/fs/read_file.c"),
33
39
  resolveFrom(import.meta.url, "../../../../runtime/ext_node/fs/write_file.c"),
40
+ resolveFrom(import.meta.url, "../../../../runtime/ext_node/fs/callbacks.c"),
34
41
  resolveFrom(import.meta.url, "../../../../runtime/ext_node/fs/fs_ops.c"),
35
42
  resolveFrom(import.meta.url, "../../../../runtime/ext_node/fs/fd_ops.c"),
36
43
  resolveFrom(import.meta.url, "../../../../runtime/ext_node/fs/meta_ops.c"),
@@ -2,18 +2,40 @@
2
2
  * Node's `process` object backed by `runtime/ext_node/process`.
3
3
  *
4
4
  * The compiler lowers `process.<method>(...)` to
5
- * `xt_process_call(<method>, argc, argv)` and `process.<property>` to
6
- * `xt_process_get(<property>)` (see `NAMESPACE_STATICS` / `NAMESPACE_PROPERTIES`
7
- * in `src/codegen/generator/tables.ts`). Importing the module aliases that
5
+ * `xt_process_call(<method>, argc, argv)`, `process.<property>` to
6
+ * `xt_process_get(<property>)` and `process.<property> = value` to
7
+ * `xt_process_set(<property>, value)` (see `NAMESPACE_STATICS` /
8
+ * `NAMESPACE_PROPERTIES` / `NAMESPACE_PROPERTY_SETTERS` in
9
+ * `src/codegen/generator/tables.ts`). Importing the module aliases that
8
10
  * namespace (`import process from "process"`); named method exports dispatch
9
11
  * through `xt_process_call`.
12
+ *
13
+ * The object is also an EventEmitter (`on`/`once`/`off`/`emit`, with "exit"
14
+ * fired before the process ends) and owns the three standard streams, so
15
+ * `process.stdin.setRawMode(true)` and `process.stdout.columns` reach the same
16
+ * singletons.
10
17
  */
11
18
 
12
19
  import type { NodeModule } from "../module.js";
13
20
  import { resolveFrom } from "../module.js";
14
21
  import type { ModuleExports } from "../../registry.js";
15
22
 
16
- const methods = ["cwd", "exit", "uptime", "hrtime", "getuid"] as const;
23
+ const methods = [
24
+ "cwd",
25
+ "exit",
26
+ "uptime",
27
+ "hrtime",
28
+ "getuid",
29
+ "on",
30
+ "once",
31
+ "addListener",
32
+ "prependListener",
33
+ "off",
34
+ "removeListener",
35
+ "removeAllListeners",
36
+ "emit",
37
+ "nextTick",
38
+ ] as const;
17
39
 
18
40
  const exports: ModuleExports = Object.fromEntries(
19
41
  methods.map((method) => [method, { namespace: "process", method }]),
@@ -22,7 +44,10 @@ const exports: ModuleExports = Object.fromEntries(
22
44
  export const processModule: NodeModule = {
23
45
  name: "process",
24
46
  namespace: "process",
25
- runtimeSources: () => [resolveFrom(import.meta.url, "../../../../runtime/ext_node/process/process.c")],
47
+ runtimeSources: () => [
48
+ resolveFrom(import.meta.url, "../../../../runtime/ext_node/process/process.c"),
49
+ resolveFrom(import.meta.url, "../../../../runtime/ext_node/process/streams.c"),
50
+ ],
26
51
  builtins: () => ({}),
27
52
  exports: () => exports,
28
53
  };
@@ -63,6 +63,13 @@ export interface ModuleExport {
63
63
  * used as `new EventEmitter()`.
64
64
  */
65
65
  readonly isConstructor?: boolean;
66
+ /**
67
+ * Runtime getter `(i32, i64*)` returning the prototype object a constructor
68
+ * *value* carries, so `class X extends EventEmitter` chains onto
69
+ * `EventEmitter.prototype` instead of a fresh plain object. Only meaningful
70
+ * together with `isConstructor`.
71
+ */
72
+ readonly prototypeSymbol?: string;
66
73
  }
67
74
 
68
75
  export type ModuleExports = Readonly<Record<string, ModuleExport>>;
@@ -173,6 +180,16 @@ export interface LinkFeatureDeclaration {
173
180
  readonly extension: string;
174
181
  }
175
182
 
183
+ /**
184
+ * A lookup table keyed by names the program writes. A plain object literal
185
+ * inherits `Object.prototype`, so `table["toString"]` would answer with
186
+ * `Object.prototype.toString` and the generator would emit that function's text
187
+ * as an LLVM symbol.
188
+ */
189
+ function lookupTable<T>(): Record<string, T> {
190
+ return Object.create(null) as Record<string, T>;
191
+ }
192
+
176
193
  export class ExtensionRegistry {
177
194
  private readonly extensions = new Map<string, Extension>();
178
195
  /**
@@ -259,7 +276,7 @@ export class ExtensionRegistry {
259
276
 
260
277
  /** Flatten every registered builtin into one lookup table. */
261
278
  builtins(): Readonly<Record<string, BuiltinFunction>> {
262
- const merged: Record<string, BuiltinFunction> = {};
279
+ const merged = lookupTable<BuiltinFunction>();
263
280
  for (const extension of this.extensions.values()) {
264
281
  const builtins = extension.builtins?.();
265
282
  if (builtins) Object.assign(merged, builtins);
@@ -269,7 +286,7 @@ export class ExtensionRegistry {
269
286
 
270
287
  /** Flatten every registered extension's global values into one lookup. */
271
288
  globalValues(): Readonly<Record<string, GlobalValue>> {
272
- const merged: Record<string, GlobalValue> = {};
289
+ const merged = lookupTable<GlobalValue>();
273
290
  for (const extension of this.extensions.values()) {
274
291
  const values = extension.globalValues?.();
275
292
  if (values) Object.assign(merged, values);
@@ -279,7 +296,7 @@ export class ExtensionRegistry {
279
296
 
280
297
  /** Flatten every registered extension's importable modules. */
281
298
  modules(): Readonly<Record<string, ExtensionModule>> {
282
- const merged: Record<string, ExtensionModule> = {};
299
+ const merged = lookupTable<ExtensionModule>();
283
300
  for (const extension of this.extensions.values()) {
284
301
  const modules = extension.modules?.();
285
302
  if (!modules) continue;
@@ -300,7 +317,7 @@ export class ExtensionRegistry {
300
317
  * extension (mirroring `modules()`).
301
318
  */
302
319
  assetLoaders(): Readonly<Record<string, AssetLoader>> {
303
- const merged: Record<string, AssetLoader> = {};
320
+ const merged = lookupTable<AssetLoader>();
304
321
  for (const extension of this.extensions.values()) {
305
322
  const loaders = extension.assetLoaders?.();
306
323
  if (loaders) Object.assign(merged, loaders);
@@ -20,7 +20,7 @@ import { punctuationMethods, type PunctuationMethods } from "./scanner/punctuati
20
20
  export class Scanner {
21
21
  text: string;
22
22
  pos = 0;
23
- readonly diagnostics: DiagnosticBag;
23
+ diagnostics: DiagnosticBag;
24
24
  readonly source: SourceFile;
25
25
  _token: Token = {
26
26
  kind: TokenKind.EndOfFile,
@@ -54,6 +54,21 @@ export class Scanner {
54
54
  this.pos = Math.max(0, Math.min(offset, this.text.length));
55
55
  }
56
56
 
57
+ /**
58
+ * Run `scan` with diagnostics collected into `sink` instead of the main bag.
59
+ * The parser uses this for lookahead: tokens read only to decide how to
60
+ * parse must not report errors, because the parser may discard them.
61
+ */
62
+ withDiagnostics<T>(sink: DiagnosticBag, scan: () => T): T {
63
+ const saved = this.diagnostics;
64
+ this.diagnostics = sink;
65
+ try {
66
+ return scan();
67
+ } finally {
68
+ this.diagnostics = saved;
69
+ }
70
+ }
71
+
57
72
  /** Advance to the next token. */
58
73
  nextToken(): Token {
59
74
  this._token = this.scan();
@@ -7,7 +7,7 @@
7
7
  * needs.
8
8
  */
9
9
 
10
- import { DiagnosticBag, DiagnosticCode } from "../diagnostics/diagnostic.js";
10
+ import { DiagnosticBag, DiagnosticCode, type Diagnostic } from "../diagnostics/diagnostic.js";
11
11
  import type { SourceFile } from "../diagnostics/source.js";
12
12
  import { Scanner } from "../lexer/scanner.js";
13
13
  import { isIdentifierNameToken, isKeywordKind, Token, TokenKind } from "../lexer/token.js";
@@ -56,6 +56,10 @@ export class ParserContext {
56
56
  * current token so a template that was only seen by lookahead stays coherent.
57
57
  */
58
58
  private readonly templateStates = new WeakMap<Token, TemplateState>();
59
+ /** Side bag that receives the diagnostics of lookahead-only scans. */
60
+ private readonly lookaheadSink = new DiagnosticBag();
61
+ /** Scan errors held back until the parser reaches the token that produced them. */
62
+ private readonly deferredTokenDiagnostics = new WeakMap<Token, Diagnostic[]>();
59
63
 
60
64
  constructor(source: SourceFile, diagnostics: DiagnosticBag) {
61
65
  this.source = source;
@@ -85,7 +89,7 @@ export class ParserContext {
85
89
 
86
90
  lookAhead(k: number): Token {
87
91
  while (this.tokens.length <= this.index + k) {
88
- this.tokens.push(this.scanToken());
92
+ this.tokens.push(this.scanToken(true));
89
93
  }
90
94
  return this.tokens[this.index + k]!;
91
95
  }
@@ -99,9 +103,21 @@ export class ParserContext {
99
103
  this.tokens = [this.scanToken()];
100
104
  this.index = 0;
101
105
  }
106
+ this.reportDeferred(this.tokens[this.index]!);
102
107
  return current;
103
108
  }
104
109
 
110
+ /**
111
+ * Report the scan errors held back for `token` once the parser actually
112
+ * reaches it (see {@link scanToken}).
113
+ */
114
+ private reportDeferred(token: Token): void {
115
+ const pending = this.deferredTokenDiagnostics.get(token);
116
+ if (pending === undefined) return;
117
+ this.deferredTokenDiagnostics.delete(token);
118
+ for (const diagnostic of pending) this.diagnostics.add(diagnostic);
119
+ }
120
+
105
121
  /**
106
122
  * Scan the next token, keeping a template literal together. A `${ … }`
107
123
  * substitution is ordinary token stream (so the parser can parse the
@@ -111,7 +127,27 @@ export class ParserContext {
111
127
  * lookahead - and `findMatchingParen` would then count characters that are
112
128
  * part of the literal.
113
129
  */
114
- private scanToken(): Token {
130
+ private scanToken(quiet = false): Token {
131
+ if (!quiet) return this.scanTokenBody();
132
+ /* Lookahead may read source the parser never consumes, and it may read it
133
+ * with the wrong `/` interpretation (a regex is scanned as a division
134
+ * because the previous token ended an expression, as in
135
+ * `for (;;) /re/.test(x)`). Collect scan errors in a side bag and attach
136
+ * them to the scanned token: they are reported only if the parser reaches
137
+ * that token, and dropped when the token is discarded by `rescanFrom`. */
138
+ const mark = this.lookaheadSink.mark();
139
+ const token = this.scanner.withDiagnostics(this.lookaheadSink, () => this.scanTokenBody());
140
+ const produced = this.lookaheadSink.diagnostics.slice(mark);
141
+ if (produced.length > 0) {
142
+ this.lookaheadSink.reset(mark);
143
+ const pending = this.deferredTokenDiagnostics.get(token);
144
+ if (pending === undefined) this.deferredTokenDiagnostics.set(token, produced);
145
+ else pending.push(...produced);
146
+ }
147
+ return token;
148
+ }
149
+
150
+ private scanTokenBody(): Token {
115
151
  let token: Token;
116
152
  if (this.pendingTemplateOffset !== undefined) {
117
153
  const offset = this.pendingTemplateOffset;
@@ -198,7 +198,10 @@ export const literalMethods: LiteralMethods = {
198
198
  flags |= NodeFlags.Generator;
199
199
  }
200
200
  let name: Identifier | undefined;
201
- if (this.at(TokenKind.Identifier)) name = this.parseIdentifier();
201
+ /* A function *expression* may be named after a contextual keyword:
202
+ * `(function get() {})`, `(function constructor() {})`, `(function type() {})`.
203
+ * Only hard-reserved words are excluded, which `isIdentifierLike` decides. */
204
+ if (this.isIdentifierLike(this.token)) name = this.parseIdentifier();
202
205
  const typeParameters = this.parseTypeParameters();
203
206
  const parameters = this.parseParameters();
204
207
  let returnType: TypeNode | undefined;
@@ -213,7 +216,8 @@ export const literalMethods: LiteralMethods = {
213
216
  parseClassExpression(this: Parser): ClassExpression {
214
217
  const start = this.parseExpected(TokenKind.ClassKeyword).start;
215
218
  let name: Identifier | undefined;
216
- if (this.at(TokenKind.Identifier)) name = this.parseIdentifier();
219
+ /* `(class type {})` — same contextual-keyword allowance as a function name. */
220
+ if (this.isIdentifierLike(this.token)) name = this.parseIdentifier();
217
221
  const typeParameters = this.parseTypeParameters();
218
222
  const heritage = this.parseHeritageClauses();
219
223
  const members = this.parseClassMembers();