@gameslate/sdk 0.3.21 → 0.3.23
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 +7 -0
- package/build/build-plugin.d.mts +4 -0
- package/build/build-plugin.mjs +46 -0
- package/lib/assets.d.ts +3 -0
- package/lib/host/asset-sandbox.d.ts +11 -0
- package/lib/host/asset-sandbox.js +42 -5
- package/lib/host/index.d.ts +1 -0
- package/lib/host/index.js +1 -0
- package/lib/host/inspect.d.ts +4 -1
- package/lib/host/inspect.js +53 -13
- package/lib/host/live-sandbox.d.ts +5 -0
- package/lib/host/live-sandbox.js +10 -0
- package/lib/host/quickjs-guest.d.ts +5 -0
- package/lib/host/quickjs-guest.js +53 -2
- package/lib/host/verify.d.ts +14 -14
- package/lib/host/wasm-host.d.ts +51 -0
- package/lib/host/wasm-host.js +270 -0
- package/lib/plugin/index.d.ts +1 -0
- package/lib/plugin/index.js +3 -2
- package/lib/plugin/sandbox-guest.js +21 -5
- package/lib/plugin/wasm.d.ts +17 -0
- package/lib/plugin/wasm.js +17 -0
- package/lib/plugin-manifest.d.ts +50 -0
- package/lib/plugin-manifest.js +19 -0
- package/lib/sandbox-protocol.d.ts +12 -0
- package/package.json +1 -1
- package/template/claude/skills/build-gameslate-plugin/SKILL.md +113 -2
- package/template/github/workflows/ci.yml +2 -2
- package/template/github/workflows/release.yml +2 -2
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
export declare const WASM_PAGE_BYTES: number;
|
|
2
|
+
/** the module the sandbox's capabilities are imported from */
|
|
3
|
+
export declare const WASM_HOST_MODULE = "gameslate";
|
|
4
|
+
/** the largest module a plugin may bundle */
|
|
5
|
+
export declare const WASM_MAX_MODULE_BYTES: number;
|
|
6
|
+
export interface WasmFile {
|
|
7
|
+
file: string;
|
|
8
|
+
bytes: Uint8Array<ArrayBuffer>;
|
|
9
|
+
}
|
|
10
|
+
/** A sandbox's capabilities as functions a module imports from `gameslate`, given a view of
|
|
11
|
+
* the module's memory as it is now (memory.grow replaces the buffer). */
|
|
12
|
+
export type WasmHostFunctions = (memory: () => Uint8Array) => Record<string, WebAssembly.ImportValue>;
|
|
13
|
+
export interface WasmSetup {
|
|
14
|
+
modules: readonly WasmFile[];
|
|
15
|
+
functions: WasmHostFunctions;
|
|
16
|
+
/** the most memory one module's instance may have */
|
|
17
|
+
maxMemoryBytes: number;
|
|
18
|
+
/** the largest range one `read` copies out to the guest */
|
|
19
|
+
maxReadBytes: number;
|
|
20
|
+
}
|
|
21
|
+
/** The function names a sandbox's modules may import from `gameslate`. */
|
|
22
|
+
export declare const wasmImportNames: (functions: WasmHostFunctions) => ReadonlySet<string>;
|
|
23
|
+
/**
|
|
24
|
+
* Why `bytes` can't run in a sandbox whose capabilities are `allowed` (function names imported
|
|
25
|
+
* from `gameslate`) and whose modules get at most `maxMemoryBytes`, or null.
|
|
26
|
+
*/
|
|
27
|
+
export declare function wasmModuleProblem(bytes: Uint8Array<ArrayBuffer>, allowed: ReadonlySet<string>, maxMemoryBytes: number): string | null;
|
|
28
|
+
/** One sandbox's modules, instantiated when first used and called within a time budget. */
|
|
29
|
+
export declare class WasmInstances {
|
|
30
|
+
private readonly setup;
|
|
31
|
+
/** called once when a module overran its budget and was stopped */
|
|
32
|
+
private readonly onStopped;
|
|
33
|
+
private readonly instances;
|
|
34
|
+
private readonly global;
|
|
35
|
+
private readonly context;
|
|
36
|
+
private stopped;
|
|
37
|
+
private readonly allowed;
|
|
38
|
+
constructor(setup: WasmSetup,
|
|
39
|
+
/** called once when a module overran its budget and was stopped */
|
|
40
|
+
onStopped: (reason: string) => void);
|
|
41
|
+
/** Calls export `fn` of `file` with numbers; its number result (undefined for none). */
|
|
42
|
+
call(file: string, fn: string, args: readonly number[], ms: number): number | undefined;
|
|
43
|
+
/** A copy of `length` bytes of `file`'s memory from `ptr`. */
|
|
44
|
+
read(file: string, ptr: number, length: number, ms: number): Uint8Array;
|
|
45
|
+
write(file: string, ptr: number, bytes: Uint8Array, ms: number): void;
|
|
46
|
+
memoryBytes(file: string, ms: number): number;
|
|
47
|
+
private memoryOf;
|
|
48
|
+
private range;
|
|
49
|
+
private instance;
|
|
50
|
+
private timed;
|
|
51
|
+
}
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import vm from 'node:vm';
|
|
2
|
+
/*
|
|
3
|
+
* A plugin's own WebAssembly modules ("Closing the sandbox" item 3 in
|
|
4
|
+
* docs/plugin-architecture.md), for work too slow in the QuickJS interpreter. The host's V8
|
|
5
|
+
* compiles and runs them, and the plugin's QuickJS code calls their exports with numbers and
|
|
6
|
+
* copies byte ranges in and out of their memory (quickjs-guest.ts bridges the two).
|
|
7
|
+
*
|
|
8
|
+
* What keeps them as boxed in as the QuickJS code:
|
|
9
|
+
* - Imports: a module may import only `env.memory` and the sandbox's capabilities as functions
|
|
10
|
+
* from `gameslate` (the allow-list). No WASI, no other function, table or global: checked with
|
|
11
|
+
* `WebAssembly.Module.imports` when the plugin loads and again when the module is instantiated.
|
|
12
|
+
* - Memory: the core owns it. A module that defines its own memory is refused (its maximum
|
|
13
|
+
* couldn't be enforced); the one it imports is created with the sandbox's maximum, so a
|
|
14
|
+
* `memory.grow` past it returns -1.
|
|
15
|
+
* - Time: every call, and the instantiation (a start function), runs under the guest call's
|
|
16
|
+
* deadline in a `vm` script with a timeout, whose watchdog stops V8 mid-loop. A module that was
|
|
17
|
+
* stopped is never called again and its guest is switched off.
|
|
18
|
+
* - Values: only numbers cross; an export that returns anything else (a function reference) is
|
|
19
|
+
* refused, so no host object reaches the guest.
|
|
20
|
+
*/
|
|
21
|
+
export const WASM_PAGE_BYTES = 64 * 1024;
|
|
22
|
+
/** the one memory a module may import: what wasm-ld (Rust, C) and AssemblyScript name it with
|
|
23
|
+
* --import-memory */
|
|
24
|
+
const MEMORY_MODULE = 'env';
|
|
25
|
+
const MEMORY_NAME = 'memory';
|
|
26
|
+
/** the module the sandbox's capabilities are imported from */
|
|
27
|
+
export const WASM_HOST_MODULE = 'gameslate';
|
|
28
|
+
/** the largest module a plugin may bundle */
|
|
29
|
+
export const WASM_MAX_MODULE_BYTES = 8 * 1024 * 1024;
|
|
30
|
+
/** The function names a sandbox's modules may import from `gameslate`. */
|
|
31
|
+
export const wasmImportNames = (functions) => new Set(Object.keys(functions(() => new Uint8Array(0))));
|
|
32
|
+
/* A cursor over a module's bytes: LEB128 numbers and names. */
|
|
33
|
+
class ModuleReader {
|
|
34
|
+
bytes;
|
|
35
|
+
pos = 8; /* past the magic number and version */
|
|
36
|
+
constructor(bytes) {
|
|
37
|
+
this.bytes = bytes;
|
|
38
|
+
}
|
|
39
|
+
byte() {
|
|
40
|
+
const byte = this.bytes[this.pos++];
|
|
41
|
+
if (byte === undefined)
|
|
42
|
+
throw new Error('a read past the end of the module');
|
|
43
|
+
return byte;
|
|
44
|
+
}
|
|
45
|
+
u32() {
|
|
46
|
+
let result = 0;
|
|
47
|
+
for (let shift = 0;; shift += 7) {
|
|
48
|
+
const byte = this.byte();
|
|
49
|
+
result += (byte & 0x7f) * 2 ** shift;
|
|
50
|
+
if (byte < 0x80)
|
|
51
|
+
return result;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
skipName() {
|
|
55
|
+
const length = this.u32();
|
|
56
|
+
this.pos += length;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/* The import section's memory, if any. Only called on imports that passed the allow-list, so
|
|
60
|
+
* they are functions and memories only. */
|
|
61
|
+
function importedMemory(r) {
|
|
62
|
+
for (let n = r.u32(); n > 0; n--) {
|
|
63
|
+
r.skipName();
|
|
64
|
+
r.skipName();
|
|
65
|
+
const kind = r.byte();
|
|
66
|
+
if (kind === 0)
|
|
67
|
+
r.u32();
|
|
68
|
+
else if (kind !== 2)
|
|
69
|
+
throw new Error(`an import of kind ${kind} got past the allow-list`);
|
|
70
|
+
else {
|
|
71
|
+
const flags = r.byte();
|
|
72
|
+
/* 0: min only; 1: min and max; anything else is shared or 64-bit */
|
|
73
|
+
if (flags !== 0 && flags !== 1)
|
|
74
|
+
return 'unsupported';
|
|
75
|
+
const minPages = r.u32();
|
|
76
|
+
/* one memory at most: the allow-list has one name and refuses it twice */
|
|
77
|
+
return { minPages, maxPages: flags === 1 ? r.u32() : null };
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
/* What the binary says about memory, which `WebAssembly.Module` doesn't reflect: whether it
|
|
83
|
+
* defines one, and the limits of the one it imports. */
|
|
84
|
+
function memoryLayout(bytes) {
|
|
85
|
+
const r = new ModuleReader(bytes);
|
|
86
|
+
let defines = false;
|
|
87
|
+
let imported = null;
|
|
88
|
+
while (r.pos < bytes.length) {
|
|
89
|
+
const id = r.byte();
|
|
90
|
+
const size = r.u32();
|
|
91
|
+
const end = r.pos + size;
|
|
92
|
+
if (id === 5)
|
|
93
|
+
defines = r.u32() > 0;
|
|
94
|
+
if (id === 2)
|
|
95
|
+
imported = importedMemory(r);
|
|
96
|
+
r.pos = end;
|
|
97
|
+
}
|
|
98
|
+
return { defines, imported };
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Why `bytes` can't run in a sandbox whose capabilities are `allowed` (function names imported
|
|
102
|
+
* from `gameslate`) and whose modules get at most `maxMemoryBytes`, or null.
|
|
103
|
+
*/
|
|
104
|
+
export function wasmModuleProblem(bytes, allowed, maxMemoryBytes) {
|
|
105
|
+
const checked = checkedModule(bytes, allowed, maxMemoryBytes);
|
|
106
|
+
return typeof checked === 'string' ? checked : null;
|
|
107
|
+
}
|
|
108
|
+
/* The compiled module, or why it can't run (see wasmModuleProblem). */
|
|
109
|
+
function checkedModule(bytes, allowed, maxMemoryBytes) {
|
|
110
|
+
if (bytes.byteLength > WASM_MAX_MODULE_BYTES)
|
|
111
|
+
return `it is over ${WASM_MAX_MODULE_BYTES} bytes`;
|
|
112
|
+
if (!WebAssembly.validate(bytes))
|
|
113
|
+
return 'it is not a valid WebAssembly module';
|
|
114
|
+
const compiled = new WebAssembly.Module(bytes);
|
|
115
|
+
const seen = new Set();
|
|
116
|
+
for (const imp of WebAssembly.Module.imports(compiled)) {
|
|
117
|
+
const name = `${imp.module}.${imp.name}`;
|
|
118
|
+
const isMemory = imp.module === MEMORY_MODULE && imp.name === MEMORY_NAME && imp.kind === 'memory';
|
|
119
|
+
const isCapability = imp.module === WASM_HOST_MODULE && allowed.has(imp.name) && imp.kind === 'function';
|
|
120
|
+
if (!isMemory && !isCapability) {
|
|
121
|
+
const list = [`${MEMORY_MODULE}.${MEMORY_NAME}`, ...allowed].join(', ');
|
|
122
|
+
return `it imports ${name} (${imp.kind}); a module may import only ${list}`;
|
|
123
|
+
}
|
|
124
|
+
if (seen.has(name))
|
|
125
|
+
return `it imports ${name} twice`;
|
|
126
|
+
seen.add(name);
|
|
127
|
+
}
|
|
128
|
+
const layout = memoryLayout(bytes);
|
|
129
|
+
if (layout.defines)
|
|
130
|
+
return 'it defines its own memory: import env.memory instead (link with --import-memory)';
|
|
131
|
+
if (layout.imported === 'unsupported')
|
|
132
|
+
return 'its memory is shared or 64-bit';
|
|
133
|
+
if (layout.imported && layout.imported.minPages * WASM_PAGE_BYTES > maxMemoryBytes)
|
|
134
|
+
return `it starts with more memory than a module may have (${maxMemoryBytes} bytes)`;
|
|
135
|
+
return compiled;
|
|
136
|
+
}
|
|
137
|
+
const TIMED_CALL = new vm.Script('__gsRun()');
|
|
138
|
+
const TIMEOUT_CODE = 'ERR_SCRIPT_EXECUTION_TIMEOUT';
|
|
139
|
+
/** One sandbox's modules, instantiated when first used and called within a time budget. */
|
|
140
|
+
export class WasmInstances {
|
|
141
|
+
setup;
|
|
142
|
+
onStopped;
|
|
143
|
+
instances = new Map();
|
|
144
|
+
/* the timed script's global object: createContext makes this object the context */
|
|
145
|
+
global = {};
|
|
146
|
+
context = vm.createContext(this.global);
|
|
147
|
+
stopped = null;
|
|
148
|
+
allowed;
|
|
149
|
+
constructor(setup,
|
|
150
|
+
/** called once when a module overran its budget and was stopped */
|
|
151
|
+
onStopped) {
|
|
152
|
+
this.setup = setup;
|
|
153
|
+
this.onStopped = onStopped;
|
|
154
|
+
this.allowed = wasmImportNames(setup.functions);
|
|
155
|
+
}
|
|
156
|
+
/** Calls export `fn` of `file` with numbers; its number result (undefined for none). */
|
|
157
|
+
call(file, fn, args, ms) {
|
|
158
|
+
return this.timed(ms, `${file} ${fn}`, () => {
|
|
159
|
+
const { exports } = this.instance(file);
|
|
160
|
+
const exported = exports[fn];
|
|
161
|
+
/* an exports object has no prototype, so only its own names are found */
|
|
162
|
+
if (typeof exported !== 'function')
|
|
163
|
+
throw new Error(`${file} exports no function ${fn}`);
|
|
164
|
+
return numberResult(file, fn, Reflect.apply(exported, undefined, args));
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
/** A copy of `length` bytes of `file`'s memory from `ptr`. */
|
|
168
|
+
read(file, ptr, length, ms) {
|
|
169
|
+
/* the copy lands in the interpreter's heap outside its memory limit, like an input read */
|
|
170
|
+
if (length > this.setup.maxReadBytes)
|
|
171
|
+
throw new Error(`a WebAssembly read of ${length} bytes is over the limit`);
|
|
172
|
+
return this.timed(ms, file, () => this.range(file, ptr, length).slice());
|
|
173
|
+
}
|
|
174
|
+
write(file, ptr, bytes, ms) {
|
|
175
|
+
this.timed(ms, file, () => this.range(file, ptr, bytes.length).set(bytes));
|
|
176
|
+
}
|
|
177
|
+
memoryBytes(file, ms) {
|
|
178
|
+
return this.timed(ms, file, () => this.memoryOf(file).buffer.byteLength);
|
|
179
|
+
}
|
|
180
|
+
memoryOf(file) {
|
|
181
|
+
const { memory } = this.instance(file);
|
|
182
|
+
if (!memory)
|
|
183
|
+
throw new Error(`${file} has no memory (it imports no env.memory)`);
|
|
184
|
+
return memory;
|
|
185
|
+
}
|
|
186
|
+
range(file, ptr, length) {
|
|
187
|
+
const memory = new Uint8Array(this.memoryOf(file).buffer);
|
|
188
|
+
if (!Number.isInteger(ptr) || !Number.isInteger(length) || ptr < 0 || length < 0)
|
|
189
|
+
throw new Error('a memory range needs a whole-number pointer and length');
|
|
190
|
+
if (ptr + length > memory.length)
|
|
191
|
+
throw new Error(`a range past the end of ${file}'s memory`);
|
|
192
|
+
return memory.subarray(ptr, ptr + length);
|
|
193
|
+
}
|
|
194
|
+
instance(file) {
|
|
195
|
+
const existing = this.instances.get(file);
|
|
196
|
+
if (existing)
|
|
197
|
+
return existing;
|
|
198
|
+
const found = this.setup.modules.find(m => m.file === file);
|
|
199
|
+
if (!found)
|
|
200
|
+
throw new Error(`the plugin bundles no WebAssembly module ${file}`);
|
|
201
|
+
const { maxMemoryBytes } = this.setup;
|
|
202
|
+
const compiled = checkedModule(found.bytes, this.allowed, maxMemoryBytes);
|
|
203
|
+
if (typeof compiled === 'string')
|
|
204
|
+
throw new Error(`${file}: ${compiled}`);
|
|
205
|
+
const limits = memoryLayout(found.bytes).imported;
|
|
206
|
+
if (limits === 'unsupported')
|
|
207
|
+
throw new Error(`${file}: its memory is shared or 64-bit`);
|
|
208
|
+
const capPages = Math.floor(maxMemoryBytes / WASM_PAGE_BYTES);
|
|
209
|
+
const memory = limits
|
|
210
|
+
? new WebAssembly.Memory({
|
|
211
|
+
initial: limits.minPages,
|
|
212
|
+
maximum: Math.min(capPages, limits.maxPages ?? capPages),
|
|
213
|
+
})
|
|
214
|
+
: null;
|
|
215
|
+
const view = () => {
|
|
216
|
+
if (!memory)
|
|
217
|
+
throw new Error(`${file} has no memory (it imports no env.memory)`);
|
|
218
|
+
return new Uint8Array(memory.buffer);
|
|
219
|
+
};
|
|
220
|
+
const imports = {
|
|
221
|
+
[MEMORY_MODULE]: memory ? { [MEMORY_NAME]: memory } : {},
|
|
222
|
+
[WASM_HOST_MODULE]: this.setup.functions(view),
|
|
223
|
+
};
|
|
224
|
+
const instance = new WebAssembly.Instance(compiled, imports);
|
|
225
|
+
const made = { exports: instance.exports, memory };
|
|
226
|
+
this.instances.set(file, made);
|
|
227
|
+
return made;
|
|
228
|
+
}
|
|
229
|
+
/* `run` with a watchdog: past `ms` V8 is stopped wherever it is, even inside a loop with no
|
|
230
|
+
* calls in it, and the error comes back here */
|
|
231
|
+
timed(ms, what, run) {
|
|
232
|
+
if (this.stopped)
|
|
233
|
+
throw new Error(`WebAssembly is switched off: ${this.stopped}`);
|
|
234
|
+
const timeout = Math.ceil(ms);
|
|
235
|
+
if (timeout < 1)
|
|
236
|
+
throw new Error(`no time left in the budget for ${what}`);
|
|
237
|
+
const results = [];
|
|
238
|
+
this.global.__gsRun = () => {
|
|
239
|
+
results.push({ value: run() });
|
|
240
|
+
};
|
|
241
|
+
try {
|
|
242
|
+
TIMED_CALL.runInContext(this.context, { timeout });
|
|
243
|
+
}
|
|
244
|
+
catch (e) {
|
|
245
|
+
/* Node marks its watchdog's error by this code (checked by shape: the error can come
|
|
246
|
+
* from another realm) */
|
|
247
|
+
if (typeof e === 'object' && e !== null && 'code' in e && e.code === TIMEOUT_CODE) {
|
|
248
|
+
this.stopped = `${what} ran past its ${timeout} ms budget and was stopped`;
|
|
249
|
+
this.instances.clear();
|
|
250
|
+
this.onStopped(this.stopped);
|
|
251
|
+
throw new Error(this.stopped);
|
|
252
|
+
}
|
|
253
|
+
throw e;
|
|
254
|
+
}
|
|
255
|
+
finally {
|
|
256
|
+
this.global.__gsRun = undefined;
|
|
257
|
+
}
|
|
258
|
+
const [result] = results;
|
|
259
|
+
if (!result)
|
|
260
|
+
throw new Error(`${what} returned nothing`);
|
|
261
|
+
return result.value;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
function numberResult(file, fn, value) {
|
|
265
|
+
if (typeof value === 'number' || value === undefined)
|
|
266
|
+
return value;
|
|
267
|
+
if (typeof value === 'bigint' && Number.isSafeInteger(Number(value)))
|
|
268
|
+
return Number(value);
|
|
269
|
+
throw new Error(`${file} ${fn} returned something other than a number`);
|
|
270
|
+
}
|
package/lib/plugin/index.d.ts
CHANGED
package/lib/plugin/index.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* `@gameslate/sdk/plugin`: what a plugin's code builds against. The contract (GamePlugin,
|
|
3
3
|
* GameMemory, AssetExtractor, PluginModule), pure helpers, and the sandbox's guest side
|
|
4
|
-
* (`exposeToHost`, `exposeAssetsToHost`). No Node APIs: plugin bundles run in the
|
|
5
|
-
* sandbox.
|
|
4
|
+
* (`exposeToHost`, `exposeAssetsToHost`, `wasmModule`). No Node APIs: plugin bundles run in the
|
|
5
|
+
* core's QuickJS sandbox.
|
|
6
6
|
*/
|
|
7
7
|
export * from './contract.js';
|
|
8
8
|
export * from './seqlock.js';
|
|
9
9
|
export * from './sandbox-guest.js';
|
|
10
|
+
export * from './wasm.js';
|
|
@@ -94,13 +94,29 @@ function assetsHost() {
|
|
|
94
94
|
throw new Error('the core gave no input capability');
|
|
95
95
|
return host;
|
|
96
96
|
}
|
|
97
|
+
/*
|
|
98
|
+
* The same input arrives as the same object on every call, so an extractor can keep what it read
|
|
99
|
+
* from a file between assets by comparing files (`cached.file === file`). Without this each call
|
|
100
|
+
* made new objects and every such cache missed: a 104-place build re-read a 16 MB ROM 104 times.
|
|
101
|
+
* Keyed on all three fields: a handle belongs to one job's file, and a sandbox lives for one job.
|
|
102
|
+
*/
|
|
103
|
+
const inputs = new Map();
|
|
97
104
|
function inputFiles(host, json) {
|
|
98
105
|
const files = SandboxInputFilesSchema.parse(JSON.parse(json));
|
|
99
|
-
return files.map(f =>
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
106
|
+
return files.map(f => {
|
|
107
|
+
const key = JSON.stringify([f.handle, f.name, f.size]);
|
|
108
|
+
const known = inputs.get(key);
|
|
109
|
+
if (known)
|
|
110
|
+
return known;
|
|
111
|
+
const file = {
|
|
112
|
+
name: f.name,
|
|
113
|
+
size: f.size,
|
|
114
|
+
handle: f.handle,
|
|
115
|
+
read: (offset, length) => new Uint8Array(host.inputRead(f.handle, offset, length)),
|
|
116
|
+
};
|
|
117
|
+
inputs.set(key, file);
|
|
118
|
+
return file;
|
|
119
|
+
});
|
|
104
120
|
}
|
|
105
121
|
/* an exact-size copy, so only the output's own bytes cross */
|
|
106
122
|
const bufferOf = (data) => data.slice().buffer;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { SandboxWasmHostApi } from '../index.js';
|
|
2
|
+
declare global {
|
|
3
|
+
var __gsWasm: SandboxWasmHostApi | undefined;
|
|
4
|
+
}
|
|
5
|
+
export interface WasmModule {
|
|
6
|
+
/** calls an export with numbers (i32, f32, f64; i64 results come back as numbers) */
|
|
7
|
+
call(fn: string, ...args: number[]): number | undefined;
|
|
8
|
+
/** a copy of `length` bytes of the module's memory from `ptr` */
|
|
9
|
+
read(ptr: number, length: number): Uint8Array;
|
|
10
|
+
/** copies `bytes` into the module's memory at `ptr` */
|
|
11
|
+
write(ptr: number, bytes: Uint8Array): void;
|
|
12
|
+
/** the module's memory size now, in bytes */
|
|
13
|
+
memoryBytes(): number;
|
|
14
|
+
}
|
|
15
|
+
/** The module the manifest lists as `file` (`palette.wasm`), as `gameslate-plugin build` copied
|
|
16
|
+
* it from the plugin's `wasm/` folder. */
|
|
17
|
+
export declare function wasmModule(file: string): WasmModule;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** The module the manifest lists as `file` (`palette.wasm`), as `gameslate-plugin build` copied
|
|
2
|
+
* it from the plugin's `wasm/` folder. */
|
|
3
|
+
export function wasmModule(file) {
|
|
4
|
+
const host = () => {
|
|
5
|
+
const bridge = globalThis.__gsWasm;
|
|
6
|
+
if (!bridge)
|
|
7
|
+
throw new Error('the core gave no WebAssembly capability');
|
|
8
|
+
return bridge;
|
|
9
|
+
};
|
|
10
|
+
return {
|
|
11
|
+
call: (fn, ...args) => host().call(file, fn, ...args),
|
|
12
|
+
read: (ptr, length) => new Uint8Array(host().read(file, ptr, length)),
|
|
13
|
+
/* an exact-size copy, so only these bytes cross */
|
|
14
|
+
write: (ptr, bytes) => host().write(file, ptr, bytes.slice().buffer),
|
|
15
|
+
memoryBytes: () => host().memoryBytes(file),
|
|
16
|
+
};
|
|
17
|
+
}
|
package/lib/plugin-manifest.d.ts
CHANGED
|
@@ -53,6 +53,23 @@ export declare const GameBuildSchema: z.ZodObject<{
|
|
|
53
53
|
name: string;
|
|
54
54
|
}>;
|
|
55
55
|
export type GameBuild = z.infer<typeof GameBuildSchema>;
|
|
56
|
+
/** How many WebAssembly modules one plugin may bundle (each gets its own capped memory). */
|
|
57
|
+
export declare const MAX_WASM_MODULES = 4;
|
|
58
|
+
/**
|
|
59
|
+
* A WebAssembly module the plugin bundles ("Closing the sandbox" item 3): its code calls the
|
|
60
|
+
* module's exports through `wasmModule(file)` and the core runs it with no imports but the
|
|
61
|
+
* sandbox's allow-list (src/host/wasm-host.ts).
|
|
62
|
+
*/
|
|
63
|
+
export declare const WasmFileSchema: z.ZodObject<{
|
|
64
|
+
file: z.ZodString;
|
|
65
|
+
sha256: z.ZodString;
|
|
66
|
+
}, "strict", z.ZodTypeAny, {
|
|
67
|
+
file: string;
|
|
68
|
+
sha256: string;
|
|
69
|
+
}, {
|
|
70
|
+
file: string;
|
|
71
|
+
sha256: string;
|
|
72
|
+
}>;
|
|
56
73
|
/** major.minor.patch, the only form `compareVersions` orders. */
|
|
57
74
|
export declare const PluginVersionSchema: z.ZodString;
|
|
58
75
|
/** The release zip a published manifest names: its URL (absolute, or relative to the URL the
|
|
@@ -97,6 +114,23 @@ export declare const PluginManifestSchema: z.ZodEffects<z.ZodObject<{
|
|
|
97
114
|
file: string;
|
|
98
115
|
sha256: string;
|
|
99
116
|
}>>;
|
|
117
|
+
/** WebAssembly modules the live and asset code may call, pinned like the code files */
|
|
118
|
+
wasm: z.ZodOptional<z.ZodEffects<z.ZodArray<z.ZodObject<{
|
|
119
|
+
file: z.ZodString;
|
|
120
|
+
sha256: z.ZodString;
|
|
121
|
+
}, "strict", z.ZodTypeAny, {
|
|
122
|
+
file: string;
|
|
123
|
+
sha256: string;
|
|
124
|
+
}, {
|
|
125
|
+
file: string;
|
|
126
|
+
sha256: string;
|
|
127
|
+
}>, "many">, {
|
|
128
|
+
file: string;
|
|
129
|
+
sha256: string;
|
|
130
|
+
}[], {
|
|
131
|
+
file: string;
|
|
132
|
+
sha256: string;
|
|
133
|
+
}[]>>;
|
|
100
134
|
games: z.ZodArray<z.ZodObject<{
|
|
101
135
|
id: z.ZodString;
|
|
102
136
|
title: z.ZodString;
|
|
@@ -241,6 +275,10 @@ export declare const PluginManifestSchema: z.ZodEffects<z.ZodObject<{
|
|
|
241
275
|
file: string;
|
|
242
276
|
sha256: string;
|
|
243
277
|
} | undefined;
|
|
278
|
+
wasm?: {
|
|
279
|
+
file: string;
|
|
280
|
+
sha256: string;
|
|
281
|
+
}[] | undefined;
|
|
244
282
|
bundle?: {
|
|
245
283
|
url: string;
|
|
246
284
|
sha256: string;
|
|
@@ -278,6 +316,10 @@ export declare const PluginManifestSchema: z.ZodEffects<z.ZodObject<{
|
|
|
278
316
|
sha256: string;
|
|
279
317
|
} | undefined;
|
|
280
318
|
testedWith?: string[] | undefined;
|
|
319
|
+
wasm?: {
|
|
320
|
+
file: string;
|
|
321
|
+
sha256: string;
|
|
322
|
+
}[] | undefined;
|
|
281
323
|
bundle?: {
|
|
282
324
|
url: string;
|
|
283
325
|
sha256: string;
|
|
@@ -315,6 +357,10 @@ export declare const PluginManifestSchema: z.ZodEffects<z.ZodObject<{
|
|
|
315
357
|
file: string;
|
|
316
358
|
sha256: string;
|
|
317
359
|
} | undefined;
|
|
360
|
+
wasm?: {
|
|
361
|
+
file: string;
|
|
362
|
+
sha256: string;
|
|
363
|
+
}[] | undefined;
|
|
318
364
|
bundle?: {
|
|
319
365
|
url: string;
|
|
320
366
|
sha256: string;
|
|
@@ -352,6 +398,10 @@ export declare const PluginManifestSchema: z.ZodEffects<z.ZodObject<{
|
|
|
352
398
|
sha256: string;
|
|
353
399
|
} | undefined;
|
|
354
400
|
testedWith?: string[] | undefined;
|
|
401
|
+
wasm?: {
|
|
402
|
+
file: string;
|
|
403
|
+
sha256: string;
|
|
404
|
+
}[] | undefined;
|
|
355
405
|
bundle?: {
|
|
356
406
|
url: string;
|
|
357
407
|
sha256: string;
|
package/lib/plugin-manifest.js
CHANGED
|
@@ -123,6 +123,19 @@ const CodeFile = z.object({
|
|
|
123
123
|
file: FileName,
|
|
124
124
|
sha256: Sha256,
|
|
125
125
|
});
|
|
126
|
+
/** How many WebAssembly modules one plugin may bundle (each gets its own capped memory). */
|
|
127
|
+
export const MAX_WASM_MODULES = 4;
|
|
128
|
+
/**
|
|
129
|
+
* A WebAssembly module the plugin bundles ("Closing the sandbox" item 3): its code calls the
|
|
130
|
+
* module's exports through `wasmModule(file)` and the core runs it with no imports but the
|
|
131
|
+
* sandbox's allow-list (src/host/wasm-host.ts).
|
|
132
|
+
*/
|
|
133
|
+
export const WasmFileSchema = z
|
|
134
|
+
.object({
|
|
135
|
+
file: FileName.regex(/\.wasm$/, 'a WebAssembly module is a .wasm file'),
|
|
136
|
+
sha256: Sha256,
|
|
137
|
+
})
|
|
138
|
+
.strict();
|
|
126
139
|
/** major.minor.patch, the only form `compareVersions` orders. */
|
|
127
140
|
export const PluginVersionSchema = z
|
|
128
141
|
.string()
|
|
@@ -145,6 +158,12 @@ export const PluginManifestSchema = z
|
|
|
145
158
|
code: CodeFile,
|
|
146
159
|
/** asset extractors, run in the core's sandbox by extraction jobs */
|
|
147
160
|
assets: CodeFile.optional(),
|
|
161
|
+
/** WebAssembly modules the live and asset code may call, pinned like the code files */
|
|
162
|
+
wasm: z
|
|
163
|
+
.array(WasmFileSchema)
|
|
164
|
+
.max(MAX_WASM_MODULES)
|
|
165
|
+
.refine(ws => new Set(ws.map(w => w.file)).size === ws.length, 'each WebAssembly file is listed once')
|
|
166
|
+
.optional(),
|
|
148
167
|
games: z
|
|
149
168
|
.array(z
|
|
150
169
|
.object({
|
|
@@ -1523,3 +1523,15 @@ export interface SandboxAssetsHostApi {
|
|
|
1523
1523
|
/** hands the core a block of output bytes; the index outputs refer to it by */
|
|
1524
1524
|
emitBinary(data: ArrayBuffer): number;
|
|
1525
1525
|
}
|
|
1526
|
+
/** The core's bridge to the plugin's own WebAssembly modules, on `globalThis.__gsWasm` (in both
|
|
1527
|
+
* sandboxes, when the manifest lists modules). Modules are named by their file name. */
|
|
1528
|
+
export interface SandboxWasmHostApi {
|
|
1529
|
+
/** calls an export with numbers; its number result, or undefined for none */
|
|
1530
|
+
call(file: string, fn: string, ...args: number[]): number | undefined;
|
|
1531
|
+
/** a copy of `length` bytes of the module's memory from `ptr` */
|
|
1532
|
+
read(file: string, ptr: number, length: number): ArrayBuffer;
|
|
1533
|
+
/** copies `data` into the module's memory at `ptr` */
|
|
1534
|
+
write(file: string, ptr: number, data: ArrayBuffer): void;
|
|
1535
|
+
/** the module's memory size now, in bytes */
|
|
1536
|
+
memoryBytes(file: string): number;
|
|
1537
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gameslate/sdk",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.23",
|
|
4
4
|
"description": "Build plugins for GameSlate, the second-screen companion for PC games: the plugin contract, game-state and catalogue schemas, and the plugin builder.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|