xbintsc 0.6.23 → 0.6.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (249) hide show
  1. package/AGENTS.md +1 -1
  2. package/dist/src/cli/extensions.js +11 -3
  3. package/dist/src/cli/extensions.js.map +1 -1
  4. package/dist/src/cli/help.d.ts +1 -1
  5. package/dist/src/cli/help.js +1 -1
  6. package/dist/src/codegen/generator/state.js +7 -0
  7. package/dist/src/codegen/generator/state.js.map +1 -1
  8. package/dist/src/codegen/generator/tables.js +19 -1
  9. package/dist/src/codegen/generator/tables.js.map +1 -1
  10. package/dist/src/driver/bundler/concatenate.d.ts +23 -0
  11. package/dist/src/driver/bundler/concatenate.js +210 -0
  12. package/dist/src/driver/bundler/concatenate.js.map +1 -0
  13. package/dist/src/driver/bundler/graph.js +9 -5
  14. package/dist/src/driver/bundler/graph.js.map +1 -1
  15. package/dist/src/driver/bundler/merge.d.ts +4 -4
  16. package/dist/src/driver/bundler/merge.js +24 -207
  17. package/dist/src/driver/bundler/merge.js.map +1 -1
  18. package/dist/src/driver/bundler/{namespace-import.d.ts → namespace-object.d.ts} +5 -1
  19. package/dist/src/driver/bundler/{namespace-import.js → namespace-object.js} +6 -2
  20. package/dist/src/driver/bundler/namespace-object.js.map +1 -0
  21. package/dist/src/driver/bundler/{reexport-fixup.d.ts → reexport-resolution.d.ts} +9 -3
  22. package/dist/src/driver/bundler/{reexport-fixup.js → reexport-resolution.js} +16 -6
  23. package/dist/src/driver/bundler/reexport-resolution.js.map +1 -0
  24. package/dist/src/driver/fingerprint.d.ts +2 -2
  25. package/dist/src/driver/fingerprint.js +3 -3
  26. package/dist/src/driver/fingerprint.js.map +1 -1
  27. package/dist/src/driver/link-features.js +1 -1
  28. package/dist/src/driver/link-features.js.map +1 -1
  29. package/dist/src/driver/modules.d.ts +12 -5
  30. package/dist/src/driver/modules.js +12 -5
  31. package/dist/src/driver/modules.js.map +1 -1
  32. package/dist/src/driver/paths.js +8 -4
  33. package/dist/src/driver/paths.js.map +1 -1
  34. package/dist/src/driver/runtime-objects.d.ts +1 -1
  35. package/dist/src/driver/runtime-objects.js +13 -1
  36. package/dist/src/driver/runtime-objects.js.map +1 -1
  37. package/dist/src/driver/version.d.ts +1 -1
  38. package/dist/src/driver/version.js +1 -1
  39. package/dist/src/extensions/catalog.js +5 -1
  40. package/dist/src/extensions/catalog.js.map +1 -1
  41. package/dist/src/extensions/gui/index.js +0 -1
  42. package/dist/src/extensions/gui/index.js.map +1 -1
  43. package/dist/src/extensions/link-feature.d.ts +8 -0
  44. package/dist/src/extensions/node/stream-web/index.d.ts +6 -1
  45. package/dist/src/extensions/node/stream-web/index.js +59 -4
  46. package/dist/src/extensions/node/stream-web/index.js.map +1 -1
  47. package/dist/src/extensions/node/worker_threads/index.d.ts +14 -4
  48. package/dist/src/extensions/node/worker_threads/index.js +24 -11
  49. package/dist/src/extensions/node/worker_threads/index.js.map +1 -1
  50. package/dist/src/extensions/registry.d.ts +9 -1
  51. package/dist/src/extensions/registry.js +4 -1
  52. package/dist/src/extensions/registry.js.map +1 -1
  53. package/dist/src/extensions/wasm-asset.d.ts +16 -0
  54. package/dist/src/extensions/wasm-asset.js +28 -0
  55. package/dist/src/extensions/wasm-asset.js.map +1 -0
  56. package/dist/src/extensions/wasm.d.ts +17 -0
  57. package/dist/src/extensions/wasm.js +68 -0
  58. package/dist/src/extensions/wasm.js.map +1 -0
  59. package/dist/src/extensions/webworker/index.d.ts +24 -0
  60. package/dist/src/extensions/webworker/index.js +54 -0
  61. package/dist/src/extensions/webworker/index.js.map +1 -0
  62. package/dist/src/index.d.ts +1 -0
  63. package/dist/src/index.js +1 -0
  64. package/dist/src/index.js.map +1 -1
  65. package/dist/tests/driver/deferred-reexport.test.js +25 -0
  66. package/dist/tests/driver/deferred-reexport.test.js.map +1 -1
  67. package/dist/tests/driver/link-features.test.js +9 -0
  68. package/dist/tests/driver/link-features.test.js.map +1 -1
  69. package/dist/tests/e2e/async-fs.test.js +44 -0
  70. package/dist/tests/e2e/async-fs.test.js.map +1 -1
  71. package/dist/tests/e2e/harness.d.ts +6 -0
  72. package/dist/tests/e2e/harness.js +13 -1
  73. package/dist/tests/e2e/harness.js.map +1 -1
  74. package/dist/tests/e2e/node-crypto-random.test.d.ts +8 -0
  75. package/dist/tests/e2e/node-crypto-random.test.js +148 -0
  76. package/dist/tests/e2e/node-crypto-random.test.js.map +1 -0
  77. package/dist/tests/e2e/node-modules.test.js +1 -1
  78. package/dist/tests/e2e/node-modules.test.js.map +1 -1
  79. package/dist/tests/e2e/web-streams.test.d.ts +8 -0
  80. package/dist/tests/e2e/web-streams.test.js +274 -0
  81. package/dist/tests/e2e/web-streams.test.js.map +1 -0
  82. package/dist/tests/e2e/web-worker.test.d.ts +7 -0
  83. package/dist/tests/e2e/web-worker.test.js +74 -0
  84. package/dist/tests/e2e/web-worker.test.js.map +1 -0
  85. package/dist/tests/e2e/webassembly.test.d.ts +8 -0
  86. package/dist/tests/e2e/webassembly.test.js +113 -0
  87. package/dist/tests/e2e/webassembly.test.js.map +1 -0
  88. package/dist/tests/e2e/worker-api.test.d.ts +14 -0
  89. package/dist/tests/e2e/worker-api.test.js +460 -0
  90. package/dist/tests/e2e/worker-api.test.js.map +1 -0
  91. package/dist/tests/e2e/worker-threads.test.d.ts +8 -0
  92. package/dist/tests/e2e/worker-threads.test.js +160 -0
  93. package/dist/tests/e2e/worker-threads.test.js.map +1 -0
  94. package/dist/tests/extensions/wasm.test.d.ts +1 -0
  95. package/dist/tests/extensions/wasm.test.js +53 -0
  96. package/dist/tests/extensions/wasm.test.js.map +1 -0
  97. package/doc/ai/cli.md +1 -1
  98. package/doc/ai/contributing.md +11 -4
  99. package/doc/ai/extensions.md +25 -2
  100. package/doc/ai/language-support.md +14 -2
  101. package/doc/ai/zh-CN/cli.md +1 -1
  102. package/doc/ai/zh-CN/contributing.md +8 -3
  103. package/doc/ai/zh-CN/extensions.md +23 -2
  104. package/doc/ai/zh-CN/language-support.md +13 -2
  105. package/doc/gui/api.md +1 -1
  106. package/doc/implemented/cli.md +1 -1
  107. package/doc/implemented/extensions.md +6 -5
  108. package/doc/implemented/node/stream-web.md +31 -0
  109. package/doc/implemented/node/worker-threads.md +88 -5
  110. package/doc/implemented/node.md +2 -1
  111. package/doc/implemented/streams.md +82 -0
  112. package/doc/implemented/wasm.md +80 -18
  113. package/doc/implemented/web-worker.md +84 -0
  114. package/doc/implemented.md +6 -2
  115. package/doc/unimplemented/deviations.md +1 -0
  116. package/doc/unimplemented/node/worker-threads.md +23 -0
  117. package/doc/unimplemented/node.md +1 -0
  118. package/doc/unimplemented/stdlib.md +2 -1
  119. package/doc/unimplemented/wasm.md +21 -9
  120. package/doc/unimplemented.md +1 -1
  121. package/doc/zh-CN/gui/api.md +1 -1
  122. package/doc/zh-CN/implemented/cli.md +1 -1
  123. package/doc/zh-CN/implemented/extensions.md +7 -6
  124. package/doc/zh-CN/implemented/node/stream-web.md +24 -0
  125. package/doc/zh-CN/implemented/node/worker-threads.md +77 -4
  126. package/doc/zh-CN/implemented/node.md +2 -1
  127. package/doc/zh-CN/implemented/streams.md +60 -0
  128. package/doc/zh-CN/implemented/wasm.md +73 -16
  129. package/doc/zh-CN/implemented/web-worker.md +79 -0
  130. package/doc/zh-CN/implemented.md +6 -2
  131. package/doc/zh-CN/unimplemented/deviations.md +1 -0
  132. package/doc/zh-CN/unimplemented/node/worker-threads.md +20 -0
  133. package/doc/zh-CN/unimplemented/node.md +1 -0
  134. package/doc/zh-CN/unimplemented/stdlib.md +2 -1
  135. package/doc/zh-CN/unimplemented/wasm.md +16 -8
  136. package/doc/zh-CN/unimplemented.md +1 -1
  137. package/llms.txt +2 -0
  138. package/package.json +8 -7
  139. package/runtime/ext_gui/browser.cpp +1 -1
  140. package/runtime/ext_node/stream_web/stream_web.c +20 -6
  141. package/runtime/worker/worker_api.c +422 -0
  142. package/runtime/worker/worker_codec.c +489 -0
  143. package/runtime/worker/worker_core.c +445 -0
  144. package/runtime/worker/worker_internal.h +223 -0
  145. package/runtime/worker/worker_platform.c +344 -0
  146. package/runtime/worker/worker_ports.c +203 -0
  147. package/runtime/worker/worker_transport.c +464 -0
  148. package/runtime/worker/worker_wire.c +150 -0
  149. package/runtime/xt_builtins.c +15 -0
  150. package/runtime/xt_constructors.c +49 -2
  151. package/runtime/xt_stdlib2/error.inc +4 -0
  152. package/runtime/xt_streams/common.inc +232 -0
  153. package/runtime/xt_streams/constructors.inc +232 -0
  154. package/runtime/xt_streams/encoding.inc +124 -0
  155. package/runtime/xt_streams/readable-extra.inc +372 -0
  156. package/runtime/xt_streams/readable.inc +415 -0
  157. package/runtime/xt_streams/transform.inc +195 -0
  158. package/runtime/xt_streams/writable.inc +542 -0
  159. package/runtime/xt_streams.c +30 -0
  160. package/runtime/xt_wasm/wasm.h +148 -0
  161. package/runtime/xt_wasm/wasm_api.c +357 -0
  162. package/runtime/xt_wasm/wasm_api.h +37 -0
  163. package/runtime/xt_wasm/wasm_bridge.c +30 -0
  164. package/runtime/xt_wasm/wasm_instance.c +591 -0
  165. package/runtime/xt_wasm/wasm_module.c +270 -0
  166. package/runtime/xt_wasm/wasm_values.c +177 -0
  167. package/runtime/xt_webassembly.c +15 -4
  168. package/scripts/fetch-vendored.ts +187 -20
  169. package/scripts/package-release.ts +3 -2
  170. package/src/cli/extensions.ts +11 -3
  171. package/src/cli/help.ts +1 -1
  172. package/src/codegen/generator/state.ts +7 -0
  173. package/src/codegen/generator/tables.ts +19 -1
  174. package/src/driver/bundler/README.md +55 -0
  175. package/src/driver/bundler/concatenate.ts +244 -0
  176. package/src/driver/bundler/graph.ts +9 -5
  177. package/src/driver/bundler/merge.ts +24 -215
  178. package/src/driver/bundler/{namespace-import.ts → namespace-object.ts} +5 -1
  179. package/src/driver/bundler/{reexport-fixup.ts → reexport-resolution.ts} +15 -5
  180. package/src/driver/fingerprint.ts +3 -3
  181. package/src/driver/link-features.ts +1 -1
  182. package/src/driver/modules.ts +12 -5
  183. package/src/driver/paths.ts +8 -4
  184. package/src/driver/runtime-objects.ts +13 -1
  185. package/src/driver/version.ts +1 -1
  186. package/src/extensions/catalog.ts +5 -1
  187. package/src/extensions/gui/index.ts +0 -1
  188. package/src/extensions/link-feature.ts +8 -0
  189. package/src/extensions/node/stream-web/index.ts +59 -4
  190. package/src/extensions/node/worker_threads/index.ts +24 -11
  191. package/src/extensions/registry.ts +13 -2
  192. package/src/extensions/wasm-asset.ts +32 -0
  193. package/src/extensions/wasm.ts +74 -0
  194. package/src/extensions/webworker/index.ts +58 -0
  195. package/src/index.ts +1 -0
  196. package/vendor/wasm3/.pin +1 -0
  197. package/vendor/wasm3/LICENSE +21 -0
  198. package/vendor/wasm3/m3_api_libc.c +265 -0
  199. package/vendor/wasm3/m3_api_libc.h +19 -0
  200. package/vendor/wasm3/m3_api_meta_wasi.c +1107 -0
  201. package/vendor/wasm3/m3_api_tracer.c +173 -0
  202. package/vendor/wasm3/m3_api_tracer.h +19 -0
  203. package/vendor/wasm3/m3_api_uvwasi.c +1290 -0
  204. package/vendor/wasm3/m3_api_wasi.c +1207 -0
  205. package/vendor/wasm3/m3_api_wasi.h +74 -0
  206. package/vendor/wasm3/m3_api_wasi_host.h +137 -0
  207. package/vendor/wasm3/m3_api_wasi_posix.h +979 -0
  208. package/vendor/wasm3/m3_api_wasi_win32.h +1093 -0
  209. package/vendor/wasm3/m3_atomic.h +114 -0
  210. package/vendor/wasm3/m3_bind.c +174 -0
  211. package/vendor/wasm3/m3_bind.h +20 -0
  212. package/vendor/wasm3/m3_code.c +232 -0
  213. package/vendor/wasm3/m3_code.h +74 -0
  214. package/vendor/wasm3/m3_compile.c +6603 -0
  215. package/vendor/wasm3/m3_compile.h +354 -0
  216. package/vendor/wasm3/m3_config.h +448 -0
  217. package/vendor/wasm3/m3_config_platforms.h +345 -0
  218. package/vendor/wasm3/m3_core.c +1034 -0
  219. package/vendor/wasm3/m3_core.h +526 -0
  220. package/vendor/wasm3/m3_deterministic.c +65 -0
  221. package/vendor/wasm3/m3_deterministic.h +59 -0
  222. package/vendor/wasm3/m3_env.c +3146 -0
  223. package/vendor/wasm3/m3_env.h +748 -0
  224. package/vendor/wasm3/m3_exception.h +33 -0
  225. package/vendor/wasm3/m3_exec.c +8 -0
  226. package/vendor/wasm3/m3_exec.h +4110 -0
  227. package/vendor/wasm3/m3_exec_atomic.h +146 -0
  228. package/vendor/wasm3/m3_exec_defs.h +76 -0
  229. package/vendor/wasm3/m3_function.c +278 -0
  230. package/vendor/wasm3/m3_function.h +244 -0
  231. package/vendor/wasm3/m3_host.h +226 -0
  232. package/vendor/wasm3/m3_host_none.h +76 -0
  233. package/vendor/wasm3/m3_host_posix.h +543 -0
  234. package/vendor/wasm3/m3_host_win32.h +253 -0
  235. package/vendor/wasm3/m3_info.c +616 -0
  236. package/vendor/wasm3/m3_info.h +38 -0
  237. package/vendor/wasm3/m3_math_utils.h +560 -0
  238. package/vendor/wasm3/m3_module.c +431 -0
  239. package/vendor/wasm3/m3_parse.c +1549 -0
  240. package/vendor/wasm3/m3_snapshot.c +3825 -0
  241. package/vendor/wasm3/m3_validate.c +1770 -0
  242. package/vendor/wasm3/m3_validate.h +25 -0
  243. package/vendor/wasm3/m3_wasi.h +10 -0
  244. package/vendor/wasm3/m3_xxh64.c +211 -0
  245. package/vendor/wasm3/wasm3.h +684 -0
  246. package/vendor/wasm3/wasm3_defs.h +350 -0
  247. package/dist/src/driver/bundler/namespace-import.js.map +0 -1
  248. package/dist/src/driver/bundler/reexport-fixup.js.map +0 -1
  249. package/runtime/ext_node/worker_threads/worker_threads.c +0 -214
