@swf2es/codegen 0.1.35 → 0.1.37
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/dist/codegen.d.ts +69 -3
- package/dist/codegen.js +46 -6
- package/dist/codegen.wasm +0 -0
- package/dist/index.d.ts +31 -8
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +160 -28
- package/dist/index.js.map +1 -1
- package/dist/keys.d.ts +46 -0
- package/dist/keys.d.ts.map +1 -0
- package/dist/keys.js +201 -0
- package/dist/keys.js.map +1 -0
- package/dist/sha256.d.ts +7 -0
- package/dist/sha256.d.ts.map +1 -0
- package/dist/sha256.js +111 -0
- package/dist/sha256.js.map +1 -0
- package/package.json +2 -2
- package/src/index.ts +197 -37
- package/src/keys.ts +296 -0
- package/src/sha256.ts +127 -0
package/src/index.ts
CHANGED
|
@@ -8,6 +8,10 @@
|
|
|
8
8
|
*/
|
|
9
9
|
import { AIR_API_VERSION, API_VERSION, checkApiVersion, isAirApiVersion } from "@swf2es/format";
|
|
10
10
|
import { instantiate } from "./codegen.js";
|
|
11
|
+
import { KeyChains, type KeyContext } from "./keys.js";
|
|
12
|
+
|
|
13
|
+
export { KeyChains, type KeyContext, type KeyLog } from "./keys.js";
|
|
14
|
+
export { hex, sha256Here } from "./sha256.js";
|
|
11
15
|
|
|
12
16
|
/** Bumped whenever generated code or the runtime ABI it calls changes. */
|
|
13
17
|
export const COMPILER_VERSION = "0.0.0";
|
|
@@ -47,9 +51,11 @@ export function libraryLog(codegen: Codegen, index: number, count: number): stri
|
|
|
47
51
|
* SWFs of several versions share the libraries' modules), every ABC its
|
|
48
52
|
* domain sees, by hash and whether it is a library, those added after it
|
|
49
53
|
* included, its own place among them, and the compiler's log about them
|
|
50
|
-
* (see Codegen.context), the libraries' part by its digest once taken
|
|
51
|
-
*
|
|
52
|
-
*
|
|
54
|
+
* (see Codegen.context), the libraries' part by its digest once taken and
|
|
55
|
+
* the rest by the chained digests of its blocks of entries but the last
|
|
56
|
+
* few (see Codegen.keyContext). The player's module cache and the swf2es
|
|
57
|
+
* command both key by it, so that the command's modules are the cache's.
|
|
58
|
+
* Null where there is no context.
|
|
53
59
|
*/
|
|
54
60
|
export function moduleKey(
|
|
55
61
|
codegen: Codegen,
|
|
@@ -57,7 +63,7 @@ export function moduleKey(
|
|
|
57
63
|
abcs: AddedAbcs,
|
|
58
64
|
libraries: LibraryDigest | null,
|
|
59
65
|
): string | null {
|
|
60
|
-
const context = codegen.
|
|
66
|
+
const context = codegen.keyContext(index, libraries?.count ?? 0);
|
|
61
67
|
if (!context) {
|
|
62
68
|
return null;
|
|
63
69
|
}
|
|
@@ -70,7 +76,8 @@ export function moduleKey(
|
|
|
70
76
|
abcs: context.abcs.map((i) => `${abcs.builtins[i] ? "builtin " : ""}${abcs.hashes[i]}`),
|
|
71
77
|
own: context.own,
|
|
72
78
|
libraries,
|
|
73
|
-
|
|
79
|
+
blocks: context.blocks,
|
|
80
|
+
log: context.rest,
|
|
74
81
|
});
|
|
75
82
|
}
|
|
76
83
|
|
|
@@ -234,18 +241,24 @@ export interface Codegen {
|
|
|
234
241
|
/**
|
|
235
242
|
* Rebuild the domain without the ABCs of dropped and evicted domains, if
|
|
236
243
|
* memory would otherwise grow and they hold enough to keep it from
|
|
237
|
-
* growing
|
|
238
|
-
*
|
|
239
|
-
*
|
|
244
|
+
* growing, or memory is far above what the live ones need: whether it
|
|
245
|
+
* did. Done in a new instance of the compiler, made ahead, which takes
|
|
246
|
+
* the old one's place, so that its memory goes with it, where that takes
|
|
247
|
+
* less memory or a rebuild in place would not fit; else in place, only
|
|
248
|
+
* where the new tables fit beside the old. A rebuild links every live
|
|
249
|
+
* ABC again and verifies them again, seconds for a large application, so
|
|
250
|
+
* a host calls this when it has time, as when idle; nothing else does.
|
|
240
251
|
*/
|
|
241
252
|
compact(): boolean;
|
|
242
253
|
/**
|
|
243
254
|
* What the ABCs hold of codegen's memory, roughly, in bytes: the live
|
|
244
255
|
* ones', and the dropped and evicted domains' that compact would free;
|
|
245
|
-
*
|
|
256
|
+
* the size of codegen's memory, which grows and never shrinks but when
|
|
257
|
+
* compact moves the domain to a new instance; and how many such moves
|
|
258
|
+
* failed, each leaving the domain as it was, rebuilt in place if at all.
|
|
246
259
|
*/
|
|
247
|
-
usage(): { live: number; dead: number; memory: number };
|
|
248
|
-
/** The size of codegen's memory in bytes,
|
|
260
|
+
usage(): { live: number; dead: number; memory: number; failedMoves: number };
|
|
261
|
+
/** The size of codegen's memory in bytes, as usage gives it: cheap, for a host that shows it often. */
|
|
249
262
|
memoryBytes(): number;
|
|
250
263
|
/**
|
|
251
264
|
* Evict application domain `appDomain` and its descendants while the host
|
|
@@ -290,6 +303,17 @@ export interface Codegen {
|
|
|
290
303
|
* for an ABC never added.
|
|
291
304
|
*/
|
|
292
305
|
context(index?: number, after?: number, upTo?: number): ModuleContext | null;
|
|
306
|
+
/**
|
|
307
|
+
* ABC `index`'s context as a module key names it, the log's entries
|
|
308
|
+
* made with `after` ABCs added or fewer left out: the log by the chained
|
|
309
|
+
* digests of its blocks of entries, those made before the last ABC was
|
|
310
|
+
* added, and the text of the entries after them. Each application
|
|
311
|
+
* domain keeps its last digest, so a key reads and digests only the
|
|
312
|
+
* entries made since the last, or since its parent's. The same domain
|
|
313
|
+
* gives the same however its keys were taken. Null for an ABC never
|
|
314
|
+
* added, or of a domain not live.
|
|
315
|
+
*/
|
|
316
|
+
keyContext(index?: number, after?: number): KeyContext | null;
|
|
293
317
|
/**
|
|
294
318
|
* A string that changes whenever a context might: a host that took one
|
|
295
319
|
* and finds this the same since need not take it again.
|
|
@@ -345,7 +369,7 @@ export interface Codegen {
|
|
|
345
369
|
* this package free of I/O. The file ships as `@swf2es/codegen/codegen.wasm`.
|
|
346
370
|
*/
|
|
347
371
|
export async function createCodegen(module: WebAssembly.Module): Promise<Codegen> {
|
|
348
|
-
|
|
372
|
+
let wasm = await instantiate(module, { env: {} });
|
|
349
373
|
const version = wasm.compilerVersion();
|
|
350
374
|
if (version !== COMPILER_VERSION) {
|
|
351
375
|
throw new Error(
|
|
@@ -362,11 +386,14 @@ export async function createCodegen(module: WebAssembly.Module): Promise<Codegen
|
|
|
362
386
|
);
|
|
363
387
|
}
|
|
364
388
|
|
|
365
|
-
// Or before it wrote a module a part at a time
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
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
|
+
}
|
|
370
397
|
}
|
|
371
398
|
|
|
372
399
|
// codegen.wasm uses AssemblyScript's minimal runtime, which collects
|
|
@@ -486,6 +513,119 @@ export async function createCodegen(module: WebAssembly.Module): Promise<Codegen
|
|
|
486
513
|
const identity = stamps.length ? String.fromCharCode(...new Uint8Array(stamps[0])) : null;
|
|
487
514
|
let epoch = 0;
|
|
488
515
|
let apiVersion = API_VERSION;
|
|
516
|
+
// Read where they are in codegen's memory, digested before the next call in.
|
|
517
|
+
const chains = new KeyChains({
|
|
518
|
+
header(index) {
|
|
519
|
+
const address = wasm.domainKeyHeaderBytes(index);
|
|
520
|
+
return address === 0 ? "" : handed(address, wasm.outputLength());
|
|
521
|
+
},
|
|
522
|
+
parent: (appDomain) => wasm.domainParent(appDomain),
|
|
523
|
+
fork: (appDomain) => wasm.domainKeyFork(appDomain),
|
|
524
|
+
log(appDomain, after, from, to, block, filled) {
|
|
525
|
+
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());
|
|
529
|
+
},
|
|
530
|
+
});
|
|
531
|
+
// The rebuild domainCompact, domainRevive or domainLoadRebuild started in
|
|
532
|
+
// `into`, a part a call, each part's garbage collected before the next
|
|
533
|
+
// (see domainRebuildStep), and the old tables once it is done, before the
|
|
534
|
+
// next call's allocations would grow memory past them: whether it was done.
|
|
535
|
+
const rebuild = (into = wasm) => {
|
|
536
|
+
let step = into.domainRebuildStep();
|
|
537
|
+
while (step === 0) {
|
|
538
|
+
into.__collect();
|
|
539
|
+
step = into.domainRebuildStep();
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
into.__collect();
|
|
543
|
+
size = wasm.memory.buffer.byteLength;
|
|
544
|
+
calls = 0;
|
|
545
|
+
return step > 0;
|
|
546
|
+
};
|
|
547
|
+
// A second instance of the compiler, made ahead as instantiating is
|
|
548
|
+
// asynchronous, that the next rebuild is done in (see moved): wasm memory
|
|
549
|
+
// never shrinks, so one done in place keeps, for good, as much as the
|
|
550
|
+
// dead tables and the old and the new took together. Made only once a
|
|
551
|
+
// move will be due before long (WANT_Spare), not for every compiler, and
|
|
552
|
+
// taken by the move; and how many moves failed, which usage tells.
|
|
553
|
+
let spare: typeof wasm | null = null;
|
|
554
|
+
let making = false;
|
|
555
|
+
let failedMoves = 0;
|
|
556
|
+
const prepare = () => {
|
|
557
|
+
if (spare === null && !making) {
|
|
558
|
+
making = true;
|
|
559
|
+
instantiate(module, { env: {} }).then(
|
|
560
|
+
(made) => {
|
|
561
|
+
spare = made;
|
|
562
|
+
making = false;
|
|
563
|
+
},
|
|
564
|
+
() => {
|
|
565
|
+
making = false;
|
|
566
|
+
},
|
|
567
|
+
);
|
|
568
|
+
}
|
|
569
|
+
};
|
|
570
|
+
// 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. The
|
|
572
|
+
// ABCs' copies in it are its garbage once parsed, collected every few
|
|
573
|
+
// megabytes.
|
|
574
|
+
const rebuiltIn = (into: typeof wasm) => {
|
|
575
|
+
const saved = wasm.domainSaveBytes();
|
|
576
|
+
if (!into.domainLoad(new Uint8Array(wasm.memory.buffer, saved, wasm.outputLength()))) {
|
|
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);
|
|
597
|
+
};
|
|
598
|
+
// The domain rebuilt in the spare instance, which then takes this one's
|
|
599
|
+
// place, whose memory goes with it: whether it was. One that failed, or
|
|
600
|
+
// trapped as its memory could not grow, is let go of, the domain left
|
|
601
|
+
// here as it was, and another made.
|
|
602
|
+
const moved = () => {
|
|
603
|
+
const into = spare;
|
|
604
|
+
if (into === null) {
|
|
605
|
+
return false;
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
spare = null;
|
|
609
|
+
let done = false;
|
|
610
|
+
try {
|
|
611
|
+
done = rebuiltIn(into);
|
|
612
|
+
} catch {
|
|
613
|
+
done = false;
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
if (!done) {
|
|
617
|
+
failedMoves++;
|
|
618
|
+
size = wasm.memory.buffer.byteLength;
|
|
619
|
+
return false;
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
// The held SWFs' copies were the old instance's (see hold).
|
|
623
|
+
holds.length = 0;
|
|
624
|
+
wasm = into;
|
|
625
|
+
size = wasm.memory.buffer.byteLength;
|
|
626
|
+
calls = 0;
|
|
627
|
+
return true;
|
|
628
|
+
};
|
|
489
629
|
const collected = <T>(result: T, reclaim = false): T => {
|
|
490
630
|
const grown = wasm.memory.buffer.byteLength;
|
|
491
631
|
if (reclaim || grown > size || ++calls >= COLLECT_EVERY) {
|
|
@@ -510,6 +650,7 @@ export async function createCodegen(module: WebAssembly.Module): Promise<Codegen
|
|
|
510
650
|
checkApiVersion(version);
|
|
511
651
|
added = 0;
|
|
512
652
|
epoch++;
|
|
653
|
+
chains.clear();
|
|
513
654
|
apiVersion = version;
|
|
514
655
|
collected(wasm.domainReset(version));
|
|
515
656
|
},
|
|
@@ -539,6 +680,7 @@ export async function createCodegen(module: WebAssembly.Module): Promise<Codegen
|
|
|
539
680
|
dropDomain(appDomain, of = epoch) {
|
|
540
681
|
if (of === epoch) {
|
|
541
682
|
collected(wasm.domainDrop(appDomain));
|
|
683
|
+
chains.forget((d) => wasm.domainState(d) === 2);
|
|
542
684
|
}
|
|
543
685
|
},
|
|
544
686
|
evictDomain(appDomain) {
|
|
@@ -546,36 +688,45 @@ export async function createCodegen(module: WebAssembly.Module): Promise<Codegen
|
|
|
546
688
|
},
|
|
547
689
|
usage() {
|
|
548
690
|
const [live, dead, memory] = collected(wasm.domainUsage()).split(" ").map(Number);
|
|
549
|
-
return { live, dead, memory };
|
|
691
|
+
return { live, dead, memory, failedMoves };
|
|
550
692
|
},
|
|
551
693
|
memoryBytes() {
|
|
552
694
|
return wasm.memory.buffer.byteLength;
|
|
553
695
|
},
|
|
554
696
|
compact() {
|
|
555
|
-
//
|
|
556
|
-
//
|
|
557
|
-
//
|
|
558
|
-
|
|
559
|
-
|
|
697
|
+
// Done in the spare instance, once there is one and that would take
|
|
698
|
+
// less memory, or in place would not fit; else in place, where the
|
|
699
|
+
// 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
|
+
// what earlier calls left is collected first, and the old tables once
|
|
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))) {
|
|
705
|
+
prepare();
|
|
560
706
|
}
|
|
561
707
|
|
|
562
|
-
wasm.
|
|
563
|
-
|
|
564
|
-
|
|
708
|
+
if (spare !== null && collected(wasm.domainWantsCompact(WANT_Move))) {
|
|
709
|
+
wasm.__collect();
|
|
710
|
+
if (moved()) {
|
|
711
|
+
return true;
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
const beside = spare !== null || making;
|
|
716
|
+
if (!collected(wasm.domainWantsCompact(beside ? WANT_InPlaceBesideSpare : WANT_InPlace))) {
|
|
565
717
|
return false;
|
|
566
718
|
}
|
|
567
719
|
|
|
568
720
|
wasm.__collect();
|
|
569
|
-
size = wasm.memory.buffer.byteLength;
|
|
570
721
|
calls = 0;
|
|
571
|
-
return true;
|
|
722
|
+
return wasm.domainCompact(true) && rebuild();
|
|
572
723
|
},
|
|
573
724
|
isLive(appDomain) {
|
|
574
725
|
return collected(wasm.domainState(appDomain)) === 0;
|
|
575
726
|
},
|
|
576
727
|
reviveDomain(appDomain, abcs) {
|
|
577
728
|
// Its ABCs' bytes are copied into the compiler only if a rebuild let go of them.
|
|
578
|
-
let revived = collected(wasm.domainRevive(appDomain));
|
|
729
|
+
let revived = collected(wasm.domainRevive(appDomain, true));
|
|
579
730
|
if (revived < 0) {
|
|
580
731
|
for (const [index, bytes] of abcs) {
|
|
581
732
|
const error = collected(wasm.domainRestore(index, bytes));
|
|
@@ -584,20 +735,15 @@ export async function createCodegen(module: WebAssembly.Module): Promise<Codegen
|
|
|
584
735
|
}
|
|
585
736
|
}
|
|
586
737
|
|
|
587
|
-
revived = collected(wasm.domainRevive(appDomain));
|
|
738
|
+
revived = collected(wasm.domainRevive(appDomain, true));
|
|
588
739
|
}
|
|
589
740
|
|
|
590
|
-
|
|
741
|
+
// Revived by a rebuild in place, mid-load: a compaction moves it later.
|
|
742
|
+
if (revived < 0 || (revived > 0 && !rebuild())) {
|
|
591
743
|
throw new Error(
|
|
592
744
|
`application domain ${appDomain}: not evicted, its parent not live, or an ABC of it not given`,
|
|
593
745
|
);
|
|
594
746
|
}
|
|
595
|
-
|
|
596
|
-
if (revived > 0) {
|
|
597
|
-
wasm.__collect();
|
|
598
|
-
size = wasm.memory.buffer.byteLength;
|
|
599
|
-
calls = 0;
|
|
600
|
-
}
|
|
601
747
|
},
|
|
602
748
|
found(d) {
|
|
603
749
|
collected(wasm.domainFound(d.domain, d.nsKind, d.uri, d.name, d.abc, d.asType));
|
|
@@ -634,6 +780,13 @@ export async function createCodegen(module: WebAssembly.Module): Promise<Codegen
|
|
|
634
780
|
log: second < 0 ? "" : text.slice(second + 1),
|
|
635
781
|
};
|
|
636
782
|
},
|
|
783
|
+
keyContext(index = -1, after = 0) {
|
|
784
|
+
if (added === 0) {
|
|
785
|
+
return null;
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
return collected(chains.context(indexOf(index), after));
|
|
789
|
+
},
|
|
637
790
|
revision() {
|
|
638
791
|
return `${epoch} ${wasm.domainRevision()}`;
|
|
639
792
|
},
|
|
@@ -716,6 +869,13 @@ function parseEntries(text: string): Map<number, string> {
|
|
|
716
869
|
}
|
|
717
870
|
|
|
718
871
|
const COLLECT_EVERY = 64;
|
|
872
|
+
/** What domainWantsCompact is asked (see wantsRebuild in assembly/avm2/link/lifetime.ts). */
|
|
873
|
+
const WANT_InPlace = 0;
|
|
874
|
+
const WANT_InPlaceBesideSpare = 1;
|
|
875
|
+
const WANT_Move = 2;
|
|
876
|
+
const WANT_Spare = 3;
|
|
877
|
+
/** How many bytes of ABCs a rebuild in the spare instance copies in between its collections. */
|
|
878
|
+
const COPIED_BETWEEN_COLLECTIONS = 4 << 20;
|
|
719
879
|
/** How many SWFs whose blocks or units are compiled codegen holds a copy of at most. */
|
|
720
880
|
const HELD_SWFS = 4;
|
|
721
881
|
|
package/src/keys.ts
ADDED
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
// The compiler's log as a module cache key names it, by digests chained a
|
|
2
|
+
// block of entries at a time, so that a key costs what the log gained
|
|
3
|
+
// since the last, not the whole of it (docs/architecture/modules.md,
|
|
4
|
+
// "Caching modules").
|
|
5
|
+
//
|
|
6
|
+
// A key names the log as its ABC's application domain sees it (codegen's
|
|
7
|
+
// domainContext). Its entries made before the last ABC the domain sees
|
|
8
|
+
// was added are settled: none changes while the domain is live and the
|
|
9
|
+
// compiler not rebuilt, and which they are depends on what the domain
|
|
10
|
+
// sees alone. A key names those by the chained digests of their blocks of
|
|
11
|
+
// KEY_BLOCK entries, sha256(previous digest + block), and the rest by
|
|
12
|
+
// their text. Each domain keeps where its chain is, the digest so far
|
|
13
|
+
// and the settled entries after the last block, so the next key reads
|
|
14
|
+
// only the entries made since; a new domain's chain starts as its
|
|
15
|
+
// parent's is where the two logs part (domainKeyFork), so a parent's
|
|
16
|
+
// chain may run past its own settled entries, and its key then splits at
|
|
17
|
+
// the last block's end before them. The blocks are the same however they
|
|
18
|
+
// were read, so the key is the same as one taken from nothing.
|
|
19
|
+
import { sha256Joined } from "./sha256.js";
|
|
20
|
+
|
|
21
|
+
/** How many entries of the log each chained digest takes. */
|
|
22
|
+
const KEY_BLOCK = 64;
|
|
23
|
+
|
|
24
|
+
/** How many digests of its blocks a domain keeps, for a new child's start. */
|
|
25
|
+
const KEPT = 8;
|
|
26
|
+
|
|
27
|
+
/** What the chain asks of codegen.wasm, as the wrapper or the test build gives it. */
|
|
28
|
+
export interface KeyLog {
|
|
29
|
+
/** domainKeyHeaderBytes' text: "" where there is no ABC `index`. */
|
|
30
|
+
header(index: number): string;
|
|
31
|
+
/** domainParent: -1 for the root. */
|
|
32
|
+
parent(appDomain: number): number;
|
|
33
|
+
/** domainKeyFork. */
|
|
34
|
+
fork(appDomain: number): number;
|
|
35
|
+
/** domainKeyLogBytes, read before the next call into codegen; null where it gave none. */
|
|
36
|
+
log(
|
|
37
|
+
appDomain: number,
|
|
38
|
+
after: number,
|
|
39
|
+
from: number,
|
|
40
|
+
to: number,
|
|
41
|
+
block: number,
|
|
42
|
+
filled: number,
|
|
43
|
+
): Uint8Array | null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** What a module key names of an ABC's domain (see moduleKey). */
|
|
47
|
+
export interface KeyContext {
|
|
48
|
+
/** The ABCs its application domain sees, by their positions among those added, in load order. */
|
|
49
|
+
abcs: number[];
|
|
50
|
+
/** Its own place in `abcs`. */
|
|
51
|
+
own: number;
|
|
52
|
+
/** The chained digest of the log's blocks; "" for none. */
|
|
53
|
+
blocks: string;
|
|
54
|
+
/** The log's entries after them, each after a line break. */
|
|
55
|
+
rest: string;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Where a domain's chain goes on from: log entry `entry`, the digest of the
|
|
60
|
+
* blocks before, and the settled entries after them, `filled` of them.
|
|
61
|
+
*/
|
|
62
|
+
interface Mark {
|
|
63
|
+
entry: number;
|
|
64
|
+
digest: string;
|
|
65
|
+
partial: Uint8Array;
|
|
66
|
+
filled: number;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* A domain's chain: where it goes on from, the last ends of its blocks,
|
|
71
|
+
* for a new child's start, and where it was at the settled entries its
|
|
72
|
+
* last key split at, where it has gone past them for a child's start.
|
|
73
|
+
*/
|
|
74
|
+
interface Chain {
|
|
75
|
+
marks: Mark[];
|
|
76
|
+
at: Mark;
|
|
77
|
+
held?: Mark;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const EMPTY = new Uint8Array(0);
|
|
81
|
+
const NONE: Mark = { entry: 0, digest: "", partial: EMPTY, filled: 0 };
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Each application domain's chain of digests, by the domain and how many
|
|
85
|
+
* ABCs' entries the log it keys leaves out. Cleared whenever its numbers
|
|
86
|
+
* may be another domain's or its entries others: as the compiler's domain
|
|
87
|
+
* is reset, and as it is rebuilt.
|
|
88
|
+
*/
|
|
89
|
+
export class KeyChains {
|
|
90
|
+
private readonly codegen: KeyLog;
|
|
91
|
+
private readonly chains = new Map<string, Chain>();
|
|
92
|
+
private generation = -1;
|
|
93
|
+
|
|
94
|
+
constructor(codegen: KeyLog) {
|
|
95
|
+
this.codegen = codegen;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
clear(): void {
|
|
99
|
+
this.chains.clear();
|
|
100
|
+
this.generation = -1;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Let go of the chains of the application domains `dropped` names. */
|
|
104
|
+
forget(dropped: (appDomain: number) => boolean): void {
|
|
105
|
+
for (const name of this.chains.keys()) {
|
|
106
|
+
if (dropped(Number(name.slice(0, name.indexOf(" "))))) {
|
|
107
|
+
this.chains.delete(name);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** ABC `index`'s context, the log's entries made with `after` ABCs added or fewer left out; null where there is none. */
|
|
113
|
+
context(index: number, after: number): KeyContext | null {
|
|
114
|
+
const header = this.codegen.header(index);
|
|
115
|
+
if (header === "") {
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const lines = header.split("\n");
|
|
120
|
+
const appDomain = Number(lines[2]);
|
|
121
|
+
const generation = Number(lines[3]);
|
|
122
|
+
const settled = Number(lines[4]);
|
|
123
|
+
if (generation !== this.generation) {
|
|
124
|
+
this.chains.clear();
|
|
125
|
+
this.generation = generation;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const chain = this.chain(appDomain, after, settled);
|
|
129
|
+
if (chain === null) {
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Gone past the settled entries for a child's start: from the last
|
|
134
|
+
// block's end before them, as the key splits its blocks there, or
|
|
135
|
+
// from where the last key split, kept, as only the last KEPT block
|
|
136
|
+
// ends are, and a chain gone further for children would read the
|
|
137
|
+
// domain's log from the start for every key until it gains an ABC.
|
|
138
|
+
let at = chain.at;
|
|
139
|
+
if (at.entry > settled) {
|
|
140
|
+
let from = before(chain.marks, settled);
|
|
141
|
+
const held = chain.held;
|
|
142
|
+
if (held !== undefined && held.entry <= settled && held.entry >= from.entry) {
|
|
143
|
+
from = held;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const back: Chain = { marks: [], at: from };
|
|
147
|
+
if (!this.extend(appDomain, after, back, settled)) {
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
at = back.at;
|
|
152
|
+
chain.held = at;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const rest = this.codegen.log(appDomain, after, settled, -1, 0, 0);
|
|
156
|
+
if (rest === null) {
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return {
|
|
161
|
+
abcs: lines[0] ? lines[0].split(",").map(Number) : [],
|
|
162
|
+
own: Number(lines[1]),
|
|
163
|
+
blocks: at.digest,
|
|
164
|
+
rest: ascii.decode(at.partial) + ascii.decode(rest.subarray(0, rest.lastIndexOf(0x0a))),
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** `appDomain`'s chain, gone on with up to entry `limit`; null, and none kept, where it is not live. */
|
|
169
|
+
private chain(appDomain: number, after: number, limit: number): Chain | null {
|
|
170
|
+
const name = `${appDomain} ${after}`;
|
|
171
|
+
const kept = this.chains.get(name);
|
|
172
|
+
const chain = kept ?? { marks: [], at: this.start(appDomain, after) };
|
|
173
|
+
if (!this.extend(appDomain, after, chain, limit)) {
|
|
174
|
+
return null;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (kept === undefined) {
|
|
178
|
+
this.chains.set(name, chain);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return chain;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** Go on with `appDomain`'s `chain` up to entry `limit`: false where the domain is not live. */
|
|
185
|
+
private extend(appDomain: number, after: number, chain: Chain, limit: number): boolean {
|
|
186
|
+
const { entry, filled } = chain.at;
|
|
187
|
+
const bytes = this.codegen.log(
|
|
188
|
+
appDomain,
|
|
189
|
+
after,
|
|
190
|
+
entry,
|
|
191
|
+
Math.max(entry, limit),
|
|
192
|
+
KEY_BLOCK,
|
|
193
|
+
filled,
|
|
194
|
+
);
|
|
195
|
+
if (bytes === null) {
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
read(bytes, chain);
|
|
200
|
+
return true;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** Where a domain's chain starts: where its parent's is last before the two part, else at none. */
|
|
204
|
+
private start(appDomain: number, after: number): Mark {
|
|
205
|
+
const parent = this.codegen.parent(appDomain);
|
|
206
|
+
if (parent < 0) {
|
|
207
|
+
return NONE;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const fork = this.codegen.fork(appDomain);
|
|
211
|
+
const chain = this.chain(parent, after, fork);
|
|
212
|
+
if (chain === null) {
|
|
213
|
+
return NONE;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
return chain.at.entry <= fork ? chain.at : before(chain.marks, fork);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** The last of `marks` at or before entry `entry`, else none. */
|
|
221
|
+
function before(marks: Mark[], entry: number): Mark {
|
|
222
|
+
for (let k = marks.length - 1; k >= 0; k--) {
|
|
223
|
+
if (marks[k].entry <= entry) {
|
|
224
|
+
return marks[k];
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
return NONE;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Go on with `chain` by domainKeyLogBytes' `bytes`: each block's digest
|
|
233
|
+
* chained, a mark at its end, the oldest let go of past KEPT, and where it
|
|
234
|
+
* goes on from next.
|
|
235
|
+
*/
|
|
236
|
+
function read(bytes: Uint8Array, chain: Chain): void {
|
|
237
|
+
let { digest, partial, filled } = chain.at;
|
|
238
|
+
let start = 0;
|
|
239
|
+
let entries = 0;
|
|
240
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
241
|
+
if (bytes[i] !== 0x0a) {
|
|
242
|
+
continue;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const marker = i + 1 < bytes.length ? bytes[i + 1] : 0;
|
|
246
|
+
if (marker !== 0x23 && marker !== 0x24) {
|
|
247
|
+
entries++;
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// A line of "#" and the entry after a block, or of "$" and the entry it ended at.
|
|
252
|
+
let entry = 0;
|
|
253
|
+
let j = i + 2;
|
|
254
|
+
for (; j < bytes.length && bytes[j] !== 0x0a; j++) {
|
|
255
|
+
entry = entry * 10 + bytes[j] - 0x30;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
if (marker === 0x23) {
|
|
259
|
+
digest = sha256Joined(digest, partial, bytes.subarray(start, i));
|
|
260
|
+
partial = EMPTY;
|
|
261
|
+
filled = 0;
|
|
262
|
+
chain.marks.push({ entry, digest, partial, filled });
|
|
263
|
+
} else {
|
|
264
|
+
partial = joined(partial, bytes.subarray(start, i));
|
|
265
|
+
filled += entries;
|
|
266
|
+
chain.at = { entry, digest, partial, filled };
|
|
267
|
+
if (chain.marks.length > KEPT) {
|
|
268
|
+
chain.marks.splice(0, chain.marks.length - KEPT);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
entries = 0;
|
|
275
|
+
start = j;
|
|
276
|
+
i = j - 1;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
throw new Error("codegen: a module key's log has no end");
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/** `a` and then `b`, copied out of codegen's memory. */
|
|
283
|
+
function joined(a: Uint8Array, b: Uint8Array): Uint8Array {
|
|
284
|
+
if (b.length === 0) {
|
|
285
|
+
return a;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
const out = new Uint8Array(a.length + b.length);
|
|
289
|
+
out.set(a);
|
|
290
|
+
out.set(b, a.length);
|
|
291
|
+
return out;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// The web's and node's, which this package declares itself (see index.ts).
|
|
295
|
+
declare const TextDecoder: new () => { decode(bytes: Uint8Array): string };
|
|
296
|
+
const ascii = new TextDecoder();
|