@gmitch215/folio 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +455 -26
- package/dist/affine-state.d.ts +171 -0
- package/dist/affine-state.js +763 -0
- package/dist/affine.d.ts +160 -0
- package/dist/affine.js +571 -0
- package/dist/bake.d.ts +8 -3
- package/dist/bake.js +5 -2
- package/dist/boundaries.d.ts +59 -0
- package/dist/boundaries.js +127 -0
- package/dist/certified.d.ts +66 -0
- package/dist/certified.js +192 -0
- package/dist/certify.d.ts +98 -0
- package/dist/certify.js +2628 -0
- package/dist/compiler.d.ts +37 -1
- package/dist/compiler.js +116 -1
- package/dist/context.d.ts +47 -0
- package/dist/context.js +139 -0
- package/dist/coordinator.d.ts +1 -1
- package/dist/coordinator.js +54 -5
- package/dist/extract.d.ts +114 -1
- package/dist/extract.js +1011 -242
- package/dist/fold.d.ts +32 -0
- package/dist/fold.js +105 -0
- package/dist/frames.d.ts +20 -2
- package/dist/frames.js +205 -10
- package/dist/inline.d.ts +182 -0
- package/dist/inline.js +766 -0
- package/dist/iteration-admission.d.ts +185 -0
- package/dist/iteration-admission.js +201 -0
- package/dist/iteration-layout.d.ts +135 -0
- package/dist/iteration-layout.js +169 -0
- package/dist/iteration-owner.d.ts +45 -0
- package/dist/iteration-owner.js +223 -0
- package/dist/iteration-worker.d.ts +1 -0
- package/dist/iteration-worker.js +216 -0
- package/dist/iteration.d.ts +329 -0
- package/dist/iteration.js +1210 -0
- package/dist/lease.d.ts +45 -0
- package/dist/lease.js +274 -0
- package/dist/managed.d.ts +78 -0
- package/dist/managed.js +448 -27
- package/dist/memo.d.ts +99 -0
- package/dist/memo.js +250 -0
- package/dist/node.d.ts +61 -5
- package/dist/node.js +421 -23
- package/dist/ownership-redirect.d.ts +30 -0
- package/dist/ownership-redirect.js +179 -0
- package/dist/ownership-slices.d.ts +44 -0
- package/dist/ownership-slices.js +72 -0
- package/dist/ownership.d.ts +161 -0
- package/dist/ownership.js +6568 -0
- package/dist/ranges.d.ts +78 -0
- package/dist/ranges.js +263 -0
- package/dist/read-scan.d.ts +18 -0
- package/dist/read-scan.js +94 -0
- package/dist/read-windows.d.ts +103 -0
- package/dist/read-windows.js +244 -0
- package/dist/router-worker.d.ts +1 -0
- package/dist/router-worker.js +4 -0
- package/dist/router.d.ts +203 -0
- package/dist/router.js +667 -0
- package/dist/runs.d.ts +44 -0
- package/dist/runs.js +182 -0
- package/dist/slices-worker.d.ts +1 -0
- package/dist/slices-worker.js +38 -0
- package/dist/slices.d.ts +126 -0
- package/dist/slices.js +441 -0
- package/dist/tasks.d.ts +14 -4
- package/dist/tasks.js +23 -9
- package/dist/transaction.d.ts +10 -0
- package/package.json +44 -4
package/dist/fold.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { TaskPool, TaskResult } from './tasks.ts';
|
|
2
|
+
/** A byte range of the input, one per task. */
|
|
3
|
+
export interface FoldChunk {
|
|
4
|
+
start: number;
|
|
5
|
+
end: number;
|
|
6
|
+
}
|
|
7
|
+
/** Options for `foldChunks`. */
|
|
8
|
+
export interface FoldOptions<State> {
|
|
9
|
+
/** Every chunk but the last starts and ends on a multiple of this. The default is 1. */
|
|
10
|
+
align?: number;
|
|
11
|
+
/** Explicit chunk ranges. The default splits the input into `pool.width` aligned chunks. */
|
|
12
|
+
chunks?: FoldChunk[];
|
|
13
|
+
/** Name of the task that runs one chunk. */
|
|
14
|
+
task(chunk: FoldChunk): string;
|
|
15
|
+
/** State before the first chunk. */
|
|
16
|
+
initial: State;
|
|
17
|
+
/** Folds one chunk's joined result into the state. Chunks arrive in order. */
|
|
18
|
+
combine(state: State, part: TaskResult, chunk: FoldChunk): State;
|
|
19
|
+
}
|
|
20
|
+
/** CRC-32 of A||B from the CRCs of A and B and the length of B. */
|
|
21
|
+
export declare function crc32Combine(crcA: number, crcB: number, lengthB: number): number;
|
|
22
|
+
/** Adler-32 of A||B from the sums of A and B and the length of B. */
|
|
23
|
+
export declare function adler32Combine(adlerA: number, adlerB: number, lengthB: number): number;
|
|
24
|
+
/** The 16-byte big-endian AES-CTR counter after `blocks` blocks, carrying through every byte. */
|
|
25
|
+
export declare function ctrAdvance(counter: Uint8Array, blocks: number): Uint8Array;
|
|
26
|
+
/** Splits `length` bytes into at most `count` ranges; all but the last are multiples of `align`. */
|
|
27
|
+
export declare function chunkRanges(length: number, count: number, align?: number): FoldChunk[];
|
|
28
|
+
/**
|
|
29
|
+
* Runs one task per chunk on `pool` and folds the results in order. The input is copied into each
|
|
30
|
+
* task (see `TaskPool.submit`), so the state is built from copies, not from the input buffer.
|
|
31
|
+
*/
|
|
32
|
+
export declare function foldChunks<State>(pool: TaskPool, input: Uint8Array, options: FoldOptions<State>): State;
|
package/dist/fold.js
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
const CRC_POLY = 0xedb88320;
|
|
2
|
+
const CRC_TABLE = Uint32Array.from({ length: 256 }, (_, n) => {
|
|
3
|
+
let c = n;
|
|
4
|
+
for (let k = 0; k < 8; k++)
|
|
5
|
+
c = c & 1 ? (c >>> 1) ^ CRC_POLY : c >>> 1;
|
|
6
|
+
return c >>> 0;
|
|
7
|
+
});
|
|
8
|
+
const crcColumn = (v) => ((v >>> 8) ^ CRC_TABLE[v & 0xff]) >>> 0;
|
|
9
|
+
// columns of the GF(2) operator that shifts a CRC register by one zero byte
|
|
10
|
+
const CRC_ZERO_BYTE = Array.from({ length: 32 }, (_, k) => crcColumn((1 << k) >>> 0));
|
|
11
|
+
function gf2Times(matrix, vector) {
|
|
12
|
+
let out = 0;
|
|
13
|
+
for (let k = 0, v = vector >>> 0; v; k++, v >>>= 1)
|
|
14
|
+
if (v & 1)
|
|
15
|
+
out ^= matrix[k];
|
|
16
|
+
return out >>> 0;
|
|
17
|
+
}
|
|
18
|
+
/** CRC-32 of A||B from the CRCs of A and B and the length of B. */
|
|
19
|
+
export function crc32Combine(crcA, crcB, lengthB) {
|
|
20
|
+
if (!Number.isSafeInteger(lengthB) || lengthB < 0)
|
|
21
|
+
throw new RangeError('invalid length');
|
|
22
|
+
// crc(AB) = M^lengthB crc(A) xor crc(B), with M the one-zero-byte operator
|
|
23
|
+
let vector = crcA >>> 0;
|
|
24
|
+
let matrix = CRC_ZERO_BYTE;
|
|
25
|
+
for (let n = lengthB; n > 0; n = Math.floor(n / 2)) {
|
|
26
|
+
if (n % 2 === 1)
|
|
27
|
+
vector = gf2Times(matrix, vector);
|
|
28
|
+
matrix = matrix.map((column) => gf2Times(matrix, column));
|
|
29
|
+
}
|
|
30
|
+
return (vector ^ crcB) >>> 0;
|
|
31
|
+
}
|
|
32
|
+
const ADLER_BASE = 65521;
|
|
33
|
+
/** Adler-32 of A||B from the sums of A and B and the length of B. */
|
|
34
|
+
export function adler32Combine(adlerA, adlerB, lengthB) {
|
|
35
|
+
if (!Number.isSafeInteger(lengthB) || lengthB < 0)
|
|
36
|
+
throw new RangeError('invalid length');
|
|
37
|
+
const s1a = adlerA & 0xffff;
|
|
38
|
+
const s2a = adlerA >>> 16;
|
|
39
|
+
const s1b = adlerB & 0xffff;
|
|
40
|
+
const s2b = adlerB >>> 16;
|
|
41
|
+
const s1 = (s1a + s1b + ADLER_BASE - 1) % ADLER_BASE;
|
|
42
|
+
const s2 = (s2a +
|
|
43
|
+
s2b +
|
|
44
|
+
(((lengthB % ADLER_BASE) * ((s1a + ADLER_BASE - 1) % ADLER_BASE)) % ADLER_BASE)) %
|
|
45
|
+
ADLER_BASE;
|
|
46
|
+
return ((s2 << 16) | s1) >>> 0;
|
|
47
|
+
}
|
|
48
|
+
/** The 16-byte big-endian AES-CTR counter after `blocks` blocks, carrying through every byte. */
|
|
49
|
+
export function ctrAdvance(counter, blocks) {
|
|
50
|
+
if (counter.length !== 16)
|
|
51
|
+
throw new RangeError('counter must be 16 bytes');
|
|
52
|
+
if (!Number.isSafeInteger(blocks) || blocks < 0)
|
|
53
|
+
throw new RangeError('invalid block count');
|
|
54
|
+
const out = Uint8Array.from(counter);
|
|
55
|
+
let carry = blocks;
|
|
56
|
+
for (let i = 15; i >= 0 && carry > 0; i--) {
|
|
57
|
+
const sum = out[i] + (carry % 256);
|
|
58
|
+
out[i] = sum % 256;
|
|
59
|
+
carry = Math.floor(carry / 256) + Math.floor(sum / 256);
|
|
60
|
+
}
|
|
61
|
+
return out;
|
|
62
|
+
}
|
|
63
|
+
/** Splits `length` bytes into at most `count` ranges; all but the last are multiples of `align`. */
|
|
64
|
+
export function chunkRanges(length, count, align = 1) {
|
|
65
|
+
if (!Number.isSafeInteger(length) || length < 0)
|
|
66
|
+
throw new RangeError('invalid length');
|
|
67
|
+
if (!Number.isSafeInteger(count) || count < 1)
|
|
68
|
+
throw new RangeError('invalid chunk count');
|
|
69
|
+
if (!Number.isSafeInteger(align) || align < 1)
|
|
70
|
+
throw new RangeError('invalid alignment');
|
|
71
|
+
const step = Math.max(align, Math.ceil(length / count / align) * align);
|
|
72
|
+
const chunks = [];
|
|
73
|
+
for (let start = 0; start < length; start += step)
|
|
74
|
+
chunks.push({ start, end: Math.min(length, start + step) });
|
|
75
|
+
return chunks;
|
|
76
|
+
}
|
|
77
|
+
function checkChunks(chunks, length, align) {
|
|
78
|
+
let at = 0;
|
|
79
|
+
chunks.forEach((chunk, index) => {
|
|
80
|
+
if (chunk.start !== at || chunk.end <= chunk.start)
|
|
81
|
+
throw new RangeError(`chunk ${index} is not contiguous`);
|
|
82
|
+
const last = index === chunks.length - 1;
|
|
83
|
+
if (!last && chunk.end % align !== 0)
|
|
84
|
+
throw new RangeError(`chunk ${index} ends at ${chunk.end}, not a multiple of ${align}`);
|
|
85
|
+
at = chunk.end;
|
|
86
|
+
});
|
|
87
|
+
if (at !== length)
|
|
88
|
+
throw new RangeError(`chunks cover ${at} of ${length} bytes`);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Runs one task per chunk on `pool` and folds the results in order. The input is copied into each
|
|
92
|
+
* task (see `TaskPool.submit`), so the state is built from copies, not from the input buffer.
|
|
93
|
+
*/
|
|
94
|
+
export function foldChunks(pool, input, options) {
|
|
95
|
+
const align = options.align ?? 1;
|
|
96
|
+
const chunks = options.chunks ?? chunkRanges(input.length, pool.width, align);
|
|
97
|
+
if (input.length)
|
|
98
|
+
checkChunks(chunks, input.length, align);
|
|
99
|
+
const handles = chunks.map((chunk) => pool.submit(options.task(chunk), input.slice(chunk.start, chunk.end)));
|
|
100
|
+
let state = options.initial;
|
|
101
|
+
chunks.forEach((chunk, index) => {
|
|
102
|
+
state = options.combine(state, pool.join(handles[index]), chunk);
|
|
103
|
+
});
|
|
104
|
+
return state;
|
|
105
|
+
}
|
package/dist/frames.d.ts
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
import type { TransactionResult } from './transaction.ts';
|
|
2
|
-
|
|
2
|
+
/** Lossless page encoding; omitted pages contain only zero bytes. */
|
|
3
|
+
export interface SnapshotPages {
|
|
4
|
+
memoryBytes: number;
|
|
5
|
+
pages: number[];
|
|
6
|
+
}
|
|
7
|
+
/** Encodes sparse arenas. A dense payload borrows the input until encodeFrame copies it. */
|
|
8
|
+
export declare function encodeSnapshot(bytes: Uint8Array): {
|
|
9
|
+
snapshot?: SnapshotPages;
|
|
10
|
+
payload: Uint8Array;
|
|
11
|
+
};
|
|
12
|
+
/** Checks sparse metadata before allocation or writes; returns the complete logical byte length. */
|
|
13
|
+
export declare function snapshotLength(snapshot: SnapshotPages | undefined, payload: Uint8Array): number;
|
|
14
|
+
/** Restores a complete snapshot, clearing pages omitted by the sparse representation. */
|
|
15
|
+
export declare function restoreSnapshot(target: Uint8Array, payload: Uint8Array, snapshot?: SnapshotPages): void;
|
|
16
|
+
export declare function encodeFrame(header: unknown, payload?: Uint8Array, maximum?: number): Uint8Array;
|
|
3
17
|
export declare function decodeFrame<T>(bytes: Uint8Array, maximum: number): {
|
|
4
18
|
header: T;
|
|
5
19
|
payload: Uint8Array;
|
|
@@ -10,10 +24,14 @@ export interface FrameIdentity {
|
|
|
10
24
|
sequence: number;
|
|
11
25
|
}
|
|
12
26
|
export declare function validateIdentity(value: FrameIdentity): void;
|
|
13
|
-
|
|
27
|
+
/** Version of the run encoding of a results frame, named in the request and the response. */
|
|
28
|
+
export declare const runsVersion = 1;
|
|
29
|
+
export declare function encodeResults(identity: FrameIdentity, results: TransactionResult[], maximum?: number, runs?: boolean): Uint8Array;
|
|
14
30
|
export declare function decodeResults(bytes: Uint8Array, identity: FrameIdentity, options: {
|
|
15
31
|
maximumFrameBytes: number;
|
|
16
32
|
memoryBytes: number;
|
|
17
33
|
maxBytes: number;
|
|
18
34
|
calls: number;
|
|
35
|
+
/** The request asked for run encoding. A reply in addresses still decodes; see `runsVersion`. */
|
|
36
|
+
runs?: boolean;
|
|
19
37
|
}): TransactionResult[];
|
package/dist/frames.js
CHANGED
|
@@ -1,14 +1,63 @@
|
|
|
1
1
|
import { decodeValue, encodeValue } from "./remote.js";
|
|
2
|
+
import { checkRuns, runBytes, runsOfSorted, writeRunsOf } from "./runs.js";
|
|
2
3
|
const magic = 0x314f4c46;
|
|
3
4
|
const encoder = new TextEncoder();
|
|
4
5
|
const decoder = new TextDecoder('utf-8', { fatal: true });
|
|
6
|
+
/** Encodes sparse arenas. A dense payload borrows the input until encodeFrame copies it. */
|
|
7
|
+
export function encodeSnapshot(bytes) {
|
|
8
|
+
if (bytes.length % 65536)
|
|
9
|
+
throw new RangeError('invalid snapshot page size');
|
|
10
|
+
const pages = [];
|
|
11
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
12
|
+
for (let offset = 0; offset < bytes.length; offset += 65536) {
|
|
13
|
+
for (let word = offset; word < offset + 65536; word += 4)
|
|
14
|
+
if (view.getUint32(word, true) !== 0) {
|
|
15
|
+
pages.push(offset / 65536);
|
|
16
|
+
break;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
if (pages.length > 4096 || pages.length * 65536 + pages.length * 8 + 64 >= bytes.length)
|
|
20
|
+
return { payload: bytes };
|
|
21
|
+
const payload = new Uint8Array(pages.length * 65536);
|
|
22
|
+
pages.forEach((page, index) => payload.set(bytes.subarray(page * 65536, (page + 1) * 65536), index * 65536));
|
|
23
|
+
return { snapshot: { memoryBytes: bytes.length, pages }, payload };
|
|
24
|
+
}
|
|
25
|
+
/** Checks sparse metadata before allocation or writes; returns the complete logical byte length. */
|
|
26
|
+
export function snapshotLength(snapshot, payload) {
|
|
27
|
+
if (snapshot === undefined)
|
|
28
|
+
return payload.length;
|
|
29
|
+
if (!Number.isSafeInteger(snapshot?.memoryBytes) ||
|
|
30
|
+
snapshot.memoryBytes < 0 ||
|
|
31
|
+
snapshot.memoryBytes % 65536 ||
|
|
32
|
+
!Array.isArray(snapshot.pages) ||
|
|
33
|
+
snapshot.pages.length * 65536 !== payload.length)
|
|
34
|
+
throw new TypeError('invalid sparse snapshot');
|
|
35
|
+
let previous = -1;
|
|
36
|
+
for (const page of snapshot.pages) {
|
|
37
|
+
if (!Number.isSafeInteger(page) || page <= previous || page * 65536 >= snapshot.memoryBytes)
|
|
38
|
+
throw new TypeError('invalid sparse snapshot page');
|
|
39
|
+
previous = page;
|
|
40
|
+
}
|
|
41
|
+
return snapshot.memoryBytes;
|
|
42
|
+
}
|
|
43
|
+
/** Restores a complete snapshot, clearing pages omitted by the sparse representation. */
|
|
44
|
+
export function restoreSnapshot(target, payload, snapshot) {
|
|
45
|
+
if (snapshotLength(snapshot, payload) !== target.length)
|
|
46
|
+
throw new RangeError('snapshot size differs');
|
|
47
|
+
if (!snapshot) {
|
|
48
|
+
target.set(payload);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
target.fill(0);
|
|
52
|
+
snapshot.pages.forEach((page, index) => target.set(payload.subarray(index * 65536, (index + 1) * 65536), page * 65536));
|
|
53
|
+
}
|
|
5
54
|
export function encodeFrame(header, payload = new Uint8Array(), maximum = 0x100100000) {
|
|
6
55
|
const metadata = encoder.encode(JSON.stringify(encodeValue(header)));
|
|
7
56
|
if (metadata.length > 65536)
|
|
8
57
|
throw new RangeError('frame header exceeds limit');
|
|
9
58
|
const length = 12 + metadata.length + payload.length;
|
|
10
59
|
if (payload.length > 0xffffffff || length > maximum)
|
|
11
|
-
throw new RangeError(
|
|
60
|
+
throw new RangeError(`frame of ${length} bytes exceeds the limit of ${maximum} bytes`);
|
|
12
61
|
const bytes = new Uint8Array(length);
|
|
13
62
|
const view = new DataView(bytes.buffer);
|
|
14
63
|
view.setUint32(0, magic, true);
|
|
@@ -42,13 +91,76 @@ export function validateIdentity(value) {
|
|
|
42
91
|
value.sequence < 0)
|
|
43
92
|
throw new TypeError('invalid frame identity');
|
|
44
93
|
}
|
|
45
|
-
|
|
46
|
-
|
|
94
|
+
/** Version of the run encoding of a results frame, named in the request and the response. */
|
|
95
|
+
export const runsVersion = 1;
|
|
96
|
+
/**
|
|
97
|
+
* Run encoding of a results frame, `runs` of the request: per result the read runs and the write
|
|
98
|
+
* runs as start, length pairs, then the written bytes back to back, then the escapes. Results
|
|
99
|
+
* that carry addresses are converted, so a lane may mix paths.
|
|
100
|
+
*/
|
|
101
|
+
function encodeRunResults(identity, results, maximum) {
|
|
102
|
+
const parts = results.map((result) => {
|
|
103
|
+
const readRuns = result.readRuns ?? runsOfSorted(Uint32Array.from(result.reads).sort());
|
|
104
|
+
const packed = result.writeRuns
|
|
105
|
+
? { runs: result.writeRuns, data: result.writeData ?? new Uint8Array() }
|
|
106
|
+
: writeRunsOf(result.writes);
|
|
107
|
+
if (packed.data.length !== runBytes(packed.runs))
|
|
108
|
+
throw new RangeError('write runs and write data differ');
|
|
109
|
+
return { result, readRuns, packed };
|
|
110
|
+
});
|
|
111
|
+
const length = parts.reduce((sum, { result, readRuns, packed }) => sum +
|
|
112
|
+
readRuns.length * 4 +
|
|
113
|
+
packed.runs.length * 4 +
|
|
114
|
+
packed.data.length +
|
|
115
|
+
(result.escapes?.length ?? 0) * 8, 0);
|
|
116
|
+
if (length > 0xffffffff || length + 12 > maximum)
|
|
117
|
+
throw new RangeError('result payload exceeds limit');
|
|
118
|
+
const payload = new Uint8Array(length), view = new DataView(payload.buffer);
|
|
119
|
+
let offset = 0;
|
|
120
|
+
const put = (runs) => {
|
|
121
|
+
for (let i = 0; i < runs.length; i++, offset += 4)
|
|
122
|
+
view.setUint32(offset, runs[i], true);
|
|
123
|
+
};
|
|
124
|
+
const descriptors = parts.map(({ result, readRuns, packed }) => {
|
|
125
|
+
const { reads, writes, readRuns: _r, writeRuns: _w, writeData: _d, escapes, ...rest } = result;
|
|
126
|
+
put(readRuns);
|
|
127
|
+
put(packed.runs);
|
|
128
|
+
payload.set(packed.data, offset);
|
|
129
|
+
offset += packed.data.length;
|
|
130
|
+
for (const [start, size] of escapes ?? []) {
|
|
131
|
+
view.setUint32(offset, start, true);
|
|
132
|
+
view.setUint32(offset + 4, size, true);
|
|
133
|
+
offset += 8;
|
|
134
|
+
}
|
|
135
|
+
return {
|
|
136
|
+
...rest,
|
|
137
|
+
reads: readRuns.length / 2,
|
|
138
|
+
writes: packed.runs.length / 2,
|
|
139
|
+
readBytes: runBytes(readRuns),
|
|
140
|
+
writeBytes: packed.data.length,
|
|
141
|
+
...(escapes?.length ? { escapes: escapes.length } : {})
|
|
142
|
+
};
|
|
143
|
+
});
|
|
144
|
+
return encodeFrame({
|
|
145
|
+
session: identity.session,
|
|
146
|
+
epoch: identity.epoch,
|
|
147
|
+
sequence: identity.sequence,
|
|
148
|
+
runs: runsVersion,
|
|
149
|
+
results: descriptors
|
|
150
|
+
}, payload, maximum);
|
|
151
|
+
}
|
|
152
|
+
export function encodeResults(identity, results, maximum = 0x100100000, runs = false) {
|
|
153
|
+
if (runs)
|
|
154
|
+
return encodeRunResults(identity, results, maximum);
|
|
155
|
+
const length = results.reduce((sum, result) => sum +
|
|
156
|
+
result.reads.length * 4 +
|
|
157
|
+
result.writes.length * 8 +
|
|
158
|
+
(result.escapes?.length ?? 0) * 8, 0);
|
|
47
159
|
if (length > 0xffffffff || length + 12 > maximum)
|
|
48
160
|
throw new RangeError('result payload exceeds limit');
|
|
49
161
|
const payload = new Uint8Array(length), view = new DataView(payload.buffer);
|
|
50
162
|
let offset = 0;
|
|
51
|
-
const descriptors = results.map(({ reads, writes, ...result }) => {
|
|
163
|
+
const descriptors = results.map(({ reads, writes, escapes, ...result }) => {
|
|
52
164
|
for (const address of reads) {
|
|
53
165
|
view.setUint32(offset, address, true);
|
|
54
166
|
offset += 4;
|
|
@@ -58,7 +170,17 @@ export function encodeResults(identity, results, maximum = 0x100100000) {
|
|
|
58
170
|
view.setUint32(offset + 4, value, true);
|
|
59
171
|
offset += 8;
|
|
60
172
|
}
|
|
61
|
-
|
|
173
|
+
for (const [start, size] of escapes ?? []) {
|
|
174
|
+
view.setUint32(offset, start, true);
|
|
175
|
+
view.setUint32(offset + 4, size, true);
|
|
176
|
+
offset += 8;
|
|
177
|
+
}
|
|
178
|
+
return {
|
|
179
|
+
...result,
|
|
180
|
+
reads: reads.length,
|
|
181
|
+
writes: writes.length,
|
|
182
|
+
...(escapes?.length ? { escapes: escapes.length } : {})
|
|
183
|
+
};
|
|
62
184
|
});
|
|
63
185
|
return encodeFrame({
|
|
64
186
|
session: identity.session,
|
|
@@ -76,17 +198,27 @@ export function decodeResults(bytes, identity, options) {
|
|
|
76
198
|
throw new TypeError('stale lane response');
|
|
77
199
|
if (!Array.isArray(header.results) || header.results.length !== options.calls)
|
|
78
200
|
throw new TypeError('invalid lane result count');
|
|
201
|
+
if (header.runs !== undefined) {
|
|
202
|
+
if (header.runs !== runsVersion)
|
|
203
|
+
throw new TypeError('unsupported results encoding');
|
|
204
|
+
if (!options.runs)
|
|
205
|
+
throw new TypeError('unrequested run encoding');
|
|
206
|
+
return decodeRunResults(header.results, payload, options);
|
|
207
|
+
}
|
|
79
208
|
let offset = 0, retained = 0;
|
|
80
209
|
const view = new DataView(payload.buffer, payload.byteOffset, payload.byteLength);
|
|
81
|
-
const results = header.results.map(({ reads, writes, ...result }) => {
|
|
82
|
-
if (![reads, writes].every((n) => Number.isSafeInteger(n) && n >= 0)
|
|
210
|
+
const results = header.results.map(({ reads, writes, escapes = 0, ...result }) => {
|
|
211
|
+
if (![reads, writes, escapes].every((n) => Number.isSafeInteger(n) && n >= 0) ||
|
|
212
|
+
escapes > reads)
|
|
83
213
|
throw new TypeError('invalid lane tracking counts');
|
|
84
214
|
retained += reads + writes;
|
|
85
|
-
if (retained > options.maxBytes + 8 ||
|
|
215
|
+
if (retained > options.maxBytes + 8 ||
|
|
216
|
+
offset + reads * 4 + writes * 8 + escapes * 8 > payload.length)
|
|
86
217
|
throw new RangeError('lane tracking exceeds limit');
|
|
87
218
|
if ((result.trap !== undefined && typeof result.trap !== 'string') ||
|
|
88
219
|
(result.errorKind !== undefined && typeof result.errorKind !== 'string') ||
|
|
89
|
-
(result.exhausted !== undefined && typeof result.exhausted !== 'boolean')
|
|
220
|
+
(result.exhausted !== undefined && typeof result.exhausted !== 'boolean') ||
|
|
221
|
+
(result.inconclusive !== undefined && typeof result.inconclusive !== 'boolean'))
|
|
90
222
|
throw new TypeError('invalid lane trap');
|
|
91
223
|
const readSet = [], writeSet = [];
|
|
92
224
|
for (let i = 0; i < reads; i++) {
|
|
@@ -103,7 +235,70 @@ export function decodeResults(bytes, identity, options) {
|
|
|
103
235
|
throw new RangeError('lane write outside arena');
|
|
104
236
|
writeSet.push([address, value]);
|
|
105
237
|
}
|
|
106
|
-
|
|
238
|
+
const escapeSet = [];
|
|
239
|
+
for (let i = 0; i < escapes; i++) {
|
|
240
|
+
const start = view.getUint32(offset, true), size = view.getUint32(offset + 4, true);
|
|
241
|
+
offset += 8;
|
|
242
|
+
if (size < 1 || start + size > options.memoryBytes)
|
|
243
|
+
throw new RangeError('lane escape outside arena');
|
|
244
|
+
escapeSet.push([start, size]);
|
|
245
|
+
}
|
|
246
|
+
return {
|
|
247
|
+
...result,
|
|
248
|
+
reads: readSet,
|
|
249
|
+
writes: writeSet,
|
|
250
|
+
...(escapes ? { escapes: escapeSet } : {})
|
|
251
|
+
};
|
|
252
|
+
});
|
|
253
|
+
if (offset !== payload.length)
|
|
254
|
+
throw new TypeError('unconsumed lane payload');
|
|
255
|
+
return results;
|
|
256
|
+
}
|
|
257
|
+
function decodeRunResults(descriptors, payload, options) {
|
|
258
|
+
let offset = 0, retained = 0;
|
|
259
|
+
const view = new DataView(payload.buffer, payload.byteOffset, payload.byteLength);
|
|
260
|
+
const take = (count) => {
|
|
261
|
+
const out = new Uint32Array(count * 2);
|
|
262
|
+
for (let i = 0; i < out.length; i++, offset += 4)
|
|
263
|
+
out[i] = view.getUint32(offset, true);
|
|
264
|
+
return out;
|
|
265
|
+
};
|
|
266
|
+
const results = descriptors.map(({ reads, writes, readBytes, writeBytes, escapes = 0, ...result }) => {
|
|
267
|
+
if (![reads, writes, readBytes, writeBytes, escapes].every((n) => Number.isSafeInteger(n) && n >= 0))
|
|
268
|
+
throw new TypeError('invalid lane tracking counts');
|
|
269
|
+
retained += readBytes + writeBytes;
|
|
270
|
+
if (retained > options.maxBytes + 8 ||
|
|
271
|
+
offset + reads * 8 + writes * 8 + writeBytes + escapes * 8 > payload.length)
|
|
272
|
+
throw new RangeError('lane tracking exceeds limit');
|
|
273
|
+
if ((result.trap !== undefined && typeof result.trap !== 'string') ||
|
|
274
|
+
(result.errorKind !== undefined && typeof result.errorKind !== 'string') ||
|
|
275
|
+
(result.exhausted !== undefined && typeof result.exhausted !== 'boolean') ||
|
|
276
|
+
(result.inconclusive !== undefined && typeof result.inconclusive !== 'boolean'))
|
|
277
|
+
throw new TypeError('invalid lane trap');
|
|
278
|
+
const readRuns = take(reads), writeRuns = take(writes);
|
|
279
|
+
checkRuns(readRuns, options.memoryBytes, 'read');
|
|
280
|
+
checkRuns(writeRuns, options.memoryBytes, 'write');
|
|
281
|
+
if (runBytes(readRuns) !== readBytes || runBytes(writeRuns) !== writeBytes)
|
|
282
|
+
throw new TypeError('invalid lane tracking counts');
|
|
283
|
+
const writeData = payload.slice(offset, offset + writeBytes);
|
|
284
|
+
offset += writeBytes;
|
|
285
|
+
const escapeSet = [];
|
|
286
|
+
for (let i = 0; i < escapes; i++) {
|
|
287
|
+
const start = view.getUint32(offset, true), size = view.getUint32(offset + 4, true);
|
|
288
|
+
offset += 8;
|
|
289
|
+
if (size < 1 || start + size > options.memoryBytes)
|
|
290
|
+
throw new RangeError('lane escape outside arena');
|
|
291
|
+
escapeSet.push([start, size]);
|
|
292
|
+
}
|
|
293
|
+
return {
|
|
294
|
+
...result,
|
|
295
|
+
reads: [],
|
|
296
|
+
writes: [],
|
|
297
|
+
readRuns,
|
|
298
|
+
writeRuns,
|
|
299
|
+
writeData,
|
|
300
|
+
...(escapes ? { escapes: escapeSet } : {})
|
|
301
|
+
};
|
|
107
302
|
});
|
|
108
303
|
if (offset !== payload.length)
|
|
109
304
|
throw new TypeError('unconsumed lane payload');
|
package/dist/inline.d.ts
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
/** Names of the exported mutable i32 globals the inline lowering adds, plus the stack export. */
|
|
2
|
+
export declare const inlineGlobals: {
|
|
3
|
+
readonly readBitmap: "__folio_rb";
|
|
4
|
+
readonly writeBitmap: "__folio_wb";
|
|
5
|
+
readonly logPointer: "__folio_lp";
|
|
6
|
+
readonly logLimit: "__folio_ll";
|
|
7
|
+
readonly stampAddress: "__folio_sa";
|
|
8
|
+
readonly stackPointer: "__folio_sp";
|
|
9
|
+
readonly frameLow: "__folio_fl";
|
|
10
|
+
readonly frameSize: "__folio_fn";
|
|
11
|
+
readonly frameShadow: "__folio_fs";
|
|
12
|
+
readonly exemptLow: "__folio_xl";
|
|
13
|
+
readonly exemptEnd: "__folio_xe";
|
|
14
|
+
readonly slotLow: "__folio_sl";
|
|
15
|
+
readonly slotEnd: "__folio_se";
|
|
16
|
+
readonly arenaLow: "__folio_al";
|
|
17
|
+
readonly arenaEnd: "__folio_ae";
|
|
18
|
+
readonly violation: "__folio_xv";
|
|
19
|
+
readonly windowLow: "__folio_wl";
|
|
20
|
+
readonly windowEnd: "__folio_we";
|
|
21
|
+
readonly windowDelta: "__folio_wd";
|
|
22
|
+
readonly readWindow0Low: "__folio_r0l";
|
|
23
|
+
readonly readWindow0Span: "__folio_r0m";
|
|
24
|
+
readonly readWindow1Low: "__folio_r1l";
|
|
25
|
+
readonly readWindow1Span: "__folio_r1m";
|
|
26
|
+
readonly readSummary: "__folio_rs";
|
|
27
|
+
readonly readHits: "__folio_ch";
|
|
28
|
+
readonly readMisses: "__folio_cm";
|
|
29
|
+
readonly readExempt: "__folio_ce";
|
|
30
|
+
};
|
|
31
|
+
/** Bytes of one undo entry: the granule address, the stamp, then the prior bytes of the granule. */
|
|
32
|
+
export declare const inlineEntryBytes: (writeGranule?: number) => number;
|
|
33
|
+
/** Options for `lowerInline`. */
|
|
34
|
+
export interface InlineOptions {
|
|
35
|
+
/** Read granule in bytes, a power of two from 16 to 4096. Default 64. */
|
|
36
|
+
readGranule?: number;
|
|
37
|
+
/** Write and undo granule in bytes, a power of two from 16 to 256. Default 32. */
|
|
38
|
+
writeGranule?: number;
|
|
39
|
+
/** Refuse a memory that is not shared. Pass false to lower a module for one thread. Default true. */
|
|
40
|
+
requireShared?: boolean;
|
|
41
|
+
/** The mutable i32 global exported as `__folio_sp`. Default: the one `stackRoute` names, if any. */
|
|
42
|
+
stack?: string;
|
|
43
|
+
/** Track exposed reads of one frame region (see `lowerInline`). Default off. */
|
|
44
|
+
frame?: boolean;
|
|
45
|
+
/** Leave one private stack window untracked and trap on another slot (see `lowerInline`). Default off. */
|
|
46
|
+
slot?: boolean;
|
|
47
|
+
/**
|
|
48
|
+
* Rebase accesses to one address window onto a slot-private copy (see `lowerInline`). Turns
|
|
49
|
+
* `frame` on, and `slot` when there is a stack global. Default off.
|
|
50
|
+
*/
|
|
51
|
+
window?: boolean;
|
|
52
|
+
/**
|
|
53
|
+
* Trap an access that overlaps the window `[__folio_al, __folio_ae)`, with no stack global
|
|
54
|
+
* needed and no exempt or own window. For a lane whose arena sits above the program memory.
|
|
55
|
+
* Default off.
|
|
56
|
+
*/
|
|
57
|
+
guard?: boolean;
|
|
58
|
+
/**
|
|
59
|
+
* Count a fuel unit at every function entry and loop start, as the host-call lowering does:
|
|
60
|
+
* `__folio_set_fuel(n)` sets the budget and `folio.exhausted` is called when it reaches 0.
|
|
61
|
+
* Default off.
|
|
62
|
+
*/
|
|
63
|
+
fuel?: boolean;
|
|
64
|
+
/**
|
|
65
|
+
* Poll the i32 at `__folio_sa + 4` at every loop start and trap with `unreachable` when it is
|
|
66
|
+
* not zero, so the host can stop an iteration that does not return. Function entries are not
|
|
67
|
+
* polled: a call cycle runs out of stack and traps by itself. Default off.
|
|
68
|
+
*/
|
|
69
|
+
stop?: boolean;
|
|
70
|
+
/**
|
|
71
|
+
* Read windows, 0 to 2: a load of at most 8 bytes that lies inside one sets no read mark and
|
|
72
|
+
* runs none of the slot or frame checks (see `lowerInline`). Default 0.
|
|
73
|
+
*/
|
|
74
|
+
readWindows?: number;
|
|
75
|
+
/**
|
|
76
|
+
* Bytes of the read summary, a power of two from 1 to 32768, or 0 for none (see `lowerInline`).
|
|
77
|
+
* Default 0.
|
|
78
|
+
*/
|
|
79
|
+
readSummary?: number;
|
|
80
|
+
/**
|
|
81
|
+
* With read windows, count the loads of at most 8 bytes by where they land: `__folio_ch` inside
|
|
82
|
+
* a window, `__folio_cm` outside every window and tracked, `__folio_ce` in the exempt window.
|
|
83
|
+
* The owner resets them. Default off.
|
|
84
|
+
*/
|
|
85
|
+
readCount?: boolean;
|
|
86
|
+
/**
|
|
87
|
+
* With read windows, test the exempt window before the windows instead of after them. An exempt
|
|
88
|
+
* load then costs what it does with no windows and a hit costs 10 instructions more. Default off.
|
|
89
|
+
*/
|
|
90
|
+
exemptFirst?: boolean;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* A second lowering, next to the host-call one in `prepare`: it marks memory accesses inline, with
|
|
94
|
+
* no host call and no added import, so a region can be validated after it ran. Nothing in
|
|
95
|
+
* `prepare` calls it yet and its output is not a `PreparedProgram` field.
|
|
96
|
+
*
|
|
97
|
+
* State lives in mutable i32 globals the owner exports and sets before each region; all start at
|
|
98
|
+
* 0, which traps on the first access. `__folio_rb` and `__folio_wb` are the bases of the read and
|
|
99
|
+
* write bitmaps, `__folio_lp` and `__folio_ll` the undo log pointer and limit, `__folio_sa` the
|
|
100
|
+
* address of a shared i32 stamp counter (4-byte aligned). The stack pointer global is exported as
|
|
101
|
+
* `__folio_sp` when the module has one. A bitmap has one bit per granule: granule `g`, with
|
|
102
|
+
* `g = address >> log2(granule)`, is bit `g & 7` of byte `base + (g >> 3)`. The bitmaps are
|
|
103
|
+
* private to a region and the owner clears them between regions.
|
|
104
|
+
*
|
|
105
|
+
* Every load, store, atomic access and SIMD load marks its read granule, and an access that
|
|
106
|
+
* crosses a boundary marks both. A store, a SIMD lane store and the destination of `memory.copy`,
|
|
107
|
+
* `memory.fill` and `memory.init` mark write granules; atomic accesses mark both sets; the source
|
|
108
|
+
* of `memory.copy` marks read granules. The first write to a write granule appends an undo entry
|
|
109
|
+
* at `__folio_lp`: the granule address (u32), a stamp from an atomic add on `__folio_sa` taken
|
|
110
|
+
* after the prior bytes were copied and before the write, then the prior bytes of the granule.
|
|
111
|
+
* `__folio_lp` then advances by `inlineEntryBytes(writeGranule)`; an entry that would pass
|
|
112
|
+
* `__folio_ll` traps with `unreachable`. The start function is not lowered.
|
|
113
|
+
*
|
|
114
|
+
* With `frame: true` the module also tracks one frame region, for a stack frame whose bytes an
|
|
115
|
+
* iteration may read after an earlier iteration wrote them. The owner sets `__folio_fl` (low
|
|
116
|
+
* address), `__folio_fn` (size in bytes) and `__folio_fs` (shadow base) before a region; with
|
|
117
|
+
* `__folio_fn` at 0 nothing is tracked. An access that overlaps the region is checked against a
|
|
118
|
+
* shadow of one written bit per frame byte, `4 * ceil(size / 32)` bytes with the bits of frame
|
|
119
|
+
* granule `g` (32 bytes) in the i32 at `__folio_fs + 4 * g`, then 4 pad bytes, then the exposed
|
|
120
|
+
* bits laid out the same way, then 4 more pad bytes. A store sets written bits after its value is
|
|
121
|
+
* computed; a load sets the exposed bit of each byte it reads whose written bit is clear, so a
|
|
122
|
+
* region reports exactly the frame bytes it read before it wrote them.
|
|
123
|
+
* `memory.copy`, `memory.fill` and `memory.init` set or check the exact byte range, an atomic
|
|
124
|
+
* read-modify-write checks then sets, and any other atomic access or SIMD load checks. The owner
|
|
125
|
+
* zeroes the shadow before each region. Accesses outside the region pay one compare. Stack below
|
|
126
|
+
* the region is not tracked.
|
|
127
|
+
*
|
|
128
|
+
* With `slot: true` and a stack global, the owner also sets three address windows before a region:
|
|
129
|
+
* the exempt window `[__folio_xl, __folio_xe)`, the whole private slot `[__folio_sl, __folio_se)`
|
|
130
|
+
* and the arena of all slots `[__folio_al, __folio_ae)`. An access inside the exempt window sets no
|
|
131
|
+
* mark and logs no undo entry. An access that overlaps the arena but does not lie inside the own
|
|
132
|
+
* slot stores its address in `__folio_xv` and traps with `unreachable`, because another iteration
|
|
133
|
+
* owns those bytes. Every other access is tracked as before, so the exempt window must not contain
|
|
134
|
+
* the tracked frame; an access that crosses the window edge is tracked. `memory.copy`,
|
|
135
|
+
* `memory.fill` and `memory.init` apply the same tests to the whole range, and a range counts as
|
|
136
|
+
* inside only when every byte is. The exempt bytes are not restored by a rollback, so the owner
|
|
137
|
+
* must treat the window as dead after the region.
|
|
138
|
+
*
|
|
139
|
+
* With `readWindows: n` the owner also sets, per window `i` below `n`, `__folio_r<i>l` (first byte)
|
|
140
|
+
* and `__folio_r<i>m` (window size minus 7, or 0 for no window) before a region, and marks every
|
|
141
|
+
* read granule that the window touches in the read bitmap itself. A plain, non-atomic load or SIMD
|
|
142
|
+
* load of at most 8 bytes whose address `a` satisfies `a - low < size - 7` lies wholly inside the
|
|
143
|
+
* window, so its read mark is already set: it sets no mark and skips the slot, arena and frame
|
|
144
|
+
* checks. Every other access, a store, an atomic, a wider load, a load that crosses a window edge
|
|
145
|
+
* and every bulk operation, is tracked as before. The window must lie outside the arena of all
|
|
146
|
+
* slots and the owner frame region, which is what those checks would have found; a false conflict
|
|
147
|
+
* is possible only for a granule of the window that another iteration writes and this one does
|
|
148
|
+
* not read. The exempt window is tested first, so an exempt load executes what it does without
|
|
149
|
+
* windows; the guards of the windows run only for a load outside it. With `readCount` the lowering
|
|
150
|
+
* also counts those loads by where they land (`__folio_ch`, `__folio_cm`, `__folio_ce`).
|
|
151
|
+
*
|
|
152
|
+
* With `readSummary: n` every read mark also sets one byte of the summary at `__folio_rs`, before
|
|
153
|
+
* the bit it summarizes. The byte is `(a >> (log2(readGranule) + 11)) & (n - 1)` for address `a`,
|
|
154
|
+
* one byte per 64 words of the read bitmap. A bitmap word is never non-zero while its summary byte
|
|
155
|
+
* is zero, so a scan may skip the blocks whose byte is zero. The owner sets the bytes of any
|
|
156
|
+
* granule it marks itself, a read window for one, and clears a byte only when it clears the words
|
|
157
|
+
* of its block. An address past the memory wraps to a byte that covers other words, which only
|
|
158
|
+
* adds blocks to scan. The owner must also set `n` large enough that every address of the memory
|
|
159
|
+
* has its own byte, or the scan visits blocks it need not.
|
|
160
|
+
*
|
|
161
|
+
* With `window: true` (which implies `frame`, and `slot` when there is a stack global) the owner also sets `__folio_wl` and
|
|
162
|
+
* `__folio_we`, a window of the address space, and `__folio_wd`, the distance from it to a private
|
|
163
|
+
* copy that lies inside the tracked frame region. A load, store, atomic or SIMD access that lies in
|
|
164
|
+
* the window runs on the copy (`address + __folio_wd`), a bulk operation does the same when its
|
|
165
|
+
* whole range is inside, and an access or range that crosses the window edge traps with
|
|
166
|
+
* `unreachable`. Accesses in the tracked frame region, the copy included, set no bitmap mark and
|
|
167
|
+
* log no undo entry: their only record is the frame shadow, whose exposed bits the owner
|
|
168
|
+
* validates, and the copy is discarded or committed byte by byte from the written bits. With the
|
|
169
|
+
* window empty (`__folio_we` at 0) nothing is rebased.
|
|
170
|
+
*
|
|
171
|
+
* Throws for a module without exactly one memory, a 64-bit memory, an unshared memory when
|
|
172
|
+
* `requireShared` is set, a start function that calls, a reserved name or an expression it does
|
|
173
|
+
* not know. An imported host function that touches memory is not seen.
|
|
174
|
+
*/
|
|
175
|
+
export declare function lowerInline(bytes: Uint8Array, options?: InlineOptions): Uint8Array;
|
|
176
|
+
/**
|
|
177
|
+
* The module a managed lane runs with inline tracking: `lowerInline` with fuel and the arena
|
|
178
|
+
* guard on, and the memory import widened to the largest maximum so a lane can place the marks
|
|
179
|
+
* and the undo log above the program memory. Build time only; the input is the control plan's
|
|
180
|
+
* `workerOriginal`.
|
|
181
|
+
*/
|
|
182
|
+
export declare function inlineLane(workerOriginal: Uint8Array): Uint8Array;
|