@@ -1,27 +1,40 @@
1
1
  /**
2
2
  * Node's `worker_threads` module.
3
3
  *
4
- * xbintsc emulates a worker by re-running the current executable as a child
5
- * process; `isMainThread`, `workerData` and `parentPort` become value bindings
6
- * (nullary runtime getters) and `Worker` is a constructor. See
7
- * `runtime/ext_node/worker_threads/worker_threads.c`.
4
+ * A worker is a second process running the same executable (xbintsc has no
5
+ * threads; see `runtime/worker/`). The worker entry is the current program, so
6
+ * pass `import.meta.filename` and branch on `isMainThread`:
7
+ *
8
+ * if (isMainThread) {
9
+ * const worker = new Worker(import.meta.filename, { workerData: "ping" });
10
+ * worker.on("message", (reply) => console.log(reply));
11
+ * worker.postMessage("hello");
12
+ * } else {
13
+ * parentPort.on("message", (value) => parentPort.postMessage(value));
14
+ * }
15
+ *
16
+ * `MessageChannel` / `MessagePort` are local to a process; transferring a port
17
+ * to a worker is not implemented.
8
18
  */
9
19
 
10
20
  import type { NodeModule } from "../module.js";
11
- import { resolveFrom } from "../module.js";
12
21
  import type { ModuleExports } from "../../registry.js";
