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/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AAE,IAAI,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;IACrF,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
@@ -0,0 +1,8 @@
1
+ /**
2
+ * WebAssembly execution and `.wasm` asset imports.
3
+ *
4
+ * The byte arrays are tiny fixed modules produced by clang/wasm-ld and are
5
+ * embedded as base64 so the test needs no wasm toolchain, only the pinned
6
+ * wasm3 interpreter that the `wasm` link feature compiles.
7
+ */
8
+ export {};
@@ -0,0 +1,113 @@
1
+ /**
2
+ * WebAssembly execution and `.wasm` asset imports.
3
+ *
4
+ * The byte arrays are tiny fixed modules produced by clang/wasm-ld and are
5
+ * embedded as base64 so the test needs no wasm toolchain, only the pinned
6
+ * wasm3 interpreter that the `wasm` link feature compiles.
7
+ */
8
+ import { writeFileSync } from "node:fs";
9
+ import { join } from "node:path";
10
+ import { expect, it } from "vitest";
11
+ import { describeE2E } from "./harness.js";
12
+ const ADD = "AGFzbQEAAAABDgNgAABgAn9/AX9gAAF/AwQDAAECBQMBAAIGKwd/AUGAiAQLfwBBgAgLfwBBgAgLfwBBgAgLfwBBgIgEC38AQQALfwBBAQsHhQEKBm1lbW9yeQIAEV9fd2FzbV9jYWxsX2N0b3JzAAADYWRkAAEFZ2V0NDIAAgxfX2Rzb19oYW5kbGUDAQpfX2RhdGFfZW5kAwINX19nbG9iYWxfYmFzZQMDC19faGVhcF9iYXNlAwQNX19tZW1vcnlfYmFzZQMFDF9fdGFibGVfYmFzZQMGChEDAgALBwAgASAAagsEAEEqCwA7BG5hbWUBIAMAEV9fd2FzbV9jYWxsX2N0b3JzAQNhZGQCBWdldDQyBxIBAA9fX3N0YWNrX3BvaW50ZXIAJglwcm9kdWNlcnMBDHByb2Nlc3NlZC1ieQEFY2xhbmcGMTUuMC4y";
13
+ const MATH = "AGFzbQEAAAABDwNgAABgAXwBfGACfn4BfgMEAwABAgUDAQACBisHfwFBgIgEC38AQYAIC38AQYAIC38AQYAIC38AQ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gIgEC38AQYAIC38AQYAIC38AQYAIC38AQYCIBAt/AEEAC38AQQELB30JBm1lbW9yeQIAEV9fd2FzbV9jYWxsX2N0b3JzAAIDcnVuAAMMX19kc29faGFuZGxlAwEKX19kYXRhX2VuZAMCDV9fZ2xvYmFsX2Jhc2UDAwtfX2hlYXBfYmFzZQMEDV9fbWVtb3J5X2Jhc2UDBQxfX3RhYmxlX2Jhc2UDBgoXAgIACxIAQQJBAxCAgICAABCBgICAAAsAPgRuYW1lASMEAANhZGQBA2xvZwIRX193YXNtX2NhbGxfY3RvcnMDA3J1bgcSAQAPX19zdGFja19wb2ludGVyACYJcHJvZHVjZXJzAQxwcm9jZXNzZWQtYnkBBWNsYW5nBjE1LjAuMg==";
16
+ /** TS source that decodes one embedded module into a `Uint8Array`. */
17
+ function bytesExpression(name, base64) {
18
+ return (`const ${name} = (() => {\n` +
19
+ ` const text = atob("${base64}");\n` +
20
+ " const bytes = new Uint8Array(text.length);\n" +
21
+ " for (let i = 0; i < text.length; i++) bytes[i] = text.charCodeAt(i);\n" +
22
+ " return bytes;\n" +
23
+ "})();\n");
24
+ }
25
+ describeE2E("WebAssembly", (harness) => {
26
+ it("validates, compiles and calls exported functions", () => {
27
+ const output = harness.runProgram(`
28
+ ${bytesExpression("bytes", ADD)}
29
+
30
+ async function main(): Promise<void> {
31
+ console.log("validate", WebAssembly.validate(bytes));
32
+ const module = await WebAssembly.compile(bytes);
33
+ const instance = await WebAssembly.instantiate(module, {});
34
+ console.log("add", instance.exports.add(2, 3));
35
+ console.log("get42", instance.exports.get42());
36
+ console.log("metadata", WebAssembly.Module.exports(module).length > 0,
37
+ WebAssembly.Module.imports(module).length);
38
+ const direct = new WebAssembly.Instance(module, {});
39
+ console.log("again", direct.exports.add(4, 5));
40
+ }
41
+ main();
42
+ `);
43
+ expect(output).toBe(["validate true", "add 5", "get42 42", "metadata true 0", "again 9"].join("\n"));
44
+ });
45
+ it("passes i64 and f64 values across the boundary", () => {
46
+ const output = harness.runProgram(`
47
+ ${bytesExpression("bytes", MATH)}
48
+
49
+ async function main(): Promise<void> {
50
+ const result = await WebAssembly.instantiate(bytes, {});
51
+ console.log("half", result.instance.exports.half(3.5));
52
+ console.log("pair", result.instance.exports.pair(2n, 3n).toString());
53
+ }
54
+ main();
55
+ `);
56
+ expect(output).toBe(["half 1.75", "pair 7"].join("\n"));
57
+ });
58
+ it("bridges JS imports and keeps exported memory in sync", () => {
59
+ const output = harness.runProgram(`
60
+ ${bytesExpression("memoryBytes", MEMORY)}
61
+ ${bytesExpression("importBytes", IMPORT)}
62
+
63
+ async function main(): Promise<void> {
64
+ const instance = (await WebAssembly.instantiate(memoryBytes, {})).instance;
65
+ const memory = instance.exports.memory;
66
+ console.log("first", instance.exports.first());
67
+ const view = new Uint8Array(memory.buffer);
68
+ instance.exports.write_byte(0, 65);
69
+ console.log("out", view[1024]);
70
+ view[1024] = 66;
71
+ console.log("in", instance.exports.first());
72
+
73
+ const imported = (await WebAssembly.instantiate(importBytes, {
74
+ env: {
75
+ add: (a: number, b: number) => a + b,
76
+ log: (value: number) => console.log("log", value),
77
+ },
78
+ })).instance;
79
+ imported.exports.run();
80
+ }
81
+ main();
82
+ `);
83
+ expect(output).toBe(["first 104", "out 65", "in 66", "log 5"].join("\n"));
84
+ });
85
+ it("reports compile errors as WebAssembly.CompileError", () => {
86
+ const output = harness.runProgram(`
87
+ try {
88
+ new WebAssembly.Module(new Uint8Array([0, 1, 2]));
89
+ } catch (error) {
90
+ console.log("compile", error instanceof WebAssembly.CompileError,
91
+ error instanceof Error, (error as Error).name);
92
+ }
93
+ console.log("invalid", WebAssembly.validate(new Uint8Array([0, 1, 2])));
94
+ console.log("empty", WebAssembly.validate(new Uint8Array([0, 97, 115, 109, 1, 0, 0, 0])));
95
+ `);
96
+ expect(output).toBe(["compile true true CompileError", "invalid false", "empty true"].join("\n"));
97
+ });
98
+ it("imports a .wasm file as an embedded Uint8Array", () => {
99
+ writeFileSync(join(harness.workdir, "embedded.wasm"), Buffer.from(ADD, "base64"));
100
+ const output = harness.runProgram(`
101
+ import bytes from "./embedded.wasm";
102
+
103
+ async function main(): Promise<void> {
104
+ console.log("bytes", bytes.length, bytes[0]);
105
+ const result = await WebAssembly.instantiate(bytes, {});
106
+ console.log("sum", result.instance.exports.add(20, 22));
107
+ }
108
+ main();
109
+ `);
110
+ expect(output).toBe(["bytes 336 0", "sum 42"].join("\n"));
111
+ });
112
+ });
113
+ //# sourceMappingURL=webassembly.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"webassembly.test.js","sourceRoot":"","sources":["../../../tests/e2e/webassembly.test.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AACxC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAC;AACpC,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAE3C,MAAM,GAAG,GAAG,kcAAkc,CAAC;AAC/c,MAAM,IAAI,GAAG,sdAAsd,CAAC;AACpe,MAAM,MAAM,GAAG,0kBAA0kB,CAAC;AAC1lB,MAAM,MAAM,GAAG,keAAke,CAAC;AAElf,sEAAsE;AACtE,SAAS,eAAe,CAAC,IAAY,EAAE,MAAc;IACnD,OAAO,CACL,SAAS,IAAI,eAAe;QAC5B,wBAAwB,MAAM,OAAO;QACrC,gDAAgD;QAChD,0EAA0E;QAC1E,mBAAmB;QACnB,SAAS,CACV,CAAC;AACJ,CAAC;AAED,WAAW,CAAC,aAAa,EAAE,CAAC,OAAO,EAAE,EAAE;IACrC,EAAE,CAAC,kDAAkD,EAAE,GAAG,EAAE;QAC1D,MAAM,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC;QAC9B,eAAe,CAAC,OAAO,EAAE,GAAG,CAAC;;;;;;;;;;;;;;KAchC,CAAC,CAAC;QACH,MAAM,CAAC,MAAM,CAAC,CA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AAe,CAAC,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC,CAAC;QAClF,MAAM,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC;;;;;;;;;KASjC,CAAC,CAAC;QACH,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,aAAa,EAAE,QAAQ,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;IAC5D,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
@@ -0,0 +1,14 @@
1
+ /**
2
+ * End-to-end tests for the worker API surface shared by `node:worker_threads`
3
+ * and the Web Worker globals.
4
+ *
5
+ * These cases cover the parts of the API that the happy-path suites do not
6
+ * reach: the structured-clone codec's scalar/collection branches, the
7
+ * EventEmitter and DOM event methods on a worker object and on `parentPort`,
8
+ * `MessagePort` methods, and the errors raised when a worker is already gone.
9
+ *
10
+ * Workers in these tests return instead of being terminated wherever possible:
11
+ * a killed process never writes its LLVM coverage profile, so a natural exit is
12
+ * what lets the runtime coverage job see the worker half of the code.
13
+ */
14
+ export {};
@@ -0,0 +1,460 @@
1
+ /**
2
+ * End-to-end tests for the worker API surface shared by `node:worker_threads`
3
+ * and the Web Worker globals.
4
+ *
5
+ * These cases cover the parts of the API that the happy-path suites do not
6
+ * reach: the structured-clone codec's scalar/collection branches, the
7
+ * EventEmitter and DOM event methods on a worker object and on `parentPort`,
8
+ * `MessagePort` methods, and the errors raised when a worker is already gone.
9
+ *
10
+ * Workers in these tests return instead of being terminated wherever possible:
11
+ * a killed process never writes its LLVM coverage profile, so a natural exit is
12
+ * what lets the runtime coverage job see the worker half of the code.
13
+ */
14
+ import { expect, it } from "vitest";
15
+ import { describeE2E } from "./harness.js";
16
+ describeE2E("worker API", (harness) => {
17
+ const { runProgram } = harness;
18
+ it("clones scalars, collections and shared references", () => {
19
+ const source = `
20
+ import { Worker, isMainThread, parentPort } from "node:worker_threads";
21
+ if (isMainThread) {
22
+ const worker = new Worker(import.meta.filename);
23
+ worker.on("message", (m) => {
24
+ const checks: boolean[] = [
25
+ 1 / m.negZero === -Infinity,
26
+ m.nan !== m.nan,
27
+ m.inf === Infinity,
28
+ m.negInf === -Infinity,
29
+ m.undef === undefined,
30
+ m.nul === null,
31
+ m.yes === true && m.no === false,
32
+ m.big === 12345678901234567890n,
33
+ m.text.length === 20,
34
+ m.text.charCodeAt(1) === 34,
35
+ m.text.charCodeAt(3) === 92,
36
+ m.text.charCodeAt(5) === 9,
37
+ m.text.charCodeAt(7) === 10,
38
+ m.text.charCodeAt(9) === 13,
39
+ m.text.charCodeAt(11) === 8,
40
+ m.text.charCodeAt(13) === 12,
41
+ m.text.charCodeAt(15) === 1,
42
+ m.text.indexOf("中文😀") === 16,
43
+ m.emptyArray.length === 0,
44
+ Object.keys(m.emptyObject).length === 0,
45
+ m.shared.left === m.shared.right,
46
+ m.map.get("k") === 1 && m.map.get("deep").x[1] === 2 && m.map.size === 2,
47
+ m.set.has(2) && m.set.size === 2,
48
+ m.date.getTime() === 1234,
49
+ m.reg.source === "a+b" && m.reg.flags === "gi",
50
+ m.nested.arr[0].x === 1 && m.nested.map.get("n").has(3),
51
+ ];
52
+ let all = true;
53
+ for (const check of checks) if (!check) all = false;
54
+ console.log(all, m.text.length, m.big, m.shared.left === m.shared.right);
55
+ });
56
+ } else {
57
+ const shared = { n: 1 };
58
+ const text =
59
+ "q" +
60
+ String.fromCharCode(34) +
61
+ "b" +
62
+ String.fromCharCode(92) +
63
+ "t" +
64
+ String.fromCharCode(9) +
65
+ "n" +
66
+ String.fromCharCode(10) +
67
+ "r" +
68
+ String.fromCharCode(13) +
69
+ "b" +
70
+ String.fromCharCode(8) +
71
+ "f" +
72
+ String.fromCharCode(12) +
73
+ "c" +
74
+ String.fromCharCode(1) +
75
+ "中文😀";
76
+ parentPort.postMessage({
77
+ negZero: -0,
78
+ nan: NaN,
79
+ inf: Infinity,
80
+ negInf: -Infinity,
81
+ undef: undefined,
82
+ nul: null,
83
+ yes: true,
84
+ no: false,
85
+ big: 12345678901234567890n,
86
+ text,
87
+ emptyArray: [],
88
+ emptyObject: {},
89
+ shared: { left: shared, right: shared },
90
+ map: new Map([["k", 1], ["deep", { x: [1, 2] }]]),
91
+ set: new Set([1, 2]),
92
+ date: new Date(1234),
93
+ reg: /a+b/gi,
94
+ nested: { arr: [{ x: 1 }], map: new Map([["n", new Set([3])]]) },
95
+ });
96
+ parentPort.close();
97
+ }
98
+ `;
99
+ expect(runProgram(source, { extensions: true })).toBe("true 20 12345678901234567890n true");
100
+ });
101
+ it("clones array buffers and every numeric typed array", () => {
102
+ const source = `
103
+ import { Worker, isMainThread, parentPort } from "node:worker_threads";
104
+ if (isMainThread) {
105
+ const worker = new Worker(import.meta.filename);
106
+ worker.on("message", (m) => {
107
+ console.log(
108
+ m.buffer.byteLength === 5,
109
+ m.emptyBuffer.byteLength === 0,
110
+ m.emptyU8.length === 0,
111
+ m.i8[0] === -1 && m.i8.length === 1,
112
+ m.u8a[0] === 7 && m.u8b[1] === 8 && m.u8c[2] === 9,
113
+ m.clamped[0] === 255,
114
+ m.i16[1] === 300 && m.u16[0] === 65535,
115
+ m.i32[0] === -3 && m.u32[0] === 4294967295,
116
+ m.f32[1] === -2.25 && m.f64[0] === -2.5,
117
+ );
118
+ });
119
+ } else {
120
+ parentPort.postMessage({
121
+ buffer: new Uint8Array([1, 2, 3, 4, 5]).buffer,
122
+ emptyBuffer: new ArrayBuffer(0),
123
+ emptyU8: new Uint8Array(0),
124
+ i8: new Int8Array([-1]),
125
+ u8a: new Uint8Array([7]),
126
+ u8b: new Uint8Array([7, 8]),
127
+ u8c: new Uint8Array([7, 8, 9]),
128
+ clamped: new Uint8ClampedArray([255]),
129
+ i16: new Int16Array([-2, 300]),
130
+ u16: new Uint16Array([65535]),
131
+ i32: new Int32Array([-3]),
132
+ u32: new Uint32Array([4294967295]),
133
+ f32: new Float32Array([1.5, -2.25]),
134
+ f64: new Float64Array([-2.5, 3.5]),
135
+ });
136
+ parentPort.close();
137
+ }
138
+ `;
139
+ expect(runProgram(source, { extensions: true })).toBe("true true true true true true true true true");
140
+ });
141
+ it("exposes the Worker emitter and DOM event surface", () => {
142
+ const source = `
143
+ import { Worker, isMainThread, markAsUncloneable } from "node:worker_threads";
144
+ if (isMainThread) {
145
+ const worker = new Worker(import.meta.filename);
146
+ const seen: string[] = [];
147
+ const onTick = () => { seen.push("on"); };
148
+ worker.on("tick", onTick);
149
+ worker.emit("tick");
150
+ worker.off("tick", onTick);
151
+ worker.emit("tick");
152
+ worker.once("tick", () => { seen.push("once"); });
153
+ worker.emit("tick");
154
+ worker.emit("tick");
155
+ worker.emit("pre", 7);
156
+ worker.on("pre", (value) => { seen.push("pre" + value); });
157
+ worker.addListener("alias", () => { seen.push("alias"); });
158
+ worker.emit("alias");
159
+ worker.removeListener("alias", () => {});
160
+ worker.removeAllListeners("alias");
161
+ worker.emit("alias");
162
+ worker.on("x", () => { seen.push("x"); });
163
+ worker.on("y", () => { seen.push("y"); });
164
+ worker.removeAllListeners();
165
+ worker.emit("x");
166
+ worker.onmessage = () => { seen.push("onmsg"); };
167
+ const onmessageHandler = worker.onmessage;
168
+ worker.addEventListener("message", () => { seen.push("dom"); });
169
+ worker.dispatchEvent({ type: "message", data: 1 });
170
+ worker.onerror = () => { seen.push("err"); };
171
+ const onerrorHandler = worker.onerror;
172
+ worker.dispatchEvent({ type: "error", message: "nope" });
173
+ worker.removeEventListener("message", () => {});
174
+ worker.ref();
175
+ worker.unref();
176
+ markAsUncloneable({ payload: 1 });
177
+ const threadId = worker.threadId;
178
+ worker.on("exit", (code) => {
179
+ console.log(
180
+ seen.join("+"),
181
+ threadId > 0,
182
+ typeof onmessageHandler === "function",
183
+ typeof onerrorHandler === "function",
184
+ code,
185
+ );
186
+ });
187
+ } else {
188
+ }
189
+ `;
190
+ expect(runProgram(source, { extensions: true })).toBe("on+once+pre7+alias+onmsg+dom+err true true true 0");
191
+ });
192
+ it("supports MessagePort methods in both directions", () => {
193
+ const source = `
194
+ import { MessageChannel, MessagePort, receiveMessageOnPort } from "node:worker_threads";
195
+ const channel = new MessageChannel();
196
+ const port1 = channel.port1;
197
+ const port2 = channel.port2;
198
+ const seen: string[] = [];
199
+ port1.onmessage = (event) => { seen.push("a" + event.data.n); };
200
+ port2.on("message", (value) => { seen.push("b" + value.n); });
201
+ port1.start();
202
+ port2.start();
203
+ port1.ref();
204
+ port1.unref();
205
+ port1.postMessage({ n: 1 });
206
+ port2.postMessage({ n: 2 });
207
+ try {
208
+ new MessagePort();
209
+ } catch (error) {
210
+ console.log("ctor", (error as Error).message.includes("not constructible"));
211
+ }
212
+ console.log("sync", receiveMessageOnPort(port1) === undefined, port2 === channel.port2);
213
+ setTimeout(() => {
214
+ port1.close();
215
+ port2.close();
216
+ console.log(seen.sort().join(","), "closed");
217
+ }, 10);
218
+ `;
219
+ expect(runProgram(source, { extensions: true })).toBe("ctor true\nsync true true\na2,b1 closed");
220
+ });
221
+ it("reports postMessage on an exited worker and terminate's value", () => {
222
+ const source = `
223
+ import { Worker, isMainThread } from "node:worker_threads";
224
+ if (isMainThread) {
225
+ const worker = new Worker(import.meta.filename);
226
+ worker.on("exit", (code) => {
227
+ let threw = false;
228
+ try {
229
+ worker.postMessage("late");
230
+ } catch (error) {
231
+ threw = (error as Error).message.includes("not running");
232
+ }
233
+ worker.terminate().then((value) => {
234
+ console.log("late", code, threw, typeof value, value === 0);
235
+ });
236
+ });
237
+ } else {
238
+ }
239
+ `;
240
+ expect(runProgram(source, { extensions: true })).toBe("late 0 true number true");
241
+ });
242
+ it("rejects uncloneable values before sending", () => {
243
+ const source = `
244
+ import { Worker, isMainThread, MessageChannel } from "node:worker_threads";
245
+ if (isMainThread) {
246
+ const worker = new Worker(import.meta.filename);
247
+ const port = new MessageChannel().port1;
248
+ const values: any[] = [Symbol("s"), Promise.resolve(1), new WeakMap(), new WeakSet(), port];
249
+ for (const value of values) {
250
+ try {
251
+ worker.postMessage(value);
252
+ console.log("accepted");
253
+ } catch (error) {
254
+ console.log("rejected", (error as Error).message.includes("could not be cloned"));
255
+ }
256
+ }
257
+ worker.terminate();
258
+ } else {
259
+ }
260
+ `;
261
+ expect(runProgram(source, { extensions: true })).toBe("rejected true\nrejected true\nrejected true\nrejected true\nrejected true");
262
+ });
263
+ it("sends through the bare Web Worker postMessage global", () => {
264
+ const source = `
265
+ if (isMainThread) {
266
+ const worker = new Worker(import.meta.filename);
267
+ worker.onmessage = (event) => { console.log("msg", event.data); };
268
+ } else {
269
+ postMessage("hi");
270
+ self.close();
271
+ }
272
+ `;
273
+ expect(runProgram(source, { webworker: true })).toBe("msg hi");
274
+ });
275
+ it("rejects constructing the Web Worker MessagePort global", () => {
276
+ const source = `
277
+ const channel = new MessageChannel();
278
+ channel.port2.onmessage = (event) => { console.log("port", event.data); };
279
+ channel.port1.postMessage(5);
280
+ try {
281
+ new MessagePort();
282
+ } catch (error) {
283
+ console.log("ctor", (error as Error).message.includes("not constructible"));
284
+ }
285
+ `;
286
+ expect(runProgram(source, { webworker: true })).toBe("ctor true\nport 5");
287
+ });
288
+ it("exposes the parentPort scope API", () => {
289
+ const source = `
290
+ import { Worker, isMainThread, parentPort } from "node:worker_threads";
291
+ if (isMainThread) {
292
+ const worker = new Worker(import.meta.filename);
293
+ worker.on("message", (summary) => { console.log(summary); });
294
+ } else {
295
+ const seen: string[] = [];
296
+ const handler = () => { seen.push("custom"); };
297
+ parentPort.addEventListener("custom", handler);
298
+ parentPort.dispatchEvent({ type: "custom" });
299
+ parentPort.removeEventListener("custom", handler);
300
+ parentPort.once("later", () => { seen.push("late"); });
301
+ parentPort.emit("later");
302
+ parentPort.emit("later");
303
+ parentPort.on("off-me", () => { seen.push("off"); });
304
+ parentPort.off("off-me", () => {});
305
+ parentPort.on("a", () => {});
306
+ parentPort.removeAllListeners();
307
+ parentPort.start();
308
+ parentPort.ref();
309
+ parentPort.unref();
310
+ parentPort.onmessage = () => { seen.push("onmsg"); };
311
+ parentPort.dispatchEvent({ type: "message", data: "x" });
312
+ parentPort.postMessage(seen.join("+"));
313
+ parentPort.close();
314
+ try {
315
+ parentPort.postMessage("late");
316
+ } catch (error) {
317
+ seen.push("caught");
318
+ }
319
+ }
320
+ `;
321
+ expect(runProgram(source, { extensions: true })).toBe("custom+late+onmsg");
322
+ });
323
+ it("delivers a large message while closing the port", () => {
324
+ const source = `
325
+ import { Worker, isMainThread, parentPort } from "node:worker_threads";
326
+ if (isMainThread) {
327
+ const worker = new Worker(import.meta.filename);
328
+ worker.on("message", (value) => {
329
+ console.log("big", value.length, value.charCodeAt(0) === 120, value.charCodeAt(value.length - 1) === 120);
330
+ });
331
+ } else {
332
+ parentPort.postMessage("x".repeat(4000000));
333
+ parentPort.close();
334
+ }
335
+ `;
336
+ expect(runProgram(source, { extensions: true })).toBe("big 4000000 true true");
337
+ });
338
+ it("keeps a waiting worker alive across poll ticks and terminates it", () => {
339
+ const source = `
340
+ import { Worker, isMainThread, parentPort } from "node:worker_threads";
341
+ if (isMainThread) {
342
+ const worker = new Worker(import.meta.filename);
343
+ worker.on("exit", (code) => { console.log("exit", code); });
344
+ setTimeout(() => {
345
+ worker.terminate().then((value) => { console.log("terminated", value); });
346
+ }, 120);
347
+ } else {
348
+ parentPort.on("message", () => {});
349
+ }
350
+ `;
351
+ expect(runProgram(source, { extensions: true })).toBe("exit 1\nterminated 1");
352
+ });
353
+ it("keeps the worker target rooted across collections", () => {
354
+ const source = `
355
+ import { Worker, isMainThread, parentPort } from "node:worker_threads";
356
+ if (isMainThread) {
357
+ const worker = new Worker(import.meta.filename);
358
+ let count = 0;
359
+ worker.on("message", (value) => {
360
+ count++;
361
+ if (count === 3) {
362
+ console.log("gc", value, count);
363
+ worker.terminate();
364
+ } else {
365
+ worker.postMessage(count);
366
+ }
367
+ });
368
+ worker.postMessage(0);
369
+ const junk: string[] = [];
370
+ for (let i = 0; i < 100; i++) junk.push("garbage" + i);
371
+ console.log("allocated", junk.length);
372
+ } else {
373
+ parentPort.on("message", (value) => { parentPort.postMessage(value + 1); });
374
+ }
375
+ `;
376
+ expect(runProgram(source, { extensions: true, env: { XT_GC_THRESHOLD: "1" } })).toBe("allocated 100\ngc 3 3");
377
+ });
378
+ it("requires a script URL", () => {
379
+ const source = `
380
+ import { Worker, isMainThread } from "node:worker_threads";
381
+ if (isMainThread) {
382
+ try {
383
+ (Worker as any)();
384
+ } catch (error) {
385
+ console.log("script", (error as Error).message.includes("requires a script URL"));
386
+ }
387
+ }
388
+ `;
389
+ expect(runProgram(source, { extensions: true })).toBe("script true");
390
+ });
391
+ it("reports postMessage outside a worker", () => {
392
+ const source = `
393
+ try {
394
+ postMessage("main");
395
+ } catch (error) {
396
+ console.log("main", (error as Error).message.includes("only available inside a worker"));
397
+ }
398
+ `;
399
+ expect(runProgram(source, { webworker: true })).toBe("main true");
400
+ });
401
+ it("supports the webworker module Worker export", () => {
402
+ const source = `
403
+ import { Worker, isMainThread, postMessage } from "webworker";
404
+ if (isMainThread) {
405
+ const worker = new Worker(import.meta.filename);
406
+ worker.onmessage = (event) => { console.log("msg", event.data); };
407
+ } else {
408
+ postMessage("hi");
409
+ self.close();
410
+ }
411
+ `;
412
+ expect(runProgram(source, { webworker: true })).toBe("msg hi");
413
+ });
414
+ it("supports the webworker module MessagePort exports", () => {
415
+ const source = `
416
+ import { MessageChannel, MessagePort } from "webworker";
417
+ const channel = new MessageChannel();
418
+ channel.port2.onmessage = (event) => { console.log("port", event.data); };
419
+ channel.port1.postMessage(9);
420
+ try {
421
+ new MessagePort();
422
+ } catch (error) {
423
+ console.log("ctor", (error as Error).message.includes("not constructible"));
424
+ }
425
+ `;
426
+ expect(runProgram(source, { webworker: true })).toBe("ctor true\nport 9");
427
+ });
428
+ it("reports a disabled WebAssembly feature as unsupported", () => {
429
+ const source = `
430
+ try {
431
+ WebAssembly.validate(new Uint8Array([0]));
432
+ } catch (error) {
433
+ console.log("validate", (error as Error).message.includes("not supported"));
434
+ }
435
+ try {
436
+ new WebAssembly.Module(new Uint8Array([0]));
437
+ } catch (error) {
438
+ console.log("module", (error as Error).message.includes("not supported"));
439
+ }
440
+ console.log(typeof WebAssembly.compile, typeof WebAssembly.CompileError);
441
+ `;
442
+ expect(runProgram(source, { noFeatures: ["wasm"] })).toBe("validate true\nmodule true\nfunction function");
443
+ });
444
+ it.runIf(process.platform === "win32")("rejects workerData over the Windows environment limit", () => {
445
+ const source = `
446
+ import { Worker, isMainThread } from "node:worker_threads";
447
+ if (isMainThread) {
448
+ try {
449
+ new Worker(import.meta.filename, { workerData: "x".repeat(40000) });
450
+ console.log("started");
451
+ } catch (error) {
452
+ console.log("large", (error as Error).message.includes("too large"));
453
+ }
454
+ } else {
455
+ }
456
+ `;
457
+ expect(runProgram(source, { extensions: true })).toBe("large true");
458
+ });
459
+ });
460
+ //# sourceMappingURL=worker-api.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"worker-api.test.js","sourceRoot":"","sources":["../../../tests/e2e/worker-api.test.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAC;AACpC,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAE3C,WAAW,CAAC,YAAY,EAAE,CAAC,OAAO,EAAE,EAAE;IACpC,MAAM,EAAE,UAAU,EAAE,GAAG,OAAO,CAAC;IAE/B,EAAE,CAAC,mDAAmD,EAAE,GAAG,EAAE;QAC3D,MAAM,MAAM,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KA+Ed,CAAC;QACF,MAAM,CAAC,UAAU,CAAC,MAAM,EAAE,EAAE,UAAU,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,IAAI,CACnD,oCAAoC,CACrC,CAAC;IACJ,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,oDAAoD,EAAE,GAAG,EAAE;QAC5D,MAAM,MA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AAC;IAEH,EAAE,CAAC,uDAAuD,EAAE,GAAG,EAAE;QAC/D,MAAM,MAAM,GAAG;;;;;;;;;;;;KAYd,CAAC;QACF,MAAM,CAAC,UAAU,CAAC,MAAM,EAAE,EAAE,UAAU,EAAE,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CACvD,+CAA+C,CAChD,CAAC;IACJ,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,QAAQ,KAAK,OAAO,CAAC,CAAC,uDAAuD,EAAE,GAAG,EAAE;QACnG,MAAM,MAAM,GAAG;;;;;;;;;;;KAWd,CAAC;QACF,MAAM,CAAC,UAAU,CAAC,MAAM,EAAE,EAAE,UAAU,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;IACtE,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
@@ -0,0 +1,8 @@
1
+ /**
2
+ * End-to-end tests for `node:worker_threads`.
3
+ *
4
+ * xbintsc emulates a worker with a second process running the same executable,
5
+ * so every program guards its body with `isMainThread` and uses
6
+ * `import.meta.filename` as the worker script.
7
+ */
8
+ export {};