@syntropika/tembra-sdk 0.2.2 → 0.3.0
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/README.md +43 -17
- package/dist/authoring/types.d.ts +64 -3
- package/dist/authoring/wit.d.ts +6 -0
- package/dist/bridge/commands.d.ts +1190 -0
- package/dist/bridge/identifiers.d.ts +2 -0
- package/dist/bridge/kernel.d.ts +18 -107
- package/dist/bridge/protocol.d.ts +3 -138
- package/dist/browser.d.ts +14 -1
- package/dist/browser.js +141 -3332
- package/dist/chunks/browser-b8jamxw2.js +3399 -0
- package/dist/chunks/execution-nsqc6nrt.js +1419 -0
- package/dist/chunks/index-01zk0xqx.js +160 -0
- package/dist/chunks/index-3fjdfdfd.js +152 -0
- package/dist/chunks/index-aaymjn7v.js +787 -0
- package/dist/chunks/index-c1w7z9hx.js +356 -0
- package/dist/chunks/index-h8t0n42q.js +738 -0
- package/dist/chunks/index-hfds9mzp.js +308 -0
- package/dist/chunks/index-kr4x67wt.js +177 -0
- package/dist/chunks/index-kr6bnh34.js +117 -0
- package/dist/chunks/index-kyt9dj0a.js +1421 -0
- package/dist/chunks/index-n1wxzc9w.js +830 -0
- package/dist/chunks/index-pvac426d.js +74 -0
- package/dist/chunks/index-smyds5ce.js +3234 -0
- package/dist/chunks/index-sse8aapb.js +121 -0
- package/dist/chunks/index-v6heph77.js +643 -0
- package/dist/chunks/index-xrdvnv37.js +3448 -0
- package/dist/chunks/index-za4mg94e.js +26 -0
- package/dist/chunks/index-zkqmnjj8.js +167 -0
- package/dist/coordinator.js +9 -500
- package/dist/error.d.ts +2 -0
- package/dist/errors.d.ts +3 -0
- package/dist/guest.d.ts +173 -0
- package/dist/guest.js +26 -0
- package/dist/host.d.ts +36 -30
- package/dist/host.js +37 -3310
- package/dist/http/bytes.d.ts +8 -0
- package/dist/http/events.d.ts +7 -0
- package/dist/http.d.ts +45 -0
- package/dist/http.js +291 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +49 -862
- package/dist/infrastructure/rpc/client.d.ts +2 -1
- package/dist/infrastructure/rpc/resources.d.ts +28 -0
- package/dist/interpreter/abi.d.ts +54 -0
- package/dist/interpreter/acquire.d.ts +18 -0
- package/dist/interpreter/browser-network.d.ts +52 -0
- package/dist/interpreter/browser-port.d.ts +55 -0
- package/dist/interpreter/browser.d.ts +79 -0
- package/dist/interpreter/cli.d.ts +5 -0
- package/dist/interpreter/codec.d.ts +42 -0
- package/dist/interpreter/component/execution.d.ts +42 -0
- package/dist/interpreter/component/host.d.ts +69 -0
- package/dist/interpreter/component/instance.d.ts +40 -0
- package/dist/interpreter/component/kernel.d.ts +120 -0
- package/dist/interpreter/component/names.d.ts +5 -0
- package/dist/interpreter/component/port.d.ts +85 -0
- package/dist/interpreter/component/prepared.d.ts +97 -0
- package/dist/interpreter/component/resources.d.ts +45 -0
- package/dist/interpreter/component/revision.d.ts +87 -0
- package/dist/interpreter/component/scope.d.ts +116 -0
- package/dist/interpreter/component/streams.d.ts +28 -0
- package/dist/interpreter/component/values.d.ts +79 -0
- package/dist/interpreter/component/wasi-filesystem.d.ts +21 -0
- package/dist/interpreter/component/wasi-host.d.ts +64 -0
- package/dist/interpreter/component/wasi-http.d.ts +33 -0
- package/dist/interpreter/component/wasi-sockets.d.ts +183 -0
- package/dist/interpreter/component/wasi.d.ts +41 -0
- package/dist/interpreter/core.d.ts +64 -0
- package/dist/interpreter/fetch-http-provider.d.ts +17 -0
- package/dist/interpreter/filesystem/contract.d.ts +85 -0
- package/dist/interpreter/filesystem/memory.d.ts +3 -0
- package/dist/interpreter/filesystem/opfs.d.ts +3 -0
- package/dist/interpreter/filesystem/scope.d.ts +35 -0
- package/dist/interpreter/filesystem.d.ts +8 -0
- package/dist/interpreter/gate.d.ts +6 -0
- package/dist/interpreter/host-protocol.d.ts +100 -0
- package/dist/interpreter/http-ingress-fetch.d.ts +7 -0
- package/dist/interpreter/http-ingress.d.ts +96 -0
- package/dist/interpreter/http-provider.d.ts +85 -0
- package/dist/interpreter/io.d.ts +20 -0
- package/dist/interpreter/mobile.d.ts +149 -0
- package/dist/interpreter/native-ingress.d.ts +95 -0
- package/dist/interpreter/node-port.d.ts +25 -0
- package/dist/interpreter/node.d.ts +36 -0
- package/dist/interpreter/revision.d.ts +64 -0
- package/dist/interpreter/scope.d.ts +89 -0
- package/dist/interpreter/socket-addresses.d.ts +50 -0
- package/dist/interpreter/socket-provider.d.ts +110 -0
- package/dist/interpreter/standard-cli.d.ts +34 -0
- package/dist/interpreter/standard-codec.d.ts +96 -0
- package/dist/interpreter/standard-filesystem.d.ts +47 -0
- package/dist/interpreter/standard-host.d.ts +71 -0
- package/dist/interpreter/standard-http-values.d.ts +159 -0
- package/dist/interpreter/standard-http.d.ts +9 -0
- package/dist/interpreter/standard-ingress.d.ts +63 -0
- package/dist/interpreter/standard-sockets.d.ts +177 -0
- package/dist/interpreter/standard-streams.d.ts +218 -0
- package/dist/interpreter/standard-system.d.ts +35 -0
- package/dist/interpreter/standard-utf8.d.ts +4 -0
- package/dist/interpreter/standard.d.ts +18 -0
- package/dist/interpreter/types.d.ts +152 -0
- package/dist/interpreter/wit-shape.d.ts +92 -0
- package/dist/manager.js +5 -332
- package/dist/msgpack.js +9 -615
- package/dist/node.d.ts +8 -1
- package/dist/node.js +248 -3321
- package/dist/rpc.js +196 -699
- package/dist/runtime/adapters.d.ts +120 -0
- package/dist/runtime/adapters.js +38 -0
- package/dist/runtime/calls.d.ts +7 -6
- package/dist/runtime/lifecycle.d.ts +3 -1
- package/dist/runtime/orbs.d.ts +5 -4
- package/dist/runtime/references.d.ts +27 -1
- package/dist/runtime/reload.d.ts +5 -3
- package/dist/runtime/scopes.d.ts +1 -1
- package/dist/runtime/signals.d.ts +4 -0
- package/dist/runtime/wit-client.d.ts +38 -0
- package/dist/telemetry.js +4 -25
- package/dist/tooling/build.d.ts +2 -2
- package/dist/tooling/client-resources.d.ts +23 -0
- package/dist/tooling/client.d.ts +10 -0
- package/dist/tooling/component-glue.d.ts +17 -0
- package/dist/tooling/component.d.ts +11 -0
- package/dist/tooling/index.d.ts +3 -1
- package/dist/tooling/index.js +1155 -102
- package/dist/tooling/resolver.d.ts +4 -4
- package/dist/tooling/typecheck.d.ts +2 -1
- package/dist/tooling/types.d.ts +27 -12
- package/dist/tooling/wit.d.ts +9 -0
- package/dist/wasi/wasi-clocks-types.d.ts +2 -0
- package/dist/wasi/wasi-http-client.d.ts +5 -0
- package/dist/wasi/wasi-http-types.d.ts +285 -0
- package/dist/wasi/wasi-sockets-ip-name-lookup.d.ts +23 -0
- package/dist/wasi/wasi-sockets-types.d.ts +146 -0
- package/package.json +62 -35
- package/src/authoring/declaration.ts +1 -1
- package/src/authoring/types.ts +53 -3
- package/src/authoring/wit.ts +15 -0
- package/src/bridge/commands.ts +1105 -0
- package/src/bridge/identifiers.ts +2 -0
- package/src/bridge/kernel.ts +56 -98
- package/src/bridge/protocol.ts +3 -142
- package/src/browser.ts +90 -6
- package/src/context-key.ts +4 -7
- package/src/error.ts +1 -1
- package/src/errors.ts +7 -0
- package/src/guest.ts +327 -0
- package/src/host.ts +128 -23
- package/src/http/bytes.ts +87 -0
- package/src/http/events.ts +34 -0
- package/src/http.ts +272 -0
- package/src/index.ts +3 -0
- package/src/infrastructure/rpc/client.ts +136 -17
- package/src/infrastructure/rpc/resources.ts +123 -0
- package/src/interpreter/abi.ts +69 -0
- package/src/interpreter/acquire.ts +58 -0
- package/src/interpreter/browser-network.ts +170 -0
- package/src/interpreter/browser-port.ts +425 -0
- package/src/interpreter/browser.ts +340 -0
- package/src/interpreter/cli.ts +30 -0
- package/src/interpreter/codec.ts +298 -0
- package/src/interpreter/component/execution.ts +264 -0
- package/src/interpreter/component/host.ts +409 -0
- package/src/interpreter/component/instance.ts +138 -0
- package/src/interpreter/component/kernel.ts +611 -0
- package/src/interpreter/component/names.ts +13 -0
- package/src/interpreter/component/port.ts +598 -0
- package/src/interpreter/component/prepared.ts +194 -0
- package/src/interpreter/component/resources.ts +345 -0
- package/src/interpreter/component/revision.ts +702 -0
- package/src/interpreter/component/scope.ts +447 -0
- package/src/interpreter/component/streams.ts +180 -0
- package/src/interpreter/component/values.ts +254 -0
- package/src/interpreter/component/wasi-filesystem.ts +472 -0
- package/src/interpreter/component/wasi-host.ts +197 -0
- package/src/interpreter/component/wasi-http.ts +939 -0
- package/src/interpreter/component/wasi-sockets.ts +958 -0
- package/src/interpreter/component/wasi.ts +238 -0
- package/src/interpreter/core.ts +386 -0
- package/src/interpreter/fetch-http-provider.ts +98 -0
- package/src/interpreter/filesystem/contract.ts +138 -0
- package/src/interpreter/filesystem/memory.ts +180 -0
- package/src/interpreter/filesystem/opfs.ts +294 -0
- package/src/interpreter/filesystem/scope.ts +390 -0
- package/src/interpreter/filesystem.ts +20 -0
- package/src/interpreter/gate.ts +55 -0
- package/src/interpreter/host-protocol.ts +242 -0
- package/src/interpreter/http-ingress-fetch.ts +133 -0
- package/src/interpreter/http-ingress.ts +203 -0
- package/src/interpreter/http-provider.ts +105 -0
- package/src/interpreter/io.ts +66 -0
- package/src/interpreter/mobile.ts +385 -0
- package/src/interpreter/native-ingress.ts +550 -0
- package/src/interpreter/node-port.ts +218 -0
- package/src/interpreter/node.ts +114 -0
- package/src/interpreter/revision.ts +450 -0
- package/src/interpreter/scope.ts +453 -0
- package/src/interpreter/socket-addresses.ts +246 -0
- package/src/interpreter/socket-provider.ts +126 -0
- package/src/interpreter/standard-cli.ts +184 -0
- package/src/interpreter/standard-codec.ts +403 -0
- package/src/interpreter/standard-filesystem.ts +545 -0
- package/src/interpreter/standard-host.ts +840 -0
- package/src/interpreter/standard-http-values.ts +624 -0
- package/src/interpreter/standard-http.ts +622 -0
- package/src/interpreter/standard-ingress.ts +344 -0
- package/src/interpreter/standard-sockets.ts +847 -0
- package/src/interpreter/standard-streams.ts +710 -0
- package/src/interpreter/standard-system.ts +182 -0
- package/src/interpreter/standard-utf8.ts +72 -0
- package/src/interpreter/standard.ts +283 -0
- package/src/interpreter/types.ts +163 -0
- package/src/interpreter/wit-shape.ts +175 -0
- package/src/node.ts +43 -14
- package/src/runtime/activities.ts +6 -6
- package/src/runtime/adapters.ts +314 -0
- package/src/runtime/calls.ts +213 -90
- package/src/runtime/context.ts +7 -7
- package/src/runtime/lifecycle.ts +36 -17
- package/src/runtime/orbs.ts +81 -39
- package/src/runtime/programs.ts +16 -8
- package/src/runtime/references.ts +128 -11
- package/src/runtime/reload.ts +71 -30
- package/src/runtime/resources.ts +2 -2
- package/src/runtime/scopes.ts +17 -17
- package/src/runtime/session.ts +3 -1
- package/src/runtime/signals.ts +31 -0
- package/src/runtime/wit-client.ts +228 -0
- package/src/tooling/build.ts +152 -54
- package/src/tooling/client-resources.ts +74 -0
- package/src/tooling/client.ts +531 -0
- package/src/tooling/component-glue.ts +288 -0
- package/src/tooling/component.ts +225 -0
- package/src/tooling/index.ts +3 -7
- package/src/tooling/resolver.ts +42 -25
- package/src/tooling/typecheck.ts +59 -4
- package/src/tooling/types.ts +27 -12
- package/src/tooling/wit.ts +188 -0
- package/dist/embedded.d.ts +0 -1
- package/dist/embedded.js +0 -1
- package/src/embedded.ts +0 -301
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WASI 0.3 for trusted prepared component bindings.
|
|
3
|
+
*
|
|
4
|
+
* `publicWasi` serves the WASI 0.3 interfaces a compiled component imports from the
|
|
5
|
+
* installation's granted providers, with JCO's component-model async conventions (the same
|
|
6
|
+
* conventions as `@bytecodealliance/preview3-shim`, whose global singletons cannot carry
|
|
7
|
+
* per-installation grants). Every instance receives fresh import objects and resources.
|
|
8
|
+
*/
|
|
9
|
+
import type { CliOptions } from "../../authoring/types.ts";
|
|
10
|
+
import type { StdioOutput, StdioStreams } from "../standard-cli.ts";
|
|
11
|
+
import type { ClockProvider, RandomProvider } from "../standard-system.ts";
|
|
12
|
+
import { filesystemImports, type StandardMount } from "./wasi-filesystem.ts";
|
|
13
|
+
import { cancelled, guestBytes, hostBytes, type WasiHost } from "./wasi-host.ts";
|
|
14
|
+
import { httpImports, type HttpClient } from "./wasi-http.ts";
|
|
15
|
+
import { socketImports, type SocketBindings } from "./wasi-sockets.ts";
|
|
16
|
+
|
|
17
|
+
/** A trap: JCO rethrows host `Error`s without a `payload`, which ends the instance. */
|
|
18
|
+
class RuntimeTrap extends Error {}
|
|
19
|
+
|
|
20
|
+
const ok = (val?: unknown) => (val === undefined ? { tag: "ok" } : { tag: "ok", val });
|
|
21
|
+
|
|
22
|
+
/** Granted providers of one installation for the public WASI 0.3 surface. */
|
|
23
|
+
export interface PublicProviders {
|
|
24
|
+
readonly clocks?: ClockProvider | undefined;
|
|
25
|
+
readonly random?: RandomProvider | undefined;
|
|
26
|
+
/** Present only with the `cli` grant. */
|
|
27
|
+
readonly cli?: {
|
|
28
|
+
readonly options: Readonly<CliOptions>;
|
|
29
|
+
readonly stdio: StdioStreams;
|
|
30
|
+
};
|
|
31
|
+
/** Granted filesystem mounts, in grant order. */
|
|
32
|
+
readonly mounts: () => readonly StandardMount[];
|
|
33
|
+
/** Physical accounting and retirement of this instance. */
|
|
34
|
+
readonly host: WasiHost;
|
|
35
|
+
/** Receives the instance's incoming-HTTP bridge, when the HTTP adapter provides one. */
|
|
36
|
+
readonly ingress?: (bridge: unknown) => void;
|
|
37
|
+
/** Exact-origin HTTP grants; `undefined` denies every request. */
|
|
38
|
+
readonly http: HttpClient | undefined;
|
|
39
|
+
/** The socket provider and grants selected by each sockets import binding. */
|
|
40
|
+
readonly sockets: SocketBindings;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Interfaces with a browser implementation; anything else imported is refused. */
|
|
44
|
+
export const BROWSER_WASI: ReadonlySet<string> = new Set([
|
|
45
|
+
"wasi:clocks/monotonic-clock@0.3.0",
|
|
46
|
+
"wasi:clocks/system-clock@0.3.0",
|
|
47
|
+
"wasi:clocks/types@0.3.0",
|
|
48
|
+
"wasi:random/random@0.3.0",
|
|
49
|
+
"wasi:random/insecure@0.3.0",
|
|
50
|
+
"wasi:random/insecure-seed@0.3.0",
|
|
51
|
+
"wasi:cli/environment@0.3.0",
|
|
52
|
+
"wasi:cli/exit@0.3.0",
|
|
53
|
+
"wasi:cli/stdin@0.3.0",
|
|
54
|
+
"wasi:cli/stdout@0.3.0",
|
|
55
|
+
"wasi:cli/stderr@0.3.0",
|
|
56
|
+
"wasi:cli/types@0.3.0",
|
|
57
|
+
"wasi:cli/terminal-input@0.3.0",
|
|
58
|
+
"wasi:cli/terminal-output@0.3.0",
|
|
59
|
+
"wasi:cli/terminal-stdin@0.3.0",
|
|
60
|
+
"wasi:cli/terminal-stdout@0.3.0",
|
|
61
|
+
"wasi:cli/terminal-stderr@0.3.0",
|
|
62
|
+
"wasi:filesystem/types@0.3.0",
|
|
63
|
+
"wasi:filesystem/preopens@0.3.0",
|
|
64
|
+
"wasi:http/types@0.3.0",
|
|
65
|
+
"wasi:http/client@0.3.0",
|
|
66
|
+
"wasi:sockets/types@0.3.0",
|
|
67
|
+
"wasi:sockets/ip-name-lookup@0.3.0",
|
|
68
|
+
]);
|
|
69
|
+
|
|
70
|
+
/** Refuse, before instantiation, a public WASI import this backend cannot serve yet. */
|
|
71
|
+
export function checkPublicWasi(names: readonly string[]): void {
|
|
72
|
+
const missing = names.filter((name) => name.startsWith("wasi:") && !BROWSER_WASI.has(name));
|
|
73
|
+
if (missing.length)
|
|
74
|
+
throw new Error(
|
|
75
|
+
`Browser component execution does not provide ${missing.join(", ")} yet; ` +
|
|
76
|
+
"this Orb cannot be installed in the browser",
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** The granted public WASI 0.3 imports of one installation, keyed by exact import name. */
|
|
81
|
+
export function publicWasi(
|
|
82
|
+
names: readonly string[],
|
|
83
|
+
providers: PublicProviders,
|
|
84
|
+
): Record<string, unknown> {
|
|
85
|
+
const { clocks, random, cli, host } = providers;
|
|
86
|
+
const signal = host.retired;
|
|
87
|
+
const need = <T>(value: T | undefined, grant: string): T => {
|
|
88
|
+
if (value === undefined) throw new Error(`The installation holds no ${grant} grant`);
|
|
89
|
+
return value;
|
|
90
|
+
};
|
|
91
|
+
const randomBytes = (secure: boolean) => (length: bigint) => {
|
|
92
|
+
const source = need(random, "random");
|
|
93
|
+
const size = Number(length > 1n << 20n ? 1n << 20n : length);
|
|
94
|
+
const bytes = secure ? source.secure(size) : source.insecure(size);
|
|
95
|
+
if (!(bytes instanceof Uint8Array) || bytes.length !== size)
|
|
96
|
+
throw new Error("random provider returned the wrong byte length");
|
|
97
|
+
return bytes;
|
|
98
|
+
};
|
|
99
|
+
const randomU64 = (secure: boolean) => () => {
|
|
100
|
+
const bytes = randomBytes(secure)(8n);
|
|
101
|
+
return new DataView(bytes.buffer, bytes.byteOffset, 8).getBigUint64(0, true);
|
|
102
|
+
};
|
|
103
|
+
let seed: readonly [bigint, bigint] | undefined;
|
|
104
|
+
const writes = new Map<StdioOutput, Promise<void>>();
|
|
105
|
+
const write = (name: "stdout" | "stderr") => async (data: unknown) => {
|
|
106
|
+
const output = need(cli, "cli").stdio[name];
|
|
107
|
+
const selected = cli!.options[name];
|
|
108
|
+
const cancel = host.signal();
|
|
109
|
+
try {
|
|
110
|
+
for await (const chunk of guestBytes(data, cancel))
|
|
111
|
+
if (output && selected !== "discard") {
|
|
112
|
+
const bytes = chunk.slice();
|
|
113
|
+
// One provider may back both output interfaces. A queued write retains its
|
|
114
|
+
// initiating callback and physical ownership before it waits for the previous sink.
|
|
115
|
+
const operation = (writes.get(output) ?? Promise.resolve()).then(() => {
|
|
116
|
+
if (cancel.aborted) throw cancelled();
|
|
117
|
+
return output.write(bytes, cancel);
|
|
118
|
+
});
|
|
119
|
+
const turn = operation.then(
|
|
120
|
+
() => {},
|
|
121
|
+
() => {},
|
|
122
|
+
);
|
|
123
|
+
writes.set(output, turn);
|
|
124
|
+
void turn.then(() => {
|
|
125
|
+
if (writes.get(output) === turn) writes.delete(output);
|
|
126
|
+
});
|
|
127
|
+
await host.physical(() => operation);
|
|
128
|
+
}
|
|
129
|
+
return ok();
|
|
130
|
+
} catch {
|
|
131
|
+
return { tag: "err", val: "io" };
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
const waitUntil = async (when: bigint): Promise<void> => {
|
|
135
|
+
const source = need(clocks, "clocks");
|
|
136
|
+
const cancel = host.signal();
|
|
137
|
+
try {
|
|
138
|
+
await host.physical(() => source.waitUntil(when, cancel));
|
|
139
|
+
} catch (error) {
|
|
140
|
+
// WASI clock waits have no error lane. Like the native host, cancellation ends the
|
|
141
|
+
// wait early; the caller observes its cancelled invocation independently.
|
|
142
|
+
if (!cancel.aborted) throw error;
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
const interfaces: Record<string, Record<string, unknown>> = {
|
|
146
|
+
"wasi:clocks/types@0.3.0": {},
|
|
147
|
+
"wasi:clocks/monotonic-clock@0.3.0": {
|
|
148
|
+
now: () => need(clocks, "clocks").monotonicNow(),
|
|
149
|
+
getResolution: () => need(clocks, "clocks").monotonicResolution(),
|
|
150
|
+
// A wait belongs to the callback that started it: its cancellation, end or the
|
|
151
|
+
// revision's retirement stops the provider wait, which stays physically accounted.
|
|
152
|
+
waitUntil,
|
|
153
|
+
waitFor: (duration: bigint) => waitUntil(need(clocks, "clocks").monotonicNow() + duration),
|
|
154
|
+
},
|
|
155
|
+
"wasi:clocks/system-clock@0.3.0": {
|
|
156
|
+
now: () => need(clocks, "clocks").systemNow(),
|
|
157
|
+
getResolution: () => need(clocks, "clocks").systemResolution(),
|
|
158
|
+
},
|
|
159
|
+
"wasi:random/random@0.3.0": {
|
|
160
|
+
getRandomBytes: randomBytes(true),
|
|
161
|
+
getRandomU64: randomU64(true),
|
|
162
|
+
},
|
|
163
|
+
"wasi:random/insecure@0.3.0": {
|
|
164
|
+
getInsecureRandomBytes: randomBytes(false),
|
|
165
|
+
getInsecureRandomU64: randomU64(false),
|
|
166
|
+
},
|
|
167
|
+
"wasi:random/insecure-seed@0.3.0": {
|
|
168
|
+
// Stable for one instance, as the interface requires.
|
|
169
|
+
getInsecureSeed: () => (seed ??= need(random, "random").insecureSeed()),
|
|
170
|
+
},
|
|
171
|
+
"wasi:cli/types@0.3.0": {},
|
|
172
|
+
"wasi:cli/environment@0.3.0": {
|
|
173
|
+
getEnvironment: () => (cli?.options.environment ?? []).map(([key, value]) => [key, value]),
|
|
174
|
+
getArguments: () => [...(cli?.options.arguments ?? [])],
|
|
175
|
+
getInitialCwd: () => cli?.options.initialCwd,
|
|
176
|
+
},
|
|
177
|
+
"wasi:cli/exit@0.3.0": {
|
|
178
|
+
// Exit ends the revision: it is a trap carrying the exact status, never a guest result.
|
|
179
|
+
exit: (status: { tag: string }) => {
|
|
180
|
+
throw new RuntimeTrap(`orb exited with status ${status?.tag === "ok" ? 0 : 1}`);
|
|
181
|
+
},
|
|
182
|
+
exitWithCode: (code: number) => {
|
|
183
|
+
throw new RuntimeTrap(`orb exited with status ${code}`);
|
|
184
|
+
},
|
|
185
|
+
},
|
|
186
|
+
"wasi:cli/stdout@0.3.0": { writeViaStream: write("stdout") },
|
|
187
|
+
"wasi:cli/stderr@0.3.0": { writeViaStream: write("stderr") },
|
|
188
|
+
"wasi:cli/stdin@0.3.0": {
|
|
189
|
+
readViaStream: () => {
|
|
190
|
+
const options = need(cli, "cli").options.stdin;
|
|
191
|
+
const input = cli!.stdio.stdin;
|
|
192
|
+
let settle!: (value: unknown) => void;
|
|
193
|
+
const result = new Promise((resolve) => (settle = resolve));
|
|
194
|
+
const fixed =
|
|
195
|
+
typeof options === "object" && "bytes" in options ? options.bytes.slice() : undefined;
|
|
196
|
+
let offset = 0;
|
|
197
|
+
const readable = hostBytes(
|
|
198
|
+
async (limit) => {
|
|
199
|
+
if (options === "empty") return undefined;
|
|
200
|
+
if (fixed) {
|
|
201
|
+
const chunk = fixed.subarray(offset, offset + limit);
|
|
202
|
+
offset += chunk.length;
|
|
203
|
+
return chunk.length ? chunk.slice() : undefined;
|
|
204
|
+
}
|
|
205
|
+
// A retained stdin stream can be read by a later callback. Capture that read's
|
|
206
|
+
// cancellation now, rather than the callback that originally opened the stream.
|
|
207
|
+
const cancel = host.signal();
|
|
208
|
+
return input ? await host.physical(() => input.read(limit, cancel)) : undefined;
|
|
209
|
+
},
|
|
210
|
+
(end) => settle(end === "failed" ? { tag: "err", val: "io" } : ok()),
|
|
211
|
+
signal,
|
|
212
|
+
);
|
|
213
|
+
return [readable, result];
|
|
214
|
+
},
|
|
215
|
+
},
|
|
216
|
+
"wasi:cli/terminal-input@0.3.0": { TerminalInput: class TerminalInput {} },
|
|
217
|
+
"wasi:cli/terminal-output@0.3.0": {
|
|
218
|
+
TerminalOutput: class TerminalOutput {},
|
|
219
|
+
},
|
|
220
|
+
"wasi:cli/terminal-stdin@0.3.0": { getTerminalStdin: () => undefined },
|
|
221
|
+
"wasi:cli/terminal-stdout@0.3.0": { getTerminalStdout: () => undefined },
|
|
222
|
+
"wasi:cli/terminal-stderr@0.3.0": { getTerminalStderr: () => undefined },
|
|
223
|
+
};
|
|
224
|
+
const filesystem = filesystemImports(host, providers.mounts);
|
|
225
|
+
interfaces["wasi:filesystem/types@0.3.0"] = filesystem.types;
|
|
226
|
+
interfaces["wasi:filesystem/preopens@0.3.0"] = filesystem.preopens;
|
|
227
|
+
const http = httpImports(host, providers.http);
|
|
228
|
+
interfaces["wasi:http/types@0.3.0"] = http.types;
|
|
229
|
+
interfaces["wasi:http/client@0.3.0"] = http.client;
|
|
230
|
+
providers.ingress?.((http as { ingress?: unknown }).ingress);
|
|
231
|
+
const sockets = socketImports(host, providers.sockets);
|
|
232
|
+
interfaces["wasi:sockets/types@0.3.0"] = sockets.types;
|
|
233
|
+
interfaces["wasi:sockets/ip-name-lookup@0.3.0"] = sockets["ip-name-lookup"];
|
|
234
|
+
checkPublicWasi(names);
|
|
235
|
+
const imports: Record<string, unknown> = {};
|
|
236
|
+
for (const name of names) if (interfaces[name]) imports[name] = interfaces[name];
|
|
237
|
+
return imports;
|
|
238
|
+
}
|
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser host for the reusable interpreter core module and its private fixed-buffer ABI.
|
|
3
|
+
*
|
|
4
|
+
* Layouts and limits come from `abi.ts`, generated from `crates/executor-wasm/src/abi.rs`.
|
|
5
|
+
* Every Orb receives its own instance, imported memory and JavaScript heap; no Orb code is
|
|
6
|
+
* evaluated in the host realm. Any exception leaving a guest call traps the instance for good.
|
|
7
|
+
*/
|
|
8
|
+
import * as abi from "./abi.ts";
|
|
9
|
+
|
|
10
|
+
/** Host-only termination that must trap the instance instead of becoming a catchable reply. */
|
|
11
|
+
export class GuestTrap extends Error {}
|
|
12
|
+
|
|
13
|
+
export interface Outcome {
|
|
14
|
+
ok: boolean;
|
|
15
|
+
value: string;
|
|
16
|
+
interrupted: boolean;
|
|
17
|
+
}
|
|
18
|
+
/** A JSON host request, or a binary standard WASI packet (first byte `STANDARD_PACKET`). */
|
|
19
|
+
export interface Effect {
|
|
20
|
+
id: number;
|
|
21
|
+
packet: string | Uint8Array;
|
|
22
|
+
}
|
|
23
|
+
/** Successful standard results are bytes; JSON results and every error are text. */
|
|
24
|
+
export interface Completion {
|
|
25
|
+
id: number;
|
|
26
|
+
ok: boolean;
|
|
27
|
+
value: string | Uint8Array;
|
|
28
|
+
}
|
|
29
|
+
export interface Progress {
|
|
30
|
+
outcome?: Outcome;
|
|
31
|
+
effects: Effect[];
|
|
32
|
+
}
|
|
33
|
+
/** Logical interpreter operations driven by the browser port. */
|
|
34
|
+
export interface Guest {
|
|
35
|
+
load(packet: string): Outcome;
|
|
36
|
+
declare(packet: string): Outcome;
|
|
37
|
+
start(packet: string): Progress;
|
|
38
|
+
resume(completed: Completion[]): Progress;
|
|
39
|
+
cancel(): Outcome;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Host services supplied to one instance; `sync` throws to report an error message.
|
|
43
|
+
* Binary standard packets arrive and reply as bytes; JSON packets stay text.
|
|
44
|
+
*/
|
|
45
|
+
export interface GuestHost {
|
|
46
|
+
sync(packet: string | Uint8Array): string | Uint8Array;
|
|
47
|
+
log(level: "log" | "error", message: string): void;
|
|
48
|
+
/** Granted per-revision clock; absence exposes a fixed zero to interpreter libc. */
|
|
49
|
+
clock?(clock: number): bigint;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const encoder = new TextEncoder();
|
|
53
|
+
const decoder = new TextDecoder("utf-8", { fatal: true });
|
|
54
|
+
|
|
55
|
+
/** Refuse engines whose imports or exports differ from the private ABI before instantiation. */
|
|
56
|
+
export function checkInterpreter(module: WebAssembly.Module): void {
|
|
57
|
+
for (const { module: namespace, name, kind } of WebAssembly.Module.imports(module)) {
|
|
58
|
+
const allowed =
|
|
59
|
+
kind === "memory"
|
|
60
|
+
? namespace === abi.MEMORY_MODULE && name === abi.MEMORY_IMPORT
|
|
61
|
+
: kind === "function" &&
|
|
62
|
+
((namespace === abi.IMPORT_MODULE &&
|
|
63
|
+
(name === abi.SYNC_IMPORT || name === abi.SYNC_READ_IMPORT)) ||
|
|
64
|
+
(namespace === abi.WASI_MODULE &&
|
|
65
|
+
(abi.WASI_IMPORTS as readonly string[]).includes(name)));
|
|
66
|
+
if (!allowed) throw new Error(`Interpreter import ${namespace}::${name} is not allowed`);
|
|
67
|
+
}
|
|
68
|
+
const exports = new Map(
|
|
69
|
+
WebAssembly.Module.exports(module).map((entry) => [entry.name, entry.kind]),
|
|
70
|
+
);
|
|
71
|
+
for (const name of [abi.INITIALIZE_EXPORT, abi.ALLOC_EXPORT, abi.CALL_EXPORT])
|
|
72
|
+
if (exports.get(name) !== "function") throw new Error(`Interpreter export ${name} is missing`);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Read one little-endian field from a view created after the latest guest call. */
|
|
76
|
+
const field = (view: DataView, offset: number): number => view.getUint32(offset, true);
|
|
77
|
+
|
|
78
|
+
/** One isolated interpreter instance with a host-bounded linear memory. */
|
|
79
|
+
export class CoreGuest implements Guest {
|
|
80
|
+
#trapped = false;
|
|
81
|
+
#reply: Uint8Array | undefined;
|
|
82
|
+
#alloc!: (length: number) => number;
|
|
83
|
+
#call!: (operation: number, pointer: number, length: number) => number;
|
|
84
|
+
|
|
85
|
+
private constructor(
|
|
86
|
+
readonly memory: WebAssembly.Memory,
|
|
87
|
+
readonly host: GuestHost,
|
|
88
|
+
) {}
|
|
89
|
+
|
|
90
|
+
/** Instantiate a checked module with a fresh memory whose maximum is the configured bound. */
|
|
91
|
+
static async create(
|
|
92
|
+
module: WebAssembly.Module,
|
|
93
|
+
memoryBytes: number,
|
|
94
|
+
host: GuestHost,
|
|
95
|
+
): Promise<CoreGuest> {
|
|
96
|
+
const maximum = Math.floor(memoryBytes / abi.PAGE_BYTES);
|
|
97
|
+
if (!Number.isSafeInteger(memoryBytes) || maximum < abi.MEMORY_INITIAL_PAGES || maximum > 65536)
|
|
98
|
+
throw new Error("Invalid interpreter memory bound");
|
|
99
|
+
const memory = new WebAssembly.Memory({ initial: abi.MEMORY_INITIAL_PAGES, maximum });
|
|
100
|
+
const guest = new CoreGuest(memory, host);
|
|
101
|
+
const instance = await WebAssembly.instantiate(module, {
|
|
102
|
+
[abi.MEMORY_MODULE]: { [abi.MEMORY_IMPORT]: memory },
|
|
103
|
+
[abi.IMPORT_MODULE]: {
|
|
104
|
+
[abi.SYNC_IMPORT]: (pointer: number, length: number) => guest.#sync(pointer, length),
|
|
105
|
+
[abi.SYNC_READ_IMPORT]: (pointer: number, length: number) => guest.#read(pointer, length),
|
|
106
|
+
},
|
|
107
|
+
[abi.WASI_MODULE]: wasi(memory, host),
|
|
108
|
+
});
|
|
109
|
+
const exports = instance.exports as Record<string, (...args: number[]) => number>;
|
|
110
|
+
guest.#alloc = exports[abi.ALLOC_EXPORT]!;
|
|
111
|
+
guest.#call = exports[abi.CALL_EXPORT]!;
|
|
112
|
+
guest.#enter(() => exports[abi.INITIALIZE_EXPORT]!());
|
|
113
|
+
return guest;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** A trapped instance is never entered again. */
|
|
117
|
+
get trapped(): boolean {
|
|
118
|
+
return this.#trapped;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Check a guest range against the current memory size. */
|
|
122
|
+
#range(pointer: number, length: number): Uint8Array {
|
|
123
|
+
const start = pointer >>> 0;
|
|
124
|
+
const buffer = this.memory.buffer;
|
|
125
|
+
if (start + length > buffer.byteLength) throw new Error("Interpreter range is outside memory");
|
|
126
|
+
return new Uint8Array(buffer, start, length);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** Answer one synchronous host request; the reply waits for the matching read. */
|
|
130
|
+
#sync(pointer: number, length: number): number {
|
|
131
|
+
if (this.#reply) throw new Error("Synchronous reply was not read");
|
|
132
|
+
// Invalid ranges or UTF-8 are ABI violations and trap; host failures become messages.
|
|
133
|
+
const range = this.#range(pointer, length >>> 0);
|
|
134
|
+
const binary = range.byteLength > 0 && range[0] === abi.STANDARD_PACKET;
|
|
135
|
+
let ok = true,
|
|
136
|
+
value: string | Uint8Array;
|
|
137
|
+
try {
|
|
138
|
+
// The packet is copied before host code runs; it never aliases guest memory.
|
|
139
|
+
value = binary ? this.host.sync(range.slice()) : this.host.sync(decoder.decode(range));
|
|
140
|
+
if (binary !== value instanceof Uint8Array) throw new Error("Host reply has the wrong kind");
|
|
141
|
+
} catch (error) {
|
|
142
|
+
if (error instanceof GuestTrap) throw error;
|
|
143
|
+
ok = false;
|
|
144
|
+
value = error instanceof Error ? error.message : String(error);
|
|
145
|
+
}
|
|
146
|
+
const bytes = typeof value === "string" ? encoder.encode(value) : value;
|
|
147
|
+
if (bytes.byteLength > abi.LENGTH_LIMIT) throw new Error("Host reply exceeds the ABI limit");
|
|
148
|
+
this.#reply = bytes;
|
|
149
|
+
return (ok ? 0 : abi.SYNC_ERROR) + bytes.byteLength;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** Copy the pending reply into the guest's buffer of exactly its length. */
|
|
153
|
+
#read(pointer: number, length: number): void {
|
|
154
|
+
const reply = this.#reply;
|
|
155
|
+
this.#reply = undefined;
|
|
156
|
+
if (!reply || reply.byteLength !== length >>> 0)
|
|
157
|
+
throw new Error("Synchronous reply read does not match");
|
|
158
|
+
this.#range(pointer, reply.byteLength).set(reply);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** Mark the instance trapped when any exception leaves the guest. */
|
|
162
|
+
#enter<T>(operation: () => T): T {
|
|
163
|
+
if (this.#trapped) throw new Error("Interpreter instance is trapped");
|
|
164
|
+
try {
|
|
165
|
+
return operation();
|
|
166
|
+
} catch (error) {
|
|
167
|
+
this.#trapped = true;
|
|
168
|
+
throw error;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** Copy an input into the guest-reserved buffer, run one operation and return its reply view. */
|
|
173
|
+
#invoke(operation: number, input?: Uint8Array): DataView {
|
|
174
|
+
return this.#enter(() => {
|
|
175
|
+
let pointer = 0,
|
|
176
|
+
length = 0;
|
|
177
|
+
if (input) {
|
|
178
|
+
length = input.byteLength;
|
|
179
|
+
if (length > abi.LENGTH_LIMIT) throw new Error("Interpreter input exceeds the ABI limit");
|
|
180
|
+
pointer = this.#alloc(length) >>> 0;
|
|
181
|
+
if (pointer === 0) throw new Error("Interpreter input reservation was refused");
|
|
182
|
+
// Allocation may grow memory; the range is created from the current buffer.
|
|
183
|
+
this.#range(pointer, length).set(input);
|
|
184
|
+
}
|
|
185
|
+
const reply = this.#call(operation, pointer, length) >>> 0;
|
|
186
|
+
const buffer = this.memory.buffer;
|
|
187
|
+
if (reply + abi.REPLY_HEADER_BYTES > buffer.byteLength)
|
|
188
|
+
throw new Error("Interpreter reply is outside memory");
|
|
189
|
+
const total = new DataView(buffer, reply, 4).getUint32(0, true);
|
|
190
|
+
if (total < abi.REPLY_HEADER_BYTES || reply + total > buffer.byteLength)
|
|
191
|
+
throw new Error("Interpreter reply is outside memory");
|
|
192
|
+
return new DataView(buffer, reply, total);
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** Decode a reply exactly as `abi::Reply::decode` does; any inconsistency traps. */
|
|
197
|
+
#progress(view: DataView, text = true): Progress & { bytes?: Uint8Array | undefined } {
|
|
198
|
+
return this.#enter(() => {
|
|
199
|
+
const kind = field(view, 4),
|
|
200
|
+
interrupted = field(view, 8),
|
|
201
|
+
count = field(view, 12);
|
|
202
|
+
if (kind > abi.ReplyKind.Error || interrupted > 1)
|
|
203
|
+
throw new Error("Invalid interpreter reply");
|
|
204
|
+
if (count > abi.EFFECT_LIMIT || (kind !== abi.ReplyKind.Pending && count !== 0))
|
|
205
|
+
throw new Error("Invalid interpreter effect count");
|
|
206
|
+
let offset = abi.REPLY_HEADER_BYTES + count * abi.EFFECT_RECORD_BYTES;
|
|
207
|
+
if (offset > view.byteLength) throw new Error("Invalid interpreter reply");
|
|
208
|
+
const body = (length: number): Uint8Array => {
|
|
209
|
+
if (offset + length > view.byteLength) throw new Error("Invalid interpreter reply");
|
|
210
|
+
const bytes = new Uint8Array(view.buffer, view.byteOffset + offset, length);
|
|
211
|
+
offset += length;
|
|
212
|
+
return bytes;
|
|
213
|
+
};
|
|
214
|
+
const value = body(field(view, 16));
|
|
215
|
+
const effects: Effect[] = [];
|
|
216
|
+
for (let index = 0; index < count; index++) {
|
|
217
|
+
const record = abi.REPLY_HEADER_BYTES + index * abi.EFFECT_RECORD_BYTES;
|
|
218
|
+
const low = field(view, record),
|
|
219
|
+
high = field(view, record + 4);
|
|
220
|
+
const id = high * 2 ** 32 + low;
|
|
221
|
+
if (id === 0 || high >= 2 ** 21) throw new Error("Invalid interpreter effect identity");
|
|
222
|
+
const packet = body(field(view, record + 8));
|
|
223
|
+
// Standard packets stay raw bytes; every other effect is UTF-8 JSON.
|
|
224
|
+
effects.push({
|
|
225
|
+
id,
|
|
226
|
+
packet:
|
|
227
|
+
packet[0] === abi.STANDARD_PACKET && packet.byteLength > 0
|
|
228
|
+
? packet.slice()
|
|
229
|
+
: decoder.decode(packet),
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
if (offset !== view.byteLength) throw new Error("Interpreter reply has trailing bytes");
|
|
233
|
+
if (kind === abi.ReplyKind.Pending) return { effects };
|
|
234
|
+
const outcome = {
|
|
235
|
+
ok: kind === abi.ReplyKind.Ok,
|
|
236
|
+
value: text || kind !== abi.ReplyKind.Ok ? decoder.decode(value) : "",
|
|
237
|
+
interrupted: interrupted === 1,
|
|
238
|
+
};
|
|
239
|
+
return { outcome, effects, bytes: text ? undefined : value.slice() };
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/** Operations that must return a final outcome. */
|
|
244
|
+
#outcome(operation: number, input?: string): Outcome {
|
|
245
|
+
const progress = this.#progress(
|
|
246
|
+
this.#invoke(operation, input === undefined ? undefined : encoder.encode(input)),
|
|
247
|
+
);
|
|
248
|
+
if (!progress.outcome) {
|
|
249
|
+
this.#trapped = true;
|
|
250
|
+
throw new Error("Interpreter operation cannot return effects");
|
|
251
|
+
}
|
|
252
|
+
return progress.outcome;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
load(packet: string): Outcome {
|
|
256
|
+
return this.#outcome(abi.Operation.Load, packet);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
declare(packet: string): Outcome {
|
|
260
|
+
return this.#outcome(abi.Operation.Declare, packet);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
start(packet: string): Progress {
|
|
264
|
+
return this.#progress(this.#invoke(abi.Operation.Start, encoder.encode(packet)));
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/** Encode settled effects in one batch; identities stay JavaScript-safe integers. */
|
|
268
|
+
resume(completed: Completion[]): Progress {
|
|
269
|
+
if (!completed.length || completed.length > abi.EFFECT_LIMIT)
|
|
270
|
+
throw new Error("Invalid completion batch");
|
|
271
|
+
const values = completed.map(({ value }) =>
|
|
272
|
+
typeof value === "string" ? encoder.encode(value) : value,
|
|
273
|
+
);
|
|
274
|
+
const records = abi.COMPLETION_HEADER_BYTES + completed.length * abi.COMPLETION_RECORD_BYTES;
|
|
275
|
+
const batch = new Uint8Array(
|
|
276
|
+
values.reduce((total, value) => total + value.byteLength, records),
|
|
277
|
+
);
|
|
278
|
+
const view = new DataView(batch.buffer);
|
|
279
|
+
view.setUint32(0, completed.length, true);
|
|
280
|
+
let offset = records;
|
|
281
|
+
completed.forEach((completion, index) => {
|
|
282
|
+
const { id } = completion;
|
|
283
|
+
if (!Number.isSafeInteger(id) || id < 1) throw new Error("Invalid effect identity");
|
|
284
|
+
const record = abi.COMPLETION_HEADER_BYTES + index * abi.COMPLETION_RECORD_BYTES;
|
|
285
|
+
view.setUint32(record, id % 2 ** 32, true);
|
|
286
|
+
view.setUint32(record + 4, Math.floor(id / 2 ** 32), true);
|
|
287
|
+
view.setUint32(record + 8, completion.ok ? abi.Status.Ok : abi.Status.Error, true);
|
|
288
|
+
view.setUint32(record + 12, values[index]!.byteLength, true);
|
|
289
|
+
batch.set(values[index]!, offset);
|
|
290
|
+
offset += values[index]!.byteLength;
|
|
291
|
+
});
|
|
292
|
+
return this.#progress(this.#invoke(abi.Operation.Resume, batch));
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
cancel(): Outcome {
|
|
296
|
+
return this.#outcome(abi.Operation.Cancel);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** Capture idle heap bytes; durable continuation restore is a separate host protocol. */
|
|
300
|
+
capture(): Uint8Array {
|
|
301
|
+
const progress = this.#progress(this.#invoke(abi.Operation.Capture), false);
|
|
302
|
+
if (!progress.outcome?.ok) throw new Error(progress.outcome?.value ?? "Capture failed");
|
|
303
|
+
return progress.bytes!;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/** Restricted WASI preview 1 functions mirroring `crates/executor-wasm/src/worker/wasi.rs`. */
|
|
308
|
+
export function wasi(memory: WebAssembly.Memory, host: GuestHost): Record<string, Function> {
|
|
309
|
+
const open = [true, true, true];
|
|
310
|
+
const isOpen = (descriptor: number): boolean => open[descriptor] === true;
|
|
311
|
+
/** A fresh view per call; memory growth detaches earlier buffers. */
|
|
312
|
+
const view = (): DataView => new DataView(memory.buffer);
|
|
313
|
+
/** Report EFAULT instead of trapping for guest-supplied ranges. */
|
|
314
|
+
const fits = (pointer: number, length: number): boolean =>
|
|
315
|
+
(pointer >>> 0) + length <= memory.buffer.byteLength;
|
|
316
|
+
return {
|
|
317
|
+
clock_time_get(clock: number, _precision: bigint, out: number): number {
|
|
318
|
+
if (clock !== abi.CLOCK_REALTIME && clock !== abi.CLOCK_MONOTONIC) return abi.ERRNO_INVAL;
|
|
319
|
+
if (!fits(out, 8)) return abi.ERRNO_FAULT;
|
|
320
|
+
const nanoseconds = host.clock?.(clock) ?? 0n;
|
|
321
|
+
// WASIp1 has an unsigned nanosecond clock; match the native host's saturating conversion.
|
|
322
|
+
const maximum = (1n << 64n) - 1n;
|
|
323
|
+
view().setBigUint64(
|
|
324
|
+
out >>> 0,
|
|
325
|
+
nanoseconds < 0n ? 0n : nanoseconds > maximum ? maximum : nanoseconds,
|
|
326
|
+
true,
|
|
327
|
+
);
|
|
328
|
+
return abi.ERRNO_SUCCESS;
|
|
329
|
+
},
|
|
330
|
+
environ_get: (): number => abi.ERRNO_SUCCESS,
|
|
331
|
+
environ_sizes_get(count: number, size: number): number {
|
|
332
|
+
if (!fits(count, 4) || !fits(size, 4)) return abi.ERRNO_FAULT;
|
|
333
|
+
view().setUint32(count >>> 0, 0, true);
|
|
334
|
+
view().setUint32(size >>> 0, 0, true);
|
|
335
|
+
return abi.ERRNO_SUCCESS;
|
|
336
|
+
},
|
|
337
|
+
fd_close(descriptor: number): number {
|
|
338
|
+
if (!isOpen(descriptor)) return abi.ERRNO_BADF;
|
|
339
|
+
open[descriptor] = false;
|
|
340
|
+
return abi.ERRNO_SUCCESS;
|
|
341
|
+
},
|
|
342
|
+
fd_fdstat_get(descriptor: number, out: number): number {
|
|
343
|
+
if (!isOpen(descriptor)) return abi.ERRNO_BADF;
|
|
344
|
+
if (!fits(out, abi.FDSTAT_BYTES)) return abi.ERRNO_FAULT;
|
|
345
|
+
new Uint8Array(memory.buffer, out >>> 0, abi.FDSTAT_BYTES).fill(0);
|
|
346
|
+
const stat = view();
|
|
347
|
+
stat.setUint8(out >>> 0, abi.FILETYPE_CHARACTER_DEVICE);
|
|
348
|
+
stat.setUint32((out >>> 0) + 8, descriptor === 0 ? 0 : abi.RIGHT_FD_WRITE, true);
|
|
349
|
+
return abi.ERRNO_SUCCESS;
|
|
350
|
+
},
|
|
351
|
+
fd_prestat_get: (): number => abi.ERRNO_BADF,
|
|
352
|
+
fd_prestat_dir_name: (): number => abi.ERRNO_BADF,
|
|
353
|
+
fd_seek: (descriptor: number): number =>
|
|
354
|
+
isOpen(descriptor) ? abi.ERRNO_SPIPE : abi.ERRNO_BADF,
|
|
355
|
+
path_open: (descriptor: number): number =>
|
|
356
|
+
isOpen(descriptor) ? abi.ERRNO_NOTDIR : abi.ERRNO_BADF,
|
|
357
|
+
fd_write(descriptor: number, vectors: number, count: number, written: number): number {
|
|
358
|
+
const level = descriptor === 1 ? "log" : descriptor === 2 ? "error" : undefined;
|
|
359
|
+
if (!level || !isOpen(descriptor)) return abi.ERRNO_BADF;
|
|
360
|
+
if (!fits(vectors, (count >>> 0) * 8)) return abi.ERRNO_FAULT;
|
|
361
|
+
const table = view();
|
|
362
|
+
const kept = new Uint8Array(abi.STDIO_LIMIT);
|
|
363
|
+
let total = 0,
|
|
364
|
+
size = 0;
|
|
365
|
+
for (let index = 0; index < count >>> 0; index++) {
|
|
366
|
+
const pointer = table.getUint32((vectors >>> 0) + index * 8, true);
|
|
367
|
+
const length = table.getUint32((vectors >>> 0) + index * 8 + 4, true);
|
|
368
|
+
if (!fits(pointer, length)) return abi.ERRNO_FAULT;
|
|
369
|
+
total += length;
|
|
370
|
+
if (total > 0xffffffff) return abi.ERRNO_INVAL;
|
|
371
|
+
const room = Math.min(length, abi.STDIO_LIMIT - size);
|
|
372
|
+
kept.set(new Uint8Array(memory.buffer, pointer >>> 0, room), size);
|
|
373
|
+
size += room;
|
|
374
|
+
}
|
|
375
|
+
if (!fits(written, 4)) return abi.ERRNO_FAULT;
|
|
376
|
+
view().setUint32(written >>> 0, total, true);
|
|
377
|
+
// Diagnostics are lossy text; the write itself is fully consumed.
|
|
378
|
+
const text = new TextDecoder().decode(kept.subarray(0, size)).replace(/\n+$/, "");
|
|
379
|
+
if (text) host.log(level, text);
|
|
380
|
+
return abi.ERRNO_SUCCESS;
|
|
381
|
+
},
|
|
382
|
+
proc_exit(code: number): never {
|
|
383
|
+
throw new Error(`Interpreter exited with status ${code}`);
|
|
384
|
+
},
|
|
385
|
+
};
|
|
386
|
+
}
|