@swf2es/codegen 0.1.37 → 0.1.39
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/LICENSE.third-party +38 -7
- package/README.md +5 -4
- package/dist/codegen.wasm +0 -0
- package/dist/index.d.ts +49 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +850 -126
- package/dist/index.js.map +1 -1
- package/dist/zig.d.ts +141 -0
- package/dist/zig.d.ts.map +1 -0
- package/dist/zig.js +384 -0
- package/dist/zig.js.map +1 -0
- package/package.json +5 -9
- package/src/index.ts +1053 -138
- package/src/webassembly.d.ts +2 -1
- package/src/zig.ts +596 -0
- package/dist/codegen.d.ts +0 -342
- package/dist/codegen.js +0 -336
package/src/webassembly.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// The ES2022 lib has no WebAssembly types, this package deliberately loads
|
|
2
2
|
// neither DOM nor node types, and @types/node does not declare them either.
|
|
3
|
-
// These are the parts the wrapper
|
|
3
|
+
// These are the parts the wrapper and the tests use;
|
|
4
4
|
// consumers get the real ones from their own environment.
|
|
5
5
|
declare namespace WebAssembly {
|
|
6
6
|
// biome-ignore lint/suspicious/noEmptyInterface: an opaque handle, like the real Module type
|
|
@@ -26,5 +26,6 @@ declare namespace WebAssembly {
|
|
|
26
26
|
interface Instance {
|
|
27
27
|
readonly exports: Record<string, unknown>;
|
|
28
28
|
}
|
|
29
|
+
var Instance: { new (module: Module, imports?: object): Instance };
|
|
29
30
|
function instantiate(module: Module, imports?: object): Promise<Instance>;
|
|
30
31
|
}
|
package/src/zig.ts
ADDED
|
@@ -0,0 +1,596 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The compiler (packages/codegen/zig, docs/zig-codegen.md), its exports
|
|
3
|
+
* bound as functions that take and give JavaScript values.
|
|
4
|
+
*
|
|
5
|
+
* Its module takes bytes, not managed objects: every call starts with the
|
|
6
|
+
* module's `input`, which gives room for its arrays and strings, staged
|
|
7
|
+
* there and each passed as its address and length; what a call hands out
|
|
8
|
+
* is an address, `outputLength` bytes, valid until the next call starts. A
|
|
9
|
+
* call that ran out of memory hands out nothing and says so in
|
|
10
|
+
* `codegenError`, and is thrown here as a CodegenOutOfMemory. A panic, a
|
|
11
|
+
* failed safety check of the ReleaseSafe build, is the module's one
|
|
12
|
+
* import, env.abort, and it and any other trap are thrown as a
|
|
13
|
+
* CodegenPanic, after which the instance is dead, every call on it
|
|
14
|
+
* throwing one, and the wrapper (index.ts) recovers in another or loses
|
|
15
|
+
* its domain.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/** codegen.wasm's exports, bound. */
|
|
19
|
+
export interface CodegenExports {
|
|
20
|
+
readonly memory: WebAssembly.Memory;
|
|
21
|
+
/** `length` bytes at `address`, a call's output, viewed inside the panic guard. */
|
|
22
|
+
view(address: number, length: number): Uint8Array;
|
|
23
|
+
compilerVersion(): string;
|
|
24
|
+
abcVersion(abc: Uint8Array): number;
|
|
25
|
+
actionsEmitEach(
|
|
26
|
+
swf: Uint8Array,
|
|
27
|
+
offsets: Int32Array,
|
|
28
|
+
lengths: Int32Array,
|
|
29
|
+
version: number,
|
|
30
|
+
blocks: string,
|
|
31
|
+
reuse: boolean,
|
|
32
|
+
): string;
|
|
33
|
+
actionsEmitEachBytes(
|
|
34
|
+
swf: Uint8Array,
|
|
35
|
+
offsets: Int32Array,
|
|
36
|
+
lengths: Int32Array,
|
|
37
|
+
version: number,
|
|
38
|
+
blocks: string,
|
|
39
|
+
reuse: boolean,
|
|
40
|
+
): number;
|
|
41
|
+
actionsHeldCount(): number;
|
|
42
|
+
actionsHeldEmitEachBytes(handle: number, version: number, blocks: string): number;
|
|
43
|
+
actionsHeldUnitBytes(
|
|
44
|
+
handle: number,
|
|
45
|
+
version: number,
|
|
46
|
+
at: number,
|
|
47
|
+
end: number,
|
|
48
|
+
bound: number,
|
|
49
|
+
five: boolean,
|
|
50
|
+
): number;
|
|
51
|
+
actionsHold(handle: number, swf: Uint8Array, offsets: Int32Array, lengths: Int32Array): void;
|
|
52
|
+
actionsModuleBytes(
|
|
53
|
+
swf: Uint8Array,
|
|
54
|
+
offsets: Int32Array,
|
|
55
|
+
lengths: Int32Array,
|
|
56
|
+
version: number,
|
|
57
|
+
keep?: boolean,
|
|
58
|
+
): number;
|
|
59
|
+
actionsModuleEntries(): string;
|
|
60
|
+
actionsModuleUnits(): string;
|
|
61
|
+
actionsRelease(handle: number): void;
|
|
62
|
+
actionsUnitBytes(
|
|
63
|
+
swf: Uint8Array,
|
|
64
|
+
offsets: Int32Array,
|
|
65
|
+
lengths: Int32Array,
|
|
66
|
+
version: number,
|
|
67
|
+
at: number,
|
|
68
|
+
end: number,
|
|
69
|
+
bound: number,
|
|
70
|
+
five: boolean,
|
|
71
|
+
): number;
|
|
72
|
+
actionsUnitLimits(): Float64Array;
|
|
73
|
+
domainAdd(bytes: Uint8Array, builtin: boolean, appDomain?: number): number;
|
|
74
|
+
domainChild(parent: number): number;
|
|
75
|
+
domainCompact(stepped?: boolean): boolean;
|
|
76
|
+
domainContext(index?: number, after?: number, upTo?: number): string;
|
|
77
|
+
domainDrop(appDomain: number): void;
|
|
78
|
+
domainEvict(appDomain: number): void;
|
|
79
|
+
domainFound(
|
|
80
|
+
appDomain: number,
|
|
81
|
+
nsKind: number,
|
|
82
|
+
uri: string,
|
|
83
|
+
name: string,
|
|
84
|
+
abc: number,
|
|
85
|
+
asType: boolean,
|
|
86
|
+
): void;
|
|
87
|
+
domainKeyFork(appDomain: number): number;
|
|
88
|
+
domainKeyHeaderBytes(index?: number): number;
|
|
89
|
+
domainKeyLogBytes(
|
|
90
|
+
appDomain: number,
|
|
91
|
+
after: number,
|
|
92
|
+
from: number,
|
|
93
|
+
to: number,
|
|
94
|
+
block: number,
|
|
95
|
+
filled: number,
|
|
96
|
+
): number;
|
|
97
|
+
domainLinked(index?: number): string;
|
|
98
|
+
domainLoad(words: Uint8Array): boolean;
|
|
99
|
+
domainLoadAbc(index: number, bytes: Uint8Array): number;
|
|
100
|
+
domainLoadRebuild(): boolean;
|
|
101
|
+
domainLogMark(): number;
|
|
102
|
+
domainLogSince(mark: number, index?: number): string;
|
|
103
|
+
domainModule(hashes?: string, index?: number, keep?: boolean, linkedOnly?: boolean): string;
|
|
104
|
+
domainModuleBytes(hashes?: string, index?: number, keep?: boolean, linkedOnly?: boolean): number;
|
|
105
|
+
domainModuleDone(): boolean;
|
|
106
|
+
domainModuleEntries(): string;
|
|
107
|
+
domainModuleMoreBytes(): number;
|
|
108
|
+
domainParent(appDomain: number): number;
|
|
109
|
+
domainRebuildStep(): number;
|
|
110
|
+
domainRebuildBudget(bytes: number): void;
|
|
111
|
+
domainReplay(log: string, index?: number): boolean;
|
|
112
|
+
domainReset(apiVersion: number): void;
|
|
113
|
+
domainRestore(index: number, bytes: Uint8Array): number;
|
|
114
|
+
domainRevision(): string;
|
|
115
|
+
domainRevive(appDomain: number, stepped?: boolean): number;
|
|
116
|
+
domainSaveBytes(): number;
|
|
117
|
+
domainSavedAbcBytes(index: number): number;
|
|
118
|
+
domainSourceMap(): string;
|
|
119
|
+
domainState(appDomain: number): number;
|
|
120
|
+
domainUsage(): string;
|
|
121
|
+
domainWantsCompact(want?: number): boolean;
|
|
122
|
+
outputLength(): number;
|
|
123
|
+
// The recovery journal's (see createCodegen's recover).
|
|
124
|
+
lastAddLinked(): boolean;
|
|
125
|
+
journalBytes(mark: number): number;
|
|
126
|
+
replayEntry(kind: number, index: number, a: number, b: number): boolean;
|
|
127
|
+
abandonRebuild(): void;
|
|
128
|
+
loadCheckpointState(words: Uint8Array): boolean;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* An export's parameters, as its bound function takes them, and its result.
|
|
133
|
+
* Parameters are space-separated: `u8s` a Uint8Array, `i32s` an
|
|
134
|
+
* Int32Array, `str` a string, each staged; `i32` and `bool` passed as they
|
|
135
|
+
* are; `=value` a default where the argument is left out.
|
|
136
|
+
* Results: `str` UTF-8 handed out, decoded leniently (fromUtf8);
|
|
137
|
+
* `address` an address handed out, as the *Bytes exports return it;
|
|
138
|
+
* `f64x2` two doubles at an address; `f64s` doubles handed out, as many as
|
|
139
|
+
* `outputLength` bytes hold.
|
|
140
|
+
*/
|
|
141
|
+
export type Signature = readonly [
|
|
142
|
+
string,
|
|
143
|
+
"void" | "i32" | "bool" | "str" | "address" | "f64x2" | "f64s",
|
|
144
|
+
];
|
|
145
|
+
|
|
146
|
+
/** Every export of codegen.wasm's, by its signature. */
|
|
147
|
+
export const SIGNATURES: {
|
|
148
|
+
readonly [name in Exclude<keyof CodegenExports, "memory" | "view">]: Signature;
|
|
149
|
+
} = {
|
|
150
|
+
compilerVersion: ["", "str"],
|
|
151
|
+
abcVersion: ["u8s", "i32"],
|
|
152
|
+
actionsEmitEach: ["u8s i32s i32s i32 str bool", "str"],
|
|
153
|
+
actionsEmitEachBytes: ["u8s i32s i32s i32 str bool", "address"],
|
|
154
|
+
actionsHeldCount: ["", "i32"],
|
|
155
|
+
actionsHeldEmitEachBytes: ["i32 i32 str", "address"],
|
|
156
|
+
actionsHeldUnitBytes: ["i32 i32 i32 i32 i32 bool", "address"],
|
|
157
|
+
actionsHold: ["i32 u8s i32s i32s", "void"],
|
|
158
|
+
actionsModuleBytes: ["u8s i32s i32s i32 bool=false", "address"],
|
|
159
|
+
actionsModuleEntries: ["", "str"],
|
|
160
|
+
actionsModuleUnits: ["", "str"],
|
|
161
|
+
actionsRelease: ["i32", "void"],
|
|
162
|
+
actionsUnitBytes: ["u8s i32s i32s i32 i32 i32 i32 bool", "address"],
|
|
163
|
+
actionsUnitLimits: ["", "f64x2"],
|
|
164
|
+
domainAdd: ["u8s bool i32=0", "i32"],
|
|
165
|
+
domainChild: ["i32", "i32"],
|
|
166
|
+
domainCompact: ["bool=false", "bool"],
|
|
167
|
+
domainContext: ["i32=-1 i32=0 i32=-1", "str"],
|
|
168
|
+
domainDrop: ["i32", "void"],
|
|
169
|
+
domainEvict: ["i32", "void"],
|
|
170
|
+
domainFound: ["i32 i32 str str i32 bool", "void"],
|
|
171
|
+
domainKeyFork: ["i32", "i32"],
|
|
172
|
+
domainKeyHeaderBytes: ["i32=-1", "address"],
|
|
173
|
+
domainKeyLogBytes: ["i32 i32 i32 i32 i32 i32", "address"],
|
|
174
|
+
domainLinked: ["i32=-1", "str"],
|
|
175
|
+
domainLoad: ["u8s", "bool"],
|
|
176
|
+
domainLoadAbc: ["i32 u8s", "i32"],
|
|
177
|
+
domainLoadRebuild: ["", "bool"],
|
|
178
|
+
domainLogMark: ["", "i32"],
|
|
179
|
+
domainLogSince: ["i32 i32=-1", "str"],
|
|
180
|
+
domainModule: ["str= i32=-1 bool=true bool=false", "str"],
|
|
181
|
+
domainModuleBytes: ["str= i32=-1 bool=true bool=false", "address"],
|
|
182
|
+
domainModuleDone: ["", "bool"],
|
|
183
|
+
domainModuleEntries: ["", "str"],
|
|
184
|
+
domainModuleMoreBytes: ["", "address"],
|
|
185
|
+
domainParent: ["i32", "i32"],
|
|
186
|
+
domainRebuildStep: ["", "i32"],
|
|
187
|
+
domainRebuildBudget: ["i32", "void"],
|
|
188
|
+
domainReplay: ["str i32=-1", "bool"],
|
|
189
|
+
domainReset: ["i32", "void"],
|
|
190
|
+
domainRestore: ["i32 u8s", "i32"],
|
|
191
|
+
domainRevision: ["", "str"],
|
|
192
|
+
domainRevive: ["i32 bool=false", "i32"],
|
|
193
|
+
domainSaveBytes: ["", "address"],
|
|
194
|
+
domainSavedAbcBytes: ["i32", "address"],
|
|
195
|
+
domainSourceMap: ["", "str"],
|
|
196
|
+
domainState: ["i32", "i32"],
|
|
197
|
+
domainUsage: ["", "str"],
|
|
198
|
+
domainWantsCompact: ["i32=0", "bool"],
|
|
199
|
+
outputLength: ["", "i32"],
|
|
200
|
+
lastAddLinked: ["", "bool"],
|
|
201
|
+
journalBytes: ["i32", "address"],
|
|
202
|
+
replayEntry: ["i32 i32 i32 i32", "bool"],
|
|
203
|
+
abandonRebuild: ["", "void"],
|
|
204
|
+
loadCheckpointState: ["u8s", "bool"],
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
/** An allocation refused by the compiler leaves it usable. */
|
|
208
|
+
export class CodegenOutOfMemory extends Error {}
|
|
209
|
+
|
|
210
|
+
/** A panic in the module: a safety check failed, and the instance is dead. */
|
|
211
|
+
export class CodegenPanic extends Error {
|
|
212
|
+
override name = "CodegenPanic";
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** The module's own exports: numbers in, numbers out. */
|
|
216
|
+
interface Raw {
|
|
217
|
+
memory: WebAssembly.Memory;
|
|
218
|
+
input(size: number): number;
|
|
219
|
+
codegenError(): number;
|
|
220
|
+
outputLength(): number;
|
|
221
|
+
[name: string]: unknown;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
type Kind = "u8s" | "i32s" | "str" | "i32" | "bool";
|
|
225
|
+
|
|
226
|
+
interface Parameter {
|
|
227
|
+
kind: Kind;
|
|
228
|
+
fallback?: number | boolean | string;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function parameters(spec: string): Parameter[] {
|
|
232
|
+
return spec
|
|
233
|
+
.split(" ")
|
|
234
|
+
.filter(Boolean)
|
|
235
|
+
.map((token) => {
|
|
236
|
+
const [kind, value] = token.split("=") as [Kind, string | undefined];
|
|
237
|
+
if (value === undefined) {
|
|
238
|
+
return { kind };
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
switch (kind) {
|
|
242
|
+
case "bool":
|
|
243
|
+
return { kind, fallback: value === "true" };
|
|
244
|
+
case "i32":
|
|
245
|
+
return { kind, fallback: Number(value) };
|
|
246
|
+
default:
|
|
247
|
+
return { kind, fallback: value };
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const align8 = (n: number) => (n + 7) & ~7;
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* The exports that read what the last call left rather than start one, as
|
|
256
|
+
* the call's output stays readable after them (a unit's limits,
|
|
257
|
+
* read after its code, the held SWFs counted or one let go).
|
|
258
|
+
*/
|
|
259
|
+
const READS_LAST_CALL = new Set([
|
|
260
|
+
"outputLength",
|
|
261
|
+
"codegenError",
|
|
262
|
+
"actionsUnitLimits",
|
|
263
|
+
"actionsHeldCount",
|
|
264
|
+
"actionsRelease",
|
|
265
|
+
"abandonRebuild",
|
|
266
|
+
"lastAddLinked",
|
|
267
|
+
]);
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* The exports of a build of the compiler, instantiated from `module`,
|
|
271
|
+
* bound: `signatures` names them, and the test builds add theirs, with
|
|
272
|
+
* those of theirs that read what the last call left rather than start a
|
|
273
|
+
* call (`readsLastCall`).
|
|
274
|
+
*/
|
|
275
|
+
export async function bind<T>(
|
|
276
|
+
module: WebAssembly.Module,
|
|
277
|
+
signatures: { readonly [name: string]: Signature },
|
|
278
|
+
readsLastCall: readonly string[] = [],
|
|
279
|
+
): Promise<T & { panicked(): string | null }> {
|
|
280
|
+
const state = panicState();
|
|
281
|
+
const instance = await WebAssembly.instantiate(module, state.imports);
|
|
282
|
+
return bindInstance<T>(instance, state, signatures, readsLastCall);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/** A panic with no compaction spare ready can instantiate the already compiled module synchronously. */
|
|
286
|
+
export function recoveryInstance(
|
|
287
|
+
module: WebAssembly.Module,
|
|
288
|
+
): CodegenExports & { panicked(): string | null } {
|
|
289
|
+
const state = panicState();
|
|
290
|
+
const instance = new WebAssembly.Instance(module, state.imports);
|
|
291
|
+
return bindInstance<CodegenExports>(instance, state, SIGNATURES, []);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
function panicState() {
|
|
295
|
+
const state = {
|
|
296
|
+
panic: null as string | null,
|
|
297
|
+
raw: null as Raw | null,
|
|
298
|
+
imports: {
|
|
299
|
+
env: {
|
|
300
|
+
abort(message: number, length: number) {
|
|
301
|
+
state.panic = state.raw
|
|
302
|
+
? fromUtf8(new Uint8Array(state.raw.memory.buffer, message >>> 0, length))
|
|
303
|
+
: "panic";
|
|
304
|
+
throw new CodegenPanic(`codegen.wasm: ${state.panic}`);
|
|
305
|
+
},
|
|
306
|
+
},
|
|
307
|
+
},
|
|
308
|
+
};
|
|
309
|
+
return state;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
function bindInstance<T>(
|
|
313
|
+
instance: WebAssembly.Instance,
|
|
314
|
+
state: ReturnType<typeof panicState>,
|
|
315
|
+
signatures: { readonly [name: string]: Signature },
|
|
316
|
+
readsLastCall: readonly string[],
|
|
317
|
+
) {
|
|
318
|
+
const exports = instance.exports as unknown as Raw;
|
|
319
|
+
state.raw = exports;
|
|
320
|
+
const alive = () => {
|
|
321
|
+
if (state.panic !== null) {
|
|
322
|
+
throw new CodegenPanic(
|
|
323
|
+
`codegen.wasm panicked earlier (${state.panic}): make another instance`,
|
|
324
|
+
);
|
|
325
|
+
}
|
|
326
|
+
};
|
|
327
|
+
// Addresses are u32, which wasm hands JavaScript as i32: negative past 2 GiB.
|
|
328
|
+
const handed = (address: number) =>
|
|
329
|
+
fromUtf8(new Uint8Array(exports.memory.buffer, address >>> 0, exports.outputLength()));
|
|
330
|
+
// Any trap but a panic, too: a stack overflow, the engine's call stack
|
|
331
|
+
// exhausted. It leaves the instance as a panic does, its shadow stack's
|
|
332
|
+
// pointer where the trap left it and its call half done.
|
|
333
|
+
const guarded = <R>(run: () => R): R => {
|
|
334
|
+
try {
|
|
335
|
+
return run();
|
|
336
|
+
} catch (error) {
|
|
337
|
+
if (!(error instanceof CodegenPanic)) {
|
|
338
|
+
state.panic = `trap: ${error instanceof Error ? error.message : String(error)}`;
|
|
339
|
+
throw new CodegenPanic(`codegen.wasm: ${state.panic}`);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
throw error;
|
|
343
|
+
}
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
const bound: Record<string, unknown> = {
|
|
347
|
+
get memory() {
|
|
348
|
+
return exports.memory;
|
|
349
|
+
},
|
|
350
|
+
panicked: () => state.panic,
|
|
351
|
+
// What a call handed out, at an address it returned, read inside the
|
|
352
|
+
// guard: a view past memory's end marks the instance dead as a trap does.
|
|
353
|
+
view: (address: number, length: number) =>
|
|
354
|
+
guarded(() => new Uint8Array(exports.memory.buffer, address >>> 0, length)),
|
|
355
|
+
};
|
|
356
|
+
for (const [name, [spec, result]] of Object.entries(signatures)) {
|
|
357
|
+
const call = exports[name] as ((...args: number[]) => number) | undefined;
|
|
358
|
+
if (typeof call !== "function") {
|
|
359
|
+
bound[name] = () => {
|
|
360
|
+
throw new Error(`this build of codegen.wasm has no ${name}`);
|
|
361
|
+
};
|
|
362
|
+
continue;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
const params = parameters(spec);
|
|
366
|
+
// These read the last call, and start none (see root.zig).
|
|
367
|
+
const starts = !READS_LAST_CALL.has(name) && !readsLastCall.includes(name);
|
|
368
|
+
bound[name] = (...args: unknown[]) => {
|
|
369
|
+
alive();
|
|
370
|
+
// A default where the argument is left out.
|
|
371
|
+
const values = params.map((p, i) => (i < args.length ? args[i] : p.fallback));
|
|
372
|
+
const parts = values.map((value, i) => {
|
|
373
|
+
const kind = params[i].kind;
|
|
374
|
+
if (kind === "i32" || kind === "bool") {
|
|
375
|
+
return null;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
if (value === null || value === undefined) {
|
|
379
|
+
throw new TypeError(`${name}: argument ${i} must not be null`);
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
if (kind === "str") {
|
|
383
|
+
return toUtf8(value as string);
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
const items = value as Uint8Array | Int32Array;
|
|
387
|
+
const bytes = new Uint8Array(items.buffer, items.byteOffset, items.byteLength);
|
|
388
|
+
// A view of the module's own memory, which input may give back or grow away.
|
|
389
|
+
return items.buffer === exports.memory.buffer ? bytes.slice() : bytes;
|
|
390
|
+
});
|
|
391
|
+
let size = 0;
|
|
392
|
+
for (const part of parts) {
|
|
393
|
+
size = part ? align8(size) + part.length : size;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
const flat: number[] = [];
|
|
397
|
+
// Only the module's own code is guarded: running out of memory is a
|
|
398
|
+
// plain Error, which leaves the instance usable.
|
|
399
|
+
if (starts) {
|
|
400
|
+
const base = guarded(() => exports.input(size)) >>> 0;
|
|
401
|
+
if (base === 0) {
|
|
402
|
+
throw new CodegenOutOfMemory(`codegen.wasm: ${name} ran out of memory`);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
// Read after input, which may have grown memory.
|
|
406
|
+
const memory = new Uint8Array(exports.memory.buffer);
|
|
407
|
+
let at = 0;
|
|
408
|
+
for (const [i, part] of parts.entries()) {
|
|
409
|
+
if (part === null) {
|
|
410
|
+
flat.push(Number(values[i]));
|
|
411
|
+
continue;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
at = align8(at);
|
|
415
|
+
memory.set(part, base + at);
|
|
416
|
+
flat.push(base + at, params[i].kind === "i32s" ? part.length / 4 : part.length);
|
|
417
|
+
at += part.length;
|
|
418
|
+
}
|
|
419
|
+
} else {
|
|
420
|
+
flat.push(...values.map(Number));
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
const value = guarded(() => call(...flat));
|
|
424
|
+
// What it handed out read inside guarded too: a view past memory's end
|
|
425
|
+
// (a RangeError) marks the instance dead as a trap in it does.
|
|
426
|
+
const outOfMemory = starts && guarded(() => exports.codegenError()) !== 0;
|
|
427
|
+
if (outOfMemory) {
|
|
428
|
+
throw new CodegenOutOfMemory(`codegen.wasm: ${name} ran out of memory`);
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
return guarded(() => {
|
|
432
|
+
switch (result) {
|
|
433
|
+
case "void":
|
|
434
|
+
return undefined;
|
|
435
|
+
case "bool":
|
|
436
|
+
return value !== 0;
|
|
437
|
+
case "str":
|
|
438
|
+
return handed(value);
|
|
439
|
+
case "address":
|
|
440
|
+
return value >>> 0;
|
|
441
|
+
case "f64x2":
|
|
442
|
+
return new Float64Array(exports.memory.buffer, value >>> 0, 2).slice();
|
|
443
|
+
case "f64s":
|
|
444
|
+
return new Float64Array(
|
|
445
|
+
exports.memory.buffer,
|
|
446
|
+
value >>> 0,
|
|
447
|
+
exports.outputLength() / 8,
|
|
448
|
+
).slice();
|
|
449
|
+
default:
|
|
450
|
+
return value;
|
|
451
|
+
}
|
|
452
|
+
});
|
|
453
|
+
};
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
return bound as T & { panicked(): string | null };
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
/**
|
|
460
|
+
* Throws, plainly, where `module` lacks an export the wrapper binds: a
|
|
461
|
+
* stale build, or another package's, which would otherwise fail on its
|
|
462
|
+
* first call as a trap.
|
|
463
|
+
*/
|
|
464
|
+
export function checkExports(module: WebAssembly.Module): void {
|
|
465
|
+
const exported = new Set(WebAssembly.Module.exports(module).map((e) => e.name));
|
|
466
|
+
const missing = ["memory", "input", "codegenError", ...Object.keys(SIGNATURES)].filter(
|
|
467
|
+
(name) => !exported.has(name),
|
|
468
|
+
);
|
|
469
|
+
if (missing.length > 0) {
|
|
470
|
+
throw new Error(
|
|
471
|
+
`this codegen.wasm does not match this @swf2es/codegen: it lacks ${missing.slice(0, 5).join(", ")}` +
|
|
472
|
+
`${missing.length > 5 ? ` and ${missing.length - 5} more` : ""}`,
|
|
473
|
+
);
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
/** The compiler's exports, bound, from its compiled module, for createCodegen. */
|
|
478
|
+
export async function instantiate(
|
|
479
|
+
module: WebAssembly.Module,
|
|
480
|
+
): Promise<CodegenExports & { panicked(): string | null }> {
|
|
481
|
+
return bind<CodegenExports>(module, SIGNATURES);
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
/**
|
|
485
|
+
* A string as the compiler takes it in: WTF-8, a lone surrogate as three
|
|
486
|
+
* bytes, where TextEncoder writes U+FFFD.
|
|
487
|
+
*/
|
|
488
|
+
export function toUtf8(text: string): Uint8Array {
|
|
489
|
+
const out = new Uint8Array(text.length * 3);
|
|
490
|
+
let at = 0;
|
|
491
|
+
for (let i = 0; i < text.length; i++) {
|
|
492
|
+
let c = text.charCodeAt(i);
|
|
493
|
+
if (c < 0x80) {
|
|
494
|
+
out[at++] = c;
|
|
495
|
+
continue;
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
if (c < 0x800) {
|
|
499
|
+
out[at++] = (c >> 6) | 0xc0;
|
|
500
|
+
out[at++] = (c & 63) | 0x80;
|
|
501
|
+
continue;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
if ((c & 0xfc00) === 0xd800 && i + 1 < text.length) {
|
|
505
|
+
const next = text.charCodeAt(i + 1);
|
|
506
|
+
if ((next & 0xfc00) === 0xdc00) {
|
|
507
|
+
c = 0x10000 + ((c & 0x3ff) << 10) + (next & 0x3ff);
|
|
508
|
+
out[at++] = (c >> 18) | 0xf0;
|
|
509
|
+
out[at++] = ((c >> 12) & 63) | 0x80;
|
|
510
|
+
out[at++] = ((c >> 6) & 63) | 0x80;
|
|
511
|
+
out[at++] = (c & 63) | 0x80;
|
|
512
|
+
i++;
|
|
513
|
+
continue;
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
out[at++] = (c >> 12) | 0xe0;
|
|
518
|
+
out[at++] = ((c >> 6) & 63) | 0x80;
|
|
519
|
+
out[at++] = (c & 63) | 0x80;
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
return out.subarray(0, at);
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
/**
|
|
526
|
+
* Bytes as the compiler makes a string of an ABC's bytes: valid UTF-8 as
|
|
527
|
+
* TextDecoder reads it, and anything else as the compiler reads it
|
|
528
|
+
* (zig/src/output.zig), checking nothing, keeping what an invalid
|
|
529
|
+
* sequence's bits say and dropping a sequence cut short at the end, where
|
|
530
|
+
* TextDecoder writes U+FFFD.
|
|
531
|
+
*/
|
|
532
|
+
export function fromUtf8(bytes: Uint8Array): string {
|
|
533
|
+
try {
|
|
534
|
+
return strict.decode(bytes);
|
|
535
|
+
} catch {
|
|
536
|
+
// Not valid UTF-8: read below.
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
const units: number[] = [];
|
|
540
|
+
let at = 0;
|
|
541
|
+
while (at < bytes.length) {
|
|
542
|
+
let u0 = bytes[at++];
|
|
543
|
+
if (!(u0 & 128)) {
|
|
544
|
+
units.push(u0);
|
|
545
|
+
continue;
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
if (at === bytes.length) {
|
|
549
|
+
break;
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
const u1 = bytes[at++] & 63;
|
|
553
|
+
if ((u0 & 224) === 192) {
|
|
554
|
+
units.push(((u0 & 31) << 6) | u1);
|
|
555
|
+
continue;
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
if (at === bytes.length) {
|
|
559
|
+
break;
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
const u2 = bytes[at++] & 63;
|
|
563
|
+
if ((u0 & 240) === 224) {
|
|
564
|
+
u0 = ((u0 & 15) << 12) | (u1 << 6) | u2;
|
|
565
|
+
} else {
|
|
566
|
+
if (at === bytes.length) {
|
|
567
|
+
break;
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
u0 = ((u0 & 7) << 18) | (u1 << 12) | (u2 << 6) | (bytes[at++] & 63);
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
if (u0 < 0x10000) {
|
|
574
|
+
units.push(u0);
|
|
575
|
+
} else {
|
|
576
|
+
// A surrogate pair, past U+10FFFF too, up to 0x1FFFFF.
|
|
577
|
+
units.push(((u0 - 0x10000) >> 10) | 0xd800, ((u0 - 0x10000) & 0x3ff) | 0xdc00);
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
let text = "";
|
|
582
|
+
for (let i = 0; i < units.length; i += 8192) {
|
|
583
|
+
text += String.fromCharCode(...units.slice(i, i + 8192));
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
return text;
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
// TextDecoder is the web's and node's alike, though not ECMAScript's,
|
|
590
|
+
// whose library alone the compiler's wrapper is typed against.
|
|
591
|
+
declare const TextDecoder: new (
|
|
592
|
+
label: string,
|
|
593
|
+
options: { fatal: boolean; ignoreBOM: boolean },
|
|
594
|
+
) => { decode(bytes: Uint8Array): string };
|
|
595
|
+
|
|
596
|
+
const strict = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
|