@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/index.ts
CHANGED
|
@@ -1,14 +1,22 @@
|
|
|
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, type KeyContext } from "./keys.js";
|
|
12
|
+
import {
|
|
13
|
+
type CodegenExports,
|
|
14
|
+
CodegenOutOfMemory,
|
|
15
|
+
CodegenPanic,
|
|
16
|
+
checkExports,
|
|
17
|
+
instantiate,
|
|
18
|
+
recoveryInstance,
|
|
19
|
+
} from "./zig.js";
|
|
12
20
|
|
|
13
21
|
export { KeyChains, type KeyContext, type KeyLog } from "./keys.js";
|
|
14
22
|
export { hex, sha256Here } from "./sha256.js";
|
|
@@ -193,6 +201,38 @@ export interface LoggedModule {
|
|
|
193
201
|
log: string | null;
|
|
194
202
|
}
|
|
195
203
|
|
|
204
|
+
/** How long compactStep may go on: as an IdleDeadline, the milliseconds left. */
|
|
205
|
+
export interface CompactDeadline {
|
|
206
|
+
timeRemaining(): number;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** What compactStep did (see Codegen.compactStep). */
|
|
210
|
+
export type CompactProgress = "none" | "moving" | "done";
|
|
211
|
+
|
|
212
|
+
/** Why a move compactStep started was given up, the domain left as it was. */
|
|
213
|
+
export type MoveAbort =
|
|
214
|
+
/** The spare instance panicked, trapped or ran out of memory. */
|
|
215
|
+
| "spare"
|
|
216
|
+
/** The spare refused: the domain too large to move, or a part that did not load, rebuild or replay. */
|
|
217
|
+
| "refused"
|
|
218
|
+
/** A call meanwhile could not be journaled, so the spare could not catch up. */
|
|
219
|
+
| "unjournaled"
|
|
220
|
+
/** The domain was reset, recovered from a panic, or rebuilt in place meanwhile. */
|
|
221
|
+
| "superseded";
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* compactStep's moves: started, completed, and given up by why; and of the
|
|
225
|
+
* last one completed, the calls replayed into its spare after its rebuild,
|
|
226
|
+
* and the bytes of its snapshot, which recovery keeps as its checkpoint.
|
|
227
|
+
*/
|
|
228
|
+
export interface MoveCounts {
|
|
229
|
+
started: number;
|
|
230
|
+
completed: number;
|
|
231
|
+
aborted: Record<MoveAbort, number>;
|
|
232
|
+
lastCaughtUp: number;
|
|
233
|
+
lastSavedBytes: number;
|
|
234
|
+
}
|
|
235
|
+
|
|
196
236
|
/**
|
|
197
237
|
* The compiler. ABCs are added to a domain in the order the SWF loads them
|
|
198
238
|
* (the builtins first), and the last one added compiles to its module,
|
|
@@ -250,14 +290,35 @@ export interface Codegen {
|
|
|
250
290
|
* a host calls this when it has time, as when idle; nothing else does.
|
|
251
291
|
*/
|
|
252
292
|
compact(): boolean;
|
|
293
|
+
/**
|
|
294
|
+
* compact a part at a time, for a host that goes on compiling meanwhile:
|
|
295
|
+
* a move into the spare instance is snapshotted, loaded and rebuilt there
|
|
296
|
+
* while `deadline.timeRemaining()` is above 0 (one part at least), the
|
|
297
|
+
* calls made meanwhile replayed into it, and it takes this instance's
|
|
298
|
+
* place once it has caught up. "moving" while some is left, to be called
|
|
299
|
+
* again; "done" once a rebuild finished in this call; "none" if none was
|
|
300
|
+
* due, or the move was given up, the domain left as it was. Only a small
|
|
301
|
+
* domain is rebuilt in place, in one call; a larger one moves.
|
|
302
|
+
*/
|
|
303
|
+
compactStep(deadline: CompactDeadline): CompactProgress;
|
|
253
304
|
/**
|
|
254
305
|
* What the ABCs hold of codegen's memory, roughly, in bytes: the live
|
|
255
306
|
* ones', and the dropped and evicted domains' that compact would free;
|
|
256
307
|
* the size of codegen's memory, which grows and never shrinks but when
|
|
257
|
-
* compact moves the domain to a new instance;
|
|
258
|
-
* failed, each leaving the domain as it was, rebuilt in place if at all
|
|
308
|
+
* compact moves the domain to a new instance; how many such moves
|
|
309
|
+
* failed, each leaving the domain as it was, rebuilt in place if at all;
|
|
310
|
+
* compactStep's moves; and what the recovery from a panic holds outside
|
|
311
|
+
* codegen's memory: its checkpoint and the inputs journaled since, in
|
|
312
|
+
* bytes.
|
|
259
313
|
*/
|
|
260
|
-
usage(): {
|
|
314
|
+
usage(): {
|
|
315
|
+
live: number;
|
|
316
|
+
dead: number;
|
|
317
|
+
memory: number;
|
|
318
|
+
failedMoves: number;
|
|
319
|
+
moves: MoveCounts;
|
|
320
|
+
recovery: number;
|
|
321
|
+
};
|
|
261
322
|
/** The size of codegen's memory in bytes, as usage gives it: cheap, for a host that shows it often. */
|
|
262
323
|
memoryBytes(): number;
|
|
263
324
|
/**
|
|
@@ -369,7 +430,8 @@ export interface Codegen {
|
|
|
369
430
|
* this package free of I/O. The file ships as `@swf2es/codegen/codegen.wasm`.
|
|
370
431
|
*/
|
|
371
432
|
export async function createCodegen(module: WebAssembly.Module): Promise<Codegen> {
|
|
372
|
-
|
|
433
|
+
checkExports(module);
|
|
434
|
+
let wasm = await instantiate(module);
|
|
373
435
|
const version = wasm.compilerVersion();
|
|
374
436
|
if (version !== COMPILER_VERSION) {
|
|
375
437
|
throw new Error(
|
|
@@ -377,32 +439,6 @@ export async function createCodegen(module: WebAssembly.Module): Promise<Codegen
|
|
|
377
439
|
);
|
|
378
440
|
}
|
|
379
441
|
|
|
380
|
-
// Of the same version, but built before its modules were handed out in
|
|
381
|
-
// place: its *Bytes exports return copies, which this wrapper cannot read.
|
|
382
|
-
// Asked of the module, as the bindings define every export they know of.
|
|
383
|
-
if (!WebAssembly.Module.exports(module).some((e) => e.name === "outputLength")) {
|
|
384
|
-
throw new Error(
|
|
385
|
-
"codegen.wasm has no outputLength export, which the wrapper expects: it is older than the wrapper",
|
|
386
|
-
);
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
// Or before it wrote a module a part at a time, or rebuilt its domain a
|
|
390
|
-
// part at a time and in another instance.
|
|
391
|
-
for (const name of ["domainModuleMoreBytes", "domainRebuildStep", "domainSaveBytes"]) {
|
|
392
|
-
if (!WebAssembly.Module.exports(module).some((e) => e.name === name)) {
|
|
393
|
-
throw new Error(
|
|
394
|
-
`codegen.wasm has no ${name} export, which the wrapper expects: it is older than the wrapper`,
|
|
395
|
-
);
|
|
396
|
-
}
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
// codegen.wasm uses AssemblyScript's minimal runtime, which collects
|
|
400
|
-
// garbage only when asked. Nothing runs in wasm between calls, so that is
|
|
401
|
-
// when: every COLLECT_EVERY calls, and after a call that grew memory.
|
|
402
|
-
// Memory never shrinks, so collecting only on growth would let each
|
|
403
|
-
// cycle's garbage grow it a little further.
|
|
404
|
-
let size = wasm.memory.buffer.byteLength;
|
|
405
|
-
let calls = 0;
|
|
406
442
|
// Whether the domain has an ABC to compile: asking before is a mistake worth a message, not a trap.
|
|
407
443
|
let added = 0;
|
|
408
444
|
const last = (what: string) => {
|
|
@@ -425,7 +461,7 @@ export async function createCodegen(module: WebAssembly.Module): Promise<Codegen
|
|
|
425
461
|
// compile took megabytes of codegen's memory a few thousand loads on.
|
|
426
462
|
const at = indexOf(index);
|
|
427
463
|
const own = at < 0 ? added - 1 : at;
|
|
428
|
-
const linked =
|
|
464
|
+
const linked = wasm.domainLinked(at);
|
|
429
465
|
const named = [hashes[own] ?? ""];
|
|
430
466
|
for (const i of linked ? linked.split(",") : []) {
|
|
431
467
|
named.push(hashes[Number(i)] ?? "");
|
|
@@ -434,23 +470,24 @@ export async function createCodegen(module: WebAssembly.Module): Promise<Codegen
|
|
|
434
470
|
// Its bytes decoded here: lifted as a string, a module of megabytes took
|
|
435
471
|
// longer than compiling it. The emitter writes only ASCII, which decodes
|
|
436
472
|
// alike either way, and to a string the engine keeps a byte a character.
|
|
437
|
-
// A large ABC's module comes a part at a time, each part's
|
|
438
|
-
//
|
|
473
|
+
// A large ABC's module comes a part at a time, each part's arena given
|
|
474
|
+
// back as the next is written (see domainModuleBytes).
|
|
439
475
|
const parts = [
|
|
440
476
|
handed(wasm.domainModuleBytes(named.join("\n"), at, keep, true), wasm.outputLength()),
|
|
441
477
|
];
|
|
442
478
|
while (!wasm.domainModuleDone()) {
|
|
443
|
-
wasm.__collect();
|
|
444
479
|
parts.push(handed(wasm.domainModuleMoreBytes(), wasm.outputLength()));
|
|
445
480
|
}
|
|
446
481
|
|
|
447
|
-
const module =
|
|
482
|
+
const module = parts.length === 1 ? parts[0] : parts.join("");
|
|
448
483
|
if (module === "") {
|
|
449
484
|
throw new Error(`ABC ${index}: its application domain was dropped`);
|
|
450
485
|
}
|
|
451
486
|
|
|
452
487
|
return module;
|
|
453
488
|
};
|
|
489
|
+
const compiled = (hashes: string[], index: number, keep: boolean) =>
|
|
490
|
+
recordChanges(() => moduleOf(hashes, index, keep));
|
|
454
491
|
const ranges = (actions: ActionBlocks) =>
|
|
455
492
|
[
|
|
456
493
|
Int32Array.from(actions.blocks, (b) => b.offset),
|
|
@@ -468,7 +505,15 @@ export async function createCodegen(module: WebAssembly.Module): Promise<Codegen
|
|
|
468
505
|
const at = holds.indexOf(handle);
|
|
469
506
|
if (at >= 0) {
|
|
470
507
|
holds.splice(at, 1);
|
|
471
|
-
|
|
508
|
+
// Also called from the finalization registry, which must not throw:
|
|
509
|
+
// a dead instance's holds went with it.
|
|
510
|
+
try {
|
|
511
|
+
wasm.actionsRelease(handle);
|
|
512
|
+
} catch (error) {
|
|
513
|
+
if (!(error instanceof CodegenPanic)) {
|
|
514
|
+
throw error;
|
|
515
|
+
}
|
|
516
|
+
}
|
|
472
517
|
}
|
|
473
518
|
};
|
|
474
519
|
const collectedSwfs = new FinalizationRegistry<number>(release);
|
|
@@ -502,16 +547,19 @@ export async function createCodegen(module: WebAssembly.Module): Promise<Codegen
|
|
|
502
547
|
const actionsModule = (bytes: Uint8Array, actions: ActionBlocks, keep: boolean) => {
|
|
503
548
|
const [offsets, lengths] = ranges(actions);
|
|
504
549
|
const address = wasm.actionsModuleBytes(bytes, offsets, lengths, actions.version, keep);
|
|
505
|
-
return
|
|
550
|
+
return handed(address, wasm.outputLength());
|
|
506
551
|
};
|
|
507
552
|
// UTF-8 a call handed out in codegen's memory, not copied (see handOut in
|
|
508
|
-
//
|
|
509
|
-
// call may grow memory
|
|
553
|
+
// zig/src/root.zig), decoded before anything else calls in: the next
|
|
554
|
+
// call may grow memory, and gives the last one's arena back.
|
|
510
555
|
const handed = (address: number, length: number) =>
|
|
511
|
-
length === 0 ? "" : utf8.decode(
|
|
556
|
+
length === 0 ? "" : utf8.decode(view(address, length));
|
|
557
|
+
// `length` bytes at `address` in codegen's memory, read inside the panic guard (see zig.ts).
|
|
558
|
+
const view = (address: number, length: number) => wasm.view(address, length);
|
|
512
559
|
const stamps = WebAssembly.Module.customSections(module, "swf2es.build");
|
|
513
560
|
const identity = stamps.length ? String.fromCharCode(...new Uint8Array(stamps[0])) : null;
|
|
514
561
|
let epoch = 0;
|
|
562
|
+
let revisionEpoch = 0;
|
|
515
563
|
let apiVersion = API_VERSION;
|
|
516
564
|
// Read where they are in codegen's memory, digested before the next call in.
|
|
517
565
|
const chains = new KeyChains({
|
|
@@ -523,27 +571,87 @@ export async function createCodegen(module: WebAssembly.Module): Promise<Codegen
|
|
|
523
571
|
fork: (appDomain) => wasm.domainKeyFork(appDomain),
|
|
524
572
|
log(appDomain, after, from, to, block, filled) {
|
|
525
573
|
const address = wasm.domainKeyLogBytes(appDomain, after, from, to, block, filled);
|
|
526
|
-
return address === 0
|
|
527
|
-
? null
|
|
528
|
-
: new Uint8Array(wasm.memory.buffer, address, wasm.outputLength());
|
|
574
|
+
return address === 0 ? null : view(address, wasm.outputLength());
|
|
529
575
|
},
|
|
530
576
|
});
|
|
577
|
+
// Snapshot the inputs of a successful Zig rebuild. Recovery then repeats that same generation
|
|
578
|
+
// and can release older findings, compiles and bytes of evicted ABCs.
|
|
579
|
+
interface Checkpoint {
|
|
580
|
+
state: Uint8Array;
|
|
581
|
+
abcs: Map<number, Uint8Array>;
|
|
582
|
+
}
|
|
583
|
+
let checkpoint: Checkpoint | null = null;
|
|
584
|
+
const saveCheckpoint = (): Checkpoint => {
|
|
585
|
+
const at = wasm.domainSaveBytes();
|
|
586
|
+
const state = view(at, wasm.outputLength()).slice();
|
|
587
|
+
const abcs = new Map<number, Uint8Array>();
|
|
588
|
+
for (let i = 0; i < added; i++) {
|
|
589
|
+
const at = wasm.domainSavedAbcBytes(i);
|
|
590
|
+
if (at) {
|
|
591
|
+
abcs.set(i, view(at, wasm.outputLength()).slice());
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
return { state, abcs };
|
|
595
|
+
};
|
|
596
|
+
const loadCheckpoint = (into: typeof wasm, saved: Checkpoint, recovery: boolean) => {
|
|
597
|
+
const loaded = recovery ? into.loadCheckpointState(saved.state) : into.domainLoad(saved.state);
|
|
598
|
+
if (!loaded) {
|
|
599
|
+
return false;
|
|
600
|
+
}
|
|
601
|
+
for (const [index, abc] of saved.abcs) {
|
|
602
|
+
if (into.domainLoadAbc(index, abc) !== 0) {
|
|
603
|
+
return false;
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
return into.domainLoadRebuild();
|
|
607
|
+
};
|
|
531
608
|
// The rebuild domainCompact, domainRevive or domainLoadRebuild started in
|
|
532
|
-
// `into`, a part a call, each part's
|
|
533
|
-
// (see domainRebuildStep)
|
|
534
|
-
|
|
535
|
-
const rebuild = (into = wasm) => {
|
|
609
|
+
// `into`, a part a call, each part's arena given back as the next starts
|
|
610
|
+
// (see domainRebuildStep): whether it was done.
|
|
611
|
+
const finishRebuild = (into: typeof wasm) => {
|
|
536
612
|
let step = into.domainRebuildStep();
|
|
537
613
|
while (step === 0) {
|
|
538
|
-
into.__collect();
|
|
539
614
|
step = into.domainRebuildStep();
|
|
540
615
|
}
|
|
541
616
|
|
|
542
|
-
into.__collect();
|
|
543
|
-
size = wasm.memory.buffer.byteLength;
|
|
544
|
-
calls = 0;
|
|
545
617
|
return step > 0;
|
|
546
618
|
};
|
|
619
|
+
const rebuild = (into = wasm, saved: Checkpoint | null = null, optional = false) => {
|
|
620
|
+
let done: boolean;
|
|
621
|
+
try {
|
|
622
|
+
if (!saved) {
|
|
623
|
+
saved = saveCheckpoint();
|
|
624
|
+
}
|
|
625
|
+
done = finishRebuild(into);
|
|
626
|
+
} catch (error) {
|
|
627
|
+
if (!(error instanceof CodegenPanic)) {
|
|
628
|
+
into.abandonRebuild();
|
|
629
|
+
if (optional && error instanceof CodegenOutOfMemory) {
|
|
630
|
+
return false;
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
throw error;
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
if (done && saved) {
|
|
638
|
+
// The journal a move under way catches up from is cleared below.
|
|
639
|
+
if (into === wasm) {
|
|
640
|
+
abortMove("superseded");
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
moveRefused = false;
|
|
644
|
+
checkpoint = saved;
|
|
645
|
+
journal.clear();
|
|
646
|
+
restores.clear();
|
|
647
|
+
for (const domain of journalDomains.values()) {
|
|
648
|
+
domain.entries = undefined;
|
|
649
|
+
}
|
|
650
|
+
replayable = true;
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
return done;
|
|
654
|
+
};
|
|
547
655
|
// A second instance of the compiler, made ahead as instantiating is
|
|
548
656
|
// asynchronous, that the next rebuild is done in (see moved): wasm memory
|
|
549
657
|
// never shrinks, so one done in place keeps, for good, as much as the
|
|
@@ -553,10 +661,39 @@ export async function createCodegen(module: WebAssembly.Module): Promise<Codegen
|
|
|
553
661
|
let spare: typeof wasm | null = null;
|
|
554
662
|
let making = false;
|
|
555
663
|
let failedMoves = 0;
|
|
664
|
+
// The move compactStep has under way, a part at a time (see Move), and what came of its moves.
|
|
665
|
+
let move: Move | null = null;
|
|
666
|
+
const moves: MoveCounts = {
|
|
667
|
+
started: 0,
|
|
668
|
+
completed: 0,
|
|
669
|
+
aborted: { spare: 0, refused: 0, unjournaled: 0, superseded: 0 },
|
|
670
|
+
lastCaughtUp: 0,
|
|
671
|
+
lastSavedBytes: 0,
|
|
672
|
+
};
|
|
673
|
+
// Whether the spare refused a move as too large, until the next rebuild: a large domain then rebuilds in place.
|
|
674
|
+
let moveRefused = false;
|
|
675
|
+
// The drops and evictions so far, and how many there must be before
|
|
676
|
+
// compactStep starts a move again after one was given up as failed: the
|
|
677
|
+
// same domain would likely fail the same way.
|
|
678
|
+
let lettings = 0;
|
|
679
|
+
let retryAt = 0;
|
|
680
|
+
const abortMove = (why: MoveAbort) => {
|
|
681
|
+
if (move !== null) {
|
|
682
|
+
move = null;
|
|
683
|
+
moves.aborted[why]++;
|
|
684
|
+
if (why !== "superseded") {
|
|
685
|
+
retryAt = lettings + RETRY_Lettings;
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
if (why === "spare" || why === "refused") {
|
|
689
|
+
failedMoves++;
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
};
|
|
556
693
|
const prepare = () => {
|
|
557
694
|
if (spare === null && !making) {
|
|
558
695
|
making = true;
|
|
559
|
-
instantiate(module
|
|
696
|
+
instantiate(module).then(
|
|
560
697
|
(made) => {
|
|
561
698
|
spare = made;
|
|
562
699
|
making = false;
|
|
@@ -568,32 +705,10 @@ export async function createCodegen(module: WebAssembly.Module): Promise<Codegen
|
|
|
568
705
|
}
|
|
569
706
|
};
|
|
570
707
|
// The domain rebuilt in `into`, a new instance, from what this one saves
|
|
571
|
-
// of it and its live ABCs' bytes, read where they are: whether it was.
|
|
572
|
-
// ABCs' copies in it are its garbage once parsed, collected every few
|
|
573
|
-
// megabytes.
|
|
708
|
+
// of it and its live ABCs' bytes, read where they are: whether it was.
|
|
574
709
|
const rebuiltIn = (into: typeof wasm) => {
|
|
575
|
-
const saved =
|
|
576
|
-
|
|
577
|
-
return false;
|
|
578
|
-
}
|
|
579
|
-
|
|
580
|
-
into.__collect();
|
|
581
|
-
let copied = 0;
|
|
582
|
-
for (let i = 0; i < added; i++) {
|
|
583
|
-
const at = wasm.domainSavedAbcBytes(i);
|
|
584
|
-
const length = wasm.outputLength();
|
|
585
|
-
if (at !== 0 && into.domainLoadAbc(i, new Uint8Array(wasm.memory.buffer, at, length)) !== 0) {
|
|
586
|
-
return false;
|
|
587
|
-
}
|
|
588
|
-
|
|
589
|
-
copied += length;
|
|
590
|
-
if (copied >= COPIED_BETWEEN_COLLECTIONS) {
|
|
591
|
-
into.__collect();
|
|
592
|
-
copied = 0;
|
|
593
|
-
}
|
|
594
|
-
}
|
|
595
|
-
|
|
596
|
-
return into.domainLoadRebuild() && rebuild(into);
|
|
710
|
+
const saved = saveCheckpoint();
|
|
711
|
+
return loadCheckpoint(into, saved, false) && rebuild(into, saved);
|
|
597
712
|
};
|
|
598
713
|
// The domain rebuilt in the spare instance, which then takes this one's
|
|
599
714
|
// place, whose memory goes with it: whether it was. One that failed, or
|
|
@@ -615,32 +730,544 @@ export async function createCodegen(module: WebAssembly.Module): Promise<Codegen
|
|
|
615
730
|
|
|
616
731
|
if (!done) {
|
|
617
732
|
failedMoves++;
|
|
618
|
-
size = wasm.memory.buffer.byteLength;
|
|
619
733
|
return false;
|
|
620
734
|
}
|
|
621
735
|
|
|
622
736
|
// The held SWFs' copies were the old instance's (see hold).
|
|
623
737
|
holds.length = 0;
|
|
624
738
|
wasm = into;
|
|
625
|
-
size = wasm.memory.buffer.byteLength;
|
|
626
|
-
calls = 0;
|
|
627
739
|
return true;
|
|
628
740
|
};
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
741
|
+
// When the compiler panics, its instance is dead (CodegenPanic). Recovery reads nothing of it: the calls
|
|
742
|
+
// that changed the domain are journaled here, in order, and replayed into
|
|
743
|
+
// a fresh instance, which is the same domain by determinism and takes
|
|
744
|
+
// the dead one's place; the call that panicked is left out. If the replay
|
|
745
|
+
// panics too, or a compile's log cannot be replayed, there is no second
|
|
746
|
+
// attempt: the domain is lost (see fresh).
|
|
747
|
+
type Entry =
|
|
748
|
+
| {
|
|
749
|
+
kind: "add";
|
|
750
|
+
abc: Uint8Array;
|
|
751
|
+
builtin: boolean;
|
|
752
|
+
appDomain: number;
|
|
753
|
+
/** Its index, or -1 where it was rejected. */
|
|
754
|
+
index: number;
|
|
755
|
+
error: number;
|
|
756
|
+
}
|
|
757
|
+
| { kind: "child"; parent: number }
|
|
758
|
+
| { kind: "drop" | "evict" | "revive"; appDomain: number }
|
|
759
|
+
| { kind: "restore"; index: number; bytes: Uint8Array }
|
|
760
|
+
| { kind: "found"; definition: FoundDefinition }
|
|
761
|
+
/** A compile's log entries of ABCs of one domain, four words each: kind, ABC, a, b. */
|
|
762
|
+
| { kind: "log"; words: Uint32Array };
|
|
763
|
+
interface JournalDomain {
|
|
764
|
+
id: number;
|
|
765
|
+
parent?: JournalDomain;
|
|
766
|
+
children?: Set<JournalDomain>;
|
|
767
|
+
entries?: Entry[];
|
|
768
|
+
}
|
|
769
|
+
// A move compactStep makes a part at a time: the spare it rebuilds in,
|
|
770
|
+
// the snapshot it rebuilds, the calls journaled since, how many of those
|
|
771
|
+
// the spare has replayed and where in one it is; the bytes of the ABCs
|
|
772
|
+
// revivals since restored in it, copied once; the saved ABCs not yet
|
|
773
|
+
// given to it; its phase; and the slices it has spent catching up.
|
|
774
|
+
interface Move {
|
|
775
|
+
into: Bound;
|
|
776
|
+
saved: Checkpoint;
|
|
777
|
+
entries: Entry[];
|
|
778
|
+
replayed: number;
|
|
779
|
+
/** Where in the log entry being replayed, a compile's, the spare is. */
|
|
780
|
+
word: number;
|
|
781
|
+
restored: Map<number, Uint8Array>;
|
|
782
|
+
abcs: IterableIterator<[number, Uint8Array]> | null;
|
|
783
|
+
phase: "load" | "abcs" | "rebuild" | "catch up";
|
|
784
|
+
catchUp: number;
|
|
785
|
+
}
|
|
786
|
+
const journalDomains = new Map<number, JournalDomain>();
|
|
787
|
+
const journal = new Set<Entry>();
|
|
788
|
+
// What the checkpoint and the journal hold, their bytes and strings' units twice.
|
|
789
|
+
const recoveryBytes = () => {
|
|
790
|
+
let bytes = 0;
|
|
791
|
+
if (checkpoint) {
|
|
792
|
+
bytes += checkpoint.state.byteLength;
|
|
793
|
+
for (const abc of checkpoint.abcs.values()) {
|
|
794
|
+
bytes += abc.byteLength;
|
|
795
|
+
}
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
for (const entry of journal) {
|
|
799
|
+
switch (entry.kind) {
|
|
800
|
+
case "add":
|
|
801
|
+
bytes += entry.abc.byteLength;
|
|
802
|
+
break;
|
|
803
|
+
case "restore":
|
|
804
|
+
bytes += entry.bytes.byteLength;
|
|
805
|
+
break;
|
|
806
|
+
case "log":
|
|
807
|
+
bytes += entry.words.byteLength;
|
|
808
|
+
break;
|
|
809
|
+
case "found":
|
|
810
|
+
bytes += (entry.definition.uri.length + entry.definition.name.length) * 2;
|
|
811
|
+
break;
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
return bytes;
|
|
816
|
+
};
|
|
817
|
+
const restores = new Map<number, Entry>();
|
|
818
|
+
const abcDomains: number[] = [];
|
|
819
|
+
// Each ABC's length, by its index, which a replay of its verification takes time in proportion to.
|
|
820
|
+
const abcLengths: number[] = [];
|
|
821
|
+
let replayable = true;
|
|
822
|
+
const journaled = (entry: Entry, owner = 0) => {
|
|
823
|
+
const id = owner | 0;
|
|
824
|
+
if (id !== 0) {
|
|
825
|
+
const domain = journalDomains.get(id);
|
|
826
|
+
if (!domain) {
|
|
827
|
+
// The native call ignored a domain that was dropped or never created.
|
|
828
|
+
return;
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
domain.entries ??= [];
|
|
832
|
+
domain.entries.push(entry);
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
journal.add(entry);
|
|
836
|
+
move?.entries.push(entry);
|
|
837
|
+
};
|
|
838
|
+
// The target is absolute so dropping a child preserves work its compiles did on its ancestors.
|
|
839
|
+
const journalLog = (mark: number) => {
|
|
840
|
+
const at = wasm.journalBytes(mark);
|
|
841
|
+
if (at === 0) {
|
|
842
|
+
return false;
|
|
843
|
+
}
|
|
844
|
+
const count = wasm.outputLength() / 4;
|
|
845
|
+
if (!count) {
|
|
846
|
+
return true;
|
|
847
|
+
}
|
|
848
|
+
// Packed, a run of entries of one domain's ABCs to a journal entry, so
|
|
849
|
+
// that a long session's logs take their words and little more.
|
|
850
|
+
const bytes = view(at, count * 4);
|
|
851
|
+
const words = new Uint32Array(bytes.buffer, bytes.byteOffset, count).slice();
|
|
852
|
+
let start = 0;
|
|
853
|
+
for (let i = 4; i <= count; i += 4) {
|
|
854
|
+
const owner = abcDomains[words[start + 1]];
|
|
855
|
+
if (i === count || abcDomains[words[i + 1]] !== owner) {
|
|
856
|
+
journaled({ kind: "log", words: words.subarray(start, i) }, owner);
|
|
857
|
+
start = i;
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
return true;
|
|
862
|
+
};
|
|
863
|
+
function recordChanges<T>(call: () => T): T {
|
|
864
|
+
if (!replayable) {
|
|
865
|
+
return call();
|
|
866
|
+
}
|
|
867
|
+
|
|
868
|
+
const mark = wasm.domainLogMark();
|
|
869
|
+
let result: T;
|
|
870
|
+
try {
|
|
871
|
+
result = call();
|
|
872
|
+
} catch (error) {
|
|
873
|
+
// A stepped verifier can have logged its start without finishing. Replaying
|
|
874
|
+
// that entry would resolve more of the domain than the interrupted compile did.
|
|
875
|
+
if (!(error instanceof CodegenPanic)) {
|
|
876
|
+
try {
|
|
877
|
+
replayable = wasm.domainLogMark() === mark;
|
|
878
|
+
} catch {
|
|
879
|
+
replayable = false;
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
throw error;
|
|
884
|
+
}
|
|
885
|
+
|
|
886
|
+
// A missing suffix makes recovery unsafe until the next successful
|
|
887
|
+
// checkpoint. A panic reading it leaves the journal as it was before the
|
|
888
|
+
// compile, which recovery then gives back, the compile's changes with
|
|
889
|
+
// the dead instance: only another failure suspends recovery.
|
|
890
|
+
try {
|
|
891
|
+
replayable = journalLog(mark);
|
|
892
|
+
} catch (error) {
|
|
893
|
+
if (!(error instanceof CodegenPanic)) {
|
|
894
|
+
replayable = false;
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
throw error;
|
|
635
898
|
}
|
|
636
899
|
|
|
637
900
|
return result;
|
|
901
|
+
}
|
|
902
|
+
// Whether application domain `d` is `own` or one of its ancestors.
|
|
903
|
+
const inChain = (d: number, own: number) => {
|
|
904
|
+
for (let at = journalDomains.get(own | 0); at; at = at.parent) {
|
|
905
|
+
if (at.id === d) {
|
|
906
|
+
return true;
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
return d === 0 && own >= 0;
|
|
911
|
+
};
|
|
912
|
+
const forgetDropped = (appDomain: number) => {
|
|
913
|
+
const root = journalDomains.get(appDomain | 0);
|
|
914
|
+
if (!root) {
|
|
915
|
+
return;
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
root.parent?.children?.delete(root);
|
|
919
|
+
const pending = [root];
|
|
920
|
+
for (const domain of pending) {
|
|
921
|
+
if (domain.children) {
|
|
922
|
+
for (const child of domain.children) {
|
|
923
|
+
pending.push(child);
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
if (domain.entries) {
|
|
928
|
+
for (const entry of domain.entries) {
|
|
929
|
+
if (entry.kind === "add" && entry.index >= 0) {
|
|
930
|
+
entry.abc = EMPTY_ABC;
|
|
931
|
+
} else {
|
|
932
|
+
journal.delete(entry);
|
|
933
|
+
if (entry.kind === "restore") {
|
|
934
|
+
restores.delete(entry.index);
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
journalDomains.delete(domain.id);
|
|
941
|
+
}
|
|
942
|
+
};
|
|
943
|
+
// Replay `entry` into `into`: whether it started a rebuild there, which
|
|
944
|
+
// only a revival `stepped` does (domainRevive's 2), for domainRebuildStep
|
|
945
|
+
// to finish before the next. Any refusal is a divergence, and throws.
|
|
946
|
+
const replayOne = (into: Bound, entry: Entry, stepped: boolean): boolean => {
|
|
947
|
+
switch (entry.kind) {
|
|
948
|
+
case "add":
|
|
949
|
+
if (into.domainAdd(entry.abc, entry.builtin, entry.appDomain) !== entry.error) {
|
|
950
|
+
throw new Error("a journaled ABC did not replay");
|
|
951
|
+
}
|
|
952
|
+
break;
|
|
953
|
+
case "child":
|
|
954
|
+
into.domainChild(entry.parent);
|
|
955
|
+
break;
|
|
956
|
+
case "drop":
|
|
957
|
+
into.domainDrop(entry.appDomain);
|
|
958
|
+
break;
|
|
959
|
+
case "evict":
|
|
960
|
+
into.domainEvict(entry.appDomain);
|
|
961
|
+
break;
|
|
962
|
+
case "restore":
|
|
963
|
+
if (into.domainRestore(entry.index, entry.bytes) !== 0) {
|
|
964
|
+
throw new Error("a journaled ABC did not restore");
|
|
965
|
+
}
|
|
966
|
+
break;
|
|
967
|
+
case "revive": {
|
|
968
|
+
// 1, or 2 stepped, where `into` rebuilt since the domain was
|
|
969
|
+
// evicted, as a move's spare has, or recovery from the checkpoint
|
|
970
|
+
// a move left: the restores journaled before give it the ABCs that
|
|
971
|
+
// rebuild let go of, and it links them again.
|
|
972
|
+
const revived = into.domainRevive(entry.appDomain, stepped);
|
|
973
|
+
if (revived < 0) {
|
|
974
|
+
throw new Error("a journaled domain did not revive");
|
|
975
|
+
}
|
|
976
|
+
|
|
977
|
+
return revived === 2;
|
|
978
|
+
}
|
|
979
|
+
case "found": {
|
|
980
|
+
const d = entry.definition;
|
|
981
|
+
into.domainFound(d.domain, d.nsKind, d.uri, d.name, d.abc, d.asType);
|
|
982
|
+
break;
|
|
983
|
+
}
|
|
984
|
+
case "log": {
|
|
985
|
+
const w = entry.words;
|
|
986
|
+
for (let i = 0; i < w.length; i += 4) {
|
|
987
|
+
if (!into.replayEntry(w[i], w[i + 1], w[i + 2], w[i + 3])) {
|
|
988
|
+
throw new Error(`a compile's log did not replay for ABC ${w[i + 1]}`);
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
|
|
992
|
+
break;
|
|
993
|
+
}
|
|
994
|
+
}
|
|
995
|
+
|
|
996
|
+
return false;
|
|
997
|
+
};
|
|
998
|
+
const replayInto = (into: Bound) => {
|
|
999
|
+
if (checkpoint) {
|
|
1000
|
+
if (!loadCheckpoint(into, checkpoint, true) || !finishRebuild(into)) {
|
|
1001
|
+
throw new Error("the compiler checkpoint did not restore");
|
|
1002
|
+
}
|
|
1003
|
+
} else {
|
|
1004
|
+
into.domainReset(apiVersion);
|
|
1005
|
+
}
|
|
1006
|
+
|
|
1007
|
+
// Any refusal is a divergence: recovery fails, and the domain is lost.
|
|
1008
|
+
for (const entry of journal) {
|
|
1009
|
+
replayOne(into, entry, false);
|
|
1010
|
+
}
|
|
1011
|
+
};
|
|
1012
|
+
const recover = (): boolean => {
|
|
1013
|
+
// A move's spare was to catch up from the journal this replaces.
|
|
1014
|
+
abortMove("superseded");
|
|
1015
|
+
if (!replayable) {
|
|
1016
|
+
return false;
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
let into: typeof wasm;
|
|
1020
|
+
try {
|
|
1021
|
+
into = spare ?? recoveryInstance(module);
|
|
1022
|
+
spare = null;
|
|
1023
|
+
replayInto(into);
|
|
1024
|
+
} catch {
|
|
1025
|
+
replayable = false;
|
|
1026
|
+
return false;
|
|
1027
|
+
}
|
|
1028
|
+
|
|
1029
|
+
// Pruning dropped domains can reuse a native log position. Invalidate
|
|
1030
|
+
// outstanding cache reads even when replay produces the same native revision.
|
|
1031
|
+
revisionEpoch++;
|
|
1032
|
+
// The held SWFs' copies, and the key chains' digests, were the dead instance's.
|
|
1033
|
+
holds.length = 0;
|
|
1034
|
+
chains.clear();
|
|
1035
|
+
wasm = into;
|
|
1036
|
+
return true;
|
|
1037
|
+
};
|
|
1038
|
+
// A move compactStep makes a part at a time (see Move): begun from a
|
|
1039
|
+
// snapshot of this instance, as a move by compact is, and never reading
|
|
1040
|
+
// this instance again, while the host goes on with it, each call it
|
|
1041
|
+
// makes journaled and replayed into the spare once the spare has rebuilt.
|
|
1042
|
+
const startMove = (into: Bound): boolean => {
|
|
1043
|
+
let saved: Checkpoint;
|
|
1044
|
+
try {
|
|
1045
|
+
saved = saveCheckpoint();
|
|
1046
|
+
} catch (error) {
|
|
1047
|
+
if (error instanceof CodegenOutOfMemory) {
|
|
1048
|
+
spare = into;
|
|
1049
|
+
return false;
|
|
1050
|
+
}
|
|
1051
|
+
|
|
1052
|
+
throw error;
|
|
1053
|
+
}
|
|
1054
|
+
|
|
1055
|
+
// Parts that block the host for little: a part's time is its garbage's, but for one large method's verification.
|
|
1056
|
+
into.domainRebuildBudget(MOVE_PartBudget);
|
|
1057
|
+
move = {
|
|
1058
|
+
into,
|
|
1059
|
+
saved,
|
|
1060
|
+
entries: [],
|
|
1061
|
+
replayed: 0,
|
|
1062
|
+
word: 0,
|
|
1063
|
+
restored: new Map(),
|
|
1064
|
+
abcs: null,
|
|
1065
|
+
phase: "load",
|
|
1066
|
+
catchUp: 0,
|
|
1067
|
+
};
|
|
1068
|
+
moves.started++;
|
|
1069
|
+
return true;
|
|
1070
|
+
};
|
|
1071
|
+
// The spare, rebuilt and caught up, takes this instance's place, as a
|
|
1072
|
+
// recovery's does, and the snapshot it rebuilt is the checkpoint, the
|
|
1073
|
+
// calls since it the journal.
|
|
1074
|
+
const swapMove = (m: Move) => {
|
|
1075
|
+
move = null;
|
|
1076
|
+
moves.completed++;
|
|
1077
|
+
const kept = m.entries.filter((entry) => journal.has(entry));
|
|
1078
|
+
moves.lastCaughtUp = kept.length;
|
|
1079
|
+
moves.lastSavedBytes = m.saved.state.length;
|
|
1080
|
+
for (const abc of m.saved.abcs.values()) {
|
|
1081
|
+
moves.lastSavedBytes += abc.length;
|
|
1082
|
+
}
|
|
1083
|
+
|
|
1084
|
+
journal.clear();
|
|
1085
|
+
restores.clear();
|
|
1086
|
+
for (const entry of kept) {
|
|
1087
|
+
journal.add(entry);
|
|
1088
|
+
if (entry.kind === "restore" && !restores.has(entry.index)) {
|
|
1089
|
+
restores.set(entry.index, entry);
|
|
1090
|
+
}
|
|
1091
|
+
}
|
|
1092
|
+
|
|
1093
|
+
for (const domain of journalDomains.values()) {
|
|
1094
|
+
const own = domain.entries?.filter((entry) => journal.has(entry));
|
|
1095
|
+
domain.entries = own?.length ? own : undefined;
|
|
1096
|
+
}
|
|
1097
|
+
|
|
1098
|
+
checkpoint = m.saved;
|
|
1099
|
+
moveRefused = false;
|
|
1100
|
+
revisionEpoch++;
|
|
1101
|
+
holds.length = 0;
|
|
1102
|
+
chains.clear();
|
|
1103
|
+
wasm = m.into;
|
|
1104
|
+
};
|
|
1105
|
+
// One part of the move: a call into the spare, or the swap. Whatever the
|
|
1106
|
+
// spare throws, a panic or running out of memory included, gives the
|
|
1107
|
+
// move up, never this instance, which goes on as it was.
|
|
1108
|
+
const moveUnit = (m: Move): CompactProgress => {
|
|
1109
|
+
if (!replayable) {
|
|
1110
|
+
abortMove("unjournaled");
|
|
1111
|
+
return "none";
|
|
1112
|
+
}
|
|
1113
|
+
|
|
1114
|
+
const into = m.into;
|
|
1115
|
+
try {
|
|
1116
|
+
switch (m.phase) {
|
|
1117
|
+
case "load":
|
|
1118
|
+
if (!into.domainLoad(m.saved.state)) {
|
|
1119
|
+
moveRefused = true;
|
|
1120
|
+
abortMove("refused");
|
|
1121
|
+
return "none";
|
|
1122
|
+
}
|
|
1123
|
+
|
|
1124
|
+
m.abcs = m.saved.abcs.entries();
|
|
1125
|
+
m.phase = "abcs";
|
|
1126
|
+
return "moving";
|
|
1127
|
+
case "abcs": {
|
|
1128
|
+
const next = m.abcs?.next();
|
|
1129
|
+
if (next && !next.done) {
|
|
1130
|
+
if (into.domainLoadAbc(next.value[0], next.value[1]) !== 0) {
|
|
1131
|
+
abortMove("refused");
|
|
1132
|
+
return "none";
|
|
1133
|
+
}
|
|
1134
|
+
|
|
1135
|
+
return "moving";
|
|
1136
|
+
}
|
|
1137
|
+
|
|
1138
|
+
m.abcs = null;
|
|
1139
|
+
if (!into.domainLoadRebuild()) {
|
|
1140
|
+
abortMove("refused");
|
|
1141
|
+
return "none";
|
|
1142
|
+
}
|
|
1143
|
+
|
|
1144
|
+
m.phase = "rebuild";
|
|
1145
|
+
return "moving";
|
|
1146
|
+
}
|
|
1147
|
+
case "rebuild": {
|
|
1148
|
+
const step = into.domainRebuildStep();
|
|
1149
|
+
if (step < 0) {
|
|
1150
|
+
abortMove("refused");
|
|
1151
|
+
return "none";
|
|
1152
|
+
}
|
|
1153
|
+
|
|
1154
|
+
if (step > 0) {
|
|
1155
|
+
m.phase = "catch up";
|
|
1156
|
+
}
|
|
1157
|
+
|
|
1158
|
+
return "moving";
|
|
1159
|
+
}
|
|
1160
|
+
case "catch up": {
|
|
1161
|
+
// Entries of domains dropped since are out of the journal (see
|
|
1162
|
+
// forgetDropped), but one half replayed is finished.
|
|
1163
|
+
while (
|
|
1164
|
+
m.word === 0 &&
|
|
1165
|
+
m.replayed < m.entries.length &&
|
|
1166
|
+
!journal.has(m.entries[m.replayed])
|
|
1167
|
+
) {
|
|
1168
|
+
m.replayed++;
|
|
1169
|
+
}
|
|
1170
|
+
|
|
1171
|
+
if (m.replayed === m.entries.length) {
|
|
1172
|
+
swapMove(m);
|
|
1173
|
+
return "done";
|
|
1174
|
+
}
|
|
1175
|
+
|
|
1176
|
+
// A compile's log a log entry a part, a run of them taking long.
|
|
1177
|
+
const entry = m.entries[m.replayed];
|
|
1178
|
+
if (entry.kind === "log") {
|
|
1179
|
+
const w = entry.words;
|
|
1180
|
+
const i = m.word;
|
|
1181
|
+
if (!into.replayEntry(w[i], w[i + 1], w[i + 2], w[i + 3])) {
|
|
1182
|
+
abortMove("refused");
|
|
1183
|
+
return "none";
|
|
1184
|
+
}
|
|
1185
|
+
|
|
1186
|
+
m.word = i + 4 < w.length ? i + 4 : 0;
|
|
1187
|
+
if (m.word === 0) {
|
|
1188
|
+
m.replayed++;
|
|
1189
|
+
}
|
|
1190
|
+
|
|
1191
|
+
return "moving";
|
|
1192
|
+
}
|
|
1193
|
+
|
|
1194
|
+
m.replayed++;
|
|
1195
|
+
// A revival that rebuilds the spare: catch-up starts over after it.
|
|
1196
|
+
if (replayOne(into, entry, true)) {
|
|
1197
|
+
m.phase = "rebuild";
|
|
1198
|
+
m.catchUp = 0;
|
|
1199
|
+
}
|
|
1200
|
+
|
|
1201
|
+
return "moving";
|
|
1202
|
+
}
|
|
1203
|
+
}
|
|
1204
|
+
} catch (error) {
|
|
1205
|
+
abortMove(
|
|
1206
|
+
error instanceof CodegenPanic || error instanceof CodegenOutOfMemory ? "spare" : "refused",
|
|
1207
|
+
);
|
|
1208
|
+
return "none";
|
|
1209
|
+
}
|
|
638
1210
|
};
|
|
1211
|
+
// Whether the move's next part replays a call that takes long in one:
|
|
1212
|
+
// a compile's verification of a large ABC, as long as the compile
|
|
1213
|
+
// verified it for, or the add of a large ABC, which parses and links it.
|
|
1214
|
+
const longNext = (m: Move) => {
|
|
1215
|
+
const entry = m.phase === "catch up" ? m.entries[m.replayed] : undefined;
|
|
1216
|
+
if (entry?.kind === "add") {
|
|
1217
|
+
return entry.abc.length > LARGE_Add;
|
|
1218
|
+
}
|
|
1219
|
+
|
|
1220
|
+
return (
|
|
1221
|
+
entry?.kind === "log" &&
|
|
1222
|
+
entry.words[m.word] === LOG_Verify &&
|
|
1223
|
+
(abcLengths[entry.words[m.word + 1]] ?? 0) > LARGE_Verify
|
|
1224
|
+
);
|
|
1225
|
+
};
|
|
1226
|
+
// What compactStep starts, as compact decides, but that a large domain
|
|
1227
|
+
// moves rather than rebuild in place, which would take one call.
|
|
1228
|
+
const beginCompaction = (): CompactProgress => {
|
|
1229
|
+
if (spare === null && !making && wasm.domainWantsCompact(WANT_Spare)) {
|
|
1230
|
+
prepare();
|
|
1231
|
+
}
|
|
1232
|
+
|
|
1233
|
+
const wantsMove = wasm.domainWantsCompact(WANT_Move);
|
|
1234
|
+
const beside = spare !== null || making;
|
|
1235
|
+
const inPlace =
|
|
1236
|
+
!wantsMove && wasm.domainWantsCompact(beside ? WANT_InPlaceBesideSpare : WANT_InPlace);
|
|
1237
|
+
if (!wantsMove && !inPlace) {
|
|
1238
|
+
return "none";
|
|
1239
|
+
}
|
|
639
1240
|
|
|
640
|
-
|
|
1241
|
+
if (inPlace && (moveRefused || Number(wasm.domainUsage().split(" ")[0]) < IN_PLACE_Live)) {
|
|
1242
|
+
return wasm.domainCompact(true) && rebuild(wasm, null, true) ? "done" : "none";
|
|
1243
|
+
}
|
|
1244
|
+
|
|
1245
|
+
// A spare could not catch up from the journal: compacted in one call, as compact does.
|
|
1246
|
+
if (!replayable) {
|
|
1247
|
+
return api.compact() ? "done" : "none";
|
|
1248
|
+
}
|
|
1249
|
+
|
|
1250
|
+
// Backed off after a move that failed.
|
|
1251
|
+
if (lettings < retryAt) {
|
|
1252
|
+
return "none";
|
|
1253
|
+
}
|
|
1254
|
+
|
|
1255
|
+
const into = spare;
|
|
1256
|
+
if (into === null) {
|
|
1257
|
+
// Called again once the spare is made, as instantiating is asynchronous.
|
|
1258
|
+
prepare();
|
|
1259
|
+
return making ? "moving" : "none";
|
|
1260
|
+
}
|
|
1261
|
+
|
|
1262
|
+
spare = null;
|
|
1263
|
+
return startMove(into) ? "moving" : "none";
|
|
1264
|
+
};
|
|
1265
|
+
// Whether the domain was lost to a panic recovery could not undo (see fresh).
|
|
1266
|
+
let lost = false;
|
|
1267
|
+
const api: Codegen = {
|
|
641
1268
|
identity,
|
|
642
1269
|
abcVersion(abc) {
|
|
643
|
-
const packed =
|
|
1270
|
+
const packed = wasm.abcVersion(abc);
|
|
644
1271
|
return packed < 0 ? null : { major: packed >>> 16, minor: packed & 0xffff };
|
|
645
1272
|
},
|
|
646
1273
|
get epoch() {
|
|
@@ -648,21 +1275,50 @@ export async function createCodegen(module: WebAssembly.Module): Promise<Codegen
|
|
|
648
1275
|
},
|
|
649
1276
|
reset(version = API_VERSION) {
|
|
650
1277
|
checkApiVersion(version);
|
|
1278
|
+
abortMove("superseded");
|
|
1279
|
+
moveRefused = false;
|
|
1280
|
+
retryAt = 0;
|
|
1281
|
+
wasm.domainReset(version);
|
|
651
1282
|
added = 0;
|
|
652
1283
|
epoch++;
|
|
1284
|
+
revisionEpoch++;
|
|
1285
|
+
journalDomains.clear();
|
|
653
1286
|
chains.clear();
|
|
654
1287
|
apiVersion = version;
|
|
655
|
-
|
|
1288
|
+
journal.clear();
|
|
1289
|
+
restores.clear();
|
|
1290
|
+
abcDomains.length = 0;
|
|
1291
|
+
abcLengths.length = 0;
|
|
1292
|
+
replayable = true;
|
|
1293
|
+
checkpoint = null;
|
|
656
1294
|
},
|
|
657
1295
|
get apiVersion() {
|
|
658
1296
|
return apiVersion;
|
|
659
1297
|
},
|
|
660
1298
|
add(abc, builtin = false, appDomain = 0) {
|
|
661
|
-
const
|
|
1299
|
+
const bytes = new Uint8Array(abc);
|
|
1300
|
+
const error = wasm.domainAdd(bytes, builtin, appDomain);
|
|
662
1301
|
if (error < 0) {
|
|
663
1302
|
throw new Error(`application domain ${appDomain}: never made, or dropped`);
|
|
664
1303
|
}
|
|
665
1304
|
|
|
1305
|
+
if (error === 0 || wasm.lastAddLinked()) {
|
|
1306
|
+
if (error === 0) {
|
|
1307
|
+
abcDomains.push(appDomain);
|
|
1308
|
+
abcLengths.push(bytes.length);
|
|
1309
|
+
}
|
|
1310
|
+
journaled(
|
|
1311
|
+
{
|
|
1312
|
+
kind: "add",
|
|
1313
|
+
abc: bytes,
|
|
1314
|
+
builtin,
|
|
1315
|
+
appDomain,
|
|
1316
|
+
index: error === 0 ? added : -1,
|
|
1317
|
+
error,
|
|
1318
|
+
},
|
|
1319
|
+
appDomain,
|
|
1320
|
+
);
|
|
1321
|
+
}
|
|
666
1322
|
if (error === 0) {
|
|
667
1323
|
added++;
|
|
668
1324
|
}
|
|
@@ -670,103 +1326,236 @@ export async function createCodegen(module: WebAssembly.Module): Promise<Codegen
|
|
|
670
1326
|
return error;
|
|
671
1327
|
},
|
|
672
1328
|
childDomain(parent) {
|
|
673
|
-
const domain =
|
|
1329
|
+
const domain = wasm.domainChild(parent);
|
|
674
1330
|
if (domain < 0) {
|
|
675
1331
|
throw new Error(`application domain ${parent}: never made, or dropped`);
|
|
676
1332
|
}
|
|
677
1333
|
|
|
1334
|
+
const owner = journalDomains.get(parent | 0);
|
|
1335
|
+
const child = { id: domain, parent: owner };
|
|
1336
|
+
if (owner) {
|
|
1337
|
+
owner.children ??= new Set();
|
|
1338
|
+
owner.children.add(child);
|
|
1339
|
+
}
|
|
1340
|
+
|
|
1341
|
+
journalDomains.set(domain, child);
|
|
1342
|
+
journaled({ kind: "child", parent });
|
|
1343
|
+
|
|
678
1344
|
return domain;
|
|
679
1345
|
},
|
|
680
1346
|
dropDomain(appDomain, of = epoch) {
|
|
681
1347
|
if (of === epoch) {
|
|
682
|
-
|
|
1348
|
+
lettings++;
|
|
1349
|
+
wasm.domainDrop(appDomain);
|
|
1350
|
+
journaled({ kind: "drop", appDomain });
|
|
1351
|
+
forgetDropped(appDomain);
|
|
1352
|
+
|
|
683
1353
|
chains.forget((d) => wasm.domainState(d) === 2);
|
|
684
1354
|
}
|
|
685
1355
|
},
|
|
686
1356
|
evictDomain(appDomain) {
|
|
687
|
-
|
|
1357
|
+
lettings++;
|
|
1358
|
+
wasm.domainEvict(appDomain);
|
|
1359
|
+
journaled({ kind: "evict", appDomain });
|
|
688
1360
|
},
|
|
689
1361
|
usage() {
|
|
690
|
-
const [live, dead, memory] =
|
|
691
|
-
return {
|
|
1362
|
+
const [live, dead, memory] = wasm.domainUsage().split(" ").map(Number);
|
|
1363
|
+
return {
|
|
1364
|
+
live,
|
|
1365
|
+
dead,
|
|
1366
|
+
memory,
|
|
1367
|
+
failedMoves,
|
|
1368
|
+
moves: { ...moves, aborted: { ...moves.aborted } },
|
|
1369
|
+
recovery: recoveryBytes(),
|
|
1370
|
+
};
|
|
692
1371
|
},
|
|
693
1372
|
memoryBytes() {
|
|
694
1373
|
return wasm.memory.buffer.byteLength;
|
|
695
1374
|
},
|
|
696
1375
|
compact() {
|
|
1376
|
+
// A move compactStep has under way, finished here.
|
|
1377
|
+
const m = move;
|
|
1378
|
+
if (m !== null) {
|
|
1379
|
+
let progress = moveUnit(m);
|
|
1380
|
+
while (progress === "moving") {
|
|
1381
|
+
progress = moveUnit(m);
|
|
1382
|
+
}
|
|
1383
|
+
|
|
1384
|
+
if (progress === "done") {
|
|
1385
|
+
return true;
|
|
1386
|
+
}
|
|
1387
|
+
}
|
|
1388
|
+
|
|
697
1389
|
// Done in the spare instance, once there is one and that would take
|
|
698
1390
|
// less memory, or in place would not fit; else in place, where the
|
|
699
1391
|
// new tables are made while the old are live, only where they fit
|
|
700
|
-
// and, beside a spare made or being made, a move would not do better
|
|
701
|
-
|
|
702
|
-
// it is done, before the next call's allocations would grow memory
|
|
703
|
-
// past them.
|
|
704
|
-
if (spare === null && !making && collected(wasm.domainWantsCompact(WANT_Spare))) {
|
|
1392
|
+
// and, beside a spare made or being made, a move would not do better.
|
|
1393
|
+
if (spare === null && !making && wasm.domainWantsCompact(WANT_Spare)) {
|
|
705
1394
|
prepare();
|
|
706
1395
|
}
|
|
707
1396
|
|
|
708
|
-
if (spare !== null &&
|
|
709
|
-
wasm.__collect();
|
|
1397
|
+
if (spare !== null && wasm.domainWantsCompact(WANT_Move)) {
|
|
710
1398
|
if (moved()) {
|
|
711
1399
|
return true;
|
|
712
1400
|
}
|
|
713
1401
|
}
|
|
714
1402
|
|
|
715
1403
|
const beside = spare !== null || making;
|
|
716
|
-
if (!
|
|
1404
|
+
if (!wasm.domainWantsCompact(beside ? WANT_InPlaceBesideSpare : WANT_InPlace)) {
|
|
717
1405
|
return false;
|
|
718
1406
|
}
|
|
719
1407
|
|
|
720
|
-
wasm.
|
|
721
|
-
|
|
722
|
-
|
|
1408
|
+
return wasm.domainCompact(true) && rebuild(wasm, null, true);
|
|
1409
|
+
},
|
|
1410
|
+
compactStep(deadline) {
|
|
1411
|
+
let m = move;
|
|
1412
|
+
if (m === null) {
|
|
1413
|
+
const begun = beginCompaction();
|
|
1414
|
+
if (begun !== "moving" || move === null) {
|
|
1415
|
+
return begun;
|
|
1416
|
+
}
|
|
1417
|
+
|
|
1418
|
+
m = move;
|
|
1419
|
+
}
|
|
1420
|
+
|
|
1421
|
+
// Catch-up chases what the host goes on doing. Past CATCH_UP_Slices
|
|
1422
|
+
// slices it goes on CATCH_UP_Overrun past the deadline, a long part
|
|
1423
|
+
// still starting a slice of its own; past twice as many, it finishes
|
|
1424
|
+
// in this slice, which has only what came in since the last to
|
|
1425
|
+
// replay (a revival's rebuild still a part at a time).
|
|
1426
|
+
let slices = 0;
|
|
1427
|
+
if (m.phase === "catch up") {
|
|
1428
|
+
slices = ++m.catchUp;
|
|
1429
|
+
}
|
|
1430
|
+
|
|
1431
|
+
for (;;) {
|
|
1432
|
+
const progress = moveUnit(m);
|
|
1433
|
+
if (progress !== "moving") {
|
|
1434
|
+
return progress;
|
|
1435
|
+
}
|
|
1436
|
+
|
|
1437
|
+
const catchingUp = m.phase === "catch up";
|
|
1438
|
+
if (catchingUp && slices > CATCH_UP_Slices * 2) {
|
|
1439
|
+
continue;
|
|
1440
|
+
}
|
|
1441
|
+
|
|
1442
|
+
// A part that takes long in one call starts a slice of its own,
|
|
1443
|
+
// so that it adds to no other part's time.
|
|
1444
|
+
if (longNext(m)) {
|
|
1445
|
+
return "moving";
|
|
1446
|
+
}
|
|
1447
|
+
|
|
1448
|
+
const overrun = catchingUp && slices > CATCH_UP_Slices ? CATCH_UP_Overrun : 0;
|
|
1449
|
+
if (deadline.timeRemaining() <= -overrun) {
|
|
1450
|
+
return "moving";
|
|
1451
|
+
}
|
|
1452
|
+
}
|
|
723
1453
|
},
|
|
724
1454
|
isLive(appDomain) {
|
|
725
|
-
return
|
|
1455
|
+
return wasm.domainState(appDomain) === 0;
|
|
726
1456
|
},
|
|
727
1457
|
reviveDomain(appDomain, abcs) {
|
|
1458
|
+
// A move's spare lets go of the ABCs of the domains evicted before its
|
|
1459
|
+
// snapshot, and before any rebuild a replayed revival makes in it,
|
|
1460
|
+
// which this instance may still hold: restores journaled before the
|
|
1461
|
+
// revival give each evicted ABC back to it (see replayOne).
|
|
1462
|
+
const evictedGiven =
|
|
1463
|
+
move === null
|
|
1464
|
+
? []
|
|
1465
|
+
: [...abcs.keys()].filter(
|
|
1466
|
+
(index) =>
|
|
1467
|
+
Number.isInteger(index) &&
|
|
1468
|
+
index >= 0 &&
|
|
1469
|
+
index < abcDomains.length &&
|
|
1470
|
+
wasm.domainState(abcDomains[index]) === 1,
|
|
1471
|
+
);
|
|
728
1472
|
// Its ABCs' bytes are copied into the compiler only if a rebuild let go of them.
|
|
729
|
-
let revived =
|
|
1473
|
+
let revived = wasm.domainRevive(appDomain, true);
|
|
730
1474
|
if (revived < 0) {
|
|
731
1475
|
for (const [index, bytes] of abcs) {
|
|
732
|
-
const
|
|
1476
|
+
const owned = new Uint8Array(bytes);
|
|
1477
|
+
const error = wasm.domainRestore(index, owned);
|
|
733
1478
|
if (error !== 0) {
|
|
734
1479
|
throw new Error(`ABC ${index}: not of an evicted domain, or rejected (${error})`);
|
|
735
1480
|
}
|
|
1481
|
+
// One restore an ABC, the first: a revival retried gives its
|
|
1482
|
+
// bytes again, which the compiler, holding them, does not take.
|
|
1483
|
+
if (!restores.has(index)) {
|
|
1484
|
+
const entry: Entry = { kind: "restore", index, bytes: owned };
|
|
1485
|
+
restores.set(index, entry);
|
|
1486
|
+
journaled(entry, abcDomains[index]);
|
|
1487
|
+
}
|
|
736
1488
|
}
|
|
737
1489
|
|
|
738
|
-
revived =
|
|
1490
|
+
revived = wasm.domainRevive(appDomain, true);
|
|
739
1491
|
}
|
|
740
1492
|
|
|
741
1493
|
// Revived by a rebuild in place, mid-load: a compaction moves it later.
|
|
742
|
-
|
|
1494
|
+
const done = revived === 0 || (revived > 0 && rebuild());
|
|
1495
|
+
if (revived === 0) {
|
|
1496
|
+
// Owned by the root, not by the domain: a revival the journal keeps
|
|
1497
|
+
// needs them though the domain is dropped later (a drop forgets the
|
|
1498
|
+
// domain's own entries, see forgetDropped), until the next checkpoint.
|
|
1499
|
+
if (move !== null) {
|
|
1500
|
+
for (const index of evictedGiven) {
|
|
1501
|
+
const given = abcs.get(index);
|
|
1502
|
+
if (given !== undefined) {
|
|
1503
|
+
let bytes = move.restored.get(index);
|
|
1504
|
+
if (bytes === undefined) {
|
|
1505
|
+
bytes = new Uint8Array(given);
|
|
1506
|
+
move.restored.set(index, bytes);
|
|
1507
|
+
}
|
|
1508
|
+
|
|
1509
|
+
journaled({ kind: "restore", index, bytes });
|
|
1510
|
+
}
|
|
1511
|
+
}
|
|
1512
|
+
}
|
|
1513
|
+
|
|
1514
|
+
journaled({ kind: "revive", appDomain });
|
|
1515
|
+
}
|
|
1516
|
+
|
|
1517
|
+
if (!done) {
|
|
743
1518
|
throw new Error(
|
|
744
1519
|
`application domain ${appDomain}: not evicted, its parent not live, or an ABC of it not given`,
|
|
745
1520
|
);
|
|
746
1521
|
}
|
|
747
1522
|
},
|
|
748
|
-
found(
|
|
749
|
-
|
|
1523
|
+
found({ domain, nsKind, uri, name, abc, asType }) {
|
|
1524
|
+
wasm.domainFound(domain, nsKind, uri, name, abc, asType);
|
|
1525
|
+
// A finding of an ABC outside the finder's chain, which the player
|
|
1526
|
+
// never reports, is not journaled: its replay can part from the
|
|
1527
|
+
// compile (the compiler takes it unchecked).
|
|
1528
|
+
if (inChain(abcDomains[abc] ?? -1, domain)) {
|
|
1529
|
+
journaled(
|
|
1530
|
+
{ kind: "found", definition: { domain, nsKind, uri, name, abc, asType } },
|
|
1531
|
+
domain,
|
|
1532
|
+
);
|
|
1533
|
+
} else if (move !== null && wasm.domainState(domain) >= 0 && wasm.domainState(domain) < 2) {
|
|
1534
|
+
// The spare could part from this instance by it; one of a domain
|
|
1535
|
+
// dropped or never made the compiler ignored.
|
|
1536
|
+
abortMove("unjournaled");
|
|
1537
|
+
}
|
|
750
1538
|
},
|
|
751
1539
|
compile(hashes = [], index = -1) {
|
|
752
1540
|
last("compile");
|
|
753
|
-
// Each part a call of its own,
|
|
754
|
-
// is gone before its entries are written.
|
|
755
|
-
const module =
|
|
756
|
-
const sourceMap =
|
|
757
|
-
const entries = parseEntries(
|
|
1541
|
+
// Each part a call of its own, whose arena the next gives back: the
|
|
1542
|
+
// module's garbage is gone before its entries are written.
|
|
1543
|
+
const module = compiled(hashes, index, true);
|
|
1544
|
+
const sourceMap = wasm.domainSourceMap();
|
|
1545
|
+
const entries = parseEntries(wasm.domainModuleEntries());
|
|
758
1546
|
return { module, sourceMap, entries };
|
|
759
1547
|
},
|
|
760
1548
|
compileModule(hashes = [], index = -1) {
|
|
761
1549
|
last("compileModule");
|
|
762
|
-
return
|
|
1550
|
+
return compiled(hashes, index, false);
|
|
763
1551
|
},
|
|
764
1552
|
context(index = -1, after = 0, upTo = -1) {
|
|
765
|
-
|
|
1553
|
+
// A domain lost to a panic has no context until the next reset.
|
|
1554
|
+
if (added === 0 || lost) {
|
|
766
1555
|
return null;
|
|
767
1556
|
}
|
|
768
1557
|
|
|
769
|
-
const text =
|
|
1558
|
+
const text = wasm.domainContext(indexOf(index), after, upTo);
|
|
770
1559
|
if (text === "") {
|
|
771
1560
|
return null;
|
|
772
1561
|
}
|
|
@@ -785,27 +1574,28 @@ export async function createCodegen(module: WebAssembly.Module): Promise<Codegen
|
|
|
785
1574
|
return null;
|
|
786
1575
|
}
|
|
787
1576
|
|
|
788
|
-
return
|
|
1577
|
+
return chains.context(indexOf(index), after);
|
|
789
1578
|
},
|
|
790
1579
|
revision() {
|
|
791
|
-
return `${
|
|
1580
|
+
return `${revisionEpoch} ${wasm.domainRevision()}`;
|
|
792
1581
|
},
|
|
793
1582
|
compileModuleLogged(hashes = [], index = -1) {
|
|
794
1583
|
last("compileModuleLogged");
|
|
795
1584
|
const mark = wasm.domainLogMark();
|
|
796
|
-
const module =
|
|
797
|
-
const log =
|
|
1585
|
+
const module = compiled(hashes, index, false);
|
|
1586
|
+
const log = wasm.domainLogSince(mark, indexOf(index));
|
|
798
1587
|
return { module, log: log === "!" ? null : log };
|
|
799
1588
|
},
|
|
800
1589
|
replay(log, index = -1) {
|
|
801
1590
|
last("replay");
|
|
802
|
-
|
|
1591
|
+
const replay = () => wasm.domainReplay(log, indexOf(index));
|
|
1592
|
+
return recordChanges(replay);
|
|
803
1593
|
},
|
|
804
1594
|
compileActions(bytes, actions) {
|
|
805
1595
|
const module = actionsModule(bytes, actions, true);
|
|
806
|
-
const entries = parseEntries(
|
|
1596
|
+
const entries = parseEntries(wasm.actionsModuleEntries());
|
|
807
1597
|
const units = new Map<string, ModuleUnit>();
|
|
808
|
-
for (const item of
|
|
1598
|
+
for (const item of wasm.actionsModuleUnits().split("\u0002")) {
|
|
809
1599
|
const [key, numbers, limits, code] = item.split("\u0001");
|
|
810
1600
|
if (code !== undefined) {
|
|
811
1601
|
const [at, end, bound, five] = numbers.split(",").map(Number);
|
|
@@ -832,14 +1622,14 @@ export async function createCodegen(module: WebAssembly.Module): Promise<Codegen
|
|
|
832
1622
|
const address = wasm.actionsHeldEmitEachBytes(handle, actions.version, indices.join(","));
|
|
833
1623
|
const text = handed(address, wasm.outputLength());
|
|
834
1624
|
// A block's analysis can go before the next block runs.
|
|
835
|
-
return parseEntries(
|
|
1625
|
+
return parseEntries(text);
|
|
836
1626
|
},
|
|
837
1627
|
compileActionUnit(bytes, actions, { at, end, bound, five }) {
|
|
838
1628
|
const handle = hold(bytes, actions);
|
|
839
1629
|
const address = wasm.actionsHeldUnitBytes(handle, actions.version, at, end, bound, five);
|
|
840
1630
|
const code = handed(address, wasm.outputLength());
|
|
841
1631
|
const [high, reach] = wasm.actionsUnitLimits();
|
|
842
|
-
return
|
|
1632
|
+
return { code, high, reach };
|
|
843
1633
|
},
|
|
844
1634
|
releaseActions(bytes) {
|
|
845
1635
|
const known = holding.get(bytes);
|
|
@@ -850,11 +1640,117 @@ export async function createCodegen(module: WebAssembly.Module): Promise<Codegen
|
|
|
850
1640
|
}
|
|
851
1641
|
},
|
|
852
1642
|
heldActions() {
|
|
853
|
-
return { held:
|
|
1643
|
+
return { held: wasm.actionsHeldCount(), copies };
|
|
854
1644
|
},
|
|
855
1645
|
};
|
|
1646
|
+
// Each method recovers from a panic of the compiler (see recover): an
|
|
1647
|
+
// ABC whose load panicked is rejected as a corrupt one is, and any other
|
|
1648
|
+
// call that panicked throws a plain Error, the compiler going on. Where
|
|
1649
|
+
// recovery cannot be, the domain is lost, but not the compiler: it goes
|
|
1650
|
+
// on in a fresh instance, for a reset and what needs no domain (AVM1),
|
|
1651
|
+
// and the domain's calls throw until the next reset.
|
|
1652
|
+
const fresh = () => {
|
|
1653
|
+
abortMove("superseded");
|
|
1654
|
+
wasm = spare ?? recoveryInstance(module);
|
|
1655
|
+
spare = null;
|
|
1656
|
+
holds.length = 0;
|
|
1657
|
+
chains.clear();
|
|
1658
|
+
};
|
|
1659
|
+
const descriptors = Object.getOwnPropertyDescriptors(api);
|
|
1660
|
+
for (const [name, descriptor] of Object.entries(descriptors)) {
|
|
1661
|
+
const method = descriptor.value;
|
|
1662
|
+
if (typeof method !== "function") {
|
|
1663
|
+
continue;
|
|
1664
|
+
}
|
|
1665
|
+
|
|
1666
|
+
const domainless = DOMAINLESS.has(name);
|
|
1667
|
+
descriptor.value = (...args: unknown[]) => {
|
|
1668
|
+
// An instance left dead outside a call of this wrapper (a finalizer's
|
|
1669
|
+
// release) is recovered from first, or given way to.
|
|
1670
|
+
if ((wasm as { panicked?: () => string | null }).panicked?.() != null && !recover()) {
|
|
1671
|
+
fresh();
|
|
1672
|
+
lost = true;
|
|
1673
|
+
}
|
|
1674
|
+
|
|
1675
|
+
// The player drops a domain from a FinalizationRegistry's callback,
|
|
1676
|
+
// and compacts from a timer, where a throw would go nowhere: a lost
|
|
1677
|
+
// domain has nothing to drop, nor to compact.
|
|
1678
|
+
if (lost && name === "dropDomain") {
|
|
1679
|
+
return undefined;
|
|
1680
|
+
}
|
|
1681
|
+
|
|
1682
|
+
if (lost && name === "compact") {
|
|
1683
|
+
return false;
|
|
1684
|
+
}
|
|
1685
|
+
|
|
1686
|
+
if (lost && name === "compactStep") {
|
|
1687
|
+
return "none";
|
|
1688
|
+
}
|
|
1689
|
+
|
|
1690
|
+
if (lost && !domainless) {
|
|
1691
|
+
throw new Error(
|
|
1692
|
+
`${name}: the compiler's domain was lost to a panic it could not recover from; reset it`,
|
|
1693
|
+
);
|
|
1694
|
+
}
|
|
1695
|
+
|
|
1696
|
+
try {
|
|
1697
|
+
const result = method.apply(api, args);
|
|
1698
|
+
if (name === "reset") {
|
|
1699
|
+
lost = false;
|
|
1700
|
+
}
|
|
1701
|
+
|
|
1702
|
+
return result;
|
|
1703
|
+
} catch (error) {
|
|
1704
|
+
if (!(error instanceof CodegenPanic)) {
|
|
1705
|
+
throw error;
|
|
1706
|
+
}
|
|
1707
|
+
|
|
1708
|
+
if (!recover()) {
|
|
1709
|
+
// A reset that panicked too: what the wrapper keeps of the domain
|
|
1710
|
+
// (its ABCs, journal, checkpoint and API version) is the dead one's.
|
|
1711
|
+
fresh();
|
|
1712
|
+
lost = true;
|
|
1713
|
+
throw new Error(
|
|
1714
|
+
`${name}: ${error.message}; the compiler went on in a new instance, its domain lost`,
|
|
1715
|
+
);
|
|
1716
|
+
}
|
|
1717
|
+
|
|
1718
|
+
if (name === "add") {
|
|
1719
|
+
return PANICKED_ABC;
|
|
1720
|
+
}
|
|
1721
|
+
|
|
1722
|
+
throw new Error(`${name}: ${error.message}; the compiler went on in a new instance`);
|
|
1723
|
+
}
|
|
1724
|
+
};
|
|
1725
|
+
}
|
|
1726
|
+
|
|
1727
|
+
return Object.defineProperties({}, descriptors) as Codegen;
|
|
856
1728
|
}
|
|
857
1729
|
|
|
1730
|
+
/** An instance of the compiler, its exports bound. */
|
|
1731
|
+
type Bound = CodegenExports & { panicked(): string | null };
|
|
1732
|
+
|
|
1733
|
+
/** What a lost domain does not stop (see lost): a reset, AVM1, and what reads no domain. */
|
|
1734
|
+
const DOMAINLESS = new Set([
|
|
1735
|
+
"reset",
|
|
1736
|
+
"abcVersion",
|
|
1737
|
+
"compileActions",
|
|
1738
|
+
"compileActionsModule",
|
|
1739
|
+
"compileActionBlocks",
|
|
1740
|
+
"compileActionUnit",
|
|
1741
|
+
"releaseActions",
|
|
1742
|
+
"heldActions",
|
|
1743
|
+
"memoryBytes",
|
|
1744
|
+
"usage",
|
|
1745
|
+
"context",
|
|
1746
|
+
]);
|
|
1747
|
+
|
|
1748
|
+
/** An ABC with nothing in it, which a dropped domain's ABCs give way to in the journal of calls. */
|
|
1749
|
+
const EMPTY_ABC = Uint8Array.of(16, 0, 46, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
|
1750
|
+
|
|
1751
|
+
/** The VerifyError an ABC whose load panicked Zig's compiler is rejected with: kCorruptABCError. */
|
|
1752
|
+
const PANICKED_ABC = 1107;
|
|
1753
|
+
|
|
858
1754
|
/** Entries as the wasm writes them: for each, the body's index, U+0001, its entry, U+0002. */
|
|
859
1755
|
function parseEntries(text: string): Map<number, string> {
|
|
860
1756
|
const entries = new Map<number, string>();
|
|
@@ -868,14 +1764,33 @@ function parseEntries(text: string): Map<number, string> {
|
|
|
868
1764
|
return entries;
|
|
869
1765
|
}
|
|
870
1766
|
|
|
871
|
-
|
|
872
|
-
/** What domainWantsCompact is asked (see wantsRebuild in assembly/avm2/link/lifetime.ts). */
|
|
1767
|
+
/** What domainWantsCompact is asked: lifetime.zig's Want, by number (a unit test holds them alike). */
|
|
873
1768
|
const WANT_InPlace = 0;
|
|
874
1769
|
const WANT_InPlaceBesideSpare = 1;
|
|
875
1770
|
const WANT_Move = 2;
|
|
876
1771
|
const WANT_Spare = 3;
|
|
877
|
-
/**
|
|
878
|
-
|
|
1772
|
+
/**
|
|
1773
|
+
* The live weight (domainUsage's first number) below which compactStep
|
|
1774
|
+
* rebuilds in place, in one call, rather than move a part at a time.
|
|
1775
|
+
*/
|
|
1776
|
+
const IN_PLACE_Live = 4 << 20;
|
|
1777
|
+
/** A compile's log entry that verified an ABC: LogKind.verify (avm2/link/domain.zig). */
|
|
1778
|
+
const LOG_Verify = 4;
|
|
1779
|
+
/**
|
|
1780
|
+
* The length of an ABC whose verification, replayed, starts a slice of its
|
|
1781
|
+
* own: some 20 ms of it on real content, 750 ms for 2.3 MB.
|
|
1782
|
+
*/
|
|
1783
|
+
const LARGE_Verify = 64 << 10;
|
|
1784
|
+
/** The garbage a part of a move's rebuild makes at most (domainRebuildBudget). */
|
|
1785
|
+
const MOVE_PartBudget = 256 << 10;
|
|
1786
|
+
/** The slices compactStep catches a move up in before it goes on past their deadlines (see compactStep). */
|
|
1787
|
+
const CATCH_UP_Slices = 32;
|
|
1788
|
+
/** How far past its deadline a slice catches up after CATCH_UP_Slices, in milliseconds. */
|
|
1789
|
+
const CATCH_UP_Overrun = 50;
|
|
1790
|
+
/** The length of an ABC whose add, replayed, starts a slice of its own: some 4 ms of parsing and linking. */
|
|
1791
|
+
const LARGE_Add = 256 << 10;
|
|
1792
|
+
/** The drops and evictions after a move given up as failed before compactStep starts another. */
|
|
1793
|
+
const RETRY_Lettings = 16;
|
|
879
1794
|
/** How many SWFs whose blocks or units are compiled codegen holds a copy of at most. */
|
|
880
1795
|
const HELD_SWFS = 4;
|
|
881
1796
|
|