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.
- package/AGENTS.md +1 -1
- package/dist/src/cli/extensions.js +11 -3
- package/dist/src/cli/extensions.js.map +1 -1
- package/dist/src/cli/help.d.ts +1 -1
- package/dist/src/cli/help.js +1 -1
- package/dist/src/codegen/generator/state.js +7 -0
- package/dist/src/codegen/generator/state.js.map +1 -1
- package/dist/src/codegen/generator/tables.js +19 -1
- package/dist/src/codegen/generator/tables.js.map +1 -1
- package/dist/src/driver/bundler/concatenate.d.ts +23 -0
- package/dist/src/driver/bundler/concatenate.js +210 -0
- package/dist/src/driver/bundler/concatenate.js.map +1 -0
- package/dist/src/driver/bundler/graph.js +9 -5
- package/dist/src/driver/bundler/graph.js.map +1 -1
- package/dist/src/driver/bundler/merge.d.ts +4 -4
- package/dist/src/driver/bundler/merge.js +24 -207
- package/dist/src/driver/bundler/merge.js.map +1 -1
- package/dist/src/driver/bundler/{namespace-import.d.ts → namespace-object.d.ts} +5 -1
- package/dist/src/driver/bundler/{namespace-import.js → namespace-object.js} +6 -2
- package/dist/src/driver/bundler/namespace-object.js.map +1 -0
- package/dist/src/driver/bundler/{reexport-fixup.d.ts → reexport-resolution.d.ts} +9 -3
- package/dist/src/driver/bundler/{reexport-fixup.js → reexport-resolution.js} +16 -6
- package/dist/src/driver/bundler/reexport-resolution.js.map +1 -0
- package/dist/src/driver/fingerprint.d.ts +2 -2
- package/dist/src/driver/fingerprint.js +3 -3
- package/dist/src/driver/fingerprint.js.map +1 -1
- package/dist/src/driver/link-features.js +1 -1
- package/dist/src/driver/link-features.js.map +1 -1
- package/dist/src/driver/modules.d.ts +12 -5
- package/dist/src/driver/modules.js +12 -5
- package/dist/src/driver/modules.js.map +1 -1
- package/dist/src/driver/paths.js +8 -4
- package/dist/src/driver/paths.js.map +1 -1
- package/dist/src/driver/runtime-objects.d.ts +1 -1
- package/dist/src/driver/runtime-objects.js +13 -1
- package/dist/src/driver/runtime-objects.js.map +1 -1
- package/dist/src/driver/version.d.ts +1 -1
- package/dist/src/driver/version.js +1 -1
- package/dist/src/extensions/catalog.js +5 -1
- package/dist/src/extensions/catalog.js.map +1 -1
- package/dist/src/extensions/gui/index.js +0 -1
- package/dist/src/extensions/gui/index.js.map +1 -1
- package/dist/src/extensions/link-feature.d.ts +8 -0
- package/dist/src/extensions/node/stream-web/index.d.ts +6 -1
- package/dist/src/extensions/node/stream-web/index.js +59 -4
- package/dist/src/extensions/node/stream-web/index.js.map +1 -1
- package/dist/src/extensions/node/worker_threads/index.d.ts +14 -4
- package/dist/src/extensions/node/worker_threads/index.js +24 -11
- package/dist/src/extensions/node/worker_threads/index.js.map +1 -1
- package/dist/src/extensions/registry.d.ts +9 -1
- package/dist/src/extensions/registry.js +4 -1
- package/dist/src/extensions/registry.js.map +1 -1
- package/dist/src/extensions/wasm-asset.d.ts +16 -0
- package/dist/src/extensions/wasm-asset.js +28 -0
- package/dist/src/extensions/wasm-asset.js.map +1 -0
- package/dist/src/extensions/wasm.d.ts +17 -0
- package/dist/src/extensions/wasm.js +68 -0
- package/dist/src/extensions/wasm.js.map +1 -0
- package/dist/src/extensions/webworker/index.d.ts +24 -0
- package/dist/src/extensions/webworker/index.js +54 -0
- package/dist/src/extensions/webworker/index.js.map +1 -0
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.js +1 -0
- package/dist/src/index.js.map +1 -1
- package/dist/tests/driver/deferred-reexport.test.js +25 -0
- package/dist/tests/driver/deferred-reexport.test.js.map +1 -1
- package/dist/tests/driver/link-features.test.js +9 -0
- package/dist/tests/driver/link-features.test.js.map +1 -1
- package/dist/tests/e2e/async-fs.test.js +44 -0
- package/dist/tests/e2e/async-fs.test.js.map +1 -1
- package/dist/tests/e2e/harness.d.ts +6 -0
- package/dist/tests/e2e/harness.js +13 -1
- package/dist/tests/e2e/harness.js.map +1 -1
- package/dist/tests/e2e/node-crypto-random.test.d.ts +8 -0
- package/dist/tests/e2e/node-crypto-random.test.js +148 -0
- package/dist/tests/e2e/node-crypto-random.test.js.map +1 -0
- package/dist/tests/e2e/node-modules.test.js +1 -1
- package/dist/tests/e2e/node-modules.test.js.map +1 -1
- package/dist/tests/e2e/web-streams.test.d.ts +8 -0
- package/dist/tests/e2e/web-streams.test.js +274 -0
- package/dist/tests/e2e/web-streams.test.js.map +1 -0
- package/dist/tests/e2e/web-worker.test.d.ts +7 -0
- package/dist/tests/e2e/web-worker.test.js +74 -0
- package/dist/tests/e2e/web-worker.test.js.map +1 -0
- package/dist/tests/e2e/webassembly.test.d.ts +8 -0
- package/dist/tests/e2e/webassembly.test.js +113 -0
- package/dist/tests/e2e/webassembly.test.js.map +1 -0
- package/dist/tests/e2e/worker-api.test.d.ts +14 -0
- package/dist/tests/e2e/worker-api.test.js +460 -0
- package/dist/tests/e2e/worker-api.test.js.map +1 -0
- package/dist/tests/e2e/worker-threads.test.d.ts +8 -0
- package/dist/tests/e2e/worker-threads.test.js +160 -0
- package/dist/tests/e2e/worker-threads.test.js.map +1 -0
- package/dist/tests/extensions/wasm.test.d.ts +1 -0
- package/dist/tests/extensions/wasm.test.js +53 -0
- package/dist/tests/extensions/wasm.test.js.map +1 -0
- package/doc/ai/cli.md +1 -1
- package/doc/ai/contributing.md +11 -4
- package/doc/ai/extensions.md +25 -2
- package/doc/ai/language-support.md +14 -2
- package/doc/ai/zh-CN/cli.md +1 -1
- package/doc/ai/zh-CN/contributing.md +8 -3
- package/doc/ai/zh-CN/extensions.md +23 -2
- package/doc/ai/zh-CN/language-support.md +13 -2
- package/doc/gui/api.md +1 -1
- package/doc/implemented/cli.md +1 -1
- package/doc/implemented/extensions.md +6 -5
- package/doc/implemented/node/stream-web.md +31 -0
- package/doc/implemented/node/worker-threads.md +88 -5
- package/doc/implemented/node.md +2 -1
- package/doc/implemented/streams.md +82 -0
- package/doc/implemented/wasm.md +80 -18
- package/doc/implemented/web-worker.md +84 -0
- package/doc/implemented.md +6 -2
- package/doc/unimplemented/deviations.md +1 -0
- package/doc/unimplemented/node/worker-threads.md +23 -0
- package/doc/unimplemented/node.md +1 -0
- package/doc/unimplemented/stdlib.md +2 -1
- package/doc/unimplemented/wasm.md +21 -9
- package/doc/unimplemented.md +1 -1
- package/doc/zh-CN/gui/api.md +1 -1
- package/doc/zh-CN/implemented/cli.md +1 -1
- package/doc/zh-CN/implemented/extensions.md +7 -6
- package/doc/zh-CN/implemented/node/stream-web.md +24 -0
- package/doc/zh-CN/implemented/node/worker-threads.md +77 -4
- package/doc/zh-CN/implemented/node.md +2 -1
- package/doc/zh-CN/implemented/streams.md +60 -0
- package/doc/zh-CN/implemented/wasm.md +73 -16
- package/doc/zh-CN/implemented/web-worker.md +79 -0
- package/doc/zh-CN/implemented.md +6 -2
- package/doc/zh-CN/unimplemented/deviations.md +1 -0
- package/doc/zh-CN/unimplemented/node/worker-threads.md +20 -0
- package/doc/zh-CN/unimplemented/node.md +1 -0
- package/doc/zh-CN/unimplemented/stdlib.md +2 -1
- package/doc/zh-CN/unimplemented/wasm.md +16 -8
- package/doc/zh-CN/unimplemented.md +1 -1
- package/llms.txt +2 -0
- package/package.json +8 -7
- package/runtime/ext_gui/browser.cpp +1 -1
- package/runtime/ext_node/stream_web/stream_web.c +20 -6
- package/runtime/worker/worker_api.c +422 -0
- package/runtime/worker/worker_codec.c +489 -0
- package/runtime/worker/worker_core.c +445 -0
- package/runtime/worker/worker_internal.h +223 -0
- package/runtime/worker/worker_platform.c +344 -0
- package/runtime/worker/worker_ports.c +203 -0
- package/runtime/worker/worker_transport.c +464 -0
- package/runtime/worker/worker_wire.c +150 -0
- package/runtime/xt_builtins.c +15 -0
- package/runtime/xt_constructors.c +49 -2
- package/runtime/xt_stdlib2/error.inc +4 -0
- package/runtime/xt_streams/common.inc +232 -0
- package/runtime/xt_streams/constructors.inc +232 -0
- package/runtime/xt_streams/encoding.inc +124 -0
- package/runtime/xt_streams/readable-extra.inc +372 -0
- package/runtime/xt_streams/readable.inc +415 -0
- package/runtime/xt_streams/transform.inc +195 -0
- package/runtime/xt_streams/writable.inc +542 -0
- package/runtime/xt_streams.c +30 -0
- package/runtime/xt_wasm/wasm.h +148 -0
- package/runtime/xt_wasm/wasm_api.c +357 -0
- package/runtime/xt_wasm/wasm_api.h +37 -0
- package/runtime/xt_wasm/wasm_bridge.c +30 -0
- package/runtime/xt_wasm/wasm_instance.c +591 -0
- package/runtime/xt_wasm/wasm_module.c +270 -0
- package/runtime/xt_wasm/wasm_values.c +177 -0
- package/runtime/xt_webassembly.c +15 -4
- package/scripts/fetch-vendored.ts +187 -20
- package/scripts/package-release.ts +3 -2
- package/src/cli/extensions.ts +11 -3
- package/src/cli/help.ts +1 -1
- package/src/codegen/generator/state.ts +7 -0
- package/src/codegen/generator/tables.ts +19 -1
- package/src/driver/bundler/README.md +55 -0
- package/src/driver/bundler/concatenate.ts +244 -0
- package/src/driver/bundler/graph.ts +9 -5
- package/src/driver/bundler/merge.ts +24 -215
- package/src/driver/bundler/{namespace-import.ts → namespace-object.ts} +5 -1
- package/src/driver/bundler/{reexport-fixup.ts → reexport-resolution.ts} +15 -5
- package/src/driver/fingerprint.ts +3 -3
- package/src/driver/link-features.ts +1 -1
- package/src/driver/modules.ts +12 -5
- package/src/driver/paths.ts +8 -4
- package/src/driver/runtime-objects.ts +13 -1
- package/src/driver/version.ts +1 -1
- package/src/extensions/catalog.ts +5 -1
- package/src/extensions/gui/index.ts +0 -1
- package/src/extensions/link-feature.ts +8 -0
- package/src/extensions/node/stream-web/index.ts +59 -4
- package/src/extensions/node/worker_threads/index.ts +24 -11
- package/src/extensions/registry.ts +13 -2
- package/src/extensions/wasm-asset.ts +32 -0
- package/src/extensions/wasm.ts +74 -0
- package/src/extensions/webworker/index.ts +58 -0
- package/src/index.ts +1 -0
- package/vendor/wasm3/.pin +1 -0
- package/vendor/wasm3/LICENSE +21 -0
- package/vendor/wasm3/m3_api_libc.c +265 -0
- package/vendor/wasm3/m3_api_libc.h +19 -0
- package/vendor/wasm3/m3_api_meta_wasi.c +1107 -0
- package/vendor/wasm3/m3_api_tracer.c +173 -0
- package/vendor/wasm3/m3_api_tracer.h +19 -0
- package/vendor/wasm3/m3_api_uvwasi.c +1290 -0
- package/vendor/wasm3/m3_api_wasi.c +1207 -0
- package/vendor/wasm3/m3_api_wasi.h +74 -0
- package/vendor/wasm3/m3_api_wasi_host.h +137 -0
- package/vendor/wasm3/m3_api_wasi_posix.h +979 -0
- package/vendor/wasm3/m3_api_wasi_win32.h +1093 -0
- package/vendor/wasm3/m3_atomic.h +114 -0
- package/vendor/wasm3/m3_bind.c +174 -0
- package/vendor/wasm3/m3_bind.h +20 -0
- package/vendor/wasm3/m3_code.c +232 -0
- package/vendor/wasm3/m3_code.h +74 -0
- package/vendor/wasm3/m3_compile.c +6603 -0
- package/vendor/wasm3/m3_compile.h +354 -0
- package/vendor/wasm3/m3_config.h +448 -0
- package/vendor/wasm3/m3_config_platforms.h +345 -0
- package/vendor/wasm3/m3_core.c +1034 -0
- package/vendor/wasm3/m3_core.h +526 -0
- package/vendor/wasm3/m3_deterministic.c +65 -0
- package/vendor/wasm3/m3_deterministic.h +59 -0
- package/vendor/wasm3/m3_env.c +3146 -0
- package/vendor/wasm3/m3_env.h +748 -0
- package/vendor/wasm3/m3_exception.h +33 -0
- package/vendor/wasm3/m3_exec.c +8 -0
- package/vendor/wasm3/m3_exec.h +4110 -0
- package/vendor/wasm3/m3_exec_atomic.h +146 -0
- package/vendor/wasm3/m3_exec_defs.h +76 -0
- package/vendor/wasm3/m3_function.c +278 -0
- package/vendor/wasm3/m3_function.h +244 -0
- package/vendor/wasm3/m3_host.h +226 -0
- package/vendor/wasm3/m3_host_none.h +76 -0
- package/vendor/wasm3/m3_host_posix.h +543 -0
- package/vendor/wasm3/m3_host_win32.h +253 -0
- package/vendor/wasm3/m3_info.c +616 -0
- package/vendor/wasm3/m3_info.h +38 -0
- package/vendor/wasm3/m3_math_utils.h +560 -0
- package/vendor/wasm3/m3_module.c +431 -0
- package/vendor/wasm3/m3_parse.c +1549 -0
- package/vendor/wasm3/m3_snapshot.c +3825 -0
- package/vendor/wasm3/m3_validate.c +1770 -0
- package/vendor/wasm3/m3_validate.h +25 -0
- package/vendor/wasm3/m3_wasi.h +10 -0
- package/vendor/wasm3/m3_xxh64.c +211 -0
- package/vendor/wasm3/wasm3.h +684 -0
- package/vendor/wasm3/wasm3_defs.h +350 -0
- package/dist/src/driver/bundler/namespace-import.js.map +0 -1
- package/dist/src/driver/bundler/reexport-fixup.js.map +0 -1
- 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
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
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: "
|
|
16
|
-
isMainThread: { valueSymbol: "
|
|
17
|
-
workerData: { valueSymbol: "
|
|
18
|
-
parentPort: { valueSymbol: "
|
|
19
|
-
|
|
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
|
-
|
|
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 =
|
|
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
|