xbintsc 0.6.24 → 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 (224) 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/graph.js +9 -5
  11. package/dist/src/driver/bundler/graph.js.map +1 -1
  12. package/dist/src/driver/fingerprint.d.ts +2 -2
  13. package/dist/src/driver/fingerprint.js +3 -3
  14. package/dist/src/driver/fingerprint.js.map +1 -1
  15. package/dist/src/driver/link-features.js +1 -1
  16. package/dist/src/driver/link-features.js.map +1 -1
  17. package/dist/src/driver/paths.js +8 -4
  18. package/dist/src/driver/paths.js.map +1 -1
  19. package/dist/src/driver/runtime-objects.d.ts +1 -1
  20. package/dist/src/driver/runtime-objects.js +13 -1
  21. package/dist/src/driver/runtime-objects.js.map +1 -1
  22. package/dist/src/driver/version.d.ts +1 -1
  23. package/dist/src/driver/version.js +1 -1
  24. package/dist/src/extensions/catalog.js +5 -1
  25. package/dist/src/extensions/catalog.js.map +1 -1
  26. package/dist/src/extensions/gui/index.js +0 -1
  27. package/dist/src/extensions/gui/index.js.map +1 -1
  28. package/dist/src/extensions/link-feature.d.ts +8 -0
  29. package/dist/src/extensions/node/stream-web/index.d.ts +6 -1
  30. package/dist/src/extensions/node/stream-web/index.js +59 -4
  31. package/dist/src/extensions/node/stream-web/index.js.map +1 -1
  32. package/dist/src/extensions/node/worker_threads/index.d.ts +14 -4
  33. package/dist/src/extensions/node/worker_threads/index.js +24 -11
  34. package/dist/src/extensions/node/worker_threads/index.js.map +1 -1
  35. package/dist/src/extensions/registry.d.ts +9 -1
  36. package/dist/src/extensions/registry.js +4 -1
  37. package/dist/src/extensions/registry.js.map +1 -1
  38. package/dist/src/extensions/wasm-asset.d.ts +16 -0
  39. package/dist/src/extensions/wasm-asset.js +28 -0
  40. package/dist/src/extensions/wasm-asset.js.map +1 -0
  41. package/dist/src/extensions/wasm.d.ts +17 -0
  42. package/dist/src/extensions/wasm.js +68 -0
  43. package/dist/src/extensions/wasm.js.map +1 -0
  44. package/dist/src/extensions/webworker/index.d.ts +24 -0
  45. package/dist/src/extensions/webworker/index.js +54 -0
  46. package/dist/src/extensions/webworker/index.js.map +1 -0
  47. package/dist/src/index.d.ts +1 -0
  48. package/dist/src/index.js +1 -0
  49. package/dist/src/index.js.map +1 -1
  50. package/dist/tests/driver/link-features.test.js +9 -0
  51. package/dist/tests/driver/link-features.test.js.map +1 -1
  52. package/dist/tests/e2e/async-fs.test.js +44 -0
  53. package/dist/tests/e2e/async-fs.test.js.map +1 -1
  54. package/dist/tests/e2e/harness.d.ts +6 -0
  55. package/dist/tests/e2e/harness.js +13 -1
  56. package/dist/tests/e2e/harness.js.map +1 -1
  57. package/dist/tests/e2e/node-crypto-random.test.d.ts +8 -0
  58. package/dist/tests/e2e/node-crypto-random.test.js +148 -0
  59. package/dist/tests/e2e/node-crypto-random.test.js.map +1 -0
  60. package/dist/tests/e2e/node-modules.test.js +1 -1
  61. package/dist/tests/e2e/node-modules.test.js.map +1 -1
  62. package/dist/tests/e2e/web-streams.test.d.ts +8 -0
  63. package/dist/tests/e2e/web-streams.test.js +274 -0
  64. package/dist/tests/e2e/web-streams.test.js.map +1 -0
  65. package/dist/tests/e2e/web-worker.test.d.ts +7 -0
  66. package/dist/tests/e2e/web-worker.test.js +74 -0
  67. package/dist/tests/e2e/web-worker.test.js.map +1 -0
  68. package/dist/tests/e2e/webassembly.test.d.ts +8 -0
  69. package/dist/tests/e2e/webassembly.test.js +113 -0
  70. package/dist/tests/e2e/webassembly.test.js.map +1 -0
  71. package/dist/tests/e2e/worker-api.test.d.ts +14 -0
  72. package/dist/tests/e2e/worker-api.test.js +460 -0
  73. package/dist/tests/e2e/worker-api.test.js.map +1 -0
  74. package/dist/tests/e2e/worker-threads.test.d.ts +8 -0
  75. package/dist/tests/e2e/worker-threads.test.js +160 -0
  76. package/dist/tests/e2e/worker-threads.test.js.map +1 -0
  77. package/dist/tests/extensions/wasm.test.d.ts +1 -0
  78. package/dist/tests/extensions/wasm.test.js +53 -0
  79. package/dist/tests/extensions/wasm.test.js.map +1 -0
  80. package/doc/ai/cli.md +1 -1
  81. package/doc/ai/contributing.md +11 -4
  82. package/doc/ai/extensions.md +25 -2
  83. package/doc/ai/language-support.md +14 -2
  84. package/doc/ai/zh-CN/cli.md +1 -1
  85. package/doc/ai/zh-CN/contributing.md +8 -3
  86. package/doc/ai/zh-CN/extensions.md +23 -2
  87. package/doc/ai/zh-CN/language-support.md +13 -2
  88. package/doc/gui/api.md +1 -1
  89. package/doc/implemented/cli.md +1 -1
  90. package/doc/implemented/extensions.md +6 -5
  91. package/doc/implemented/node/stream-web.md +31 -0
  92. package/doc/implemented/node/worker-threads.md +88 -5
  93. package/doc/implemented/node.md +2 -1
  94. package/doc/implemented/streams.md +82 -0
  95. package/doc/implemented/wasm.md +80 -18
  96. package/doc/implemented/web-worker.md +84 -0
  97. package/doc/implemented.md +6 -2
  98. package/doc/unimplemented/deviations.md +1 -0
  99. package/doc/unimplemented/node/worker-threads.md +23 -0
  100. package/doc/unimplemented/node.md +1 -0
  101. package/doc/unimplemented/stdlib.md +2 -1
  102. package/doc/unimplemented/wasm.md +21 -9
  103. package/doc/unimplemented.md +1 -1
  104. package/doc/zh-CN/gui/api.md +1 -1
  105. package/doc/zh-CN/implemented/cli.md +1 -1
  106. package/doc/zh-CN/implemented/extensions.md +7 -6
  107. package/doc/zh-CN/implemented/node/stream-web.md +24 -0
  108. package/doc/zh-CN/implemented/node/worker-threads.md +77 -4
  109. package/doc/zh-CN/implemented/node.md +2 -1
  110. package/doc/zh-CN/implemented/streams.md +60 -0
  111. package/doc/zh-CN/implemented/wasm.md +73 -16
  112. package/doc/zh-CN/implemented/web-worker.md +79 -0
  113. package/doc/zh-CN/implemented.md +6 -2
  114. package/doc/zh-CN/unimplemented/deviations.md +1 -0
  115. package/doc/zh-CN/unimplemented/node/worker-threads.md +20 -0
  116. package/doc/zh-CN/unimplemented/node.md +1 -0
  117. package/doc/zh-CN/unimplemented/stdlib.md +2 -1
  118. package/doc/zh-CN/unimplemented/wasm.md +16 -8
  119. package/doc/zh-CN/unimplemented.md +1 -1
  120. package/llms.txt +2 -0
  121. package/package.json +8 -7
  122. package/runtime/ext_gui/browser.cpp +1 -1
  123. package/runtime/ext_node/stream_web/stream_web.c +20 -6
  124. package/runtime/worker/worker_api.c +422 -0
  125. package/runtime/worker/worker_codec.c +489 -0
  126. package/runtime/worker/worker_core.c +445 -0
  127. package/runtime/worker/worker_internal.h +223 -0
  128. package/runtime/worker/worker_platform.c +344 -0
  129. package/runtime/worker/worker_ports.c +203 -0
  130. package/runtime/worker/worker_transport.c +464 -0
  131. package/runtime/worker/worker_wire.c +150 -0
  132. package/runtime/xt_builtins.c +15 -0
  133. package/runtime/xt_constructors.c +49 -2
  134. package/runtime/xt_stdlib2/error.inc +4 -0
  135. package/runtime/xt_streams/common.inc +232 -0
  136. package/runtime/xt_streams/constructors.inc +232 -0
  137. package/runtime/xt_streams/encoding.inc +124 -0
  138. package/runtime/xt_streams/readable-extra.inc +372 -0
  139. package/runtime/xt_streams/readable.inc +415 -0
  140. package/runtime/xt_streams/transform.inc +195 -0
  141. package/runtime/xt_streams/writable.inc +542 -0
  142. package/runtime/xt_streams.c +30 -0
  143. package/runtime/xt_wasm/wasm.h +148 -0
  144. package/runtime/xt_wasm/wasm_api.c +357 -0
  145. package/runtime/xt_wasm/wasm_api.h +37 -0
  146. package/runtime/xt_wasm/wasm_bridge.c +30 -0
  147. package/runtime/xt_wasm/wasm_instance.c +591 -0
  148. package/runtime/xt_wasm/wasm_module.c +270 -0
  149. package/runtime/xt_wasm/wasm_values.c +177 -0
  150. package/runtime/xt_webassembly.c +15 -4
  151. package/scripts/fetch-vendored.ts +187 -20
  152. package/scripts/package-release.ts +3 -2
  153. package/src/cli/extensions.ts +11 -3
  154. package/src/cli/help.ts +1 -1
  155. package/src/codegen/generator/state.ts +7 -0
  156. package/src/codegen/generator/tables.ts +19 -1
  157. package/src/driver/bundler/graph.ts +9 -5
  158. package/src/driver/fingerprint.ts +3 -3
  159. package/src/driver/link-features.ts +1 -1
  160. package/src/driver/paths.ts +8 -4
  161. package/src/driver/runtime-objects.ts +13 -1
  162. package/src/driver/version.ts +1 -1
  163. package/src/extensions/catalog.ts +5 -1
  164. package/src/extensions/gui/index.ts +0 -1
  165. package/src/extensions/link-feature.ts +8 -0
  166. package/src/extensions/node/stream-web/index.ts +59 -4
  167. package/src/extensions/node/worker_threads/index.ts +24 -11
  168. package/src/extensions/registry.ts +13 -2
  169. package/src/extensions/wasm-asset.ts +32 -0
  170. package/src/extensions/wasm.ts +74 -0
  171. package/src/extensions/webworker/index.ts +58 -0
  172. package/src/index.ts +1 -0
  173. package/vendor/wasm3/.pin +1 -0
  174. package/vendor/wasm3/LICENSE +21 -0
  175. package/vendor/wasm3/m3_api_libc.c +265 -0
  176. package/vendor/wasm3/m3_api_libc.h +19 -0
  177. package/vendor/wasm3/m3_api_meta_wasi.c +1107 -0
  178. package/vendor/wasm3/m3_api_tracer.c +173 -0
  179. package/vendor/wasm3/m3_api_tracer.h +19 -0
  180. package/vendor/wasm3/m3_api_uvwasi.c +1290 -0
  181. package/vendor/wasm3/m3_api_wasi.c +1207 -0
  182. package/vendor/wasm3/m3_api_wasi.h +74 -0
  183. package/vendor/wasm3/m3_api_wasi_host.h +137 -0
  184. package/vendor/wasm3/m3_api_wasi_posix.h +979 -0
  185. package/vendor/wasm3/m3_api_wasi_win32.h +1093 -0
  186. package/vendor/wasm3/m3_atomic.h +114 -0
  187. package/vendor/wasm3/m3_bind.c +174 -0
  188. package/vendor/wasm3/m3_bind.h +20 -0
  189. package/vendor/wasm3/m3_code.c +232 -0
  190. package/vendor/wasm3/m3_code.h +74 -0
  191. package/vendor/wasm3/m3_compile.c +6603 -0
  192. package/vendor/wasm3/m3_compile.h +354 -0
  193. package/vendor/wasm3/m3_config.h +448 -0
  194. package/vendor/wasm3/m3_config_platforms.h +345 -0
  195. package/vendor/wasm3/m3_core.c +1034 -0
  196. package/vendor/wasm3/m3_core.h +526 -0
  197. package/vendor/wasm3/m3_deterministic.c +65 -0
  198. package/vendor/wasm3/m3_deterministic.h +59 -0
  199. package/vendor/wasm3/m3_env.c +3146 -0
  200. package/vendor/wasm3/m3_env.h +748 -0
  201. package/vendor/wasm3/m3_exception.h +33 -0
  202. package/vendor/wasm3/m3_exec.c +8 -0
  203. package/vendor/wasm3/m3_exec.h +4110 -0
  204. package/vendor/wasm3/m3_exec_atomic.h +146 -0
  205. package/vendor/wasm3/m3_exec_defs.h +76 -0
  206. package/vendor/wasm3/m3_function.c +278 -0
  207. package/vendor/wasm3/m3_function.h +244 -0
  208. package/vendor/wasm3/m3_host.h +226 -0
  209. package/vendor/wasm3/m3_host_none.h +76 -0
  210. package/vendor/wasm3/m3_host_posix.h +543 -0
  211. package/vendor/wasm3/m3_host_win32.h +253 -0
  212. package/vendor/wasm3/m3_info.c +616 -0
  213. package/vendor/wasm3/m3_info.h +38 -0
  214. package/vendor/wasm3/m3_math_utils.h +560 -0
  215. package/vendor/wasm3/m3_module.c +431 -0
  216. package/vendor/wasm3/m3_parse.c +1549 -0
  217. package/vendor/wasm3/m3_snapshot.c +3825 -0
  218. package/vendor/wasm3/m3_validate.c +1770 -0
  219. package/vendor/wasm3/m3_validate.h +25 -0
  220. package/vendor/wasm3/m3_wasi.h +10 -0
  221. package/vendor/wasm3/m3_xxh64.c +211 -0
  222. package/vendor/wasm3/wasm3.h +684 -0
  223. package/vendor/wasm3/wasm3_defs.h +350 -0
  224. package/runtime/ext_node/worker_threads/worker_threads.c +0 -214
