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
package/doc/implemented/wasm.md
CHANGED
|
@@ -2,21 +2,83 @@
|
|
|
2
2
|
|
|
3
3
|
> Part of [Implemented syntax and features](../implemented.md) · [简体中文](../zh-CN/implemented/wasm.md)
|
|
4
4
|
|
|
5
|
-
Location: `src/
|
|
6
|
-
`runtime/xt_webassembly.c` (
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
5
|
+
Location: `src/extensions/wasm.ts` (link feature), `runtime/xt_wasm/` (bridge and
|
|
6
|
+
API), `runtime/xt_webassembly.c` (core namespace stub), `vendor/wasm3/`
|
|
7
|
+
(interpreter, fetched by `npm run vendor`).
|
|
8
|
+
|
|
9
|
+
## The `wasm` link feature
|
|
10
|
+
|
|
11
|
+
`WebAssembly` is a first-class namespace object, like `Math` or `crypto`, so
|
|
12
|
+
`const W = WebAssembly` and `typeof WebAssembly` both work. The interpreter
|
|
13
|
+
behind it is not linked into every program: a usage scan selects the **`wasm`
|
|
14
|
+
link feature** when the bundled source mentions `WebAssembly`, and only then
|
|
15
|
+
compiles the pinned wasm3 sources plus the bridge. `--feature wasm` forces it on
|
|
16
|
+
(a program that reaches `globalThis["WebAssembly"]` through a string needs
|
|
17
|
+
that), and `--no-feature wasm` keeps it out, in which case the namespace members
|
|
18
|
+
throw `WebAssembly is not supported in this build` at run time.
|
|
19
|
+
|
|
20
|
+
The feature is *required*: if `vendor/wasm3` is missing, the build fails with the
|
|
21
|
+
`npm run vendor` instruction instead of warning and producing a binary with an
|
|
22
|
+
unresolved symbol.
|
|
23
|
+
|
|
24
|
+
## Supported API
|
|
25
|
+
|
|
26
|
+
| Surface | Behaviour |
|
|
27
|
+
| --- | --- |
|
|
28
|
+
| `WebAssembly.validate(bytes)` | parses/validates a module; invalid bytecode returns `false` |
|
|
29
|
+
| `WebAssembly.compile(bytes)` | `Promise<Module>`; invalid bytecode rejects with `CompileError` |
|
|
30
|
+
| `WebAssembly.instantiate(bytes, imports?)` | `Promise<{ module, instance }>` |
|
|
31
|
+
| `WebAssembly.instantiate(module, imports?)` | `Promise<Instance>` |
|
|
32
|
+
| `new WebAssembly.Module(bytes)` | synchronous compile; throws `CompileError` |
|
|
33
|
+
| `new WebAssembly.Instance(module, imports?)` | synchronous instantiation; throws `LinkError` |
|
|
34
|
+
| `WebAssembly.Module.exports(module)` / `.imports(module)` | descriptor arrays (`{ name, kind }` / `{ module, name, kind }`) |
|
|
35
|
+
| `WebAssembly.Module.customSections(module, name)` | returns `[]` (custom sections are not exposed yet) |
|
|
36
|
+
| `new WebAssembly.Memory({ initial, maximum? })` | a standalone page-backed `ArrayBuffer` with `buffer` and `grow()` |
|
|
37
|
+
| `instance.exports.<function>` | callable from JS; `i32`/`f32`/`f64` are numbers, `i64` is a BigInt |
|
|
38
|
+
| `instance.exports.<memory>` | `.buffer` is an `ArrayBuffer` snapshot and `.grow(pages)` grows the wasm memory |
|
|
39
|
+
| JS function imports | called from wasm with their declared numeric types; a thrown JS exception propagates back to the wasm caller |
|
|
40
|
+
| `CompileError` / `LinkError` / `RuntimeError` | real `Error` subclasses; `instanceof WebAssembly.CompileError` and `instanceof Error` work |
|
|
41
|
+
|
|
42
|
+
Both `WebAssembly.compile` and `instantiate` settle through the normal promise
|
|
43
|
+
queue, so `await` works and timers/sockets continue to run while they are
|
|
44
|
+
pending.
|
|
45
|
+
|
|
46
|
+
### Memory is synchronised around calls
|
|
47
|
+
|
|
48
|
+
The runtime's `ArrayBuffer` is a JavaScript-heap byte array, not a live alias of
|
|
49
|
+
wasm3's linear memory. `instance.exports.<memory>.buffer` copies the wasm bytes
|
|
50
|
+
out, and every exported function call copies the JS buffer into wasm memory
|
|
51
|
+
before the call and back out afterwards. Reads and writes made through a typed
|
|
52
|
+
array are therefore visible to the next wasm call (and vice versa), but a view
|
|
53
|
+
is not a zero-copy window: a write is observed on the next call or `.buffer`
|
|
54
|
+
read, not instantly.
|
|
55
|
+
|
|
56
|
+
### `.wasm` imports
|
|
57
|
+
|
|
58
|
+
The core extension installs a binary asset loader, so a module can import a wasm
|
|
59
|
+
file directly:
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
import bytes from "./add.wasm";
|
|
63
|
+
|
|
64
|
+
const { instance } = await WebAssembly.instantiate(bytes, {});
|
|
65
|
+
console.log(instance.exports.add(2, 3));
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
The bytes are embedded in the executable (base64 in the generated module), so
|
|
69
|
+
the built program needs no `.wasm` file at run time.
|
|
70
|
+
|
|
71
|
+
## Limits
|
|
72
|
+
|
|
73
|
+
- Only **function** imports are bridged. A module that imports a memory, table or
|
|
74
|
+
global is rejected with a `LinkError`.
|
|
75
|
+
- Exported **tables** and **globals** are listed by `Module.exports` but are not
|
|
76
|
+
wrapped on `instance.exports`; only functions and memories are.
|
|
77
|
+
- `WebAssembly.Table` and `WebAssembly.Global` constructors are not implemented.
|
|
78
|
+
- `compileStreaming` / `instantiateStreaming` are not exposed.
|
|
79
|
+
- wasm3's supported instruction set bounds execution (no SIMD opcodes, no GC,
|
|
80
|
+
no host threads); the parser accepts the proposals the pinned wasm3 build
|
|
81
|
+
enables, and an unsupported body fails with a WebAssembly error when the
|
|
82
|
+
module is compiled or instantiated.
|
|
83
|
+
|
|
84
|
+
See [Unimplemented: WebAssembly](../unimplemented/wasm.md) for the tracked gaps.
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# Web Worker
|
|
2
|
+
|
|
3
|
+
> Part of [Implemented syntax and features](../implemented.md) · [简体中文](../zh-CN/implemented/web-worker.md)
|
|
4
|
+
|
|
5
|
+
Location: `src/extensions/webworker/index.ts`, `runtime/worker/`
|
|
6
|
+
|
|
7
|
+
Enable the extension with `--ext webworker` (or `"extensions": ["webworker"]`
|
|
8
|
+
in `xbintsc.config.json`). It adds the browser globals `Worker`,
|
|
9
|
+
`MessageChannel`, `MessagePort`, `self`, `isMainThread` and the bare
|
|
10
|
+
`postMessage` function used inside a worker script.
|
|
11
|
+
|
|
12
|
+
## How a worker runs
|
|
13
|
+
|
|
14
|
+
xbintsc has no threads: the value model, the collector and the event loop are
|
|
15
|
+
single-threaded. A worker is a **second process** running the same executable.
|
|
16
|
+
`new Worker(...)` starts it, the two processes talk over a loopback control
|
|
17
|
+
socket, and every message is structured-cloned in the sender and rebuilt in the
|
|
18
|
+
receiver (arrays, objects, `Map`, `Set`, `Date`, `RegExp`, `ArrayBuffer`,
|
|
19
|
+
typed arrays, `BigInt`, `undefined`, shared references and cycles).
|
|
20
|
+
|
|
21
|
+
Because the binary is compiled ahead of time, the only worker script it can run
|
|
22
|
+
is itself. Pass `import.meta.filename` and branch on the `isMainThread` global:
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
const worker = new Worker(import.meta.filename, { name: "echo" });
|
|
26
|
+
worker.onmessage = (event) => console.log("reply", event.data);
|
|
27
|
+
|
|
28
|
+
if (!isMainThread) {
|
|
29
|
+
self.onmessage = (event) => {
|
|
30
|
+
self.postMessage(String(event.data).toUpperCase());
|
|
31
|
+
self.close();
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
A different script path throws an actionable error instead of silently running
|
|
37
|
+
the wrong code.
|
|
38
|
+
|
|
39
|
+
## Surface
|
|
40
|
+
|
|
41
|
+
| API | Notes |
|
|
42
|
+
| --- | --- |
|
|
43
|
+
| `new Worker(url, options?)` | `url` must name this executable (`import.meta.filename`); `options.name` becomes `self.name`; `options.workerData` is not a Web Worker option (use the Node API) |
|
|
44
|
+
| `worker.postMessage(value, transfer?)` | The value is structured-cloned; `transfer` is accepted but copies |
|
|
45
|
+
| `worker.terminate()` | Kills the worker process and emits `exit` |
|
|
46
|
+
| `worker.onmessage` / `onmessageerror` / `onerror` | Receive `MessageEvent`-shaped objects (`type`, `data`, `target`) |
|
|
47
|
+
| `worker.addEventListener(type, fn)` / `removeEventListener` | `message`, `error`, `messageerror` |
|
|
48
|
+
| `self.postMessage(value)` | Worker → parent |
|
|
49
|
+
| `self.onmessage` / `addEventListener` | Parent → worker |
|
|
50
|
+
| `self.close()` | Stops the worker; the process exits once its loop drains |
|
|
51
|
+
| `self.name` | The `name` the parent passed |
|
|
52
|
+
| `postMessage(value)` | The bare global form of `self.postMessage` inside a worker |
|
|
53
|
+
| `new MessageChannel()` | `port1`/`port2`, with `postMessage`, `onmessage`, `addEventListener`, `close` |
|
|
54
|
+
|
|
55
|
+
Both conventions share one event list, so `worker.on("message", fn)` (Node) and
|
|
56
|
+
`worker.onmessage = fn` can be mixed: the Node listener receives the raw value,
|
|
57
|
+
the browser listener receives the `MessageEvent`. The same names are also
|
|
58
|
+
importable from the `webworker` module
|
|
59
|
+
(`import { Worker, self, isMainThread } from "webworker"`).
|
|
60
|
+
|
|
61
|
+
## Lifetime
|
|
62
|
+
|
|
63
|
+
The parent process stays alive while a worker runs. A worker stays alive while
|
|
64
|
+
it has a message listener (or other event-loop work such as a timer); a worker
|
|
65
|
+
that only posts and returns exits on its own. `self.close()` or
|
|
66
|
+
`worker.terminate()` ends it early, and the parent then emits `exit`.
|
|
67
|
+
|
|
68
|
+
## Combining with the GUI extension
|
|
69
|
+
|
|
70
|
+
`gui` also defines a `self` global (the window). `webworker` is registered after
|
|
71
|
+
`gui`, so when both extensions are enabled `self` is the worker scope and a page
|
|
72
|
+
uses `window` for the window object. Do not rely on `self === window` in a page
|
|
73
|
+
built with both.
|
|
74
|
+
|
|
75
|
+
## Limitations
|
|
76
|
+
|
|
77
|
+
- A worker cannot run a separate source file: everything is one binary, so the
|
|
78
|
+
script is the current program and the program branches on `isMainThread`.
|
|
79
|
+
- A `MessagePort` cannot be transferred to another process yet; sending one
|
|
80
|
+
throws `MessagePort could not be cloned.`
|
|
81
|
+
- `importScripts`, `SharedArrayBuffer` and real parallelism are not available
|
|
82
|
+
(workers are processes; values are copied, never shared).
|
|
83
|
+
- An uncaught exception in a worker prints on stderr and ends that process with
|
|
84
|
+
a non-zero code; the parent observes the `exit` event, not `error`.
|
package/doc/implemented.md
CHANGED
|
@@ -39,13 +39,15 @@ small file instead of a single monolithic document.
|
|
|
39
39
|
- [Typed arrays and binary data](./implemented/typed-arrays.md) — typed arrays, ArrayBuffer, DataView
|
|
40
40
|
- [Async, promises and generators](./implemented/async.md) — async/await, async generators, promises, timers
|
|
41
41
|
- [fetch](./implemented/fetch.md) — reactor-driven HTTP/1.1 client (HTTP and HTTPS) and Headers
|
|
42
|
-
- [
|
|
42
|
+
- [Web Streams](./implemented/streams.md) — `ReadableStream`, `WritableStream`, `TransformStream`, readers/writers and text streams
|
|
43
|
+
- [WebAssembly](./implemented/wasm.md) — wasm3-backed compilation/execution, the `WebAssembly` namespace and `.wasm` imports
|
|
44
|
+
- [Web Worker](./implemented/web-worker.md) — `Worker`, `self`, `MessageChannel` and the `webworker` extension
|
|
43
45
|
|
|
44
46
|
### Modules and tooling
|
|
45
47
|
|
|
46
48
|
- [Module system](./implemented/modules.md) — import/export, bundling, `node_modules`, `import.meta`
|
|
47
49
|
- [Extension mechanism](./implemented/extensions.md) — registry, the `node` extension, native C++/Rust extensions
|
|
48
|
-
- [Node extension](./implemented/node.md) —
|
|
50
|
+
- [Node extension](./implemented/node.md) — 25 Node built-in modules, one page per module
|
|
49
51
|
- [Storage extension](./implemented/storage.md) — SQLite-backed `localStorage`, `sessionStorage` and `indexedDB`
|
|
50
52
|
- [Driver, incremental compilation and toolchain](./implemented/driver.md) — cache, clang/LTO/PGO, config, icons
|
|
51
53
|
- [CLI and programmatic API](./implemented/cli.md) — commands, flags, embedder API
|
|
@@ -72,6 +74,8 @@ same file.
|
|
|
72
74
|
| typed arrays, ArrayBuffer, DataView | [Typed arrays](./implemented/typed-arrays.md) |
|
|
73
75
|
| async/await, promises, generators, timers | [Async](./implemented/async.md) |
|
|
74
76
|
| HTTP client | [fetch](./implemented/fetch.md) |
|
|
77
|
+
| `ReadableStream` / `WritableStream` / `TransformStream` | [Web Streams](./implemented/streams.md) |
|
|
78
|
+
| `Worker`, `self`, `MessageChannel` | [Web Worker](./implemented/web-worker.md) |
|
|
75
79
|
| import/export and bundling | [Modules](./implemented/modules.md) |
|
|
76
80
|
| extension registry or the `node` extension | [Extensions](./implemented/extensions.md) |
|
|
77
81
|
| a specific Node module (`fs`, `path`, `http`, ...) | [Node extension](./implemented/node.md) |
|
|
@@ -23,6 +23,7 @@ These features **compile and run**, but the result does not fully match ECMAScri
|
|
|
23
23
|
| `let` in a `for` loop header | One binding per loop, not one per iteration: closures created inside the body all observe the final value (`for (let i = 0; i < 3; i++) fns.push(() => i)` gives `3,3,3`) |
|
|
24
24
|
| Module live bindings | Namespace imports and imported bindings are snapshots (see [Module system](./modules.md)) |
|
|
25
25
|
| `import.meta` | ✓ `import.meta.url` / `.dirname` / `.filename`, resolved from the executable path |
|
|
26
|
+
| Workers | A worker is a separate **process**, not a thread: values are structured-cloned and never shared, `postMessage` transfer lists copy, and the worker script is the current binary (`import.meta.filename` plus `isMainThread`) |
|
|
26
27
|
|
|
27
28
|
> Number formatting, loose equality, `+` / relational `ToPrimitive`, whole-chain
|
|
28
29
|
> optional chaining, `finally` on early exit and object-key ordering have all
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# `worker_threads` (unimplemented)
|
|
2
|
+
|
|
3
|
+
> Part of [Node extension: unimplemented](../node.md) · [简体中文](../../zh-CN/unimplemented/node/worker-threads.md)
|
|
4
|
+
|
|
5
|
+
The transport is a second process, not an OS thread, and a port is local to one
|
|
6
|
+
process. The following are therefore missing:
|
|
7
|
+
|
|
8
|
+
- Transferring a `MessagePort` / `ArrayBuffer` to another worker or the parent
|
|
9
|
+
(`postMessage(value, [port])`). The transfer list is accepted but values are
|
|
10
|
+
copied, and sending a port throws `MessagePort could not be cloned.`
|
|
11
|
+
- `BroadcastChannel`, `setEnvironmentData` / `getEnvironmentData`,
|
|
12
|
+
`SHARE_ENV` and `resourceLimits`.
|
|
13
|
+
- Real parallelism: workers are processes, so values are copied and
|
|
14
|
+
`SharedArrayBuffer` cannot be shared. The worker always runs the current
|
|
15
|
+
program (`import.meta.filename`), not an arbitrary script file.
|
|
16
|
+
- Worker `"error"` events: an uncaught exception in the worker prints on that
|
|
17
|
+
process's stderr and ends it with a non-zero code; the parent sees `"exit"`.
|
|
18
|
+
- `worker.unref()` / `worker.ref()` are accepted but do not change whether the
|
|
19
|
+
parent's event loop stays alive (a running worker keeps it alive).
|
|
20
|
+
- `receiveMessageOnPort` returns `undefined`; a local port already delivers its
|
|
21
|
+
queue on a zero-delay timer.
|
|
22
|
+
- `workerData` travels through the environment, so it is limited to 32 KiB on
|
|
23
|
+
Windows.
|
|
@@ -12,6 +12,7 @@ is missing or differs from Node.
|
|
|
12
12
|
- [`fs`](./node/fs.md) — callback-style async operations, real file watching, option callbacks
|
|
13
13
|
- [`fs/promises`](./node/fs-promises.md) — `FileHandle` append/read offsets, immediate `Dir` callbacks, inert `watch`, no cancellation
|
|
14
14
|
- [Fully unimplemented modules](./node/modules.md) — `https`, `tls`, `cluster`, `vm`, ...
|
|
15
|
+
- [`worker_threads`](./node/worker-threads.md) — no port transfer, no real threads, no `BroadcastChannel`
|
|
15
16
|
- [Global objects and namespaces](./node/globals.md) — `global`, `__dirname`, `require` outside `node_modules`
|
|
16
17
|
- [Async and the event-loop model](./node/async.md) — blocked file watching, blocking `net.connect`, no keep-alive, fixed-size offload pool
|
|
17
18
|
- [The Bun extension](./node/bun.md) — design example only
|
|
@@ -33,7 +33,8 @@
|
|
|
33
33
|
| Generators (`function*`, `yield`, `yield*`, `next`/`throw`/`return`) | ✓ implemented with stackful coroutines; `for...of`, spread and delegation supported. `return()` completes without running `finally` |
|
|
34
34
|
| Timers and microtasks | ✓ `setTimeout` / `clearTimeout` / `setInterval` / `clearInterval` / `setImmediate` / `clearImmediate` / `queueMicrotask` and `performance.now` (numeric ids, no `Timeout` / `Immediate` object). A promise settled from a timer can be awaited: the async body suspends on the pending promise and resumes when the timer fires |
|
|
35
35
|
| I/O / process and other host APIs | only via extensions (e.g. Node `fs`) |
|
|
36
|
-
| Web-platform globals (`globalThis`, `AbortController`, `AbortSignal`, `Reflect`, `Proxy`, `Intl`, `FormData`, `Blob`, `Headers`, `Request`, `Response`, `
|
|
36
|
+
| Web-platform globals (`globalThis`, `AbortController`, `AbortSignal`, `Reflect`, `Proxy`, `Intl`, `FormData`, `Blob`, `Headers`, `Request`, `Response`, `EventTarget`) | ✗ recognized (they type-check and lower to `undefined`), but no runtime implementation |
|
|
37
|
+
| Web Streams (`ReadableStream`, `WritableStream`, `TransformStream`, readers/writers, text streams) | ✓ implemented as core globals; see [Web Streams](../implemented/streams.md). BYOB byte streams and compression streams are not implemented |
|
|
37
38
|
|
|
38
39
|
## Typed arrays
|
|
39
40
|
|
|
@@ -4,12 +4,24 @@
|
|
|
4
4
|
|
|
5
5
|
## Not implemented
|
|
6
6
|
|
|
7
|
-
- **
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
- `WebAssembly.
|
|
14
|
-
|
|
15
|
-
|
|
7
|
+
- **Non-function imports.** A module that imports a memory, table or global is
|
|
8
|
+
rejected with a `LinkError`; only imported functions are bridged from the JS
|
|
9
|
+
import object.
|
|
10
|
+
- **Exported tables and globals.** `WebAssembly.Module.exports` lists them, but
|
|
11
|
+
`instance.exports` only wraps functions and memories. `WebAssembly.Table` and
|
|
12
|
+
`WebAssembly.Global` constructors throw "not supported".
|
|
13
|
+
- **Streaming.** `WebAssembly.compileStreaming` and
|
|
14
|
+
`WebAssembly.instantiateStreaming` are not exposed.
|
|
15
|
+
- **Custom sections.** `WebAssembly.Module.customSections` returns an empty
|
|
16
|
+
array; section contents are not preserved.
|
|
17
|
+
- **Instruction-set coverage beyond wasm3.** The pinned interpreter implements a
|
|
18
|
+
large core subset (the parser also accepts several proposals), but SIMD
|
|
19
|
+
opcodes, the GC proposal, host threads/shared memory and WASI are not
|
|
20
|
+
available. A body using an unsupported opcode fails when the module is
|
|
21
|
+
compiled or instantiated.
|
|
22
|
+
- **Multi-module linking.** Importing one WebAssembly module's exports into
|
|
23
|
+
another is not wired up; each instantiation is self-contained plus JS
|
|
24
|
+
function imports.
|
|
25
|
+
|
|
26
|
+
The execution path itself is implemented (wasm3 behind the namespace) — see
|
|
27
|
+
[Implemented: WebAssembly](../implemented/wasm.md).
|
package/doc/unimplemented.md
CHANGED
|
@@ -27,6 +27,6 @@ under [implemented](./implemented.md); git history records when it changed.
|
|
|
27
27
|
- [Classes and OO](./unimplemented/classes.md) — access control, `abstract`/`implements`, private-name collisions
|
|
28
28
|
- [Type system](./unimplemented/types.md) — parsed and erased; only inference-free facts are checked
|
|
29
29
|
- [Runtime deviations](./unimplemented/deviations.md) — compiles and runs, but differs from ECMAScript
|
|
30
|
-
- [WebAssembly](./unimplemented/wasm.md) —
|
|
30
|
+
- [WebAssembly](./unimplemented/wasm.md) — imported memories/tables/globals, streaming and the remaining API gaps
|
|
31
31
|
- [Toolchain and platform](./unimplemented/toolchain.md) — type checker, async I/O, GC modes, platform gaps
|
|
32
32
|
- [Node extension](./unimplemented/node.md) — Node module gaps, one page per area
|
package/doc/zh-CN/gui/api.md
CHANGED
|
@@ -107,7 +107,7 @@ requestAnimationFrame(frame);
|
|
|
107
107
|
| `HTMLCanvasElement`、`HTMLImageElement`、`WebGLRenderingContext`、`WebGL2RenderingContext`、`WebGLBuffer`/`WebGLTexture`/`WebGLShader`/`WebGLProgram`/`WebGLFramebuffer`/`WebGLRenderbuffer`/`WebGLUniformLocation`/`WebGLActiveInfo`/`WebGLVertexArrayObject`/`WebGLSampler`/`WebGLQuery`/`WebGLSync`/`WebGLTransformFeedback` | 真实的构造函数值。`canvas.getContext("webgl")` 与 `getContext("webgl2")` 返回 GPU 上下文(见 [WebGL](./webgl.md))。 |
|
|
108
108
|
| `ImageBitmap`、`createImageBitmap(source)` | 由已解码像素支撑的不可变位图(`<img>`、`<canvas>`、`<video>` 帧、`OffscreenCanvas` 与 `ImageData` 形状);promise 立即 settle,`texImage2D` 可直接接受该对象 |
|
|
109
109
|
| `OffscreenCanvas` | 真正的 2D 离屏位图,带 `getContext("2d")`、`width`/`height`、`transferToImageBitmap()` 与 `convertToBlob()`;`canvas.transferControlToOffscreen()` 会把元素位图交给它(见 [Canvas](./canvas.md))。离屏 WebGL 上下文不提供。 |
|
|
110
|
-
| `ImageData`、`XRWebGL*`、`Request`、`Headers`、`Response`、`
|
|
110
|
+
| `ImageData`、`XRWebGL*`、`Request`、`Headers`、`Response`、`DOMParser` | `typeof` 探测返回 `"undefined"`(没有 XR、DOM 解析器或流式后端),让页面走自己的“不支持”分支,而不是调用空壳。`fetch` 仍然可用,并返回 response 形状的对象,但没有 `Response` 构造函数,且 `response.arrayBuffer()` 目前返回字节数组而不是 `ArrayBuffer`;`ReadableStream` 等 Web Streams 类来自核心运行时。Three.js 页面在本后端上使用真实渲染器(见[已知问题](./known-issues.md))。 |
|
|
111
111
|
| `requestAnimationFrame` / `cancelAnimationFrame` | 排入窗口的动画帧列表,见[运行时集成](./runtime.md) |
|
|
112
112
|
| `alert` / `confirm` / `prompt`、`matchMedia`、`getComputedStyle`、`escape` | 宿主实现。`matchMedia` 返回**实时**列表(按查询缓存):`matches` 在窗口尺寸变化时重新求值,翻转时触发 `change` 监听器。`confirm`/`prompt` 仅在 `XT_GUI_DIALOGS=1` 时使用原生对话框,否则把消息写到 stderr 并返回文档化的默认值(Annex B `escape` 按 UTF-16 码元编码,与 `decodeURIComponent(escape(bytes))` 一致) |
|
|
113
113
|
|
|
@@ -28,7 +28,7 @@ xbintsc help 帮助
|
|
|
28
28
|
--emit <kind> exe | obj | ir(默认 exe)
|
|
29
29
|
-O0..-O3 优化级别(默认 -O2)
|
|
30
30
|
--ext <names> 逗号分隔扩展(如 node)
|
|
31
|
-
--feature <names> 强制开启可选链接特性(media,gpu,images,shaping,webgl,all)
|
|
31
|
+
--feature <names> 强制开启可选链接特性(media,gpu,images,shaping,webgl,storage,tls,wasm,all)
|
|
32
32
|
--no-feature <n> 即使检测到也不链接该可选特性
|
|
33
33
|
--ext-native <m> 从 JSON manifest 注册 C++/Rust 扩展
|
|
34
34
|
--config <path> 使用项目配置(默认自动查找 xbintsc.config.json)
|
|
@@ -2,21 +2,22 @@
|
|
|
2
2
|
|
|
3
3
|
> 属于[已实现语法与功能](../implemented.md) · [English](../../implemented/extensions.md)
|
|
4
4
|
|
|
5
|
-
实现位置:`src/extensions/registry.ts`、`src/extensions/node/`、`src/extensions/link-feature.ts`、`src/driver/usage.ts`、`src/driver/link-features.ts`
|
|
5
|
+
实现位置:`src/extensions/registry.ts`、`src/extensions/node/`、`src/extensions/wasm.ts`、`src/extensions/link-feature.ts`、`runtime/xt_wasm/`、`src/driver/usage.ts`、`src/driver/link-features.ts`
|
|
6
6
|
|
|
7
7
|
- 扩展是普通对象:`runtimeSources()`(额外 C 源)、`linkerFlags()`(额外链接参数)、`builtins()`(全局标识符 → 运行时符号,统一 `(argc, argv)` ABI)、`modules()`(导入说明符 → 命名导出 / 命名空间)与 `linkFeatures()`(可选的重型能力)。
|
|
8
8
|
- `ExtensionRegistry`:注册 / 注销 / 查询 / 汇总 builtins、modules、runtime 源、链接参数与链接特性。
|
|
9
|
-
- **可选链接特性**(`media`、`gpu`、`images`、`shaping`、`webgl`):
|
|
10
|
-
- 特性声明 `runtimeSources()`(只在启用时编译的 C bridge)、`nativeObjects()`(只在启用时链接的归档)、`linkerFlags()`、可选的 `requires
|
|
9
|
+
- **可选链接特性**(`media`、`gpu`、`images`、`shaping`、`webgl`、`storage`、`wasm`):
|
|
10
|
+
- 特性声明 `runtimeSources()`(只在启用时编译的 C bridge)、`nativeObjects()`(只在启用时链接的归档)、`linkerFlags()`、可选的 `requires`(依赖的另一个特性)、可选的 `required`(源不可用时构建报错而不是 warning)与 `used(usage)`。
|
|
11
11
|
- driver 从打包后的 AST 构造 `ProgramUsage`(`src/driver/usage.ts` 的 `collectUsage`):字符串字面量(含导入 HTML 与内联 CSS)、标识符/属性名、`getContext(...)` 的 kind,以及 markup 是否可能在运行时拼出。简单的 `const` 别名会被解析;无法解析的 markup 让程序走保守回退。
|
|
12
|
-
- `resolveLinkFeatures` 应用 `--feature` / `--no-feature`(以及 `all`),展开 `requires
|
|
12
|
+
- `resolveLinkFeatures` 应用 `--feature` / `--no-feature`(以及 `all`),展开 `requires`,把自动检测到但不可用的可选特性报为 warning、把 required 或强制开启却不可用的报为 error,并返回要链接的 bridge/归档/参数。
|
|
13
13
|
- bridge 就是强引用:构造函数安装特性的分发表,其引用迫使链接器取出实现;没有 bridge 时表保持 null,实现成员永不被链接。
|
|
14
|
-
- 当前客户端:`media`(FFmpeg,`runtime/xt_media/media_bridge.c` + `media.a`)、`gpu`(glslang/SPIRV-Cross,`runtime/ext_gui/gpu_bridge.c` + `gui.a` 内的 `gpu_feature.cpp`)、`images`(stb_image/NanoSVG,`runtime/ext_gui/image_bridge.c` + `image_feature.cpp`)、`shaping`(HarfBuzz/FreeType,`runtime/ext_gui/text_bridge.c` + `text_hb.cpp`)、`webgl`(context/DOM 绑定,`runtime/ext_gui/webgl_bridge.c` + `webgl_feature.cpp`,依赖 `gpu
|
|
14
|
+
- 当前客户端:`media`(FFmpeg,`runtime/xt_media/media_bridge.c` + `media.a`)、`gpu`(glslang/SPIRV-Cross,`runtime/ext_gui/gpu_bridge.c` + `gui.a` 内的 `gpu_feature.cpp`)、`images`(stb_image/NanoSVG,`runtime/ext_gui/image_bridge.c` + `image_feature.cpp`)、`shaping`(HarfBuzz/FreeType,`runtime/ext_gui/text_bridge.c` + `text_hb.cpp`)、`webgl`(context/DOM 绑定,`runtime/ext_gui/webgl_bridge.c` + `webgl_feature.cpp`,依赖 `gpu`)、`storage`(基于 SQLite 的 Web Storage/IndexedDB,其实现完全位于该特性的 `runtimeSources` —— `runtime/xt_storage/*.c`,因为这些全局变量本身就是强引用)以及 `wasm`(`WebAssembly` 命名空间背后的 wasm3;`runtime/xt_wasm/wasm_bridge.c` 安装分发表,`vendor/wasm3` 提供解释器)。
|
|
15
15
|
- TLS/HTTP2 客户端:`tls`(`runtime/xt_tls/tls_core.c` 中的 mbedTLS 传输层加 `tls` 归档,供 `fetch` 与 `node:tls` 使用)、`node-tls`(`TLSSocket`/服务端接口,依赖 `tls`)与 `http2`(`runtime/ext_node/http2/http2_backend.c` 中的 nghttp2 后端,依赖 `tls` 与 `node-tls`;`npm run tls` 构建两个归档)。
|
|
16
|
-
- 内置核心扩展 `core`:暴露 `print`(映射 `xt_println
|
|
16
|
+
- 内置核心扩展 `core`:暴露 `print`(映射 `xt_println`)、Web Storage 全局对象、`storage`/`tls`/`wasm` 链接特性以及二进制 `.wasm` 资源加载器,始终注册。
|
|
17
17
|
- Node 扩展 `node`:
|
|
18
18
|
- 模块化组织:`src/extensions/node/fs/` + `runtime/ext_node/fs/read_file.c`
|
|
19
19
|
- 暴露可导入模块(`fs`、`fs/promises`、`path`、`os`、`process` 等),同时支持裸名称与 `node:` 前缀;`import { readFileSync } from "fs"` 解析到 `xt_node_read_text_file`,`path`/`os`/`process` 的导出映射到命名空间分发器。
|
|
20
|
+
- Web Worker 扩展 `webworker`:浏览器全局对象 `Worker`、`MessageChannel`、`MessagePort`、`self`、`isMainThread` 与裸 `postMessage`。worker 是运行同一可执行文件的第二个进程(没有线程),因此入口模式是 `new Worker(import.meta.filename)` 加 `isMainThread` 分支。传输层(`runtime/worker/`)与 `node:worker_threads` 共用;见 [Web Worker](./web-worker.md)。
|
|
20
21
|
- 添加新模块只需新增目录 + C 实现,核心编译器无需改动。
|
|
21
22
|
- 原生 C++/Rust 扩展(`src/extensions/native.ts`、`--ext-native`):
|
|
22
23
|
- `nativeObjects()` 链接以 `(argc, argv)` ABI 暴露 `extern "C"` 符号的预编译对象/静态库;JSON manifest 把它们映射为 builtins/modules(`linkerFlags` / `linkerFlagsByPlatform` 用于 C++/Rust 运行时)。
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# `stream/web`
|
|
2
|
+
|
|
3
|
+
> 属于[Node 扩展:已实现](../node.md) · [English](../../../implemented/node/stream-web.md)
|
|
4
|
+
|
|
5
|
+
实现位置:`src/extensions/node/stream-web/index.ts`、`runtime/ext_node/stream_web/stream_web.c`。
|
|
6
|
+
|
|
7
|
+
Node 的 `stream/web` 子路径重新导出 WHATWG 流类。实现位于核心运行时(见 [Web Streams](../streams.md)),因此编译后的程序既可以把它当作浏览器全局使用,也可以按名字导入:
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { ReadableStream, TransformStream } from "node:stream/web";
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
| 导出 | 说明 |
|
|
14
|
+
| --- | --- |
|
|
15
|
+
| `ReadableStream`、`ReadableStreamDefaultReader`、`ReadableStreamDefaultController` | 完整实现 |
|
|
16
|
+
| `WritableStream`、`WritableStreamDefaultWriter`、`WritableStreamDefaultController` | 完整实现 |
|
|
17
|
+
| `TransformStream`、`TransformStreamDefaultController` | 完整实现 |
|
|
18
|
+
| `TextEncoderStream`、`TextDecoderStream` | UTF-8 文本转换流 |
|
|
19
|
+
| `CountQueuingStrategy`、`ByteLengthQueuingStrategy` | 队列策略 |
|
|
20
|
+
| `ReadableStreamBYOBReader`、`ReadableStreamBYOBRequest` | 有导出;构造函数抛错(不支持字节流) |
|
|
21
|
+
| `CompressionStream`、`DecompressionStream` | 有导出;构造函数抛错(不支持压缩流) |
|
|
22
|
+
| `ReadableStreamTee` | 为 `undefined`,undici 的 `ReadableStreamTee?.(...) ?? body.stream.tee()` 会回退到 body 自身的 `tee` |
|
|
23
|
+
|
|
24
|
+
构造函数是一等值:`const RS = ReadableStream`、`new RS(...)`、`stream instanceof ReadableStream` 与 `ReadableStream.from(...)` 均可用。异步模型、支持的方法与剩余限制(BYOB、压缩流)见 [Web Streams](../streams.md)。
|
|
@@ -2,8 +2,81 @@
|
|
|
2
2
|
|
|
3
3
|
> 属于[Node 扩展:已实现](../node.md) · [English](../../../implemented/node/worker-threads.md)
|
|
4
4
|
|
|
5
|
-
实现位置:`src/extensions/node/worker_threads/index.ts`、`runtime/
|
|
5
|
+
实现位置:`src/extensions/node/worker_threads/index.ts`、`runtime/worker/`
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
xbintsc 没有线程(值模型、垃圾回收器与事件循环都是单线程的),所以 worker 是运行
|
|
8
|
+
同一个可执行文件的**第二个进程**。worker 入口就是当前程序:传入
|
|
9
|
+
`import.meta.filename`,并用 `isMainThread` 分支。
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import { Worker, isMainThread, parentPort, workerData } from "node:worker_threads";
|
|
13
|
+
|
|
14
|
+
if (isMainThread) {
|
|
15
|
+
const worker = new Worker(import.meta.filename, { workerData: { n: 2 } });
|
|
16
|
+
worker.on("message", (reply) => {
|
|
17
|
+
console.log(reply.total);
|
|
18
|
+
worker.terminate();
|
|
19
|
+
});
|
|
20
|
+
worker.postMessage({ values: [1, 2, 3] });
|
|
21
|
+
} else {
|
|
22
|
+
parentPort.on("message", (message) => {
|
|
23
|
+
const values: number[] = message.values;
|
|
24
|
+
parentPort.postMessage({ total: values[0] + values[1] + values[2] });
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## API 表面
|
|
30
|
+
|
|
31
|
+
- `Worker(script, options?)` —— `options.workerData`(结构化克隆)与
|
|
32
|
+
`options.name`;`script` 必须是本可执行文件,否则构造器抛出明确的错误。
|
|
33
|
+
`options.env`、`argv`、`execArgv`、`resourceLimits`、
|
|
34
|
+
`stdin`/`stdout`/`stderr` 接受但忽略。
|
|
35
|
+
- `worker.postMessage(value, transferList?)` —— 值做结构化克隆;接受 transfer
|
|
36
|
+
列表但按复制处理。发送函数、symbol、promise、`WeakMap`/`WeakSet`/`WeakRef`、
|
|
37
|
+
生成器或 `MessagePort` 会抛出 `... could not be cloned.`。
|
|
38
|
+
- `worker.on("message", ...)` / `worker.once` / `off` / `removeAllListeners` ——
|
|
39
|
+
`message` 传递原始值。
|
|
40
|
+
- `worker.on("online" | "error" | "exit", ...)` —— 控制通道建立后触发
|
|
41
|
+
`online`;`exit` 携带进程退出码(被终止的 worker 报 `1`)。帧解码失败时触发
|
|
42
|
+
`messageerror`。
|
|
43
|
+
- `worker.terminate(): Promise<number>` —— 终止进程,resolve 退出码;
|
|
44
|
+
`ref()`/`unref()` 接受但为空操作。
|
|
45
|
+
- `worker.threadId` —— 每个进程的 worker id。
|
|
46
|
+
- `isMainThread`、`workerData`(主线程为 null)、`parentPort`(主线程为 null),
|
|
47
|
+
后者支持 `postMessage`、`on("message")`、`close()`、`start()`、
|
|
48
|
+
`ref()`/`unref()`。
|
|
49
|
+
- `new MessageChannel()` / `MessagePort` —— 进程内通道:`port1.postMessage`
|
|
50
|
+
通过零延时定时器投递给 `port2`;两个端口都支持 `on("message")`(原始值)与
|
|
51
|
+
`onmessage` / `addEventListener`(`MessageEvent`)。`receiveMessageOnPort`
|
|
52
|
+
存在,但本地端口本来就通过定时器投递,因此返回 `undefined`。
|
|
53
|
+
- `markAsUncloneable(value)` —— 接受;编解码器本来就拒绝流与宿主句柄。
|
|
54
|
+
|
|
55
|
+
## 消息数据
|
|
56
|
+
|
|
57
|
+
消息与 `workerData` 使用 `runtime/worker/worker_codec.c` 的结构化克隆编解码器:
|
|
58
|
+
`undefined`、布尔、数字(含 `NaN`、`±Infinity`、`-0`)、字符串、`BigInt`、
|
|
59
|
+
数组、普通对象、`Map`、`Set`、`Date`、`RegExp`、`ArrayBuffer`、类型化数组、
|
|
60
|
+
共享引用与环。类实例按普通对象到达;函数与 symbol 会被拒绝。
|
|
61
|
+
|
|
62
|
+
## 生命周期与错误
|
|
63
|
+
|
|
64
|
+
worker 运行期间父进程保持存活。worker 拥有 `message` 监听器(或其他事件循环
|
|
65
|
+
工作)时保持存活;只 post 一次就返回的 worker 会自行退出,所以长驻 worker 的
|
|
66
|
+
常见模式是 `worker.on("message")` 加 `terminate()`。worker 返回时会在进程结束前
|
|
67
|
+
关闭控制通道,因此退出前 post 的消息仍会送达。worker 内未捕获的异常由该
|
|
68
|
+
进程打印到 stderr 并非零退出;父进程看到的是 `exit`,而不是 `error`。父进程最后
|
|
69
|
+
一个句柄关闭时,worker 会因 socket EOF 而结束。
|
|
70
|
+
|
|
71
|
+
## 平台差异
|
|
72
|
+
|
|
73
|
+
传输层是回环 TCP socket(Windows 的 `select(2)` 只接受 socket,管道需要另一套
|
|
74
|
+
实现)。POSIX 使用 `fork`/`execvp` 与 `waitpid(WNOHANG)`;Windows 使用
|
|
75
|
+
`_spawnvp(_P_NOWAIT)` 与进程句柄上的 `WaitForSingleObject`。端口与随机握手
|
|
76
|
+
token 通过环境变量传递,因此 Windows 上 `workerData` 上限为 32 KiB。
|
|
77
|
+
|
|
78
|
+
## 限制
|
|
79
|
+
|
|
80
|
+
跨进程 `MessagePort` 转移、`BroadcastChannel`、
|
|
81
|
+
`setEnvironmentData`/`getEnvironmentData`、`SHARE_ENV` 与真正的线程并行尚未
|
|
82
|
+
实现——见 [Node 扩展:未实现](../../unimplemented/node.md)。
|
|
@@ -24,6 +24,7 @@ TypeScript 导出与 C 实现一一对应。新增模块不需要改动核心编
|
|
|
24
24
|
| `buffer` | [buffer](./node/buffer.md) | `Buffer` 以普通对象表示 |
|
|
25
25
|
| `stream` | [stream](./node/stream.md) | `Readable` / `Writable` / `Duplex` / `Transform` / `PassThrough` |
|
|
26
26
|
| `stream/promises` | [stream/promises](./node/stream-promises.md) | 仅 `pipeline` |
|
|
27
|
+
| `stream/web` | [stream/web](./node/stream-web.md) | WHATWG 流,重新导出核心运行时的实现 |
|
|
27
28
|
| `net` | [net](./node/net.md) | TCP 服务端与客户端 |
|
|
28
29
|
| `dgram` | [dgram](./node/dgram.md) | UDP 套接字 |
|
|
29
30
|
| `http` | [http](./node/http.md) | 服务端与客户端,`Connection: close` |
|
|
@@ -39,7 +40,7 @@ TypeScript 导出与 C 实现一一对应。新增模块不需要改动核心编
|
|
|
39
40
|
| `crypto` | [crypto](./node/crypto.md) | `createHash`(SHA-1 / SHA-256) |
|
|
40
41
|
| `url` | [url](./node/url.md) | `pathToFileURL` / `fileURLToPath` |
|
|
41
42
|
| `zlib` | [zlib](./node/zlib.md) | 仅 `createGzip` |
|
|
42
|
-
| `worker_threads` | [worker_threads](./node/worker-threads.md) |
|
|
43
|
+
| `worker_threads` | [worker_threads](./node/worker-threads.md) | 进程隔离的 worker:双向消息、`MessageChannel`、结构化克隆 |
|
|
43
44
|
|
|
44
45
|
`tls` 与 `http2` 以可选**链接特性**实现,底层是随仓库引入的 mbedTLS 与 nghttp2:
|
|
45
46
|
`npm run tls` 会下载并构建 `runtime/lib/<os>-<arch>/{tls,nghttp2}.{a,lib}`,
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# Web Streams
|
|
2
|
+
|
|
3
|
+
> 属于[已实现的语法与特性](../implemented.md) · [English](../../implemented/streams.md)
|
|
4
|
+
|
|
5
|
+
实现位置:`runtime/xt_streams.c` 与 `runtime/xt_streams/`。这些类是核心运行时全局对象,无需任何扩展开关即可使用;Node 的 `stream/web` 模块重新导出同一批构造函数(见 [stream/web](./node/stream-web.md))。
|
|
6
|
+
|
|
7
|
+
## 类
|
|
8
|
+
|
|
9
|
+
| 类 | 说明 |
|
|
10
|
+
| --- | --- |
|
|
11
|
+
| `ReadableStream` | underlying source 的 `start` / `pull` / `cancel`;`getReader`、`cancel`、`tee`、`pipeTo`、`pipeThrough`、`values`、`[Symbol.asyncIterator]`、`locked` |
|
|
12
|
+
| `ReadableStreamDefaultReader` | `read`、`cancel`、`releaseLock`、`closed` |
|
|
13
|
+
| `ReadableStreamDefaultController` | `enqueue`、`close`、`error`、`desiredSize` |
|
|
14
|
+
| `WritableStream` | underlying sink 的 `start` / `write` / `close` / `abort`;`getWriter`、`close`、`abort`、`locked` |
|
|
15
|
+
| `WritableStreamDefaultWriter` | `write`、`close`、`abort`、`releaseLock`、`closed`、`ready`、`desiredSize` |
|
|
16
|
+
| `WritableStreamDefaultController` | `error` |
|
|
17
|
+
| `TransformStream` | transformer 的 `start` / `transform` / `flush`;`readable`、`writable` |
|
|
18
|
+
| `TransformStreamDefaultController` | `enqueue`、`error`、`terminate`、`desiredSize` |
|
|
19
|
+
| `TextEncoderStream` | 输入字符串块,输出 UTF-8 `Uint8Array` 块 |
|
|
20
|
+
| `TextDecoderStream` | 输入 `Uint8Array` 块,输出字符串块 |
|
|
21
|
+
| `CountQueuingStrategy` / `ByteLengthQueuingStrategy` | `highWaterMark` 与 `size(chunk)` |
|
|
22
|
+
|
|
23
|
+
`ReadableStream.from(iterable)` 可从数组、字符串或任何同步/异步可迭代对象创建可读流。构造函数是一等值,因此 `const RS = ReadableStream; new RS(...)`、`ReadableStream.from(...)` 与 `stream instanceof ReadableStream` 均可用。
|
|
24
|
+
|
|
25
|
+
### 示例
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
async function main(): Promise<void> {
|
|
29
|
+
const transform = new TransformStream<string, string>({
|
|
30
|
+
transform(chunk, controller) {
|
|
31
|
+
controller.enqueue(chunk.toUpperCase());
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
const writer = transform.writable.getWriter();
|
|
35
|
+
const reader = transform.readable.getReader();
|
|
36
|
+
|
|
37
|
+
const first = reader.read();
|
|
38
|
+
await writer.write("hello");
|
|
39
|
+
console.log(JSON.stringify(await first)); // {"value":"HELLO","done":false}
|
|
40
|
+
const done = reader.read();
|
|
41
|
+
await writer.close();
|
|
42
|
+
console.log(JSON.stringify(await done)); // {"done":true}
|
|
43
|
+
}
|
|
44
|
+
main();
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## 异步模型
|
|
48
|
+
|
|
49
|
+
`start`、`pull`、`write`、`close`、`abort`、`cancel`、`transform` 与 `flush` 都可以返回 Promise。流机制会把它接到运行时 Promise 队列上:等待定时器或 socket 的 `pull` 会让事件循环继续工作,慢速 sink 会串行化写入而不阻塞事件循环。读写方法返回 Promise;空流上的 `reader.read()` 会保持 pending,直到 `pull`(或其他生产者)入队数据。
|
|
50
|
+
|
|
51
|
+
背压通过 `desiredSize` 与 `writer.ready` 体现:只要 reader 有需求且可读队列低于 high-water mark 就会调用 `pull`;可写队列满时 `writer.ready` 保持 pending。
|
|
52
|
+
|
|
53
|
+
## 限制
|
|
54
|
+
|
|
55
|
+
- **不支持字节流 / BYOB。** `getReader({ mode: "byob" })` 抛出 `TypeError`;`ReadableStreamBYOBReader` 与 `ReadableStreamBYOBRequest` 只有名字,构造函数会抛错。
|
|
56
|
+
- **不支持压缩流。** `node:stream/web` 会导出 `CompressionStream` 与 `DecompressionStream`,但构造函数抛出明确错误。
|
|
57
|
+
- `TextDecoderStream` 按块独立解码,跨块拆分的 UTF-8 序列不会重新拼接。
|
|
58
|
+
- `tee` 的两个分支没有独立背压;无界源 + 慢分支会持续累积数据。
|
|
59
|
+
- `pipeTo` 忽略 `preventClose` / `preventAbort` / `signal` 选项。
|
|
60
|
+
- 这些类是普通运行时对象;Web IDL 品牌检查、`postMessage` 转移以及 `fetch` 响应的 `ReadableStream` 所有权尚未实现。
|