13
22
 
14
23
  const exports: ModuleExports = {
15
- Worker: { symbol: "xt_worker_ctor", isConstructor: true },
16
- isMainThread: { valueSymbol: "xt_worker_is_main_thread" },
17
- workerData: { valueSymbol: "xt_worker_data" },
18
- parentPort: { valueSymbol: "xt_worker_parent_port" },
19
- markAsUncloneable: { symbol: "xt_worker_mark_as_uncloneable" },
24
+ Worker: { symbol: "xt_wt_worker", isConstructor: true },
25
+ isMainThread: { valueSymbol: "xt_wt_is_main_thread" },
26
+ workerData: { valueSymbol: "xt_wt_worker_data" },
27
+ parentPort: { valueSymbol: "xt_wt_parent_port" },
28
+ MessageChannel: { symbol: "xt_wt_message_channel", isConstructor: true },
29
+ MessagePort: { symbol: "xt_wt_message_port", isConstructor: true },
30
+ receiveMessageOnPort: { symbol: "xt_wt_receive_message_on_port" },
31
+ markAsUncloneable: { symbol: "xt_wt_mark_as_uncloneable" },
20
32
  };
21
33
 
22
34
  export const workerThreadsModule: NodeModule = {
23
35
  name: "worker_threads",
24
- runtimeSources: () => [resolveFrom(import.meta.url, "../../../../runtime/ext_node/worker_threads/worker_threads.c")],
36
+ /** The transport lives in the core runtime; only the bindings are here. */
37
+ runtimeSources: () => [],
25
38
  builtins: () => ({}),
26
39
  exports: () => exports,
27
40
  };
