@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/dist/index.js
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Compiles ABC and AVM1 bytecode to ES modules.
|
|
3
3
|
*
|
|
4
|
-
* The compiler itself is
|
|
5
|
-
* This wrapper instantiates it and gives its exports their
|
|
4
|
+
* The compiler itself is Zig (zig/), built to codegen.wasm, whose exports
|
|
5
|
+
* zig.ts binds. This wrapper instantiates it and gives its exports their
|
|
6
|
+
* types. The same wasm runs as the browser JIT
|
|
6
7
|
* (a module per ABC as its SWF loads) and as the ahead-of-time compiler,
|
|
7
8
|
* so both produce identical output. See docs/architecture/overview.md.
|
|
8
9
|
*/
|
|
9
10
|
import { AIR_API_VERSION, API_VERSION, checkApiVersion, isAirApiVersion } from "@swf2es/format";
|
|
10
|
-
import { instantiate } from "./codegen.js";
|
|
11
11
|
import { KeyChains } from "./keys.js";
|
|
12
|
+
import { CodegenOutOfMemory, CodegenPanic, checkExports, instantiate, recoveryInstance, } from "./zig.js";
|
|
12
13
|
export { KeyChains } from "./keys.js";
|
|
13
14
|
export { hex, sha256Here } from "./sha256.js";
|
|
14
15
|
/** Bumped whenever generated code or the runtime ABI it calls changes. */
|
|
@@ -69,31 +70,12 @@ export function actionsKey(codegen, hash, version) {
|
|
|
69
70
|
* this package free of I/O. The file ships as `@swf2es/codegen/codegen.wasm`.
|
|
70
71
|
*/
|
|
71
72
|
export async function createCodegen(module) {
|
|
72
|
-
|
|
73
|
+
checkExports(module);
|
|
74
|
+
let wasm = await instantiate(module);
|
|
73
75
|
const version = wasm.compilerVersion();
|
|
74
76
|
if (version !== COMPILER_VERSION) {
|
|
75
77
|
throw new Error(`codegen.wasm is version ${version} but the wrapper expects ${COMPILER_VERSION}`);
|
|
76
78
|
}
|
|
77
|
-
// Of the same version, but built before its modules were handed out in
|
|
78
|
-
// place: its *Bytes exports return copies, which this wrapper cannot read.
|
|
79
|
-
// Asked of the module, as the bindings define every export they know of.
|
|
80
|
-
if (!WebAssembly.Module.exports(module).some((e) => e.name === "outputLength")) {
|
|
81
|
-
throw new Error("codegen.wasm has no outputLength export, which the wrapper expects: it is older than the wrapper");
|
|
82
|
-
}
|
|
83
|
-
// Or before it wrote a module a part at a time, or rebuilt its domain a
|
|
84
|
-
// part at a time and in another instance.
|
|
85
|
-
for (const name of ["domainModuleMoreBytes", "domainRebuildStep", "domainSaveBytes"]) {
|
|
86
|
-
if (!WebAssembly.Module.exports(module).some((e) => e.name === name)) {
|
|
87
|
-
throw new Error(`codegen.wasm has no ${name} export, which the wrapper expects: it is older than the wrapper`);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
// codegen.wasm uses AssemblyScript's minimal runtime, which collects
|
|
91
|
-
// garbage only when asked. Nothing runs in wasm between calls, so that is
|
|
92
|
-
// when: every COLLECT_EVERY calls, and after a call that grew memory.
|
|
93
|
-
// Memory never shrinks, so collecting only on growth would let each
|
|
94
|
-
// cycle's garbage grow it a little further.
|
|
95
|
-
let size = wasm.memory.buffer.byteLength;
|
|
96
|
-
let calls = 0;
|
|
97
79
|
// Whether the domain has an ABC to compile: asking before is a mistake worth a message, not a trap.
|
|
98
80
|
let added = 0;
|
|
99
81
|
const last = (what) => {
|
|
@@ -115,7 +97,7 @@ export async function createCodegen(module) {
|
|
|
115
97
|
// compile took megabytes of codegen's memory a few thousand loads on.
|
|
116
98
|
const at = indexOf(index);
|
|
117
99
|
const own = at < 0 ? added - 1 : at;
|
|
118
|
-
const linked =
|
|
100
|
+
const linked = wasm.domainLinked(at);
|
|
119
101
|
const named = [hashes[own] ?? ""];
|
|
120
102
|
for (const i of linked ? linked.split(",") : []) {
|
|
121
103
|
named.push(hashes[Number(i)] ?? "");
|
|
@@ -123,21 +105,21 @@ export async function createCodegen(module) {
|
|
|
123
105
|
// Its bytes decoded here: lifted as a string, a module of megabytes took
|
|
124
106
|
// longer than compiling it. The emitter writes only ASCII, which decodes
|
|
125
107
|
// alike either way, and to a string the engine keeps a byte a character.
|
|
126
|
-
// A large ABC's module comes a part at a time, each part's
|
|
127
|
-
//
|
|
108
|
+
// A large ABC's module comes a part at a time, each part's arena given
|
|
109
|
+
// back as the next is written (see domainModuleBytes).
|
|
128
110
|
const parts = [
|
|
129
111
|
handed(wasm.domainModuleBytes(named.join("\n"), at, keep, true), wasm.outputLength()),
|
|
130
112
|
];
|
|
131
113
|
while (!wasm.domainModuleDone()) {
|
|
132
|
-
wasm.__collect();
|
|
133
114
|
parts.push(handed(wasm.domainModuleMoreBytes(), wasm.outputLength()));
|
|
134
115
|
}
|
|
135
|
-
const module =
|
|
116
|
+
const module = parts.length === 1 ? parts[0] : parts.join("");
|
|
136
117
|
if (module === "") {
|
|
137
118
|
throw new Error(`ABC ${index}: its application domain was dropped`);
|
|
138
119
|
}
|
|
139
120
|
return module;
|
|
140
121
|
};
|
|
122
|
+
const compiled = (hashes, index, keep) => recordChanges(() => moduleOf(hashes, index, keep));
|
|
141
123
|
const ranges = (actions) => [
|
|
142
124
|
Int32Array.from(actions.blocks, (b) => b.offset),
|
|
143
125
|
Int32Array.from(actions.blocks, (b) => b.length),
|
|
@@ -154,7 +136,16 @@ export async function createCodegen(module) {
|
|
|
154
136
|
const at = holds.indexOf(handle);
|
|
155
137
|
if (at >= 0) {
|
|
156
138
|
holds.splice(at, 1);
|
|
157
|
-
|
|
139
|
+
// Also called from the finalization registry, which must not throw:
|
|
140
|
+
// a dead instance's holds went with it.
|
|
141
|
+
try {
|
|
142
|
+
wasm.actionsRelease(handle);
|
|
143
|
+
}
|
|
144
|
+
catch (error) {
|
|
145
|
+
if (!(error instanceof CodegenPanic)) {
|
|
146
|
+
throw error;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
158
149
|
}
|
|
159
150
|
};
|
|
160
151
|
const collectedSwfs = new FinalizationRegistry(release);
|
|
@@ -185,15 +176,18 @@ export async function createCodegen(module) {
|
|
|
185
176
|
const actionsModule = (bytes, actions, keep) => {
|
|
186
177
|
const [offsets, lengths] = ranges(actions);
|
|
187
178
|
const address = wasm.actionsModuleBytes(bytes, offsets, lengths, actions.version, keep);
|
|
188
|
-
return
|
|
179
|
+
return handed(address, wasm.outputLength());
|
|
189
180
|
};
|
|
190
181
|
// UTF-8 a call handed out in codegen's memory, not copied (see handOut in
|
|
191
|
-
//
|
|
192
|
-
// call may grow memory
|
|
193
|
-
const handed = (address, length) => length === 0 ? "" : utf8.decode(
|
|
182
|
+
// zig/src/root.zig), decoded before anything else calls in: the next
|
|
183
|
+
// call may grow memory, and gives the last one's arena back.
|
|
184
|
+
const handed = (address, length) => length === 0 ? "" : utf8.decode(view(address, length));
|
|
185
|
+
// `length` bytes at `address` in codegen's memory, read inside the panic guard (see zig.ts).
|
|
186
|
+
const view = (address, length) => wasm.view(address, length);
|
|
194
187
|
const stamps = WebAssembly.Module.customSections(module, "swf2es.build");
|
|
195
188
|
const identity = stamps.length ? String.fromCharCode(...new Uint8Array(stamps[0])) : null;
|
|
196
189
|
let epoch = 0;
|
|
190
|
+
let revisionEpoch = 0;
|
|
197
191
|
let apiVersion = API_VERSION;
|
|
198
192
|
// Read where they are in codegen's memory, digested before the next call in.
|
|
199
193
|
const chains = new KeyChains({
|
|
@@ -205,26 +199,77 @@ export async function createCodegen(module) {
|
|
|
205
199
|
fork: (appDomain) => wasm.domainKeyFork(appDomain),
|
|
206
200
|
log(appDomain, after, from, to, block, filled) {
|
|
207
201
|
const address = wasm.domainKeyLogBytes(appDomain, after, from, to, block, filled);
|
|
208
|
-
return address === 0
|
|
209
|
-
? null
|
|
210
|
-
: new Uint8Array(wasm.memory.buffer, address, wasm.outputLength());
|
|
202
|
+
return address === 0 ? null : view(address, wasm.outputLength());
|
|
211
203
|
},
|
|
212
204
|
});
|
|
205
|
+
let checkpoint = null;
|
|
206
|
+
const saveCheckpoint = () => {
|
|
207
|
+
const at = wasm.domainSaveBytes();
|
|
208
|
+
const state = view(at, wasm.outputLength()).slice();
|
|
209
|
+
const abcs = new Map();
|
|
210
|
+
for (let i = 0; i < added; i++) {
|
|
211
|
+
const at = wasm.domainSavedAbcBytes(i);
|
|
212
|
+
if (at) {
|
|
213
|
+
abcs.set(i, view(at, wasm.outputLength()).slice());
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
return { state, abcs };
|
|
217
|
+
};
|
|
218
|
+
const loadCheckpoint = (into, saved, recovery) => {
|
|
219
|
+
const loaded = recovery ? into.loadCheckpointState(saved.state) : into.domainLoad(saved.state);
|
|
220
|
+
if (!loaded) {
|
|
221
|
+
return false;
|
|
222
|
+
}
|
|
223
|
+
for (const [index, abc] of saved.abcs) {
|
|
224
|
+
if (into.domainLoadAbc(index, abc) !== 0) {
|
|
225
|
+
return false;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
return into.domainLoadRebuild();
|
|
229
|
+
};
|
|
213
230
|
// The rebuild domainCompact, domainRevive or domainLoadRebuild started in
|
|
214
|
-
// `into`, a part a call, each part's
|
|
215
|
-
// (see domainRebuildStep)
|
|
216
|
-
|
|
217
|
-
const rebuild = (into = wasm) => {
|
|
231
|
+
// `into`, a part a call, each part's arena given back as the next starts
|
|
232
|
+
// (see domainRebuildStep): whether it was done.
|
|
233
|
+
const finishRebuild = (into) => {
|
|
218
234
|
let step = into.domainRebuildStep();
|
|
219
235
|
while (step === 0) {
|
|
220
|
-
into.__collect();
|
|
221
236
|
step = into.domainRebuildStep();
|
|
222
237
|
}
|
|
223
|
-
into.__collect();
|
|
224
|
-
size = wasm.memory.buffer.byteLength;
|
|
225
|
-
calls = 0;
|
|
226
238
|
return step > 0;
|
|
227
239
|
};
|
|
240
|
+
const rebuild = (into = wasm, saved = null, optional = false) => {
|
|
241
|
+
let done;
|
|
242
|
+
try {
|
|
243
|
+
if (!saved) {
|
|
244
|
+
saved = saveCheckpoint();
|
|
245
|
+
}
|
|
246
|
+
done = finishRebuild(into);
|
|
247
|
+
}
|
|
248
|
+
catch (error) {
|
|
249
|
+
if (!(error instanceof CodegenPanic)) {
|
|
250
|
+
into.abandonRebuild();
|
|
251
|
+
if (optional && error instanceof CodegenOutOfMemory) {
|
|
252
|
+
return false;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
throw error;
|
|
256
|
+
}
|
|
257
|
+
if (done && saved) {
|
|
258
|
+
// The journal a move under way catches up from is cleared below.
|
|
259
|
+
if (into === wasm) {
|
|
260
|
+
abortMove("superseded");
|
|
261
|
+
}
|
|
262
|
+
moveRefused = false;
|
|
263
|
+
checkpoint = saved;
|
|
264
|
+
journal.clear();
|
|
265
|
+
restores.clear();
|
|
266
|
+
for (const domain of journalDomains.values()) {
|
|
267
|
+
domain.entries = undefined;
|
|
268
|
+
}
|
|
269
|
+
replayable = true;
|
|
270
|
+
}
|
|
271
|
+
return done;
|
|
272
|
+
};
|
|
228
273
|
// A second instance of the compiler, made ahead as instantiating is
|
|
229
274
|
// asynchronous, that the next rebuild is done in (see moved): wasm memory
|
|
230
275
|
// never shrinks, so one done in place keeps, for good, as much as the
|
|
@@ -234,10 +279,38 @@ export async function createCodegen(module) {
|
|
|
234
279
|
let spare = null;
|
|
235
280
|
let making = false;
|
|
236
281
|
let failedMoves = 0;
|
|
282
|
+
// The move compactStep has under way, a part at a time (see Move), and what came of its moves.
|
|
283
|
+
let move = null;
|
|
284
|
+
const moves = {
|
|
285
|
+
started: 0,
|
|
286
|
+
completed: 0,
|
|
287
|
+
aborted: { spare: 0, refused: 0, unjournaled: 0, superseded: 0 },
|
|
288
|
+
lastCaughtUp: 0,
|
|
289
|
+
lastSavedBytes: 0,
|
|
290
|
+
};
|
|
291
|
+
// Whether the spare refused a move as too large, until the next rebuild: a large domain then rebuilds in place.
|
|
292
|
+
let moveRefused = false;
|
|
293
|
+
// The drops and evictions so far, and how many there must be before
|
|
294
|
+
// compactStep starts a move again after one was given up as failed: the
|
|
295
|
+
// same domain would likely fail the same way.
|
|
296
|
+
let lettings = 0;
|
|
297
|
+
let retryAt = 0;
|
|
298
|
+
const abortMove = (why) => {
|
|
299
|
+
if (move !== null) {
|
|
300
|
+
move = null;
|
|
301
|
+
moves.aborted[why]++;
|
|
302
|
+
if (why !== "superseded") {
|
|
303
|
+
retryAt = lettings + RETRY_Lettings;
|
|
304
|
+
}
|
|
305
|
+
if (why === "spare" || why === "refused") {
|
|
306
|
+
failedMoves++;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
};
|
|
237
310
|
const prepare = () => {
|
|
238
311
|
if (spare === null && !making) {
|
|
239
312
|
making = true;
|
|
240
|
-
instantiate(module
|
|
313
|
+
instantiate(module).then((made) => {
|
|
241
314
|
spare = made;
|
|
242
315
|
making = false;
|
|
243
316
|
}, () => {
|
|
@@ -246,29 +319,10 @@ export async function createCodegen(module) {
|
|
|
246
319
|
}
|
|
247
320
|
};
|
|
248
321
|
// The domain rebuilt in `into`, a new instance, from what this one saves
|
|
249
|
-
// of it and its live ABCs' bytes, read where they are: whether it was.
|
|
250
|
-
// ABCs' copies in it are its garbage once parsed, collected every few
|
|
251
|
-
// megabytes.
|
|
322
|
+
// of it and its live ABCs' bytes, read where they are: whether it was.
|
|
252
323
|
const rebuiltIn = (into) => {
|
|
253
|
-
const saved =
|
|
254
|
-
|
|
255
|
-
return false;
|
|
256
|
-
}
|
|
257
|
-
into.__collect();
|
|
258
|
-
let copied = 0;
|
|
259
|
-
for (let i = 0; i < added; i++) {
|
|
260
|
-
const at = wasm.domainSavedAbcBytes(i);
|
|
261
|
-
const length = wasm.outputLength();
|
|
262
|
-
if (at !== 0 && into.domainLoadAbc(i, new Uint8Array(wasm.memory.buffer, at, length)) !== 0) {
|
|
263
|
-
return false;
|
|
264
|
-
}
|
|
265
|
-
copied += length;
|
|
266
|
-
if (copied >= COPIED_BETWEEN_COLLECTIONS) {
|
|
267
|
-
into.__collect();
|
|
268
|
-
copied = 0;
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
return into.domainLoadRebuild() && rebuild(into);
|
|
324
|
+
const saved = saveCheckpoint();
|
|
325
|
+
return loadCheckpoint(into, saved, false) && rebuild(into, saved);
|
|
272
326
|
};
|
|
273
327
|
// The domain rebuilt in the spare instance, which then takes this one's
|
|
274
328
|
// place, whose memory goes with it: whether it was. One that failed, or
|
|
@@ -289,29 +343,454 @@ export async function createCodegen(module) {
|
|
|
289
343
|
}
|
|
290
344
|
if (!done) {
|
|
291
345
|
failedMoves++;
|
|
292
|
-
size = wasm.memory.buffer.byteLength;
|
|
293
346
|
return false;
|
|
294
347
|
}
|
|
295
348
|
// The held SWFs' copies were the old instance's (see hold).
|
|
296
349
|
holds.length = 0;
|
|
297
350
|
wasm = into;
|
|
298
|
-
size = wasm.memory.buffer.byteLength;
|
|
299
|
-
calls = 0;
|
|
300
351
|
return true;
|
|
301
352
|
};
|
|
302
|
-
const
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
353
|
+
const journalDomains = new Map();
|
|
354
|
+
const journal = new Set();
|
|
355
|
+
// What the checkpoint and the journal hold, their bytes and strings' units twice.
|
|
356
|
+
const recoveryBytes = () => {
|
|
357
|
+
let bytes = 0;
|
|
358
|
+
if (checkpoint) {
|
|
359
|
+
bytes += checkpoint.state.byteLength;
|
|
360
|
+
for (const abc of checkpoint.abcs.values()) {
|
|
361
|
+
bytes += abc.byteLength;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
for (const entry of journal) {
|
|
365
|
+
switch (entry.kind) {
|
|
366
|
+
case "add":
|
|
367
|
+
bytes += entry.abc.byteLength;
|
|
368
|
+
break;
|
|
369
|
+
case "restore":
|
|
370
|
+
bytes += entry.bytes.byteLength;
|
|
371
|
+
break;
|
|
372
|
+
case "log":
|
|
373
|
+
bytes += entry.words.byteLength;
|
|
374
|
+
break;
|
|
375
|
+
case "found":
|
|
376
|
+
bytes += (entry.definition.uri.length + entry.definition.name.length) * 2;
|
|
377
|
+
break;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
return bytes;
|
|
381
|
+
};
|
|
382
|
+
const restores = new Map();
|
|
383
|
+
const abcDomains = [];
|
|
384
|
+
// Each ABC's length, by its index, which a replay of its verification takes time in proportion to.
|
|
385
|
+
const abcLengths = [];
|
|
386
|
+
let replayable = true;
|
|
387
|
+
const journaled = (entry, owner = 0) => {
|
|
388
|
+
const id = owner | 0;
|
|
389
|
+
if (id !== 0) {
|
|
390
|
+
const domain = journalDomains.get(id);
|
|
391
|
+
if (!domain) {
|
|
392
|
+
// The native call ignored a domain that was dropped or never created.
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
domain.entries ??= [];
|
|
396
|
+
domain.entries.push(entry);
|
|
397
|
+
}
|
|
398
|
+
journal.add(entry);
|
|
399
|
+
move?.entries.push(entry);
|
|
400
|
+
};
|
|
401
|
+
// The target is absolute so dropping a child preserves work its compiles did on its ancestors.
|
|
402
|
+
const journalLog = (mark) => {
|
|
403
|
+
const at = wasm.journalBytes(mark);
|
|
404
|
+
if (at === 0) {
|
|
405
|
+
return false;
|
|
406
|
+
}
|
|
407
|
+
const count = wasm.outputLength() / 4;
|
|
408
|
+
if (!count) {
|
|
409
|
+
return true;
|
|
410
|
+
}
|
|
411
|
+
// Packed, a run of entries of one domain's ABCs to a journal entry, so
|
|
412
|
+
// that a long session's logs take their words and little more.
|
|
413
|
+
const bytes = view(at, count * 4);
|
|
414
|
+
const words = new Uint32Array(bytes.buffer, bytes.byteOffset, count).slice();
|
|
415
|
+
let start = 0;
|
|
416
|
+
for (let i = 4; i <= count; i += 4) {
|
|
417
|
+
const owner = abcDomains[words[start + 1]];
|
|
418
|
+
if (i === count || abcDomains[words[i + 1]] !== owner) {
|
|
419
|
+
journaled({ kind: "log", words: words.subarray(start, i) }, owner);
|
|
420
|
+
start = i;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
return true;
|
|
424
|
+
};
|
|
425
|
+
function recordChanges(call) {
|
|
426
|
+
if (!replayable) {
|
|
427
|
+
return call();
|
|
428
|
+
}
|
|
429
|
+
const mark = wasm.domainLogMark();
|
|
430
|
+
let result;
|
|
431
|
+
try {
|
|
432
|
+
result = call();
|
|
433
|
+
}
|
|
434
|
+
catch (error) {
|
|
435
|
+
// A stepped verifier can have logged its start without finishing. Replaying
|
|
436
|
+
// that entry would resolve more of the domain than the interrupted compile did.
|
|
437
|
+
if (!(error instanceof CodegenPanic)) {
|
|
438
|
+
try {
|
|
439
|
+
replayable = wasm.domainLogMark() === mark;
|
|
440
|
+
}
|
|
441
|
+
catch {
|
|
442
|
+
replayable = false;
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
throw error;
|
|
446
|
+
}
|
|
447
|
+
// A missing suffix makes recovery unsafe until the next successful
|
|
448
|
+
// checkpoint. A panic reading it leaves the journal as it was before the
|
|
449
|
+
// compile, which recovery then gives back, the compile's changes with
|
|
450
|
+
// the dead instance: only another failure suspends recovery.
|
|
451
|
+
try {
|
|
452
|
+
replayable = journalLog(mark);
|
|
453
|
+
}
|
|
454
|
+
catch (error) {
|
|
455
|
+
if (!(error instanceof CodegenPanic)) {
|
|
456
|
+
replayable = false;
|
|
457
|
+
}
|
|
458
|
+
throw error;
|
|
308
459
|
}
|
|
309
460
|
return result;
|
|
461
|
+
}
|
|
462
|
+
// Whether application domain `d` is `own` or one of its ancestors.
|
|
463
|
+
const inChain = (d, own) => {
|
|
464
|
+
for (let at = journalDomains.get(own | 0); at; at = at.parent) {
|
|
465
|
+
if (at.id === d) {
|
|
466
|
+
return true;
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
return d === 0 && own >= 0;
|
|
470
|
+
};
|
|
471
|
+
const forgetDropped = (appDomain) => {
|
|
472
|
+
const root = journalDomains.get(appDomain | 0);
|
|
473
|
+
if (!root) {
|
|
474
|
+
return;
|
|
475
|
+
}
|
|
476
|
+
root.parent?.children?.delete(root);
|
|
477
|
+
const pending = [root];
|
|
478
|
+
for (const domain of pending) {
|
|
479
|
+
if (domain.children) {
|
|
480
|
+
for (const child of domain.children) {
|
|
481
|
+
pending.push(child);
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
if (domain.entries) {
|
|
485
|
+
for (const entry of domain.entries) {
|
|
486
|
+
if (entry.kind === "add" && entry.index >= 0) {
|
|
487
|
+
entry.abc = EMPTY_ABC;
|
|
488
|
+
}
|
|
489
|
+
else {
|
|
490
|
+
journal.delete(entry);
|
|
491
|
+
if (entry.kind === "restore") {
|
|
492
|
+
restores.delete(entry.index);
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
journalDomains.delete(domain.id);
|
|
498
|
+
}
|
|
499
|
+
};
|
|
500
|
+
// Replay `entry` into `into`: whether it started a rebuild there, which
|
|
501
|
+
// only a revival `stepped` does (domainRevive's 2), for domainRebuildStep
|
|
502
|
+
// to finish before the next. Any refusal is a divergence, and throws.
|
|
503
|
+
const replayOne = (into, entry, stepped) => {
|
|
504
|
+
switch (entry.kind) {
|
|
505
|
+
case "add":
|
|
506
|
+
if (into.domainAdd(entry.abc, entry.builtin, entry.appDomain) !== entry.error) {
|
|
507
|
+
throw new Error("a journaled ABC did not replay");
|
|
508
|
+
}
|
|
509
|
+
break;
|
|
510
|
+
case "child":
|
|
511
|
+
into.domainChild(entry.parent);
|
|
512
|
+
break;
|
|
513
|
+
case "drop":
|
|
514
|
+
into.domainDrop(entry.appDomain);
|
|
515
|
+
break;
|
|
516
|
+
case "evict":
|
|
517
|
+
into.domainEvict(entry.appDomain);
|
|
518
|
+
break;
|
|
519
|
+
case "restore":
|
|
520
|
+
if (into.domainRestore(entry.index, entry.bytes) !== 0) {
|
|
521
|
+
throw new Error("a journaled ABC did not restore");
|
|
522
|
+
}
|
|
523
|
+
break;
|
|
524
|
+
case "revive": {
|
|
525
|
+
// 1, or 2 stepped, where `into` rebuilt since the domain was
|
|
526
|
+
// evicted, as a move's spare has, or recovery from the checkpoint
|
|
527
|
+
// a move left: the restores journaled before give it the ABCs that
|
|
528
|
+
// rebuild let go of, and it links them again.
|
|
529
|
+
const revived = into.domainRevive(entry.appDomain, stepped);
|
|
530
|
+
if (revived < 0) {
|
|
531
|
+
throw new Error("a journaled domain did not revive");
|
|
532
|
+
}
|
|
533
|
+
return revived === 2;
|
|
534
|
+
}
|
|
535
|
+
case "found": {
|
|
536
|
+
const d = entry.definition;
|
|
537
|
+
into.domainFound(d.domain, d.nsKind, d.uri, d.name, d.abc, d.asType);
|
|
538
|
+
break;
|
|
539
|
+
}
|
|
540
|
+
case "log": {
|
|
541
|
+
const w = entry.words;
|
|
542
|
+
for (let i = 0; i < w.length; i += 4) {
|
|
543
|
+
if (!into.replayEntry(w[i], w[i + 1], w[i + 2], w[i + 3])) {
|
|
544
|
+
throw new Error(`a compile's log did not replay for ABC ${w[i + 1]}`);
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
break;
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
return false;
|
|
551
|
+
};
|
|
552
|
+
const replayInto = (into) => {
|
|
553
|
+
if (checkpoint) {
|
|
554
|
+
if (!loadCheckpoint(into, checkpoint, true) || !finishRebuild(into)) {
|
|
555
|
+
throw new Error("the compiler checkpoint did not restore");
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
else {
|
|
559
|
+
into.domainReset(apiVersion);
|
|
560
|
+
}
|
|
561
|
+
// Any refusal is a divergence: recovery fails, and the domain is lost.
|
|
562
|
+
for (const entry of journal) {
|
|
563
|
+
replayOne(into, entry, false);
|
|
564
|
+
}
|
|
565
|
+
};
|
|
566
|
+
const recover = () => {
|
|
567
|
+
// A move's spare was to catch up from the journal this replaces.
|
|
568
|
+
abortMove("superseded");
|
|
569
|
+
if (!replayable) {
|
|
570
|
+
return false;
|
|
571
|
+
}
|
|
572
|
+
let into;
|
|
573
|
+
try {
|
|
574
|
+
into = spare ?? recoveryInstance(module);
|
|
575
|
+
spare = null;
|
|
576
|
+
replayInto(into);
|
|
577
|
+
}
|
|
578
|
+
catch {
|
|
579
|
+
replayable = false;
|
|
580
|
+
return false;
|
|
581
|
+
}
|
|
582
|
+
// Pruning dropped domains can reuse a native log position. Invalidate
|
|
583
|
+
// outstanding cache reads even when replay produces the same native revision.
|
|
584
|
+
revisionEpoch++;
|
|
585
|
+
// The held SWFs' copies, and the key chains' digests, were the dead instance's.
|
|
586
|
+
holds.length = 0;
|
|
587
|
+
chains.clear();
|
|
588
|
+
wasm = into;
|
|
589
|
+
return true;
|
|
590
|
+
};
|
|
591
|
+
// A move compactStep makes a part at a time (see Move): begun from a
|
|
592
|
+
// snapshot of this instance, as a move by compact is, and never reading
|
|
593
|
+
// this instance again, while the host goes on with it, each call it
|
|
594
|
+
// makes journaled and replayed into the spare once the spare has rebuilt.
|
|
595
|
+
const startMove = (into) => {
|
|
596
|
+
let saved;
|
|
597
|
+
try {
|
|
598
|
+
saved = saveCheckpoint();
|
|
599
|
+
}
|
|
600
|
+
catch (error) {
|
|
601
|
+
if (error instanceof CodegenOutOfMemory) {
|
|
602
|
+
spare = into;
|
|
603
|
+
return false;
|
|
604
|
+
}
|
|
605
|
+
throw error;
|
|
606
|
+
}
|
|
607
|
+
// Parts that block the host for little: a part's time is its garbage's, but for one large method's verification.
|
|
608
|
+
into.domainRebuildBudget(MOVE_PartBudget);
|
|
609
|
+
move = {
|
|
610
|
+
into,
|
|
611
|
+
saved,
|
|
612
|
+
entries: [],
|
|
613
|
+
replayed: 0,
|
|
614
|
+
word: 0,
|
|
615
|
+
restored: new Map(),
|
|
616
|
+
abcs: null,
|
|
617
|
+
phase: "load",
|
|
618
|
+
catchUp: 0,
|
|
619
|
+
};
|
|
620
|
+
moves.started++;
|
|
621
|
+
return true;
|
|
622
|
+
};
|
|
623
|
+
// The spare, rebuilt and caught up, takes this instance's place, as a
|
|
624
|
+
// recovery's does, and the snapshot it rebuilt is the checkpoint, the
|
|
625
|
+
// calls since it the journal.
|
|
626
|
+
const swapMove = (m) => {
|
|
627
|
+
move = null;
|
|
628
|
+
moves.completed++;
|
|
629
|
+
const kept = m.entries.filter((entry) => journal.has(entry));
|
|
630
|
+
moves.lastCaughtUp = kept.length;
|
|
631
|
+
moves.lastSavedBytes = m.saved.state.length;
|
|
632
|
+
for (const abc of m.saved.abcs.values()) {
|
|
633
|
+
moves.lastSavedBytes += abc.length;
|
|
634
|
+
}
|
|
635
|
+
journal.clear();
|
|
636
|
+
restores.clear();
|
|
637
|
+
for (const entry of kept) {
|
|
638
|
+
journal.add(entry);
|
|
639
|
+
if (entry.kind === "restore" && !restores.has(entry.index)) {
|
|
640
|
+
restores.set(entry.index, entry);
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
for (const domain of journalDomains.values()) {
|
|
644
|
+
const own = domain.entries?.filter((entry) => journal.has(entry));
|
|
645
|
+
domain.entries = own?.length ? own : undefined;
|
|
646
|
+
}
|
|
647
|
+
checkpoint = m.saved;
|
|
648
|
+
moveRefused = false;
|
|
649
|
+
revisionEpoch++;
|
|
650
|
+
holds.length = 0;
|
|
651
|
+
chains.clear();
|
|
652
|
+
wasm = m.into;
|
|
653
|
+
};
|
|
654
|
+
// One part of the move: a call into the spare, or the swap. Whatever the
|
|
655
|
+
// spare throws, a panic or running out of memory included, gives the
|
|
656
|
+
// move up, never this instance, which goes on as it was.
|
|
657
|
+
const moveUnit = (m) => {
|
|
658
|
+
if (!replayable) {
|
|
659
|
+
abortMove("unjournaled");
|
|
660
|
+
return "none";
|
|
661
|
+
}
|
|
662
|
+
const into = m.into;
|
|
663
|
+
try {
|
|
664
|
+
switch (m.phase) {
|
|
665
|
+
case "load":
|
|
666
|
+
if (!into.domainLoad(m.saved.state)) {
|
|
667
|
+
moveRefused = true;
|
|
668
|
+
abortMove("refused");
|
|
669
|
+
return "none";
|
|
670
|
+
}
|
|
671
|
+
m.abcs = m.saved.abcs.entries();
|
|
672
|
+
m.phase = "abcs";
|
|
673
|
+
return "moving";
|
|
674
|
+
case "abcs": {
|
|
675
|
+
const next = m.abcs?.next();
|
|
676
|
+
if (next && !next.done) {
|
|
677
|
+
if (into.domainLoadAbc(next.value[0], next.value[1]) !== 0) {
|
|
678
|
+
abortMove("refused");
|
|
679
|
+
return "none";
|
|
680
|
+
}
|
|
681
|
+
return "moving";
|
|
682
|
+
}
|
|
683
|
+
m.abcs = null;
|
|
684
|
+
if (!into.domainLoadRebuild()) {
|
|
685
|
+
abortMove("refused");
|
|
686
|
+
return "none";
|
|
687
|
+
}
|
|
688
|
+
m.phase = "rebuild";
|
|
689
|
+
return "moving";
|
|
690
|
+
}
|
|
691
|
+
case "rebuild": {
|
|
692
|
+
const step = into.domainRebuildStep();
|
|
693
|
+
if (step < 0) {
|
|
694
|
+
abortMove("refused");
|
|
695
|
+
return "none";
|
|
696
|
+
}
|
|
697
|
+
if (step > 0) {
|
|
698
|
+
m.phase = "catch up";
|
|
699
|
+
}
|
|
700
|
+
return "moving";
|
|
701
|
+
}
|
|
702
|
+
case "catch up": {
|
|
703
|
+
// Entries of domains dropped since are out of the journal (see
|
|
704
|
+
// forgetDropped), but one half replayed is finished.
|
|
705
|
+
while (m.word === 0 &&
|
|
706
|
+
m.replayed < m.entries.length &&
|
|
707
|
+
!journal.has(m.entries[m.replayed])) {
|
|
708
|
+
m.replayed++;
|
|
709
|
+
}
|
|
710
|
+
if (m.replayed === m.entries.length) {
|
|
711
|
+
swapMove(m);
|
|
712
|
+
return "done";
|
|
713
|
+
}
|
|
714
|
+
// A compile's log a log entry a part, a run of them taking long.
|
|
715
|
+
const entry = m.entries[m.replayed];
|
|
716
|
+
if (entry.kind === "log") {
|
|
717
|
+
const w = entry.words;
|
|
718
|
+
const i = m.word;
|
|
719
|
+
if (!into.replayEntry(w[i], w[i + 1], w[i + 2], w[i + 3])) {
|
|
720
|
+
abortMove("refused");
|
|
721
|
+
return "none";
|
|
722
|
+
}
|
|
723
|
+
m.word = i + 4 < w.length ? i + 4 : 0;
|
|
724
|
+
if (m.word === 0) {
|
|
725
|
+
m.replayed++;
|
|
726
|
+
}
|
|
727
|
+
return "moving";
|
|
728
|
+
}
|
|
729
|
+
m.replayed++;
|
|
730
|
+
// A revival that rebuilds the spare: catch-up starts over after it.
|
|
731
|
+
if (replayOne(into, entry, true)) {
|
|
732
|
+
m.phase = "rebuild";
|
|
733
|
+
m.catchUp = 0;
|
|
734
|
+
}
|
|
735
|
+
return "moving";
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
catch (error) {
|
|
740
|
+
abortMove(error instanceof CodegenPanic || error instanceof CodegenOutOfMemory ? "spare" : "refused");
|
|
741
|
+
return "none";
|
|
742
|
+
}
|
|
743
|
+
};
|
|
744
|
+
// Whether the move's next part replays a call that takes long in one:
|
|
745
|
+
// a compile's verification of a large ABC, as long as the compile
|
|
746
|
+
// verified it for, or the add of a large ABC, which parses and links it.
|
|
747
|
+
const longNext = (m) => {
|
|
748
|
+
const entry = m.phase === "catch up" ? m.entries[m.replayed] : undefined;
|
|
749
|
+
if (entry?.kind === "add") {
|
|
750
|
+
return entry.abc.length > LARGE_Add;
|
|
751
|
+
}
|
|
752
|
+
return (entry?.kind === "log" &&
|
|
753
|
+
entry.words[m.word] === LOG_Verify &&
|
|
754
|
+
(abcLengths[entry.words[m.word + 1]] ?? 0) > LARGE_Verify);
|
|
755
|
+
};
|
|
756
|
+
// What compactStep starts, as compact decides, but that a large domain
|
|
757
|
+
// moves rather than rebuild in place, which would take one call.
|
|
758
|
+
const beginCompaction = () => {
|
|
759
|
+
if (spare === null && !making && wasm.domainWantsCompact(WANT_Spare)) {
|
|
760
|
+
prepare();
|
|
761
|
+
}
|
|
762
|
+
const wantsMove = wasm.domainWantsCompact(WANT_Move);
|
|
763
|
+
const beside = spare !== null || making;
|
|
764
|
+
const inPlace = !wantsMove && wasm.domainWantsCompact(beside ? WANT_InPlaceBesideSpare : WANT_InPlace);
|
|
765
|
+
if (!wantsMove && !inPlace) {
|
|
766
|
+
return "none";
|
|
767
|
+
}
|
|
768
|
+
if (inPlace && (moveRefused || Number(wasm.domainUsage().split(" ")[0]) < IN_PLACE_Live)) {
|
|
769
|
+
return wasm.domainCompact(true) && rebuild(wasm, null, true) ? "done" : "none";
|
|
770
|
+
}
|
|
771
|
+
// A spare could not catch up from the journal: compacted in one call, as compact does.
|
|
772
|
+
if (!replayable) {
|
|
773
|
+
return api.compact() ? "done" : "none";
|
|
774
|
+
}
|
|
775
|
+
// Backed off after a move that failed.
|
|
776
|
+
if (lettings < retryAt) {
|
|
777
|
+
return "none";
|
|
778
|
+
}
|
|
779
|
+
const into = spare;
|
|
780
|
+
if (into === null) {
|
|
781
|
+
// Called again once the spare is made, as instantiating is asynchronous.
|
|
782
|
+
prepare();
|
|
783
|
+
return making ? "moving" : "none";
|
|
784
|
+
}
|
|
785
|
+
spare = null;
|
|
786
|
+
return startMove(into) ? "moving" : "none";
|
|
310
787
|
};
|
|
311
|
-
|
|
788
|
+
// Whether the domain was lost to a panic recovery could not undo (see fresh).
|
|
789
|
+
let lost = false;
|
|
790
|
+
const api = {
|
|
312
791
|
identity,
|
|
313
792
|
abcVersion(abc) {
|
|
314
|
-
const packed =
|
|
793
|
+
const packed = wasm.abcVersion(abc);
|
|
315
794
|
return packed < 0 ? null : { major: packed >>> 16, minor: packed & 0xffff };
|
|
316
795
|
},
|
|
317
796
|
get epoch() {
|
|
@@ -319,114 +798,253 @@ export async function createCodegen(module) {
|
|
|
319
798
|
},
|
|
320
799
|
reset(version = API_VERSION) {
|
|
321
800
|
checkApiVersion(version);
|
|
801
|
+
abortMove("superseded");
|
|
802
|
+
moveRefused = false;
|
|
803
|
+
retryAt = 0;
|
|
804
|
+
wasm.domainReset(version);
|
|
322
805
|
added = 0;
|
|
323
806
|
epoch++;
|
|
807
|
+
revisionEpoch++;
|
|
808
|
+
journalDomains.clear();
|
|
324
809
|
chains.clear();
|
|
325
810
|
apiVersion = version;
|
|
326
|
-
|
|
811
|
+
journal.clear();
|
|
812
|
+
restores.clear();
|
|
813
|
+
abcDomains.length = 0;
|
|
814
|
+
abcLengths.length = 0;
|
|
815
|
+
replayable = true;
|
|
816
|
+
checkpoint = null;
|
|
327
817
|
},
|
|
328
818
|
get apiVersion() {
|
|
329
819
|
return apiVersion;
|
|
330
820
|
},
|
|
331
821
|
add(abc, builtin = false, appDomain = 0) {
|
|
332
|
-
const
|
|
822
|
+
const bytes = new Uint8Array(abc);
|
|
823
|
+
const error = wasm.domainAdd(bytes, builtin, appDomain);
|
|
333
824
|
if (error < 0) {
|
|
334
825
|
throw new Error(`application domain ${appDomain}: never made, or dropped`);
|
|
335
826
|
}
|
|
827
|
+
if (error === 0 || wasm.lastAddLinked()) {
|
|
828
|
+
if (error === 0) {
|
|
829
|
+
abcDomains.push(appDomain);
|
|
830
|
+
abcLengths.push(bytes.length);
|
|
831
|
+
}
|
|
832
|
+
journaled({
|
|
833
|
+
kind: "add",
|
|
834
|
+
abc: bytes,
|
|
835
|
+
builtin,
|
|
836
|
+
appDomain,
|
|
837
|
+
index: error === 0 ? added : -1,
|
|
838
|
+
error,
|
|
839
|
+
}, appDomain);
|
|
840
|
+
}
|
|
336
841
|
if (error === 0) {
|
|
337
842
|
added++;
|
|
338
843
|
}
|
|
339
844
|
return error;
|
|
340
845
|
},
|
|
341
846
|
childDomain(parent) {
|
|
342
|
-
const domain =
|
|
847
|
+
const domain = wasm.domainChild(parent);
|
|
343
848
|
if (domain < 0) {
|
|
344
849
|
throw new Error(`application domain ${parent}: never made, or dropped`);
|
|
345
850
|
}
|
|
851
|
+
const owner = journalDomains.get(parent | 0);
|
|
852
|
+
const child = { id: domain, parent: owner };
|
|
853
|
+
if (owner) {
|
|
854
|
+
owner.children ??= new Set();
|
|
855
|
+
owner.children.add(child);
|
|
856
|
+
}
|
|
857
|
+
journalDomains.set(domain, child);
|
|
858
|
+
journaled({ kind: "child", parent });
|
|
346
859
|
return domain;
|
|
347
860
|
},
|
|
348
861
|
dropDomain(appDomain, of = epoch) {
|
|
349
862
|
if (of === epoch) {
|
|
350
|
-
|
|
863
|
+
lettings++;
|
|
864
|
+
wasm.domainDrop(appDomain);
|
|
865
|
+
journaled({ kind: "drop", appDomain });
|
|
866
|
+
forgetDropped(appDomain);
|
|
351
867
|
chains.forget((d) => wasm.domainState(d) === 2);
|
|
352
868
|
}
|
|
353
869
|
},
|
|
354
870
|
evictDomain(appDomain) {
|
|
355
|
-
|
|
871
|
+
lettings++;
|
|
872
|
+
wasm.domainEvict(appDomain);
|
|
873
|
+
journaled({ kind: "evict", appDomain });
|
|
356
874
|
},
|
|
357
875
|
usage() {
|
|
358
|
-
const [live, dead, memory] =
|
|
359
|
-
return {
|
|
876
|
+
const [live, dead, memory] = wasm.domainUsage().split(" ").map(Number);
|
|
877
|
+
return {
|
|
878
|
+
live,
|
|
879
|
+
dead,
|
|
880
|
+
memory,
|
|
881
|
+
failedMoves,
|
|
882
|
+
moves: { ...moves, aborted: { ...moves.aborted } },
|
|
883
|
+
recovery: recoveryBytes(),
|
|
884
|
+
};
|
|
360
885
|
},
|
|
361
886
|
memoryBytes() {
|
|
362
887
|
return wasm.memory.buffer.byteLength;
|
|
363
888
|
},
|
|
364
889
|
compact() {
|
|
890
|
+
// A move compactStep has under way, finished here.
|
|
891
|
+
const m = move;
|
|
892
|
+
if (m !== null) {
|
|
893
|
+
let progress = moveUnit(m);
|
|
894
|
+
while (progress === "moving") {
|
|
895
|
+
progress = moveUnit(m);
|
|
896
|
+
}
|
|
897
|
+
if (progress === "done") {
|
|
898
|
+
return true;
|
|
899
|
+
}
|
|
900
|
+
}
|
|
365
901
|
// Done in the spare instance, once there is one and that would take
|
|
366
902
|
// less memory, or in place would not fit; else in place, where the
|
|
367
903
|
// new tables are made while the old are live, only where they fit
|
|
368
|
-
// and, beside a spare made or being made, a move would not do better
|
|
369
|
-
|
|
370
|
-
// it is done, before the next call's allocations would grow memory
|
|
371
|
-
// past them.
|
|
372
|
-
if (spare === null && !making && collected(wasm.domainWantsCompact(WANT_Spare))) {
|
|
904
|
+
// and, beside a spare made or being made, a move would not do better.
|
|
905
|
+
if (spare === null && !making && wasm.domainWantsCompact(WANT_Spare)) {
|
|
373
906
|
prepare();
|
|
374
907
|
}
|
|
375
|
-
if (spare !== null &&
|
|
376
|
-
wasm.__collect();
|
|
908
|
+
if (spare !== null && wasm.domainWantsCompact(WANT_Move)) {
|
|
377
909
|
if (moved()) {
|
|
378
910
|
return true;
|
|
379
911
|
}
|
|
380
912
|
}
|
|
381
913
|
const beside = spare !== null || making;
|
|
382
|
-
if (!
|
|
914
|
+
if (!wasm.domainWantsCompact(beside ? WANT_InPlaceBesideSpare : WANT_InPlace)) {
|
|
383
915
|
return false;
|
|
384
916
|
}
|
|
385
|
-
wasm.
|
|
386
|
-
|
|
387
|
-
|
|
917
|
+
return wasm.domainCompact(true) && rebuild(wasm, null, true);
|
|
918
|
+
},
|
|
919
|
+
compactStep(deadline) {
|
|
920
|
+
let m = move;
|
|
921
|
+
if (m === null) {
|
|
922
|
+
const begun = beginCompaction();
|
|
923
|
+
if (begun !== "moving" || move === null) {
|
|
924
|
+
return begun;
|
|
925
|
+
}
|
|
926
|
+
m = move;
|
|
927
|
+
}
|
|
928
|
+
// Catch-up chases what the host goes on doing. Past CATCH_UP_Slices
|
|
929
|
+
// slices it goes on CATCH_UP_Overrun past the deadline, a long part
|
|
930
|
+
// still starting a slice of its own; past twice as many, it finishes
|
|
931
|
+
// in this slice, which has only what came in since the last to
|
|
932
|
+
// replay (a revival's rebuild still a part at a time).
|
|
933
|
+
let slices = 0;
|
|
934
|
+
if (m.phase === "catch up") {
|
|
935
|
+
slices = ++m.catchUp;
|
|
936
|
+
}
|
|
937
|
+
for (;;) {
|
|
938
|
+
const progress = moveUnit(m);
|
|
939
|
+
if (progress !== "moving") {
|
|
940
|
+
return progress;
|
|
941
|
+
}
|
|
942
|
+
const catchingUp = m.phase === "catch up";
|
|
943
|
+
if (catchingUp && slices > CATCH_UP_Slices * 2) {
|
|
944
|
+
continue;
|
|
945
|
+
}
|
|
946
|
+
// A part that takes long in one call starts a slice of its own,
|
|
947
|
+
// so that it adds to no other part's time.
|
|
948
|
+
if (longNext(m)) {
|
|
949
|
+
return "moving";
|
|
950
|
+
}
|
|
951
|
+
const overrun = catchingUp && slices > CATCH_UP_Slices ? CATCH_UP_Overrun : 0;
|
|
952
|
+
if (deadline.timeRemaining() <= -overrun) {
|
|
953
|
+
return "moving";
|
|
954
|
+
}
|
|
955
|
+
}
|
|
388
956
|
},
|
|
389
957
|
isLive(appDomain) {
|
|
390
|
-
return
|
|
958
|
+
return wasm.domainState(appDomain) === 0;
|
|
391
959
|
},
|
|
392
960
|
reviveDomain(appDomain, abcs) {
|
|
961
|
+
// A move's spare lets go of the ABCs of the domains evicted before its
|
|
962
|
+
// snapshot, and before any rebuild a replayed revival makes in it,
|
|
963
|
+
// which this instance may still hold: restores journaled before the
|
|
964
|
+
// revival give each evicted ABC back to it (see replayOne).
|
|
965
|
+
const evictedGiven = move === null
|
|
966
|
+
? []
|
|
967
|
+
: [...abcs.keys()].filter((index) => Number.isInteger(index) &&
|
|
968
|
+
index >= 0 &&
|
|
969
|
+
index < abcDomains.length &&
|
|
970
|
+
wasm.domainState(abcDomains[index]) === 1);
|
|
393
971
|
// Its ABCs' bytes are copied into the compiler only if a rebuild let go of them.
|
|
394
|
-
let revived =
|
|
972
|
+
let revived = wasm.domainRevive(appDomain, true);
|
|
395
973
|
if (revived < 0) {
|
|
396
974
|
for (const [index, bytes] of abcs) {
|
|
397
|
-
const
|
|
975
|
+
const owned = new Uint8Array(bytes);
|
|
976
|
+
const error = wasm.domainRestore(index, owned);
|
|
398
977
|
if (error !== 0) {
|
|
399
978
|
throw new Error(`ABC ${index}: not of an evicted domain, or rejected (${error})`);
|
|
400
979
|
}
|
|
980
|
+
// One restore an ABC, the first: a revival retried gives its
|
|
981
|
+
// bytes again, which the compiler, holding them, does not take.
|
|
982
|
+
if (!restores.has(index)) {
|
|
983
|
+
const entry = { kind: "restore", index, bytes: owned };
|
|
984
|
+
restores.set(index, entry);
|
|
985
|
+
journaled(entry, abcDomains[index]);
|
|
986
|
+
}
|
|
401
987
|
}
|
|
402
|
-
revived =
|
|
988
|
+
revived = wasm.domainRevive(appDomain, true);
|
|
403
989
|
}
|
|
404
990
|
// Revived by a rebuild in place, mid-load: a compaction moves it later.
|
|
405
|
-
|
|
991
|
+
const done = revived === 0 || (revived > 0 && rebuild());
|
|
992
|
+
if (revived === 0) {
|
|
993
|
+
// Owned by the root, not by the domain: a revival the journal keeps
|
|
994
|
+
// needs them though the domain is dropped later (a drop forgets the
|
|
995
|
+
// domain's own entries, see forgetDropped), until the next checkpoint.
|
|
996
|
+
if (move !== null) {
|
|
997
|
+
for (const index of evictedGiven) {
|
|
998
|
+
const given = abcs.get(index);
|
|
999
|
+
if (given !== undefined) {
|
|
1000
|
+
let bytes = move.restored.get(index);
|
|
1001
|
+
if (bytes === undefined) {
|
|
1002
|
+
bytes = new Uint8Array(given);
|
|
1003
|
+
move.restored.set(index, bytes);
|
|
1004
|
+
}
|
|
1005
|
+
journaled({ kind: "restore", index, bytes });
|
|
1006
|
+
}
|
|
1007
|
+
}
|
|
1008
|
+
}
|
|
1009
|
+
journaled({ kind: "revive", appDomain });
|
|
1010
|
+
}
|
|
1011
|
+
if (!done) {
|
|
406
1012
|
throw new Error(`application domain ${appDomain}: not evicted, its parent not live, or an ABC of it not given`);
|
|
407
1013
|
}
|
|
408
1014
|
},
|
|
409
|
-
found(
|
|
410
|
-
|
|
1015
|
+
found({ domain, nsKind, uri, name, abc, asType }) {
|
|
1016
|
+
wasm.domainFound(domain, nsKind, uri, name, abc, asType);
|
|
1017
|
+
// A finding of an ABC outside the finder's chain, which the player
|
|
1018
|
+
// never reports, is not journaled: its replay can part from the
|
|
1019
|
+
// compile (the compiler takes it unchecked).
|
|
1020
|
+
if (inChain(abcDomains[abc] ?? -1, domain)) {
|
|
1021
|
+
journaled({ kind: "found", definition: { domain, nsKind, uri, name, abc, asType } }, domain);
|
|
1022
|
+
}
|
|
1023
|
+
else if (move !== null && wasm.domainState(domain) >= 0 && wasm.domainState(domain) < 2) {
|
|
1024
|
+
// The spare could part from this instance by it; one of a domain
|
|
1025
|
+
// dropped or never made the compiler ignored.
|
|
1026
|
+
abortMove("unjournaled");
|
|
1027
|
+
}
|
|
411
1028
|
},
|
|
412
1029
|
compile(hashes = [], index = -1) {
|
|
413
1030
|
last("compile");
|
|
414
|
-
// Each part a call of its own,
|
|
415
|
-
// is gone before its entries are written.
|
|
416
|
-
const module =
|
|
417
|
-
const sourceMap =
|
|
418
|
-
const entries = parseEntries(
|
|
1031
|
+
// Each part a call of its own, whose arena the next gives back: the
|
|
1032
|
+
// module's garbage is gone before its entries are written.
|
|
1033
|
+
const module = compiled(hashes, index, true);
|
|
1034
|
+
const sourceMap = wasm.domainSourceMap();
|
|
1035
|
+
const entries = parseEntries(wasm.domainModuleEntries());
|
|
419
1036
|
return { module, sourceMap, entries };
|
|
420
1037
|
},
|
|
421
1038
|
compileModule(hashes = [], index = -1) {
|
|
422
1039
|
last("compileModule");
|
|
423
|
-
return
|
|
1040
|
+
return compiled(hashes, index, false);
|
|
424
1041
|
},
|
|
425
1042
|
context(index = -1, after = 0, upTo = -1) {
|
|
426
|
-
|
|
1043
|
+
// A domain lost to a panic has no context until the next reset.
|
|
1044
|
+
if (added === 0 || lost) {
|
|
427
1045
|
return null;
|
|
428
1046
|
}
|
|
429
|
-
const text =
|
|
1047
|
+
const text = wasm.domainContext(indexOf(index), after, upTo);
|
|
430
1048
|
if (text === "") {
|
|
431
1049
|
return null;
|
|
432
1050
|
}
|
|
@@ -443,27 +1061,28 @@ export async function createCodegen(module) {
|
|
|
443
1061
|
if (added === 0) {
|
|
444
1062
|
return null;
|
|
445
1063
|
}
|
|
446
|
-
return
|
|
1064
|
+
return chains.context(indexOf(index), after);
|
|
447
1065
|
},
|
|
448
1066
|
revision() {
|
|
449
|
-
return `${
|
|
1067
|
+
return `${revisionEpoch} ${wasm.domainRevision()}`;
|
|
450
1068
|
},
|
|
451
1069
|
compileModuleLogged(hashes = [], index = -1) {
|
|
452
1070
|
last("compileModuleLogged");
|
|
453
1071
|
const mark = wasm.domainLogMark();
|
|
454
|
-
const module =
|
|
455
|
-
const log =
|
|
1072
|
+
const module = compiled(hashes, index, false);
|
|
1073
|
+
const log = wasm.domainLogSince(mark, indexOf(index));
|
|
456
1074
|
return { module, log: log === "!" ? null : log };
|
|
457
1075
|
},
|
|
458
1076
|
replay(log, index = -1) {
|
|
459
1077
|
last("replay");
|
|
460
|
-
|
|
1078
|
+
const replay = () => wasm.domainReplay(log, indexOf(index));
|
|
1079
|
+
return recordChanges(replay);
|
|
461
1080
|
},
|
|
462
1081
|
compileActions(bytes, actions) {
|
|
463
1082
|
const module = actionsModule(bytes, actions, true);
|
|
464
|
-
const entries = parseEntries(
|
|
1083
|
+
const entries = parseEntries(wasm.actionsModuleEntries());
|
|
465
1084
|
const units = new Map();
|
|
466
|
-
for (const item of
|
|
1085
|
+
for (const item of wasm.actionsModuleUnits().split("\u0002")) {
|
|
467
1086
|
const [key, numbers, limits, code] = item.split("\u0001");
|
|
468
1087
|
if (code !== undefined) {
|
|
469
1088
|
const [at, end, bound, five] = numbers.split(",").map(Number);
|
|
@@ -488,14 +1107,14 @@ export async function createCodegen(module) {
|
|
|
488
1107
|
const address = wasm.actionsHeldEmitEachBytes(handle, actions.version, indices.join(","));
|
|
489
1108
|
const text = handed(address, wasm.outputLength());
|
|
490
1109
|
// A block's analysis can go before the next block runs.
|
|
491
|
-
return parseEntries(
|
|
1110
|
+
return parseEntries(text);
|
|
492
1111
|
},
|
|
493
1112
|
compileActionUnit(bytes, actions, { at, end, bound, five }) {
|
|
494
1113
|
const handle = hold(bytes, actions);
|
|
495
1114
|
const address = wasm.actionsHeldUnitBytes(handle, actions.version, at, end, bound, five);
|
|
496
1115
|
const code = handed(address, wasm.outputLength());
|
|
497
1116
|
const [high, reach] = wasm.actionsUnitLimits();
|
|
498
|
-
return
|
|
1117
|
+
return { code, high, reach };
|
|
499
1118
|
},
|
|
500
1119
|
releaseActions(bytes) {
|
|
501
1120
|
const known = holding.get(bytes);
|
|
@@ -506,10 +1125,96 @@ export async function createCodegen(module) {
|
|
|
506
1125
|
}
|
|
507
1126
|
},
|
|
508
1127
|
heldActions() {
|
|
509
|
-
return { held:
|
|
1128
|
+
return { held: wasm.actionsHeldCount(), copies };
|
|
510
1129
|
},
|
|
511
1130
|
};
|
|
1131
|
+
// Each method recovers from a panic of the compiler (see recover): an
|
|
1132
|
+
// ABC whose load panicked is rejected as a corrupt one is, and any other
|
|
1133
|
+
// call that panicked throws a plain Error, the compiler going on. Where
|
|
1134
|
+
// recovery cannot be, the domain is lost, but not the compiler: it goes
|
|
1135
|
+
// on in a fresh instance, for a reset and what needs no domain (AVM1),
|
|
1136
|
+
// and the domain's calls throw until the next reset.
|
|
1137
|
+
const fresh = () => {
|
|
1138
|
+
abortMove("superseded");
|
|
1139
|
+
wasm = spare ?? recoveryInstance(module);
|
|
1140
|
+
spare = null;
|
|
1141
|
+
holds.length = 0;
|
|
1142
|
+
chains.clear();
|
|
1143
|
+
};
|
|
1144
|
+
const descriptors = Object.getOwnPropertyDescriptors(api);
|
|
1145
|
+
for (const [name, descriptor] of Object.entries(descriptors)) {
|
|
1146
|
+
const method = descriptor.value;
|
|
1147
|
+
if (typeof method !== "function") {
|
|
1148
|
+
continue;
|
|
1149
|
+
}
|
|
1150
|
+
const domainless = DOMAINLESS.has(name);
|
|
1151
|
+
descriptor.value = (...args) => {
|
|
1152
|
+
// An instance left dead outside a call of this wrapper (a finalizer's
|
|
1153
|
+
// release) is recovered from first, or given way to.
|
|
1154
|
+
if (wasm.panicked?.() != null && !recover()) {
|
|
1155
|
+
fresh();
|
|
1156
|
+
lost = true;
|
|
1157
|
+
}
|
|
1158
|
+
// The player drops a domain from a FinalizationRegistry's callback,
|
|
1159
|
+
// and compacts from a timer, where a throw would go nowhere: a lost
|
|
1160
|
+
// domain has nothing to drop, nor to compact.
|
|
1161
|
+
if (lost && name === "dropDomain") {
|
|
1162
|
+
return undefined;
|
|
1163
|
+
}
|
|
1164
|
+
if (lost && name === "compact") {
|
|
1165
|
+
return false;
|
|
1166
|
+
}
|
|
1167
|
+
if (lost && name === "compactStep") {
|
|
1168
|
+
return "none";
|
|
1169
|
+
}
|
|
1170
|
+
if (lost && !domainless) {
|
|
1171
|
+
throw new Error(`${name}: the compiler's domain was lost to a panic it could not recover from; reset it`);
|
|
1172
|
+
}
|
|
1173
|
+
try {
|
|
1174
|
+
const result = method.apply(api, args);
|
|
1175
|
+
if (name === "reset") {
|
|
1176
|
+
lost = false;
|
|
1177
|
+
}
|
|
1178
|
+
return result;
|
|
1179
|
+
}
|
|
1180
|
+
catch (error) {
|
|
1181
|
+
if (!(error instanceof CodegenPanic)) {
|
|
1182
|
+
throw error;
|
|
1183
|
+
}
|
|
1184
|
+
if (!recover()) {
|
|
1185
|
+
// A reset that panicked too: what the wrapper keeps of the domain
|
|
1186
|
+
// (its ABCs, journal, checkpoint and API version) is the dead one's.
|
|
1187
|
+
fresh();
|
|
1188
|
+
lost = true;
|
|
1189
|
+
throw new Error(`${name}: ${error.message}; the compiler went on in a new instance, its domain lost`);
|
|
1190
|
+
}
|
|
1191
|
+
if (name === "add") {
|
|
1192
|
+
return PANICKED_ABC;
|
|
1193
|
+
}
|
|
1194
|
+
throw new Error(`${name}: ${error.message}; the compiler went on in a new instance`);
|
|
1195
|
+
}
|
|
1196
|
+
};
|
|
1197
|
+
}
|
|
1198
|
+
return Object.defineProperties({}, descriptors);
|
|
512
1199
|
}
|
|
1200
|
+
/** What a lost domain does not stop (see lost): a reset, AVM1, and what reads no domain. */
|
|
1201
|
+
const DOMAINLESS = new Set([
|
|
1202
|
+
"reset",
|
|
1203
|
+
"abcVersion",
|
|
1204
|
+
"compileActions",
|
|
1205
|
+
"compileActionsModule",
|
|
1206
|
+
"compileActionBlocks",
|
|
1207
|
+
"compileActionUnit",
|
|
1208
|
+
"releaseActions",
|
|
1209
|
+
"heldActions",
|
|
1210
|
+
"memoryBytes",
|
|
1211
|
+
"usage",
|
|
1212
|
+
"context",
|
|
1213
|
+
]);
|
|
1214
|
+
/** An ABC with nothing in it, which a dropped domain's ABCs give way to in the journal of calls. */
|
|
1215
|
+
const EMPTY_ABC = Uint8Array.of(16, 0, 46, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
|
1216
|
+
/** The VerifyError an ABC whose load panicked Zig's compiler is rejected with: kCorruptABCError. */
|
|
1217
|
+
const PANICKED_ABC = 1107;
|
|
513
1218
|
/** Entries as the wasm writes them: for each, the body's index, U+0001, its entry, U+0002. */
|
|
514
1219
|
function parseEntries(text) {
|
|
515
1220
|
const entries = new Map();
|
|
@@ -521,14 +1226,33 @@ function parseEntries(text) {
|
|
|
521
1226
|
}
|
|
522
1227
|
return entries;
|
|
523
1228
|
}
|
|
524
|
-
|
|
525
|
-
/** What domainWantsCompact is asked (see wantsRebuild in assembly/avm2/link/lifetime.ts). */
|
|
1229
|
+
/** What domainWantsCompact is asked: lifetime.zig's Want, by number (a unit test holds them alike). */
|
|
526
1230
|
const WANT_InPlace = 0;
|
|
527
1231
|
const WANT_InPlaceBesideSpare = 1;
|
|
528
1232
|
const WANT_Move = 2;
|
|
529
1233
|
const WANT_Spare = 3;
|
|
530
|
-
/**
|
|
531
|
-
|
|
1234
|
+
/**
|
|
1235
|
+
* The live weight (domainUsage's first number) below which compactStep
|
|
1236
|
+
* rebuilds in place, in one call, rather than move a part at a time.
|
|
1237
|
+
*/
|
|
1238
|
+
const IN_PLACE_Live = 4 << 20;
|
|
1239
|
+
/** A compile's log entry that verified an ABC: LogKind.verify (avm2/link/domain.zig). */
|
|
1240
|
+
const LOG_Verify = 4;
|
|
1241
|
+
/**
|
|
1242
|
+
* The length of an ABC whose verification, replayed, starts a slice of its
|
|
1243
|
+
* own: some 20 ms of it on real content, 750 ms for 2.3 MB.
|
|
1244
|
+
*/
|
|
1245
|
+
const LARGE_Verify = 64 << 10;
|
|
1246
|
+
/** The garbage a part of a move's rebuild makes at most (domainRebuildBudget). */
|
|
1247
|
+
const MOVE_PartBudget = 256 << 10;
|
|
1248
|
+
/** The slices compactStep catches a move up in before it goes on past their deadlines (see compactStep). */
|
|
1249
|
+
const CATCH_UP_Slices = 32;
|
|
1250
|
+
/** How far past its deadline a slice catches up after CATCH_UP_Slices, in milliseconds. */
|
|
1251
|
+
const CATCH_UP_Overrun = 50;
|
|
1252
|
+
/** The length of an ABC whose add, replayed, starts a slice of its own: some 4 ms of parsing and linking. */
|
|
1253
|
+
const LARGE_Add = 256 << 10;
|
|
1254
|
+
/** The drops and evictions after a move given up as failed before compactStep starts another. */
|
|
1255
|
+
const RETRY_Lettings = 16;
|
|
532
1256
|
/** How many SWFs whose blocks or units are compiled codegen holds a copy of at most. */
|
|
533
1257
|
const HELD_SWFS = 4;
|
|
534
1258
|
const utf8 = new TextDecoder();
|