@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,611 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The kernel's own interfaces (`tembra:guest`) for a prepared component.
|
|
3
|
+
*
|
|
4
|
+
* A compiled Orb keeps kernel-managed state, opens root resources, declares hooks and reads its
|
|
5
|
+
* call's context through the bundled `tembra:guest` package. Native hosts serve it by turning
|
|
6
|
+
* each operation into the private host request a source Orb issues (`state.declare`,
|
|
7
|
+
* `resource.declare`, `hook.declare`, `hook.run`, ...), answered by the same code as for a
|
|
8
|
+
* source Orb: the native executor on Rust hosts, the interpreter `Scope` on Node and mobile. A
|
|
9
|
+
* prepared browser component makes the same translation and lends its tables to that same
|
|
10
|
+
* `Scope`, so one Orb keeps one behavior on every host:
|
|
11
|
+
*
|
|
12
|
+
* - While each environment is declared, the root binds the hooks this Orb runs and declares the
|
|
13
|
+
* resources its component lists, with callback numbers of its own; a service object declares
|
|
14
|
+
* the resources and the state its component describes for its service. The address of each
|
|
15
|
+
* environment is read then.
|
|
16
|
+
* - The kernel later opens and closes resources through those numbers and, before a reload
|
|
17
|
+
* adopts a retained state, asks the replacement component to validate it.
|
|
18
|
+
* - Imports issue `state.read`, `state.update`, `resource.read`, the context requests and
|
|
19
|
+
* `hook.run` under the authority of the callback that physically issued them; a refusal
|
|
20
|
+
* rejects the suspending import, which traps the guest.
|
|
21
|
+
*
|
|
22
|
+
* Mirrors the component worker of `crates/executor-wasm/src/component/worker.rs`.
|
|
23
|
+
*/
|
|
24
|
+
import type { Declaration } from "../../authoring/declaration.ts";
|
|
25
|
+
import { Code, Reason } from "../../bridge/identifiers.ts";
|
|
26
|
+
import { TembraError } from "../../error.ts";
|
|
27
|
+
import type { Gate } from "../gate.ts";
|
|
28
|
+
import { encode, VALUE_LIMIT, type Encoded } from "../codec.ts";
|
|
29
|
+
import type { LogLevel } from "../host-protocol.ts";
|
|
30
|
+
import { Scope, type Authority, type ScopeRevision } from "../scope.ts";
|
|
31
|
+
import type { ContractPort, ExecutionScope, HookBinding } from "../types.ts";
|
|
32
|
+
import { lift, type Signature } from "../wit-shape.ts";
|
|
33
|
+
import type { ComponentPort } from "./port.ts";
|
|
34
|
+
import type { Described } from "./revision.ts";
|
|
35
|
+
import type { CallbackScope, ComponentScope } from "./scope.ts";
|
|
36
|
+
import { converters, type FunctionValues, type GraphFunction } from "./values.ts";
|
|
37
|
+
|
|
38
|
+
/** Kernel interface identities; `crates/contracts/src/guest.rs` names the same. */
|
|
39
|
+
export const STATE = "tembra:guest/state@1.0.0";
|
|
40
|
+
export const STATE_INIT = "tembra:guest/state-init@1.0.0";
|
|
41
|
+
export const RESOURCES = "tembra:guest/resources@1.0.0";
|
|
42
|
+
export const RESOURCES_INIT = "tembra:guest/resources-init@1.0.0";
|
|
43
|
+
export const SERVICE_RESOURCES_INIT = "tembra:guest/service-resources-init@1.0.0";
|
|
44
|
+
export const CONTEXT = "tembra:guest/context@1.0.0";
|
|
45
|
+
|
|
46
|
+
/** Whether an interface identity belongs to the namespace reserved for the kernel. */
|
|
47
|
+
export function isKernel(contract: string | null | undefined): boolean {
|
|
48
|
+
return contract?.startsWith("tembra:") ?? false;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const LEVELS: readonly string[] = ["log", "debug", "info", "warn", "error"] satisfies LogLevel[];
|
|
52
|
+
|
|
53
|
+
/** The kernel handles of one environment, issued from its own `Scope` table. */
|
|
54
|
+
export interface KernelTable {
|
|
55
|
+
/** Serves the environment's requests exactly as for a source Orb. */
|
|
56
|
+
readonly scope: Scope;
|
|
57
|
+
/** The Orb root's table, which owns the resources and hooks; the root's own for the root. */
|
|
58
|
+
readonly root: KernelTable;
|
|
59
|
+
/** The state handle of a service object that declared state. */
|
|
60
|
+
state?: number;
|
|
61
|
+
/** The resources this environment declared by name: the root's, or a service object's own. */
|
|
62
|
+
readonly resources: Map<string, number>;
|
|
63
|
+
/** The root's hook handles by the import binding that runs each hook. */
|
|
64
|
+
readonly hooks: Map<string, number>;
|
|
65
|
+
/** Where the environment's object lives, read while it was declared. */
|
|
66
|
+
address?: unknown;
|
|
67
|
+
/** Whether the component imports the context interface: an admitted call reads its principal. */
|
|
68
|
+
readonly context: boolean;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** One callback's access to its environment's table, under the callback's own authority. */
|
|
72
|
+
export interface KernelAccess {
|
|
73
|
+
readonly table: KernelTable;
|
|
74
|
+
readonly execution: ExecutionScope;
|
|
75
|
+
/**
|
|
76
|
+
* The principal of an admitted call, read when the call was admitted, for a component importing
|
|
77
|
+
* the context interface: the kernel refuses to read it once the call is cancelled, and it never
|
|
78
|
+
* changes. A resource's opener and closer run as "host".
|
|
79
|
+
*/
|
|
80
|
+
readonly principal?: string | undefined;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* A number this revision issued in a declaration, which the kernel calls back later; the opener
|
|
85
|
+
* and the closer of a service object's resource are told the interface identity of its service.
|
|
86
|
+
*/
|
|
87
|
+
type Numbered =
|
|
88
|
+
| {
|
|
89
|
+
readonly kind: "open" | "close";
|
|
90
|
+
readonly environment: number;
|
|
91
|
+
readonly name: string;
|
|
92
|
+
readonly service?: string;
|
|
93
|
+
}
|
|
94
|
+
| { readonly kind: "validate"; readonly environment: number; readonly service: string };
|
|
95
|
+
|
|
96
|
+
/** One function of a kernel interface the component exports, with its conversions. */
|
|
97
|
+
interface Exported {
|
|
98
|
+
readonly binding: string;
|
|
99
|
+
readonly spelling: string;
|
|
100
|
+
readonly values: FunctionValues;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** What the kernel reads of its component revision. */
|
|
104
|
+
export interface KernelOwner {
|
|
105
|
+
readonly port: ComponentPort;
|
|
106
|
+
readonly contract: ContractPort;
|
|
107
|
+
readonly gate: Gate;
|
|
108
|
+
readonly graph: Described;
|
|
109
|
+
readonly signatures: Map<number, Signature>;
|
|
110
|
+
readonly scopes: ReadonlyMap<number, ComponentScope>;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Where a compiled Orb's diagnostic records go, as for a source Orb's console. */
|
|
114
|
+
export type LogObserver = (record: { orb: string; level: string; message: string }) => void;
|
|
115
|
+
|
|
116
|
+
const isU32 = (value: unknown): value is number =>
|
|
117
|
+
Number.isInteger(value) && (value as number) >= 0 && (value as number) <= 0xffff_ffff;
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Parse JSON text a component wrote, naming what it was in the refusal. A negative zero becomes
|
|
121
|
+
* zero, as `JSON.stringify` writes it and as the native worker stores it: the portable value
|
|
122
|
+
* profile has none, so a retained `-0` could never be validated or encoded again.
|
|
123
|
+
*/
|
|
124
|
+
function json(text: unknown, what: string): unknown {
|
|
125
|
+
if (typeof text !== "string") throw new Error(`${what} is not JSON text`);
|
|
126
|
+
try {
|
|
127
|
+
return JSON.parse(text, (_key, value: unknown) => (Object.is(value, -0) ? 0 : value));
|
|
128
|
+
} catch (error) {
|
|
129
|
+
throw new Error(`${what} is not JSON: ${(error as Error).message}`);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* The value an opener of the resource `name` answered in its `result<u32, string>`: its `err`
|
|
135
|
+
* fails this open with the message, `unavailable/resource` as a native opener's, so the kernel
|
|
136
|
+
* closes what already opened; an answer that is no such result fails this open the same way,
|
|
137
|
+
* naming the resource, and never the component. Mirrors `opened` in the native worker.
|
|
138
|
+
*/
|
|
139
|
+
export function opened(name: string, answer: unknown): unknown {
|
|
140
|
+
const failed = (message: string) =>
|
|
141
|
+
new TembraError({ code: Code.Unavailable, reason: Reason.Resource, message });
|
|
142
|
+
const { tag, val } = (answer ?? {}) as { tag?: unknown; val?: unknown };
|
|
143
|
+
if (tag === "ok") return val;
|
|
144
|
+
if (tag === "err")
|
|
145
|
+
throw failed(
|
|
146
|
+
typeof val === "string"
|
|
147
|
+
? `the resource \`${name}\` did not open: ${val}`
|
|
148
|
+
: `the resource \`${name}\` did not open`,
|
|
149
|
+
);
|
|
150
|
+
throw failed(`the resource \`${name}\` did not open: its opener answered no result`);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** The kernel's interfaces for one prepared component revision. */
|
|
154
|
+
export class ComponentKernel {
|
|
155
|
+
/** The source scopes serving each environment's requests; a service's parent is found here. */
|
|
156
|
+
readonly #scopes = new Map<number, Scope>();
|
|
157
|
+
/** The hooks this Orb declares, by the alias `hook.declare` names. */
|
|
158
|
+
readonly #hooks = new Map<string, HookBinding>();
|
|
159
|
+
/** The import binding that runs each declared hook, to that hook's alias. */
|
|
160
|
+
readonly #runs = new Map<string, string>();
|
|
161
|
+
readonly #numbers = new Map<number, Numbered>();
|
|
162
|
+
#next = 0;
|
|
163
|
+
/** Kernel callbacks the component exports, as `interface#function`. */
|
|
164
|
+
readonly #exports = new Map<string, Exported>();
|
|
165
|
+
readonly #convert: (fn: GraphFunction) => FunctionValues;
|
|
166
|
+
/** The tables `Scope` serves this revision's requests with. */
|
|
167
|
+
readonly #revision: ScopeRevision;
|
|
168
|
+
/** Whether the component imports the context interface: every environment reads its address. */
|
|
169
|
+
readonly #context: boolean;
|
|
170
|
+
#used: boolean;
|
|
171
|
+
|
|
172
|
+
constructor(
|
|
173
|
+
readonly owner: KernelOwner,
|
|
174
|
+
readonly orb: string,
|
|
175
|
+
readonly log: LogObserver | undefined,
|
|
176
|
+
) {
|
|
177
|
+
const graph = owner.graph;
|
|
178
|
+
this.#convert = converters(graph);
|
|
179
|
+
for (const entry of graph.exports) {
|
|
180
|
+
if (![STATE_INIT, RESOURCES_INIT, SERVICE_RESOURCES_INIT].includes(entry.contract ?? ""))
|
|
181
|
+
continue;
|
|
182
|
+
for (const [name, fn] of Object.entries(entry.functions ?? {})) {
|
|
183
|
+
const spelling = graph.javascriptNames[name];
|
|
184
|
+
if (spelling === undefined) throw new Error(`Missing WIT JavaScript name for ${name}`);
|
|
185
|
+
this.#exports.set(`${entry.contract}#${name}`, {
|
|
186
|
+
binding: entry.binding,
|
|
187
|
+
spelling,
|
|
188
|
+
values: this.#convert(fn as GraphFunction),
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
this.#context = graph.imports.some((entry) => entry.contract === CONTEXT);
|
|
193
|
+
this.#used = this.#exports.size > 0 || graph.imports.some((entry) => isKernel(entry.contract));
|
|
194
|
+
const empty = new Map<never, never>();
|
|
195
|
+
this.#revision = {
|
|
196
|
+
contract: owner.contract,
|
|
197
|
+
scopes: this.#scopes,
|
|
198
|
+
// A component's Orb dependencies and reference views are served by its typed imports.
|
|
199
|
+
views: empty,
|
|
200
|
+
imports: empty,
|
|
201
|
+
importMethods: empty,
|
|
202
|
+
importSignatures: empty,
|
|
203
|
+
hooks: this.#hooks,
|
|
204
|
+
signatures: owner.signatures,
|
|
205
|
+
execute: (command, execution, authority) => this.#execute(command, execution, authority),
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Adopt the hooks the checked implementation found this Orb declaring, with their callback
|
|
211
|
+
* signatures, and the import that runs each one (a hook without one is never run by its Orb).
|
|
212
|
+
*/
|
|
213
|
+
declareHooks(hooks: readonly HookBinding[], signature: (hook: HookBinding) => Signature): void {
|
|
214
|
+
for (const hook of hooks) {
|
|
215
|
+
this.#hooks.set(hook.alias, hook);
|
|
216
|
+
this.owner.signatures.set(hook.slot, signature(hook));
|
|
217
|
+
const entry = this.owner.graph.imports.find((item) => item.contract === hook.name);
|
|
218
|
+
if (entry) this.#runs.set(entry.binding, hook.alias);
|
|
219
|
+
}
|
|
220
|
+
if (hooks.length) this.#used = true;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Declare one environment's kernel handles while its factory runs, as the native component
|
|
225
|
+
* worker does: the root reads its address, binds the hooks this Orb runs and declares the
|
|
226
|
+
* resources its component lists; a service object reads its address and declares the resources
|
|
227
|
+
* and the state its component describes for the service interface `contract`.
|
|
228
|
+
*/
|
|
229
|
+
async declare(
|
|
230
|
+
environment: ComponentScope,
|
|
231
|
+
declaration: Declaration,
|
|
232
|
+
service: string | null,
|
|
233
|
+
contract: string | undefined,
|
|
234
|
+
): Promise<void> {
|
|
235
|
+
if (!this.#used) return;
|
|
236
|
+
const scope = new Scope(this.#revision, declaration, service);
|
|
237
|
+
this.#scopes.set(scope.id, scope);
|
|
238
|
+
const root = service === null ? undefined : environment.parent?.kernel;
|
|
239
|
+
if (service !== null && !root) throw new Error("Missing component Orb kernel table");
|
|
240
|
+
const table = { scope, resources: new Map(), hooks: new Map(), context: this.#context } as {
|
|
241
|
+
-readonly [Field in keyof KernelTable]: KernelTable[Field];
|
|
242
|
+
};
|
|
243
|
+
table.root = root ?? table;
|
|
244
|
+
environment.kernel = table;
|
|
245
|
+
const callback = environment.callback({ declaration });
|
|
246
|
+
const { execution } = callback.kernel!;
|
|
247
|
+
try {
|
|
248
|
+
if (this.#context) table.address = execution.sync({ operation: "address" });
|
|
249
|
+
if (service === null) {
|
|
250
|
+
for (const [binding, alias] of this.#runs)
|
|
251
|
+
table.hooks.set(
|
|
252
|
+
binding,
|
|
253
|
+
execution.sync({ operation: "hook.declare", name: alias }) as number,
|
|
254
|
+
);
|
|
255
|
+
const list = this.#exports.get(`${RESOURCES_INIT}#declare`);
|
|
256
|
+
if (!list) return;
|
|
257
|
+
const declared = await this.#call(environment, callback, list, []);
|
|
258
|
+
this.#declareResources(environment, table, execution, declared, undefined);
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
// Only the objects of a service the component exports own kernel state and resources.
|
|
262
|
+
if (contract === undefined) return;
|
|
263
|
+
// The state first, then the resources, as the native worker asks: neither declaration
|
|
264
|
+
// reads a resource of this object, which the object has not declared yet.
|
|
265
|
+
const describe = this.#exports.get(`${STATE_INIT}#declare`);
|
|
266
|
+
const declared = describe
|
|
267
|
+
? ((await this.#call(environment, callback, describe, [contract])) as {
|
|
268
|
+
initial?: unknown;
|
|
269
|
+
version?: unknown;
|
|
270
|
+
validated?: unknown;
|
|
271
|
+
} | null)
|
|
272
|
+
: null;
|
|
273
|
+
if (declared !== null) {
|
|
274
|
+
if (!isU32(declared.version)) throw new Error("The state declaration carries no version");
|
|
275
|
+
if (typeof declared.validated !== "boolean")
|
|
276
|
+
throw new Error("The state declaration does not say whether it is validated");
|
|
277
|
+
table.state = execution.sync({
|
|
278
|
+
operation: "state.declare",
|
|
279
|
+
initial: json(declared.initial, "The initial state"),
|
|
280
|
+
version: declared.version,
|
|
281
|
+
// A validated state gets a number of its own; the kernel runs it, through
|
|
282
|
+
// `validateEnvironment`, on the value a reload would hand to this instance.
|
|
283
|
+
validate: declared.validated
|
|
284
|
+
? this.#number({ kind: "validate", environment: environment.id, service: contract })
|
|
285
|
+
: null,
|
|
286
|
+
}) as number;
|
|
287
|
+
}
|
|
288
|
+
// This object's resources: the component lists them for the service, each declared in the
|
|
289
|
+
// object's own table; the kernel opens them before the object answers a call.
|
|
290
|
+
const resources = this.#exports.get(`${SERVICE_RESOURCES_INIT}#declare`);
|
|
291
|
+
if (!resources) return;
|
|
292
|
+
const listed = await this.#call(environment, callback, resources, [contract]);
|
|
293
|
+
this.#declareResources(environment, table, execution, listed, contract);
|
|
294
|
+
} finally {
|
|
295
|
+
callback.cancel();
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Declare the resources a component listed for the root (`service` is `undefined`) or for one
|
|
301
|
+
* object of the service `service`, in the environment's own table, with numbers of this
|
|
302
|
+
* revision. Each resource follows the resources it depends on, so each dependency's name maps to
|
|
303
|
+
* a handle the kernel already gave: one of this list, in this table, else one of the root's, in
|
|
304
|
+
* the root's table, for an object.
|
|
305
|
+
*/
|
|
306
|
+
#declareResources(
|
|
307
|
+
environment: ComponentScope,
|
|
308
|
+
table: KernelTable,
|
|
309
|
+
execution: ExecutionScope,
|
|
310
|
+
declared: unknown,
|
|
311
|
+
service: string | undefined,
|
|
312
|
+
): void {
|
|
313
|
+
if (!Array.isArray(declared)) throw new Error("The resource declarations are not a list");
|
|
314
|
+
type Listed = { name?: unknown; version?: unknown; dependencies?: unknown };
|
|
315
|
+
for (const resource of declared as Listed[]) {
|
|
316
|
+
const { name, version, dependencies: names } = resource;
|
|
317
|
+
if (typeof name !== "string") throw new Error("A resource declaration carries no name");
|
|
318
|
+
// Resource names are unique in the Orb: `read(name)` answers the object's resource in a
|
|
319
|
+
// call and the root's in a contribution, so a root resource's name is taken.
|
|
320
|
+
if (service !== undefined && table.root !== table && table.root.resources.has(name))
|
|
321
|
+
throw new Error(
|
|
322
|
+
`the resource \`${name}\` of each object of ${service} takes the name of the root's resource \`${name}\`: resource names are unique in the Orb`,
|
|
323
|
+
);
|
|
324
|
+
if (!isU32(version)) throw new Error("A resource declaration carries no version");
|
|
325
|
+
if (!Array.isArray(names)) throw new Error("A resource declaration carries no dependencies");
|
|
326
|
+
const dependencies = names.map((dependency: unknown) => {
|
|
327
|
+
if (typeof dependency !== "string") throw new Error("A resource dependency is not a name");
|
|
328
|
+
const id = table.resources.get(dependency);
|
|
329
|
+
if (id !== undefined) return { id };
|
|
330
|
+
const root = table.root.resources.get(dependency);
|
|
331
|
+
if (table.root !== table && root !== undefined)
|
|
332
|
+
return { scope: table.root.scope.id, id: root };
|
|
333
|
+
throw new Error(
|
|
334
|
+
`the resource \`${name}\` depends on \`${dependency}\`, which the Orb does not declare before it`,
|
|
335
|
+
);
|
|
336
|
+
});
|
|
337
|
+
const numbered = { environment: environment.id, name, ...(service && { service }) };
|
|
338
|
+
const id = execution.sync({
|
|
339
|
+
operation: "resource.declare",
|
|
340
|
+
name,
|
|
341
|
+
version,
|
|
342
|
+
open: this.#number({ kind: "open", ...numbered }),
|
|
343
|
+
close: this.#number({ kind: "close", ...numbered }),
|
|
344
|
+
dependencies,
|
|
345
|
+
});
|
|
346
|
+
if (table.resources.has(name)) throw new Error("A resource is declared twice");
|
|
347
|
+
table.resources.set(name, id as number);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/** Ask the component whether it accepts the retained state a reload would hand to it. */
|
|
352
|
+
async validate(environment: ComponentScope, value: unknown): Promise<void> {
|
|
353
|
+
const table = environment.kernel;
|
|
354
|
+
if (!table) return;
|
|
355
|
+
for (const number of table.scope.validators) {
|
|
356
|
+
const numbered = this.#numbers.get(number);
|
|
357
|
+
if (numbered?.kind !== "validate" || numbered.environment !== environment.id)
|
|
358
|
+
throw new Error("Unknown compiled state validator");
|
|
359
|
+
const validator = this.#exports.get(`${STATE_INIT}#validate`);
|
|
360
|
+
if (!validator) throw new Error("The component declares no kernel state validator");
|
|
361
|
+
// The validator may only read the retained state, as a source Orb's.
|
|
362
|
+
const callback = environment.callback({ validation: true });
|
|
363
|
+
let verdict: { tag?: unknown; val?: unknown };
|
|
364
|
+
try {
|
|
365
|
+
verdict = (await this.#call(environment, callback, validator, [
|
|
366
|
+
numbered.service,
|
|
367
|
+
encode(value).text,
|
|
368
|
+
])) as typeof verdict;
|
|
369
|
+
} finally {
|
|
370
|
+
callback.cancel();
|
|
371
|
+
}
|
|
372
|
+
if (verdict.tag === "err")
|
|
373
|
+
throw new Error(
|
|
374
|
+
typeof verdict.val === "string"
|
|
375
|
+
? `the state was refused: ${verdict.val}`
|
|
376
|
+
: "the state was refused",
|
|
377
|
+
);
|
|
378
|
+
if (verdict.tag !== "ok") throw new Error("the state validator answered no result");
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/** Forget an environment's table and the numbers it issued once the kernel released it. */
|
|
383
|
+
release(environment: number): void {
|
|
384
|
+
this.#scopes.delete(environment);
|
|
385
|
+
for (const [number, numbered] of this.#numbers)
|
|
386
|
+
if (numbered.environment === environment) this.#numbers.delete(number);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* The import object of a kernel interface, or of the import that runs a hook this Orb
|
|
391
|
+
* declares; built before instantiation, it resolves the hook when the guest calls it. Each
|
|
392
|
+
* function serves the callback owning the guest's current root task.
|
|
393
|
+
*/
|
|
394
|
+
imports(binding: string): Record<string, unknown> {
|
|
395
|
+
const entry = this.owner.graph.imports.find((item) => item.binding === binding);
|
|
396
|
+
if (!entry) throw new Error(`Browser component execution cannot provide import ${binding}`);
|
|
397
|
+
const functions = Object.entries(entry.functions ?? {}).map(([name, fn]) => {
|
|
398
|
+
const spelling = this.owner.graph.javascriptNames[name];
|
|
399
|
+
if (spelling === undefined) throw new Error(`Missing WIT JavaScript name for ${name}`);
|
|
400
|
+
return { name, spelling, values: this.#convert(fn as GraphFunction) };
|
|
401
|
+
});
|
|
402
|
+
const serve = (
|
|
403
|
+
answer: (name: string, values: FunctionValues, args: unknown[]) => unknown,
|
|
404
|
+
): Record<string, unknown> =>
|
|
405
|
+
Object.fromEntries(
|
|
406
|
+
functions.map(({ name, spelling, values }) => [
|
|
407
|
+
spelling,
|
|
408
|
+
(...args: unknown[]) => answer(name, values, values.hostArguments(args)),
|
|
409
|
+
]),
|
|
410
|
+
);
|
|
411
|
+
switch (entry.contract) {
|
|
412
|
+
case STATE:
|
|
413
|
+
return serve((name, values, args) => values.toGuest(this.#state(name, args)));
|
|
414
|
+
case RESOURCES:
|
|
415
|
+
return serve((name, _, args) => this.#resource(name, args));
|
|
416
|
+
case CONTEXT:
|
|
417
|
+
return serve((name, values, args) => values.toGuest(this.#contextual(name, args)));
|
|
418
|
+
}
|
|
419
|
+
if (isKernel(entry.contract)) throw new Error(`The kernel serves no ${entry.contract}`);
|
|
420
|
+
return serve((_, values, args) => this.#run(binding, values, args));
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/** The current callback and its kernel access; a call between callbacks has neither. */
|
|
424
|
+
#current() {
|
|
425
|
+
const callback = this.owner.port.current();
|
|
426
|
+
const access = callback.scope.kernel;
|
|
427
|
+
if (!access) throw new Error("The kernel interfaces need a callback of this Orb");
|
|
428
|
+
return { callback, ...access };
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/** `state.read` and `state.update` of the state of the object the call runs on. */
|
|
432
|
+
#state(name: string, args: readonly unknown[]): string {
|
|
433
|
+
const { table, execution } = this.#current();
|
|
434
|
+
if (table.state === undefined)
|
|
435
|
+
throw new Error(
|
|
436
|
+
table.root === table
|
|
437
|
+
? "contributions run in the Orb root, which owns no kernel state"
|
|
438
|
+
: "this service object declares no kernel state",
|
|
439
|
+
);
|
|
440
|
+
let snapshot: unknown;
|
|
441
|
+
if (name === "read" && args.length === 0)
|
|
442
|
+
snapshot = execution.sync({ operation: "state.read", id: table.state });
|
|
443
|
+
else if (name === "update" && args.length === 1) {
|
|
444
|
+
const value = json(args[0], "The state value");
|
|
445
|
+
try {
|
|
446
|
+
snapshot = execution.sync({ operation: "state.update", id: table.state, value });
|
|
447
|
+
} catch (error) {
|
|
448
|
+
// The kernel keeps the previous value when it refuses the update of a cancelled call, and
|
|
449
|
+
// `update` answers with the value the kernel retained, as it promises.
|
|
450
|
+
if (execution.sync({ operation: "signal.cancelled" }) !== true) throw error;
|
|
451
|
+
snapshot = execution.sync({ operation: "state.read", id: table.state });
|
|
452
|
+
}
|
|
453
|
+
} else throw new Error("unknown kernel state function");
|
|
454
|
+
return JSON.stringify((snapshot as { value: unknown }).value);
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/**
|
|
458
|
+
* `resource.read`: the handle the component's opener returned for a resource of the object the
|
|
459
|
+
* call runs on, else of its root.
|
|
460
|
+
*/
|
|
461
|
+
#resource(name: string, args: readonly unknown[]): number {
|
|
462
|
+
const { table, execution } = this.#current();
|
|
463
|
+
const [resource] = args;
|
|
464
|
+
if (name !== "read" || args.length !== 1 || typeof resource !== "string")
|
|
465
|
+
throw new Error("unknown kernel resource function");
|
|
466
|
+
const owner = [table, table.root].find((each) => each.resources.has(resource));
|
|
467
|
+
if (!owner)
|
|
468
|
+
throw new Error(`neither this object nor the Orb root declares a resource named ${resource}`);
|
|
469
|
+
const id = owner.resources.get(resource)!;
|
|
470
|
+
const handle = execution.sync({ operation: "resource.read", scope: owner.scope.id, id });
|
|
471
|
+
if (!isU32(handle)) throw new Error("resource handle is not a u32");
|
|
472
|
+
return handle;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
/** The call's principal and cancellation, the object's address and the Orb's log. */
|
|
476
|
+
#contextual(name: string, args: readonly unknown[]): unknown {
|
|
477
|
+
const { table, execution, principal } = this.#current();
|
|
478
|
+
switch (name) {
|
|
479
|
+
case "object":
|
|
480
|
+
if (table.address === undefined)
|
|
481
|
+
throw new Error("the address of this environment's object was not declared");
|
|
482
|
+
return table.address;
|
|
483
|
+
case "principal":
|
|
484
|
+
return principal ?? execution.sync({ operation: "context.principal" });
|
|
485
|
+
case "cancelled":
|
|
486
|
+
return execution.sync({ operation: "signal.cancelled" });
|
|
487
|
+
case "cancel":
|
|
488
|
+
try {
|
|
489
|
+
execution.sync({ operation: "context.cancel" });
|
|
490
|
+
} catch (error) {
|
|
491
|
+
// Cancelling a call that is already cancelled changes nothing; the kernel refuses it.
|
|
492
|
+
if (execution.sync({ operation: "signal.cancelled" }) !== true) throw error;
|
|
493
|
+
}
|
|
494
|
+
return null;
|
|
495
|
+
case "log": {
|
|
496
|
+
const [level, message] = args;
|
|
497
|
+
if (typeof level !== "string" || !LEVELS.includes(level))
|
|
498
|
+
throw new Error("Invalid console level");
|
|
499
|
+
try {
|
|
500
|
+
this.log?.({ orb: this.orb, level, message: String(message) });
|
|
501
|
+
} catch {
|
|
502
|
+
/* Logging observers do not control the guest. */
|
|
503
|
+
}
|
|
504
|
+
return null;
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
throw new Error("unknown kernel context function");
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
/**
|
|
511
|
+
* `hook.run` of a hook this Orb declares: the kernel runs the plan of contributions over the
|
|
512
|
+
* single input, checked by the hook callback's WIT signature in both directions.
|
|
513
|
+
*/
|
|
514
|
+
#run(binding: string, values: FunctionValues, args: unknown[]): Promise<unknown> {
|
|
515
|
+
const { callback, table, execution } = this.#current();
|
|
516
|
+
const alias = this.#runs.get(binding);
|
|
517
|
+
const id = table.root.hooks.get(binding);
|
|
518
|
+
if (alias === undefined || id === undefined)
|
|
519
|
+
throw new Error(`the Orb root declares no hook for the import ${binding}`);
|
|
520
|
+
if (args.length !== 1) throw new Error("a hook callback takes one input");
|
|
521
|
+
const shape = this.owner.signatures.get(this.#hooks.get(alias)!.slot);
|
|
522
|
+
const [value] = JSON.parse(encode(args, VALUE_LIMIT, shape?.params).text) as [unknown];
|
|
523
|
+
const run = execution.async({
|
|
524
|
+
operation: "hook.run",
|
|
525
|
+
scope: table.root.scope.id,
|
|
526
|
+
id,
|
|
527
|
+
value,
|
|
528
|
+
}) as Promise<Encoded>;
|
|
529
|
+
return this.owner.port
|
|
530
|
+
.physical(callback, run)
|
|
531
|
+
.then((result) => values.toGuest(lift(shape?.result, JSON.parse(result.text))));
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
/** Issue one callback number; the numbers of a revision never repeat. */
|
|
535
|
+
#number(numbered: Numbered): number {
|
|
536
|
+
if (this.#next >= 0xffff_ffff) throw new Error("callback space exhausted");
|
|
537
|
+
this.#numbers.set(++this.#next, numbered);
|
|
538
|
+
return this.#next;
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
/** Run one kernel callback the component exports, admitted as `callback`. */
|
|
542
|
+
#call(
|
|
543
|
+
environment: ComponentScope,
|
|
544
|
+
callback: CallbackScope,
|
|
545
|
+
exported: Exported,
|
|
546
|
+
args: readonly unknown[],
|
|
547
|
+
): Promise<unknown> {
|
|
548
|
+
const port = this.owner.port;
|
|
549
|
+
return port.run(BigInt(environment.id), callback, () =>
|
|
550
|
+
exported.values.fromGuest(() => {
|
|
551
|
+
const fn = port.exports?.[exported.binding]?.[exported.spelling];
|
|
552
|
+
if (typeof fn !== "function")
|
|
553
|
+
throw new Error(`The component does not export ${exported.binding}#${exported.spelling}`);
|
|
554
|
+
return fn(...exported.values.guestArguments(args));
|
|
555
|
+
}),
|
|
556
|
+
);
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
/**
|
|
560
|
+
* Open or close a resource of the root or of a service object by the number this revision
|
|
561
|
+
* declared it with, under the authority the kernel issued: a signal for an opener, an admitted
|
|
562
|
+
* root call for a closer.
|
|
563
|
+
*/
|
|
564
|
+
async #execute(command: string, execution: ExecutionScope, authority: Authority) {
|
|
565
|
+
const { callback: number, value } = JSON.parse(command) as {
|
|
566
|
+
callback: number;
|
|
567
|
+
value: unknown;
|
|
568
|
+
};
|
|
569
|
+
const numbered = this.#numbers.get(number);
|
|
570
|
+
if (numbered?.kind !== "open" && numbered?.kind !== "close")
|
|
571
|
+
throw new Error("Unknown compiled resource callback");
|
|
572
|
+
const environment = this.owner.scopes.get(numbered.environment);
|
|
573
|
+
if (!environment) throw new Error("The resource's Orb scope is gone");
|
|
574
|
+
// A service object's callbacks are told its service first, as `declare` was.
|
|
575
|
+
const { service } = numbered;
|
|
576
|
+
const exported = this.#exports.get(
|
|
577
|
+
`${service === undefined ? RESOURCES_INIT : SERVICE_RESOURCES_INIT}#${numbered.kind}`,
|
|
578
|
+
);
|
|
579
|
+
if (!exported) throw new Error("The component declares no kernel resource callbacks");
|
|
580
|
+
const named = service === undefined ? [numbered.name] : [service, numbered.name];
|
|
581
|
+
const issued =
|
|
582
|
+
"context" in authority
|
|
583
|
+
? environment.exchange(authority.context, execution)
|
|
584
|
+
: environment.callback(authority, execution);
|
|
585
|
+
// The callback runs under the kernel's opening or closing, where no state is read, as the
|
|
586
|
+
// host: its principal is "host", as in the native worker.
|
|
587
|
+
const access = issued.kernel;
|
|
588
|
+
let callback = issued;
|
|
589
|
+
if (access) {
|
|
590
|
+
const { state: _state, ...table } = access.table;
|
|
591
|
+
callback = { ...issued, kernel: { ...access, table, principal: "host" } };
|
|
592
|
+
}
|
|
593
|
+
const release = await this.owner.gate.enter(undefined, callback.signal);
|
|
594
|
+
try {
|
|
595
|
+
if (numbered.kind === "open") {
|
|
596
|
+
// An opener answers `result<u32, string>`: its error fails this open with the message,
|
|
597
|
+
// `unavailable/resource` as a native opener's, so the kernel closes what already opened.
|
|
598
|
+
const answer = await this.#call(environment, callback, exported, named);
|
|
599
|
+
const handle = opened(numbered.name, answer);
|
|
600
|
+
if (!isU32(handle)) throw new Error("resource handle is not a u32");
|
|
601
|
+
return handle;
|
|
602
|
+
}
|
|
603
|
+
if (!isU32(value)) throw new Error("resource closer without a handle");
|
|
604
|
+
await this.#call(environment, callback, exported, [...named, value]);
|
|
605
|
+
return null;
|
|
606
|
+
} finally {
|
|
607
|
+
callback.cancel();
|
|
608
|
+
release();
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The generated bindings' spelling of one WIT identifier: kebab-case words, each entirely lower
|
|
3
|
+
* or upper case, joined in lower (functions, methods) or upper (resource classes) camel case.
|
|
4
|
+
*/
|
|
5
|
+
export function bindingName(name: string, upper: boolean): string {
|
|
6
|
+
return name
|
|
7
|
+
.split("-")
|
|
8
|
+
.map((word, index) => {
|
|
9
|
+
const lower = word.toLowerCase();
|
|
10
|
+
return index === 0 && !upper ? lower : lower.charAt(0).toUpperCase() + lower.slice(1);
|
|
11
|
+
})
|
|
12
|
+
.join("");
|
|
13
|
+
}
|