@@ -14,6 +14,8 @@
14
14
  import type { LinkFeature } from "./link-feature.js";
15
15
  import { storageGlobals, storageLinkFeatures } from "./storage.js";
16
16
  import { tlsFeature } from "./tls.js";
17
+ import { wasmFeature } from "./wasm.js";
18
+ import { loadWasmAsset } from "./wasm-asset.js";
17
19
 
18
20
  export interface BuiltinFunction {
19
21
  /** Exported C symbol, called as `xt_value symbol(int32_t argc, xt_value *argv)`. */
@@ -104,7 +106,15 @@ export interface AssetSourceRange {
104
106
  }
105
107
 
106
108
  /** Rewrites an imported file (selected by extension) into a TS module. */
107
- export type AssetLoader = (path: string, source: string) => AssetLoadResult;
109
+ export type AssetLoader = {
110
+ (path: string, source: string): AssetLoadResult;
111
+ /**
112
+ * The loader consumes raw bytes rather than UTF-8 text. The bundler then
113
+ * decodes the file as a byte string (Latin-1) so every byte survives the
114
+ * `source` parameter; a `.wasm` loader uses this to embed a binary module.
115
+ */
116
+ readonly binary?: boolean;
117
+ };
108
118
 
109
119
  export interface Extension {
110
120
  readonly name: string;
@@ -356,7 +366,8 @@ export const coreExtension: Extension = {
356
366
  print: { symbol: "xt_println" },
357
367
  }),
358
368
  globalValues: () => storageGlobals,
359
- linkFeatures: () => [...storageLinkFeatures, tlsFeature],
369
+ linkFeatures: () => [...storageLinkFeatures, tlsFeature, wasmFeature],
370
+ assetLoaders: () => ({ ".wasm": loadWasmAsset }),
360
371
  };