@@ -14,9 +14,10 @@
14
14
  * vendor/nanosvg/nanosvg.h, nanosvgrast.h
15
15
  * vendor/miniz/… the `compression` link feature
16
16
  * vendor/sqlite3/sqlite3.{c,h} the `storage` link feature
17
+ * vendor/wasm3/… the `wasm` link feature
17
18
  *
18
19
  * `build-gui.ts` fetches just `stb`/`nanosvg`; `npm test` fetches everything.
19
- * CI caches the four directories keyed on this script.
20
+ * CI caches the fetched directories keyed on this script.
20
21
  *
21
22
  * Note: miniz's upstream tree has no `miniz_export.h` (its CMake build generates
22
23
  * one). The static-only shim below is written here rather than tracked.
@@ -24,22 +25,22 @@
24
25
  * Run via `npm run vendor` (or `tsx scripts/fetch-vendored.ts`).
25
26
  */
26
27
 
27
- import { spawnSync } from "node:child_process";
28
28
  import { createHash } from "node:crypto";
29
- import { copyFileSync, existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
30
- import { basename, dirname, join, resolve } from "node:path";
29
+ import { copyFileSync, existsSync, mkdirSync, readFileSync, readdirSync, rmSync, writeFileSync } from "node:fs";
30
+ import { basename, dirname, join, resolve, sep } from "node:path";
31
31
  import { fileURLToPath, pathToFileURL } from "node:url";
32
+ import { gunzipSync, inflateRawSync } from "node:zlib";
32
33
  import { downloadTarball } from "./fetch-tarball.js";
33
34
 
34
35
  const root = resolve(dirname(fileURLToPath(import.meta.url)), "..");
35
36
  export const vendorDir = join(root, "vendor");
36
37
 
37
38
  /** The vendored packages, each a directory under `vendor/`. */
38
- export const VENDOR_PACKAGES = ["stb", "nanosvg", "miniz", "sqlite3"] as const;
39
+ export const VENDOR_PACKAGES = ["stb", "nanosvg", "miniz", "sqlite3", "wasm3"] as const;
39
40
  export type VendorPackage = (typeof VENDOR_PACKAGES)[number];
40
41
 
41
42
  interface VendoredFile {
42
- readonly package: Exclude<VendorPackage, "sqlite3">;
43
+ readonly package: Exclude<VendorPackage, "sqlite3" | "wasm3">;
43
44
  readonly name: string;
44
45
  readonly url: string;
45
46
  readonly sha256: string;
@@ -122,6 +123,15 @@ const SQLITE_FILES = [
122
123
  },
123
124
  ] as const;
124
125
 
126
+ /* wasm3's interpreter is fetched as one commit archive: its `source/` tree is
127
+ * small but the headers and the `.c` files have to stay in lockstep, so a
128
+ * single pinned tarball is the reviewable unit. Only `source/` and the license
129
+ * are copied out; the archive's tools, tests and examples are not compiled. */
130
+ const WASM3_COMMIT = "28ecb9af6d2040e474a70f7cb7f43666740141fb";
131
+ const WASM3_TARBALL_URL = `https://github.com/wasm3/wasm3/archive/${WASM3_COMMIT}.tar.gz`;
132
+ const WASM3_TARBALL_SHA256 = "5ecbb8851bd9acb54a83be10f0536167cb0da13b007406578761672b0bd94b18";
133
+ const WASM3_PIN = `${WASM3_COMMIT} ${WASM3_TARBALL_SHA256}\n`;
134
+
125
135
  function sha256(file: string): string {
126
136
  return createHash("sha256").update(readFileSync(file)).digest("hex");
127
137
  }
@@ -154,22 +164,68 @@ function ensureMinizExportHeader(): void {
154
164
  writeFileSync(target, MINIZ_EXPORT_H);
155
165
  }
156
166
 
157
- /** Extract a zip archive; Windows' bsdtar and POSIX's unzip cover the hosts. */
167
+ /** A 32-bit little-endian field of a zip record. */
168
+ function zipU32(bytes: Uint8Array, offset: number): number {
169
+ return new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength).getUint32(offset, true);
170
+ }
171
+
172
+ /** A 16-bit little-endian field of a zip record. */
173
+ function zipU16(bytes: Uint8Array, offset: number): number {
174
+ return new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength).getUint16(offset, true);
175
+ }
176
+
177
+ /**
178
+ * Extract a zip archive in Node.
179
+ *
180
+ * An external `unzip`/`tar` is not guaranteed on every runner image, and a CI
181
+ * cache miss (for example after editing this script) must not turn a missing
182
+ * shell tool into a failed job. The central directory is the authoritative
183
+ * index, so it is read instead of walking local headers; stored and deflated
184
+ * entries cover the pinned SQLite amalgamation. Contents are pinned by SHA-256
185
+ * after extraction, which is why the CRC is not rechecked here.
186
+ */
158
187
  function extractZip(zip: string, destination: string): void {
159
- const attempts: readonly (readonly [string, readonly string[]])[] =
160
- process.platform === "win32"
161
- ? [
162
- ["tar", ["-xf", zip, "-C", destination]],
163
- ["unzip", ["-q", "-o", zip, "-d", destination]],
164
- ]
165
- : [
166
- ["unzip", ["-q", "-o", zip, "-d", destination]],
167
- ["tar", ["-xf", zip, "-C", destination]],
168
- ];
169
- for (const [command, args] of attempts) {
170
- if (spawnSync(command, args, { stdio: "ignore" }).status === 0) return;
188
+ const archive = readFileSync(zip);
189
+ const SIGNATURE_EOCD = 0x06054b50;
190
+ const SIGNATURE_CENTRAL = 0x02014b50;
191
+ let eocd = -1;
192
+ for (let offset = archive.length - 22; offset >= 0 && offset >= archive.length - 65557; offset--) {
193
+ if (zipU32(archive, offset) === SIGNATURE_EOCD) {
194
+ eocd = offset;
195
+ break;
196
+ }
197
+ }
198
+ if (eocd < 0) throw new Error(`${basename(zip)}: not a zip archive`);
199
+ const count = zipU16(archive, eocd + 10);
200
+ let offset = zipU32(archive, eocd + 16);
201
+ for (let index = 0; index < count; index++) {
202
+ if (zipU32(archive, offset) !== SIGNATURE_CENTRAL) {
203
+ throw new Error(`${basename(zip)}: corrupt central directory`);
204
+ }
205
+ const method = zipU16(archive, offset + 10);
206
+ const compressedSize = zipU32(archive, offset + 20);
207
+ const nameLength = zipU16(archive, offset + 28);
208
+ const extraLength = zipU16(archive, offset + 30);
209
+ const commentLength = zipU16(archive, offset + 32);
210
+ const localOffset = zipU32(archive, offset + 42);
211
+ const name = Buffer.from(archive.subarray(offset + 46, offset + 46 + nameLength)).toString("utf8");
212
+ offset += 46 + nameLength + extraLength + commentLength;
213
+ if (name.endsWith("/")) continue;
214
+ const localNameLength = zipU16(archive, localOffset + 26);
215
+ const localExtraLength = zipU16(archive, localOffset + 28);
216
+ const dataStart = localOffset + 30 + localNameLength + localExtraLength;
217
+ const payload = archive.subarray(dataStart, dataStart + compressedSize);
218
+ let content: Buffer;
219
+ if (method === 0) content = Buffer.from(payload);
220
+ else if (method === 8) content = inflateRawSync(payload);
221
+ else throw new Error(`${basename(zip)}: unsupported compression method ${method}`);
222
+ const target = resolve(destination, name);
223
+ if (target !== destination && !target.startsWith(`${destination}${sep}`)) {
224
+ throw new Error(`refusing to extract ${name} outside ${destination}`);
225
+ }
226
+ mkdirSync(dirname(target), { recursive: true });
227
+ writeFileSync(target, content);
171
228
  }
172
- throw new Error(`cannot extract ${basename(zip)}; install unzip (or a tar that reads zip archives)`);
173
229
  }
174
230
 
175
231
  /** Fetch the pinned SQLite amalgamation and copy out its two sources. */
@@ -213,6 +269,113 @@ function ensureSqlite(): void {
213
269
  rmSync(zip, { force: true });
214
270
  }
215
271
 
272
+ /** Read a NUL/space-padded text field out of a tar block. */
273
+ function tarText(bytes: Uint8Array, start: number, length: number): string {
274
+ let end = start;
275
+ while (end < start + length && bytes[end] !== 0) end++;
276
+ return Buffer.from(bytes.subarray(start, end)).toString("utf8").replace(/\s+$/, "");
277
+ }
278
+
279
+ /** Read an octal field (tar pads it with spaces and/or NULs). */
280
+ function tarSize(bytes: Uint8Array, start: number, length: number): number {
281
+ const text = tarText(bytes, start, length);
282
+ return text.length === 0 ? 0 : parseInt(text, 8);
283
+ }
284
+
285
+ /**
286
+ * Extract selected members of a gzip-compressed tarball.
287
+ *
288
+ * An external `tar` is not reliable here: an older bsdtar on the Windows
289
+ * runners rejects this archive, and a runner image may not ship `tar` at all.
290
+ * Node's gzip decoder plus a minimal ustar reader keeps a cache miss working —
291
+ * the GitHub archive is plain ustar. Only regular files under the named members
292
+ * are written; directories, symlinks and the other repository trees are
293
+ * skipped (which is also why the wasm3 archive's platform symlinks cannot
294
+ * break extraction).
295
+ */
296
+ function extractTarball(tarball: string, destination: string, members: readonly string[]): void {
297
+ const archive = gunzipSync(readFileSync(tarball));
298
+ const wanted = (path: string): boolean =>
299
+ members.some((member) => path === member || path.startsWith(`${member}/`));
300
+ let offset = 0;
301
+ while (offset + 512 <= archive.length) {
302
+ const header = archive.subarray(offset, offset + 512);
303
+ /* Two zero blocks mark the end of the archive; one is enough to stop. */
304
+ if (header.every((byte) => byte === 0)) return;
305
+ const name = tarText(header, 0, 100);
306
+ const prefix = tarText(header, 345, 155);
307
+ const type = header[156] === 0 ? "0" : String.fromCharCode(header[156]);
308
+ const size = tarSize(header, 124, 12);
309
+ const path = prefix.length > 0 ? `${prefix}/${name}` : name;
310
+ const dataStart = offset + 512;
311
+ const next = dataStart + Math.ceil(size / 512) * 512;
312
+ if ((type === "0" || type === "7") && wanted(path)) {
313
+ const target = resolve(destination, path);
314
+ if (target !== destination && !target.startsWith(`${destination}${sep}`)) {
315
+ throw new Error(`refusing to extract ${path} outside ${destination}`);
316
+ }
317
+ mkdirSync(dirname(target), { recursive: true });
318
+ writeFileSync(target, archive.subarray(dataStart, dataStart + size));
319
+ }
320
+ offset = next;
321
+ }
322
+ throw new Error(`${basename(tarball)}: truncated tar archive`);
323
+ }
324
+
325
+ /**
326
+ * Fetch the pinned wasm3 archive and copy its interpreter sources into
327
+ * `vendor/wasm3`. A `.pin` marker records the commit and tarball hash, so an
328
+ * already-fetched tree is reused without touching the network and a changed
329
+ * pin re-extracts instead of silently mixing revisions.
330
+ */
331
+ function ensureWasm3(): void {
332
+ const directory = join(vendorDir, "wasm3");
333
+ const pin = join(directory, ".pin");
334
+ if (
335
+ existsSync(join(directory, "wasm3.h")) &&
336
+ existsSync(pin) &&
337
+ readFileSync(pin, "utf8").replace(/\r\n/g, "\n") === WASM3_PIN
338
+ ) {
339
+ return;
340
+ }
341
+
342
+ const cache = join(vendorDir, ".cache");
343
+ mkdirSync(cache, { recursive: true });
344
+ const tarball = join(cache, `wasm3-${WASM3_COMMIT}.tar.gz`);
345
+ if (!isPinned(tarball, WASM3_TARBALL_SHA256)) {
346
+ rmSync(tarball, { force: true });
347
+ downloadTarball(tarball, WASM3_TARBALL_URL, `wasm3-${WASM3_COMMIT}.tar.gz`);
348
+ if (!isPinned(tarball, WASM3_TARBALL_SHA256)) {
349
+ const actual = sha256(tarball);
350
+ rmSync(tarball, { force: true });
351
+ throw new Error(`wasm3-${WASM3_COMMIT}.tar.gz: expected sha256 ${WASM3_TARBALL_SHA256}, got ${actual}`);
352
+ }
353
+ }
354
+
355
+ const extracted = join(cache, `wasm3-${WASM3_COMMIT}`);
356
+ rmSync(extracted, { recursive: true, force: true });
357
+ mkdirSync(extracted, { recursive: true });
358
+ const archiveRoot = `wasm3-${WASM3_COMMIT}`;
359
+ extractTarball(tarball, extracted, [`${archiveRoot}/source`, `${archiveRoot}/LICENSE`]);
360
+ const sourceDir = join(extracted, archiveRoot, "source");
361
+ if (!existsSync(join(sourceDir, "wasm3.h"))) {
362
+ throw new Error(`expected ${archiveRoot}/source/wasm3.h after extracting the archive`);
363
+ }
364
+
365
+ rmSync(directory, { recursive: true, force: true });
366
+ mkdirSync(directory, { recursive: true });
367
+ for (const entry of readdirSync(sourceDir, { withFileTypes: true })) {
368
+ if (!entry.isFile()) continue;
369
+ if (!entry.name.endsWith(".c") && !entry.name.endsWith(".h")) continue;
370
+ copyFileSync(join(sourceDir, entry.name), join(directory, entry.name));
371
+ }
372
+ const license = join(extracted, `wasm3-${WASM3_COMMIT}`, "LICENSE");
373
+ if (existsSync(license)) copyFileSync(license, join(directory, "LICENSE"));
374
+ writeFileSync(pin, WASM3_PIN);
375
+ rmSync(extracted, { recursive: true, force: true });
376
+ rmSync(tarball, { force: true });
377
+ }
378
+
216
379
  /**
217
380
  * Ensure the requested vendored packages exist and match their pins.
218
381
  * Downloads only what is missing; everything else is a content check.
@@ -223,6 +386,10 @@ export function ensureVendoredSources(packages: readonly VendorPackage[] = VENDO
223
386
  ensureSqlite();
224
387
  continue;
225
388
  }
389
+ if (name === "wasm3") {
390
+ ensureWasm3();
391
+ continue;
392
+ }
226
393
  for (const file of FILES) {
227
394
  if (file.package === name) ensureFile(file);
228
395
  }
@@ -110,8 +110,9 @@ cpSync(findRuntimeDir(), join(stage, "runtime"), { recursive: true });
110
110
  console.log("xbintsc: packaged runtime/");
111
111
 
112
112
  // 3. Vendored sources the driver compiles on demand (SQLite for `storage`,
113
- // miniz for `compression`). They are fetched by `npm run vendor`, not committed.
114
- for (const name of ["stb", "nanosvg", "miniz", "sqlite3"]) {
113
+ // miniz for `compression`, wasm3 for `wasm`). They are fetched by `npm run vendor`,
114
+ // not committed.
115
+ for (const name of ["stb", "nanosvg", "miniz", "sqlite3", "wasm3"]) {
115
116
  const source = join(root, "vendor", name);
116
117
  if (!existsSync(source)) fail(`missing vendor/${name}; run \`npm run vendor\` before packaging`);
117
118
  cpSync(source, join(stage, "vendor", name), { recursive: true });
@@ -18,10 +18,18 @@ export function buildRegistry(
18
18
  ): ExtensionRegistry {
19
19
  const registry = createDefaultRegistry();
20
20
  const bundled = bundledExtensions();
21
+ const requested = new Set(extNames);
21
22
  for (const name of extNames) {
22
- const extension = bundled.find((candidate) => candidate.name === name);
23
- if (!extension) throw new Error(`Unknown extension '${name}'`);
24
- registry.register(extension);
23
+ if (!bundled.some((candidate) => candidate.name === name)) {
24
+ throw new Error(`Unknown extension '${name}'`);
25
+ }
26
+ }
27
+ /* Register in catalog order, not flag order: later extensions override
28
+ * earlier global names (`gui` and `webworker` both define `self`), and a
29
+ * deterministic winner is easier to document than "whatever the user typed
30
+ * last". */
31
+ for (const extension of bundled) {
32
+ if (requested.has(extension.name)) registry.register(extension);
25
33
  }
26
34
  // Each manifest describes a pre-built C++ or Rust library.
27
35
  for (const manifest of nativeManifests) {
package/src/cli/help.ts CHANGED
@@ -26,7 +26,7 @@ Options:
26
26
  --emit <kind> exe | obj | ir (default: exe)
27
27
  -O0..-O3 Optimization level (default: -O2)
28
28
  --ext <names> Comma separated extensions (e.g. node,gui)
29
- --feature <names> Force optional link features on (media,gpu,images,shaping,webgl,all)
29
+ --feature <names> Force optional link features on (media,gpu,images,shaping,webgl,storage,tls,wasm,all)
30
30
  --no-feature <n> Keep optional link features out even when detected
31
31
  --ext-native <m> Register a C++/Rust extension from a JSON manifest
32
32
  (comma separated for several)
@@ -207,6 +207,13 @@ export const RUNTIME_DECLARATIONS: readonly string[] = [
207
207
  "declare i64 @xt_finalization_registry_ctor(i32, i64*)",
208
208
  "declare i64 @xt_text_encoder_ctor(i32, i64*)",
209
209
  "declare i64 @xt_text_decoder_ctor(i32, i64*)",
210
+ "declare i64 @xt_readable_stream_ctor(i32, i64*)",
211
+ "declare i64 @xt_writable_stream_ctor(i32, i64*)",
212
+ "declare i64 @xt_transform_stream_ctor(i32, i64*)",
213
+ "declare i64 @xt_count_queuing_strategy_ctor(i32, i64*)",
214
+ "declare i64 @xt_byte_length_queuing_strategy_ctor(i32, i64*)",
215
+ "declare i64 @xt_text_encoder_stream_ctor(i32, i64*)",
216
+ "declare i64 @xt_text_decoder_stream_ctor(i32, i64*)",
210
217
  "declare i64 @xt_date_ctor(i32, i64*)",
211
218
  "declare i64 @xt_regexp_ctor(i32, i64*)",
212
219
  "declare i64 @xt_url_ctor(i32, i64*)",
@@ -49,6 +49,11 @@ export const BUILTIN_INSTANCEOF: Readonly<Record<string, number>> = {
49
49
  Int32Array: 22,
50
50
  Float32Array: 23,
51
51
  Float64Array: 24,
52
+ ReadableStream: 11,
53
+ WritableStream: 12,
54
+ TransformStream: 13,
55
+ TextEncoderStream: 14,
56
+ TextDecoderStream: 15,
52
57
  };
53
58
 
54
59
  export const BINARY_RUNTIME: Record<string, string | undefined> = {
@@ -311,7 +316,6 @@ export const WEB_GLOBALS: ReadonlySet<string> = new Set<string>([
311
316
  "Headers",
312
317
  "Request",
313
318
  "Response",
314
- "ReadableStream",
315
319
  "EventTarget",
316
320
  ]);
317
321
 
@@ -349,6 +353,13 @@ export const CTOR_FUNCTIONS: Record<string, string> = {
349
353
  URLSearchParams: "xt_url_search_params_ctor",
350
354
  TextEncoder: "xt_text_encoder_ctor",
351
355
  TextDecoder: "xt_text_decoder_ctor",
356
+ ReadableStream: "xt_readable_stream_ctor",
357
+ WritableStream: "xt_writable_stream_ctor",
358
+ TransformStream: "xt_transform_stream_ctor",
359
+ CountQueuingStrategy: "xt_count_queuing_strategy_ctor",
360
+ ByteLengthQueuingStrategy: "xt_byte_length_queuing_strategy_ctor",
361
+ TextEncoderStream: "xt_text_encoder_stream_ctor",
362
+ TextDecoderStream: "xt_text_decoder_stream_ctor",
352
363
  ArrayBuffer: "xt_array_buffer_ctor",
353
364
  DataView: "xt_data_view_ctor",
354
365
  ...ERROR_CONSTRUCTORS,
@@ -381,6 +392,13 @@ export const FIRST_CLASS_CONSTRUCTORS = new Set<string>([
381
392
  "Boolean",
382
393
  "BigInt",
383
394
  "Symbol",
395
+ "ReadableStream",
396
+ "WritableStream",
397
+ "TransformStream",
398
+ "CountQueuingStrategy",
399
+ "ByteLengthQueuingStrategy",
400
+ "TextEncoderStream",
401
+ "TextDecoderStream",
384
402
  ]);
385
403
 
386
404
  export const BUILTIN_METHODS = new Set<string>([
@@ -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.
@@ -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(
@@ -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.24";
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 = {