@syntropika/tembra-sdk 0.2.1 → 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 -3298
- 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 -3276
- 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 -3287
- 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 +9 -3
- 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 +218 -56
- 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 -293
|
@@ -0,0 +1,453 @@
|
|
|
1
|
+
import { checkSignal, combinedSignal } from "../runtime/signals.ts";
|
|
2
|
+
import type { CapturedInvocation } from "#tembra-adapters";
|
|
3
|
+
import type { Declaration } from "../authoring/declaration.ts";
|
|
4
|
+
import type { DynamicReference, Resource } from "../authoring/types.ts";
|
|
5
|
+
import type { Context } from "../runtime/context.ts";
|
|
6
|
+
import type { State } from "../state.ts";
|
|
7
|
+
import type { ExecutionScope } from "./types.ts";
|
|
8
|
+
import {
|
|
9
|
+
type AsyncHostRequest,
|
|
10
|
+
type HostRequest,
|
|
11
|
+
WAIT_LIMIT_MILLISECONDS,
|
|
12
|
+
survivesCancellation,
|
|
13
|
+
} from "./host-protocol.ts";
|
|
14
|
+
import type { Revision } from "./revision.ts";
|
|
15
|
+
import { check, encode, VALUE_LIMIT } from "./codec.ts";
|
|
16
|
+
import { lift } from "./wit-shape.ts";
|
|
17
|
+
import { acquireReference } from "./acquire.ts";
|
|
18
|
+
|
|
19
|
+
const scopeEnded = Object.freeze(new Error("Interpreter callback scope ended"));
|
|
20
|
+
|
|
21
|
+
/** The host-issued authority one callback's requests are served under. */
|
|
22
|
+
export type Authority =
|
|
23
|
+
| { declaration: Declaration }
|
|
24
|
+
| { context: Context }
|
|
25
|
+
| { signal: AbortSignal }
|
|
26
|
+
| { validation: true };
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The revision tables and callback executor a scope serves requests with. A source revision is
|
|
30
|
+
* one; a prepared browser component lends its own for the kernel interfaces its Orb imports
|
|
31
|
+
* (`component/kernel.ts`), so both answer the same requests with this same code.
|
|
32
|
+
*/
|
|
33
|
+
export type ScopeRevision = Pick<
|
|
34
|
+
Revision,
|
|
35
|
+
| "contract"
|
|
36
|
+
| "scopes"
|
|
37
|
+
| "views"
|
|
38
|
+
| "imports"
|
|
39
|
+
| "importMethods"
|
|
40
|
+
| "importSignatures"
|
|
41
|
+
| "hooks"
|
|
42
|
+
| "signatures"
|
|
43
|
+
> & {
|
|
44
|
+
/**
|
|
45
|
+
* Run a resource callback the guest numbered with the scope its `authority` issued. A source
|
|
46
|
+
* revision needs only the scope; a prepared component also admits its dependency calls with
|
|
47
|
+
* the authority itself, which stays on the host.
|
|
48
|
+
*/
|
|
49
|
+
execute(command: string, scope: ExecutionScope, authority: Authority): Promise<unknown>;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/** Read cancellation from the host-issued authority without transferring it to the guest. */
|
|
53
|
+
function authoritySignal(authority: Authority): AbortSignal | undefined {
|
|
54
|
+
if ("context" in authority) return authority.context.signal;
|
|
55
|
+
if ("signal" in authority) return authority.signal;
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Host-owned tables restrict guest numbers to one environment and its explicit Orb parent. */
|
|
60
|
+
export class Scope {
|
|
61
|
+
readonly id: number;
|
|
62
|
+
readonly parent: Scope | undefined;
|
|
63
|
+
readonly validators: number[] = [];
|
|
64
|
+
readonly states = new Map<number, State<unknown>>();
|
|
65
|
+
/**
|
|
66
|
+
* Declared references belong to this environment. A `root` marks a `Context.get` handle: the
|
|
67
|
+
* runtime's root claim list owns it and releases it when that root ends.
|
|
68
|
+
*/
|
|
69
|
+
readonly references = new Map<
|
|
70
|
+
number,
|
|
71
|
+
{ reference: DynamicReference; binding: string; root?: number }
|
|
72
|
+
>();
|
|
73
|
+
readonly resources = new Map<number, Resource<number>>();
|
|
74
|
+
readonly hooks = new Map<
|
|
75
|
+
number,
|
|
76
|
+
{ handle: ReturnType<Declaration["hook"]>; binding: import("./types.ts").HookBinding }
|
|
77
|
+
>();
|
|
78
|
+
#next = 0;
|
|
79
|
+
|
|
80
|
+
/** Retain only issued declarations; service scopes inherit their own revision's Orb resources. */
|
|
81
|
+
constructor(
|
|
82
|
+
readonly revision: ScopeRevision,
|
|
83
|
+
readonly declaration: Declaration,
|
|
84
|
+
readonly service: string | null,
|
|
85
|
+
) {
|
|
86
|
+
this.id = declaration.environment.id;
|
|
87
|
+
const parent = service === null ? undefined : declaration.environment.orb.scope;
|
|
88
|
+
this.parent = service === null || !parent ? undefined : revision.scopes.get(parent.id);
|
|
89
|
+
if (service !== null && !this.parent) throw new Error("Missing interpreter Orb scope");
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Adapt an issued host authority to the interpreter transport without serializing it. */
|
|
93
|
+
exchange(authority: Authority): ExecutionScope {
|
|
94
|
+
let ended: AbortController | undefined;
|
|
95
|
+
let cancelled = false;
|
|
96
|
+
const signal = authoritySignal(authority);
|
|
97
|
+
return {
|
|
98
|
+
cancel: () => {
|
|
99
|
+
if (cancelled) return;
|
|
100
|
+
cancelled = true;
|
|
101
|
+
ended?.abort(scopeEnded);
|
|
102
|
+
},
|
|
103
|
+
...(signal ? { signal } : {}),
|
|
104
|
+
check: () => {
|
|
105
|
+
if ("context" in authority) authority.context.check();
|
|
106
|
+
if ("signal" in authority) checkSignal(authority.signal);
|
|
107
|
+
},
|
|
108
|
+
sync: (request) => this.sync(request, authority),
|
|
109
|
+
async: (request) => {
|
|
110
|
+
if (cancelled) return Promise.reject(scopeEnded);
|
|
111
|
+
ended ??= new AbortController();
|
|
112
|
+
return this.async(request, authority, ended.signal);
|
|
113
|
+
},
|
|
114
|
+
deliver: (request, value, publish) => {
|
|
115
|
+
let accepted = false;
|
|
116
|
+
try {
|
|
117
|
+
accepted = publish(value);
|
|
118
|
+
} finally {
|
|
119
|
+
// The worker may cancel an import while this host is awaiting readiness. Never
|
|
120
|
+
// leave a ready but undelivered view owned by an otherwise long-lived root.
|
|
121
|
+
if (
|
|
122
|
+
!accepted &&
|
|
123
|
+
request.operation === "reference.acquire" &&
|
|
124
|
+
this.revision.views.has(request.service) &&
|
|
125
|
+
typeof value === "number"
|
|
126
|
+
) {
|
|
127
|
+
const table = this.parent ?? this;
|
|
128
|
+
const slot = table.references.get(value);
|
|
129
|
+
const root = "context" in authority ? authority.context.root : undefined;
|
|
130
|
+
if (slot?.binding === request.service && slot.root === root) {
|
|
131
|
+
table.references.delete(value);
|
|
132
|
+
slot.reference.release();
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
},
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** Require the actual admitted call for writes and dependent invocations. */
|
|
141
|
+
private context(authority: Authority): Context {
|
|
142
|
+
if (!("context" in authority)) throw new Error("Operation requires an admitted invocation");
|
|
143
|
+
authority.context.check();
|
|
144
|
+
return authority.context;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** Reject declarations outside the original synchronous pass. */
|
|
148
|
+
private factory(authority: Authority): Declaration {
|
|
149
|
+
if (!("declaration" in authority)) throw new Error("Operation requires an active declaration");
|
|
150
|
+
return authority.declaration;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** Select this table or the issued parent's; a scope is a table selector, never an authority. */
|
|
154
|
+
private selected(scope: number | undefined): Scope {
|
|
155
|
+
if (scope === undefined || scope === this.id) return this;
|
|
156
|
+
if (this.parent !== undefined && scope === this.parent.id) return this.parent;
|
|
157
|
+
throw new Error("Foreign environment handle");
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** Resolve only handles issued from the selected host table. */
|
|
161
|
+
private handle<T>(table: Map<number, T>, id: number, kind: string): T {
|
|
162
|
+
const value = table.get(id);
|
|
163
|
+
if (value === undefined) throw new Error(`Unknown ${kind} handle`);
|
|
164
|
+
return value;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Match the native u32 handle space and never reuse an expired handle's identity. */
|
|
168
|
+
private allocate(): number {
|
|
169
|
+
if (this.#next >= 0xffff_ffff) throw new Error("Scope handle space exhausted");
|
|
170
|
+
return ++this.#next;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Return the kernel's snapshot revision rather than inventing guest state identities. When the
|
|
175
|
+
* guest already retains the current revision, only the identity crosses back; the protected value
|
|
176
|
+
* is omitted because a revision never names two different values.
|
|
177
|
+
*/
|
|
178
|
+
private snapshot(value: unknown, object: number, known?: string): unknown {
|
|
179
|
+
const snapshot = this.declaration.session.snapshot(object);
|
|
180
|
+
return snapshot.revision === known
|
|
181
|
+
? { revision: snapshot.revision, version: snapshot.version }
|
|
182
|
+
: { value, revision: snapshot.revision, version: snapshot.version };
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** Service synchronous requests against one immutable authority selected by the host callback. */
|
|
186
|
+
private sync(request: HostRequest, authority: Authority): unknown {
|
|
187
|
+
if (request.operation === "console")
|
|
188
|
+
throw new Error("Console records are consumed by the interpreter port");
|
|
189
|
+
if ("validation" in authority && request.operation !== "state.read")
|
|
190
|
+
throw new Error("Validators may only read retained state");
|
|
191
|
+
if (!survivesCancellation(request)) {
|
|
192
|
+
if ("context" in authority) authority.context.check();
|
|
193
|
+
if ("signal" in authority) checkSignal(authority.signal);
|
|
194
|
+
}
|
|
195
|
+
switch (request.operation) {
|
|
196
|
+
case "address": {
|
|
197
|
+
const declaration = this.factory(authority);
|
|
198
|
+
return {
|
|
199
|
+
key: this.service === null ? [] : declaration.key,
|
|
200
|
+
principal: declaration.principal,
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
case "orb":
|
|
204
|
+
this.factory(authority);
|
|
205
|
+
// The root Orb environment has no parent; the guest observes `undefined`.
|
|
206
|
+
return this.parent?.id ?? null;
|
|
207
|
+
case "state.declare": {
|
|
208
|
+
const declaration = this.factory(authority);
|
|
209
|
+
const id = this.allocate();
|
|
210
|
+
const state = declaration.state(request.initial, { version: request.version });
|
|
211
|
+
this.states.set(id, state);
|
|
212
|
+
if (request.validate !== null) this.validators.push(request.validate);
|
|
213
|
+
return id;
|
|
214
|
+
}
|
|
215
|
+
case "state.read": {
|
|
216
|
+
// Ownership and the state's lifetime are checked even when its value is not resent.
|
|
217
|
+
const selected = this.selected(request.scope);
|
|
218
|
+
return selected.snapshot(
|
|
219
|
+
this.handle(selected.states, request.id, "state").value,
|
|
220
|
+
selected.declaration.environment.object!.id,
|
|
221
|
+
request.known,
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
case "state.update": {
|
|
225
|
+
const selected = this.selected(request.scope);
|
|
226
|
+
const state = this.handle(selected.states, request.id, "state");
|
|
227
|
+
const value = state.update(this.context(authority), () => request.value);
|
|
228
|
+
return selected.snapshot(value, selected.declaration.environment.object!.id);
|
|
229
|
+
}
|
|
230
|
+
case "hook.declare": {
|
|
231
|
+
const binding = this.revision.hooks.get(request.name);
|
|
232
|
+
if (!binding) throw new Error("Hook is absent from the checked WIT declaration");
|
|
233
|
+
const handle = this.factory(authority).hook(binding.name);
|
|
234
|
+
const id = this.allocate();
|
|
235
|
+
this.hooks.set(id, { handle, binding });
|
|
236
|
+
return id;
|
|
237
|
+
}
|
|
238
|
+
case "reference.declare": {
|
|
239
|
+
const service = this.revision.imports.get(request.service);
|
|
240
|
+
if (!service) throw new Error("Reference is not a selected WIT Orb import");
|
|
241
|
+
const reference = this.factory(authority).get(service, request.key);
|
|
242
|
+
const id = this.allocate();
|
|
243
|
+
this.references.set(id, { reference, binding: request.service });
|
|
244
|
+
return id;
|
|
245
|
+
}
|
|
246
|
+
case "reference.release": {
|
|
247
|
+
const slot = this.reference(request, authority);
|
|
248
|
+
slot.reference.release();
|
|
249
|
+
if (this.revision.views.has(slot.binding))
|
|
250
|
+
this.selected(request.scope).references.delete(request.id);
|
|
251
|
+
return null;
|
|
252
|
+
}
|
|
253
|
+
case "reference.status":
|
|
254
|
+
return this.reference(request, authority).reference.status;
|
|
255
|
+
case "reference.state": {
|
|
256
|
+
const reference = this.reference(request, authority).reference;
|
|
257
|
+
const record = this.declaration.session.handles.reference(reference);
|
|
258
|
+
return this.snapshot(reference.state.value, record.object, request.known);
|
|
259
|
+
}
|
|
260
|
+
case "resource.declare": {
|
|
261
|
+
const declaration = this.factory(authority);
|
|
262
|
+
const dependencies = request.dependencies.map((dependency) =>
|
|
263
|
+
this.handle(
|
|
264
|
+
this.selected(dependency.scope).resources,
|
|
265
|
+
dependency.id,
|
|
266
|
+
"resource dependency",
|
|
267
|
+
),
|
|
268
|
+
);
|
|
269
|
+
const { open, close } = request;
|
|
270
|
+
const run = (callback: number, value: number | null, authority: Authority) =>
|
|
271
|
+
this.revision.execute(
|
|
272
|
+
JSON.stringify({ kind: "callback", callback, value }),
|
|
273
|
+
this.exchange(authority),
|
|
274
|
+
authority,
|
|
275
|
+
);
|
|
276
|
+
const options = {
|
|
277
|
+
version: request.version,
|
|
278
|
+
deps: dependencies,
|
|
279
|
+
open: (signal: AbortSignal) => run(open, null, { signal }) as Promise<number>,
|
|
280
|
+
close: (value: number, context: Context) => run(close, value, { context }),
|
|
281
|
+
};
|
|
282
|
+
const resource = declaration.resource<number>(request.name, options);
|
|
283
|
+
const id = this.allocate();
|
|
284
|
+
this.resources.set(id, resource);
|
|
285
|
+
return id;
|
|
286
|
+
}
|
|
287
|
+
case "resource.read":
|
|
288
|
+
return this.handle(this.selected(request.scope).resources, request.id, "resource").value;
|
|
289
|
+
case "context.check":
|
|
290
|
+
if (!("context" in authority)) throw new Error("Operation requires an admitted invocation");
|
|
291
|
+
return null;
|
|
292
|
+
case "context.cancel":
|
|
293
|
+
this.context(authority).cancel();
|
|
294
|
+
return null;
|
|
295
|
+
case "context.principal":
|
|
296
|
+
return this.context(authority).principal;
|
|
297
|
+
case "signal.cancelled":
|
|
298
|
+
return authoritySignal(authority)?.aborted ?? false;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Resolve a reference handle and its WIT binding for the current callback. Declared handles come
|
|
304
|
+
* from this table or the issued parent's. A `Context.get` handle resolves only in the table that
|
|
305
|
+
* issued it, for a callback of the root owning its claim, and only while that root lasts.
|
|
306
|
+
*/
|
|
307
|
+
private reference(request: { scope?: number; id: number }, authority: Authority) {
|
|
308
|
+
const selected = this.selected(request.scope);
|
|
309
|
+
const slot = this.handle(selected.references, request.id, "reference");
|
|
310
|
+
if (slot.root === undefined) return slot;
|
|
311
|
+
const current = "context" in authority ? authority.context.root : undefined;
|
|
312
|
+
const issued =
|
|
313
|
+
selected === this || (this.revision.views.has(slot.binding) && selected === this.parent);
|
|
314
|
+
if (!issued || current !== slot.root) throw new Error("Reference belongs to another root");
|
|
315
|
+
if (!this.declaration.session.rootReferences.has(slot.root))
|
|
316
|
+
throw new Error("Reference expired with its root");
|
|
317
|
+
return slot;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* `Context.get`: acquire a selected WIT Orb import for the active invocation (see
|
|
322
|
+
* `acquire.ts`). Only a ready claim receives a handle, filed with the root owning it.
|
|
323
|
+
*/
|
|
324
|
+
private async acquire(
|
|
325
|
+
request: { service: string; key: readonly string[] },
|
|
326
|
+
authority: Authority,
|
|
327
|
+
ended: AbortSignal,
|
|
328
|
+
): Promise<number> {
|
|
329
|
+
const context = this.context(authority);
|
|
330
|
+
const binding = this.revision.imports.get(request.service);
|
|
331
|
+
if (!binding) throw new Error("Reference is not a selected WIT Orb import");
|
|
332
|
+
const session = this.declaration.session;
|
|
333
|
+
const table = this.revision.views.has(request.service) ? (this.parent ?? this) : this;
|
|
334
|
+
const { id, reference } = await acquireReference(
|
|
335
|
+
this.declaration,
|
|
336
|
+
binding,
|
|
337
|
+
request.key,
|
|
338
|
+
context,
|
|
339
|
+
ended,
|
|
340
|
+
(reference) => ({ id: table.allocate(), reference }),
|
|
341
|
+
);
|
|
342
|
+
// Root claims are released by the runtime; forget the names of those whose roots have ended.
|
|
343
|
+
for (const [handle, slot] of table.references)
|
|
344
|
+
if (slot.root !== undefined && !session.rootReferences.has(slot.root))
|
|
345
|
+
table.references.delete(handle);
|
|
346
|
+
table.references.set(id, { reference, binding: request.service, root: context.root });
|
|
347
|
+
return id;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/** Await only issued references and bounded cooperative timers. */
|
|
351
|
+
private async async(
|
|
352
|
+
request: AsyncHostRequest,
|
|
353
|
+
authority: Authority,
|
|
354
|
+
ended: AbortSignal,
|
|
355
|
+
): Promise<unknown> {
|
|
356
|
+
checkSignal(ended);
|
|
357
|
+
switch (request.operation) {
|
|
358
|
+
case "hook.run": {
|
|
359
|
+
const context = this.context(authority);
|
|
360
|
+
const { handle, binding } = this.handle(
|
|
361
|
+
this.selected(request.scope).hooks,
|
|
362
|
+
request.id,
|
|
363
|
+
"hook",
|
|
364
|
+
);
|
|
365
|
+
// The guest's wire text is checked as is; host participants then receive bigint values.
|
|
366
|
+
check(this.revision.contract, binding.slot, true, encode([request.value]));
|
|
367
|
+
const shape = this.revision.signatures.get(binding.slot);
|
|
368
|
+
const [input] = lift(shape?.params, [request.value]) as [unknown];
|
|
369
|
+
const result = (await handle.run(context, input)) ?? null;
|
|
370
|
+
return check(
|
|
371
|
+
this.revision.contract,
|
|
372
|
+
binding.slot,
|
|
373
|
+
false,
|
|
374
|
+
encode(result, VALUE_LIMIT, shape?.result),
|
|
375
|
+
);
|
|
376
|
+
}
|
|
377
|
+
case "reference.ready": {
|
|
378
|
+
const owner = authoritySignal(authority);
|
|
379
|
+
await this.declaration.session.calls.observe(
|
|
380
|
+
this.reference(request, authority).reference.ready,
|
|
381
|
+
owner ? combinedSignal(owner, ended) : ended,
|
|
382
|
+
);
|
|
383
|
+
return null;
|
|
384
|
+
}
|
|
385
|
+
case "reference.acquire":
|
|
386
|
+
return this.acquire(request, authority, ended);
|
|
387
|
+
case "reference.call": {
|
|
388
|
+
const context = this.context(authority);
|
|
389
|
+
const { reference, binding } = this.reference(request, authority);
|
|
390
|
+
const bound = this.revision.importMethods.get(binding)?.get(request.method);
|
|
391
|
+
if (!bound) throw new Error("Unknown prepared import method");
|
|
392
|
+
const method = bound.name;
|
|
393
|
+
// The importing heap is untrusted: check its decoded arguments under its own signature.
|
|
394
|
+
const input = check(this.revision.contract, bound.slot, true, encode(request.args));
|
|
395
|
+
const signature = this.revision.importSignatures.get(binding);
|
|
396
|
+
const capture: CapturedInvocation | undefined = signature
|
|
397
|
+
? { signature, input, dataResult: true }
|
|
398
|
+
: undefined;
|
|
399
|
+
// The WIT check above already rejects non-list arguments; this narrows the payload.
|
|
400
|
+
if (!Array.isArray(request.args)) throw new Error("Import arguments must be a list");
|
|
401
|
+
const record = this.declaration.session.handles.reference(reference);
|
|
402
|
+
if (!record.methods.has(method)) throw new Error("Reference does not expose this method");
|
|
403
|
+
// Whether the captured route runs is decided by the provider at invocation, not by the
|
|
404
|
+
// existence of a signature. Every non-captured route (a native adapter or a host handler)
|
|
405
|
+
// takes the JavaScript domain form, WIT 64-bit positions as bigint; the captured route
|
|
406
|
+
// ignores these arguments and splices the checked wire text `input` instead.
|
|
407
|
+
const shape = this.revision.signatures.get(bound.slot);
|
|
408
|
+
const result =
|
|
409
|
+
(await this.declaration.session.calls.invokePhysical(
|
|
410
|
+
record,
|
|
411
|
+
method,
|
|
412
|
+
lift(shape?.params, request.args) as unknown[],
|
|
413
|
+
context,
|
|
414
|
+
capture,
|
|
415
|
+
)) ?? null;
|
|
416
|
+
// The provider may be an ordinary host implementation returning hostile JavaScript. Reply
|
|
417
|
+
// with the checked capture, never with the original object graph.
|
|
418
|
+
return (
|
|
419
|
+
capture?.output ??
|
|
420
|
+
check(
|
|
421
|
+
this.revision.contract,
|
|
422
|
+
bound.slot,
|
|
423
|
+
false,
|
|
424
|
+
encode(result, VALUE_LIMIT, shape?.result),
|
|
425
|
+
)
|
|
426
|
+
);
|
|
427
|
+
}
|
|
428
|
+
case "wait": {
|
|
429
|
+
const { milliseconds } = request;
|
|
430
|
+
if (milliseconds > WAIT_LIMIT_MILLISECONDS)
|
|
431
|
+
throw new Error("Timer exceeds the callback wait limit");
|
|
432
|
+
if ("context" in authority) this.context(authority);
|
|
433
|
+
const ownerSignal = authoritySignal(authority);
|
|
434
|
+
if (!ownerSignal) throw new Error("Timer requires an active callback");
|
|
435
|
+
const signal = combinedSignal(ownerSignal, ended);
|
|
436
|
+
checkSignal(signal);
|
|
437
|
+
await new Promise<void>((resolve, reject) => {
|
|
438
|
+
const cancel = () => {
|
|
439
|
+
clearTimeout(timer);
|
|
440
|
+
signal.removeEventListener("abort", cancel);
|
|
441
|
+
reject(new Error("Operation cancelled"));
|
|
442
|
+
};
|
|
443
|
+
const timer = setTimeout(() => {
|
|
444
|
+
signal.removeEventListener("abort", cancel);
|
|
445
|
+
resolve();
|
|
446
|
+
}, milliseconds);
|
|
447
|
+
signal.addEventListener("abort", cancel, { once: true });
|
|
448
|
+
});
|
|
449
|
+
return null;
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
}
|