361
372
 
362
373
  export function createDefaultRegistry(): ExtensionRegistry {
@@ -0,0 +1,32 @@
1
+ /**
2
+ * `.wasm` asset loader: embed an imported WebAssembly binary as a `Uint8Array`.
3
+ *
4
+ * The bundler passes binary loaders a byte string (one Latin-1 code unit per
5
+ * byte) so the module's bytes are not mangled by UTF-8 decoding. The generated
6
+ * module decodes base64 at startup and exports the typed array, which is
7
+ * exactly what `WebAssembly.compile` / `instantiate` accept:
8
+ *
9
+ * import bytes from "./add.wasm";
10
+ * const { instance } = await WebAssembly.instantiate(bytes);
11
+ *
12
+ * This keeps the binary inside the executable — the compiled program needs no
13
+ * file at run time.
14
+ */
15
+
16
+ import type { AssetLoader } from "./registry.js";
17
+
18
+ const loadWasmBytes: AssetLoader = (_path, source) => {
19
+ const base64 = Buffer.from(source, "latin1").toString("base64");
20
+ const moduleSource =
21
+ `const __xt_wasm_text = atob("${base64}");\n` +
22
+ "const __xt_wasm_bytes = new Uint8Array(__xt_wasm_text.length);\n" +
23
+ "for (let __xt_wasm_index = 0; __xt_wasm_index < __xt_wasm_text.length; __xt_wasm_index++) {\n" +
24
+ " __xt_wasm_bytes[__xt_wasm_index] = __xt_wasm_text.charCodeAt(__xt_wasm_index);\n" +
25
+ "}\n" +
26
+ "export default __xt_wasm_bytes;\n";
27
+ return { moduleSource };
28
+ };
29
+
30
+ export const loadWasmAsset: AssetLoader = Object.assign(loadWasmBytes, {
31
+ binary: true as const,
32
+ });
@@ -0,0 +1,74 @@
1
+ /**
2
+ * WebAssembly execution, contributed as the optional `wasm` link feature.
3
+ *
4
+ * `WebAssembly` is a browser namespace recognised by the core runtime, but the
5
+ * interpreter behind it (wasm3) is not something every program should link.
6
+ * The driver scans the bundled program for the identifier and compiles the
7
+ * bridge plus the pinned wasm3 sources only when it is reached. A program that
8
+ * never mentions `WebAssembly` links neither.
9
+ *
10
+ * wasm3 is vendored by commit in `vendor/wasm3` (see
11
+ * `scripts/fetch-vendored.ts`); the bridge installs its dispatch table from a
12
+ * constructor, which is what the core `xt_webassembly.c` calls into.
13
+ */
14
+
15
+ import { existsSync } from "node:fs";
16
+ import { join } from "node:path";
17
+ import type { LinkFeature, ProgramUsage } from "./link-feature.js";
18
+ import { findRuntimeDir, findVendorDir } from "../driver/paths.js";
19
+
20
+ /**
21
+ * The wasm3 translation units the interpreter needs. The API modules
22
+ * (`m3_api_*`) are deliberately absent: xbintsc supplies its own JS import
23
+ * bridge instead of wasi/libc, and the remaining `m3_*` files are the parser,
24
+ * validator, compiler and interpreter proper.
25
+ */
26
+ const WASM3_SOURCES = [
27
+ "m3_bind.c",
28
+ "m3_code.c",
29
+ "m3_compile.c",
30
+ "m3_core.c",
31
+ "m3_deterministic.c",
32
+ "m3_env.c",
33
+ "m3_exec.c",
34
+ "m3_function.c",
35
+ "m3_info.c",
36
+ "m3_module.c",
37
+ "m3_parse.c",
38
+ "m3_snapshot.c",
39
+ "m3_validate.c",
40
+ "m3_xxh64.c",
41
+ ] as const;
42
+
43
+ /** True when the bundled program can reach `WebAssembly`. */
44
+ export function hasWasmUsage(usage: ProgramUsage): boolean {
45
+ return usage.identifiers.has("WebAssembly");
46
+ }
47
+
48
+ export const wasmFeature: LinkFeature = {
49
+ name: "wasm",
50
+ description: "WebAssembly execution backed by the vendored wasm3 interpreter",
51
+ /* A program that uses WebAssembly cannot degrade to the core stub: the
52
+ * missing-vendor failure has to be a build error with the `npm run vendor`
53
+ * instruction, not a warning followed by an unresolved symbol at link time. */
54
+ required: true,
55
+ runtimeSources: () => {
56
+ const runtime = findRuntimeDir();
57
+ const vendor = join(findVendorDir(), "wasm3");
58
+ if (!existsSync(join(vendor, "wasm3.h"))) {
59
+ throw new Error(
60
+ "wasm3 sources are missing (vendor/wasm3/wasm3.h); run `npm run vendor` in a source " +
61
+ "checkout, or reinstall xbintsc so the package's vendored sources are present.",
62
+ );
63
+ }
64
+ return [
65
+ ...WASM3_SOURCES.map((source) => join(vendor, source)),
66
+ join(runtime, "xt_wasm", "wasm_bridge.c"),
67
+ join(runtime, "xt_wasm", "wasm_api.c"),
68
+ join(runtime, "xt_wasm", "wasm_values.c"),
69
+ join(runtime, "xt_wasm", "wasm_module.c"),
70
+ join(runtime, "xt_wasm", "wasm_instance.c"),
71
+ ];
72
+ },
73
+ used: hasWasmUsage,
74
+ };
@@ -0,0 +1,58 @@
1
+ /**
2
+ * Web Worker API.
3
+ *
4
+ * xbintsc has no threads: the value model, the collector and the event loop are
5
+ * single-threaded. A worker is therefore a **second process** running the same
6
+ * executable (see `runtime/worker/`). The public surface follows the Web
7
+ * Worker API:
8
+ *
9
+ * const worker = new Worker(import.meta.filename);
10
+ * worker.onmessage = (event) => console.log(event.data);
11
+ * worker.postMessage("ping");
12
+ *
13
+ * // inside the worker process
14
+ * self.onmessage = (event) => self.postMessage(event.data.toUpperCase());
15
+ *
16
+ * Because the binary is compiled ahead of time, the only worker script it can
17
+ * run is itself: pass `import.meta.filename` and branch on the `isMainThread`
18
+ * global (the same flag Node's `worker_threads` exports).
19
+ *
20
+ * `MessageChannel` / `MessagePort` work within one process (a worker cannot
21
+ * transfer a port to its parent yet).
22
+ */
23
+
24
+ import type { Extension, ExtensionModule, GlobalValue } from "../registry.js";
25
+
26
+ const globals: Record<string, GlobalValue> = {
27
+ Worker: { symbol: "xt_webworker_worker" },
28
+ MessageChannel: { symbol: "xt_webworker_message_channel" },
29
+ MessagePort: { symbol: "xt_webworker_message_port" },
30
+ /** The worker global scope. In the main thread it exists but `postMessage`
31
+ * throws: messages go through a `Worker` object there. */
32
+ self: { symbol: "xt_webworker_self" },
33
+ /** True in the main program, false inside a worker. */
34
+ isMainThread: { symbol: "xt_webworker_is_main_thread" },
35
+ };
36
+
37
+ const webworkerModule: ExtensionModule = {
38
+ namespace: "webworker",
39
+ exports: {
40
+ Worker: { symbol: "xt_webworker_worker_new", isConstructor: true },
41
+ MessageChannel: { symbol: "xt_webworker_message_channel_new", isConstructor: true },
42
+ MessagePort: { symbol: "xt_webworker_message_port_new", isConstructor: true },
43
+ self: { valueSymbol: "xt_webworker_self" },
44
+ isMainThread: { valueSymbol: "xt_webworker_is_main_thread" },
45
+ postMessage: { symbol: "xt_webworker_post_message" },
46
+ },
47
+ };
48
+
49
+ export const webworkerExtension: Extension = {
50
+ name: "webworker",
51
+ description: "Web Worker API (Worker, self, MessageChannel) backed by process-isolated workers",
52
+ globalValues: () => globals,
53
+ builtins: () => ({
54
+ /** Bare `postMessage(data)` inside a worker, as browser scripts use it. */
55
+ postMessage: { symbol: "xt_webworker_post_message" },
56
+ }),
57
+ modules: () => ({ webworker: webworkerModule }),
58
+ };
package/src/index.ts CHANGED
@@ -35,6 +35,7 @@ export type { LinkFeature, ProgramUsage } from "./extensions/link-feature.js";
35
35
  export type { LinkFeatureRequest, ResolvedLinkFeature } from "./driver/link-features.js";
36
36
  export { bundledExtensions } from "./extensions/catalog.js";
37
37
  export { nodeExtension } from "./extensions/node/index.js";
38
+ export { webworkerExtension } from "./extensions/webworker/index.js";
38
39
  export type { NodeModule } from "./extensions/node/module.js";
39
40
  export {
40
41
  NativeExtensionError,
@@ -0,0 +1 @@
1
+ 28ecb9af6d2040e474a70f7cb7f43666740141fb 5ecbb8851bd9acb54a83be10f0536167cb0da13b007406578761672b0bd94b18
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 Steven Massey, Volodymyr Shymanskyy
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,265 @@
1
+ //
2
+ // m3_api_libc.c
3
+ //
4
+ // Created by Volodymyr Shymanskyy on 11/20/19.
5
+ // Copyright © 2019 Volodymyr Shymanskyy. All rights reserved.
6
+ //
7
+
8
+ #include "m3_api_libc.h"
9
+
10
+ #include "m3_env.h"
11
+ #include "m3_deterministic.h"
12
+ #include "m3_exception.h"
13
+
14
+ #include <time.h>
15
+ #include <errno.h>
16
+ #include <stdio.h>
17
+
18
+ typedef uint32_t wasm_ptr_t;
19
+ typedef uint32_t wasm_size_t;
20
+
21
+ m3ApiRawFunction(m3_libc_abort)
22
+ {
23
+ m3ApiTrap(m3Err_trapAbort);
24
+ }
25
+
26
+ m3ApiRawFunction(m3_libc_exit)
27
+ {
28
+ m3ApiGetArg(int32_t, code)
29
+ (void)code;
30
+
31
+ m3ApiTrap(m3Err_trapExit);
32
+ }
33
+
34
+
35
+ m3ApiRawFunction(m3_libc_memset)
36
+ {
37
+ m3ApiReturnType(int32_t)
38
+
39
+ m3ApiGetArgMem(void*, i_ptr)
40
+ m3ApiGetArg(int32_t, i_value)
41
+ m3ApiGetArg(wasm_size_t, i_size)
42
+
43
+ m3ApiCheckMem(i_ptr, i_size);
44
+
45
+ u32 result = m3ApiPtrToOffset(memset(i_ptr, i_value, i_size));
46
+ m3ApiReturn(result);
47
+ }
48
+
49
+ m3ApiRawFunction(m3_libc_memmove)
50
+ {
51
+ m3ApiReturnType(int32_t)
52
+
53
+ m3ApiGetArgMem(void*, o_dst)
54
+ m3ApiGetArgMem(void*, i_src)
55
+ m3ApiGetArg(wasm_size_t, i_size)
56
+
57
+ m3ApiCheckMem(o_dst, i_size);
58
+ m3ApiCheckMem(i_src, i_size);
59
+
60
+ u32 result = m3ApiPtrToOffset(memmove(o_dst, i_src, i_size));
61
+ m3ApiReturn(result);
62
+ }
63
+
64
+ m3ApiRawFunction(m3_libc_print)
65
+ {
66
+ m3ApiReturnType(uint32_t)
67
+
68
+ m3ApiGetArgMem(void*, i_ptr)
69
+ m3ApiGetArg(wasm_size_t, i_size)
70
+
71
+ m3ApiCheckMem(i_ptr, i_size);
72
+
73
+ fwrite(i_ptr, i_size, 1, stdout);
74
+ fflush(stdout);
75
+
76
+ m3ApiReturn(i_size);
77
+ }
78
+
79
+ static
80
+ void internal_itoa (int n, char s[], int radix)
81
+ {
82
+ static char const HEXDIGITS[0x10] = {
83
+ '0', '1', '2', '3', '4', '5', '6', '7',
84
+ '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
85
+ };
86
+
87
+ int i, j, sign;
88
+ char c;
89
+
90
+ if ((sign = n) < 0) {
91
+ n = -n;
92
+ }
93
+ i = 0;
94
+ do {
95
+ s[i++] = HEXDIGITS[n % radix];
96
+ } while ((n /= radix) > 0);
97
+
98
+ if (sign < 0) {
99
+ s[i++] = '-';
100
+ }
101
+ s[i] = '\0';
102
+
103
+ // reverse
104
+ for (i = 0, j = (int)strlen(s) - 1; i < j; i++, j--) {
105
+ c = s[i];
106
+ s[i] = s[j];
107
+ s[j] = c;
108
+ }
109
+ }
110
+
111
+ m3ApiRawFunction(m3_libc_printf)
112
+ {
113
+ m3ApiReturnType(int32_t)
114
+
115
+ m3ApiGetArgMem(const char*, i_fmt)
116
+ m3ApiGetArgMem(wasm_ptr_t*, i_args)
117
+
118
+ if (m3ApiIsNullPtr(i_fmt)) {
119
+ m3ApiReturn(0);
120
+ }
121
+
122
+ m3ApiCheckMem(i_fmt, 1);
123
+ size_t fmt_len = strnlen(i_fmt, 1024);
124
+ m3ApiCheckMem(i_fmt, fmt_len + 1); // include `\0`
125
+
126
+ FILE* file = stdout;
127
+ int32_t length = 0;
128
+
129
+ char ch;
130
+ while ((ch = *i_fmt++)) {
131
+ if ('%' != ch) {
132
+ putc(ch, file);
133
+ length++;
134
+ continue;
135
+ }
136
+ // A trailing '%' has no conversion character: the next byte is the
137
+ // terminating NUL. Stop here rather than consuming it, which would
138
+ // step the cursor past the end of the guest string.
139
+ if (not *i_fmt) {
140
+ break;
141
+ }
142
+ ch = *i_fmt++;
143
+ switch (ch) {
144
+ case 'c': {
145
+ m3ApiCheckMem(i_args, sizeof(wasm_ptr_t));
146
+ char char_temp = *i_args++;
147
+ fputc(char_temp, file);
148
+ length++;
149
+ break;
150
+ }
151
+ case 'd':
152
+ case 'x': {
153
+ m3ApiCheckMem(i_args, sizeof(wasm_ptr_t));
154
+ int int_temp = *i_args++;
155
+ char buffer[32] = { 0 };
156
+ internal_itoa(int_temp, buffer, (ch == 'x') ? 16 : 10);
157
+ fputs(buffer, file);
158
+ length += (int32_t)strnlen(buffer, sizeof(buffer));
159
+ break;
160
+ }
161
+ case 's': {
162
+ m3ApiCheckMem(i_args, sizeof(wasm_ptr_t));
163
+ const char* string_temp;
164
+ size_t string_len;
165
+
166
+ string_temp = (const char*)m3ApiOffsetToPtr(*i_args++);
167
+ if (m3ApiIsNullPtr(string_temp)) {
168
+ string_temp = "(null)";
169
+ string_len = 6;
170
+ } else {
171
+ string_len = strnlen(string_temp, 1024);
172
+ m3ApiCheckMem(string_temp, string_len + 1);
173
+ }
174
+
175
+ fwrite(string_temp, 1, string_len, file);
176
+ length += (int32_t)string_len;
177
+ break;
178
+ default:
179
+ fputc(ch, file);
180
+ length++;
181
+ break;
182
+ }
183
+ }
184
+ }
185
+
186
+ m3ApiReturn(length);
187
+ }
188
+
189
+ static
190
+ uint64_t clock_ms (IM3Runtime i_runtime)
191
+ {
192
+ #if d_m3DeterministicProfile
193
+ return i_runtime ? Deterministic_Time(i_runtime) / 1000000 : 0;
194
+ #else
195
+ (void)i_runtime;
196
+
197
+ # ifdef CLOCKS_PER_SEC
198
+ const clock_t clock_divider = CLOCKS_PER_SEC / 1000;
199
+ return (uint64_t)(clock() / (clock_divider > 0 ? clock_divider : 1));
200
+ # else
201
+ return (uint64_t)clock();
202
+ # endif
203
+ #endif
204
+ }
205
+
206
+ m3ApiRawFunction(m3_libc_clock_ms)
207
+ {
208
+ m3ApiReturnType(uint32_t)
209
+
210
+ m3ApiReturn((uint32_t)clock_ms(runtime));
211
+ }
212
+
213
+ m3ApiRawFunction(m3_libc_clock_ms_i64)
214
+ {
215
+ m3ApiReturnType(uint64_t)
216
+
217
+ m3ApiReturn(clock_ms(runtime));
218
+ }
219
+
220
+ static
221
+ M3Result SuppressLookupFailure (M3Result i_result)
222
+ {
223
+ if (i_result == m3Err_functionLookupFailed or i_result == m3Err_globalLookupFailed) {
224
+ return m3Err_none;
225
+ } else {
226
+ return i_result;
227
+ }
228
+ }
229
+
230
+ // wasm-coremark's minimal build imports `u64 env.clock_ms()`, while older ones
231
+ // - source/extra/coremark_minimal.wasm.h among them - import the i32 form.
232
+ // Bind whichever of the two the module actually declares.
233
+ static
234
+ M3Result LinkClockMs (IM3Module module, ccstr_t i_moduleName)
235
+ {
236
+ M3Result result = m3_LinkRawFunction(module, i_moduleName, "clock_ms", "I()", &m3_libc_clock_ms_i64);
237
+
238
+ if (result == m3Err_functionSignatureMismatch) {
239
+ result = m3_LinkRawFunction(module, i_moduleName, "clock_ms", "i()", &m3_libc_clock_ms);
240
+ }
241
+
242
+ return SuppressLookupFailure(result);
243
+ }
244
+
245
+ M3Result m3_LinkLibC (IM3Module module)
246
+ {
247
+ M3Result result = m3Err_none;
248
+
249
+ const char* env = "env";
250
+ // clang-format off
251
+
252
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, env, "_debug", "i(*i)", &m3_libc_print)));
253
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, env, "_memset", "*(*ii)", &m3_libc_memset)));
254
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, env, "_memmove", "*(**i)", &m3_libc_memmove)));
255
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, env, "_memcpy", "*(**i)", &m3_libc_memmove))); // just alias of memmove
256
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, env, "_abort", "v()", &m3_libc_abort)));
257
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, env, "_exit", "v(i)", &m3_libc_exit)));
258
+ _ (LinkClockMs(module, env));
259
+ _ (SuppressLookupFailure(m3_LinkRawFunction(module, env, "printf", "i(**)", &m3_libc_printf)));
260
+
261
+ // clang-format on
262
+
263
+ _catch:
264
+ return result;
265
+ }
@@ -0,0 +1,19 @@
1
+ //
2
+ // m3_api_libc.h
3
+ //
4
+ // Created by Volodymyr Shymanskyy on 11/20/19.
5
+ // Copyright © 2019 Volodymyr Shymanskyy. All rights reserved.
6
+ //
7
+
8
+ #ifndef m3_api_libc_h
9
+ #define m3_api_libc_h
10
+
11
+ #include "m3_core.h"
12
+
13
+ d_m3BeginExternC
14
+
15
+ M3Result m3_LinkLibC (IM3Module io_module);
16
+
17
+ d_m3EndExternC
18
+
19
+ #endif // m3_api_libc_h