@gmitch215/folio 1.0.1 → 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 +398 -28
- 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/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/context.d.ts +16 -2
- package/dist/context.js +60 -14
- package/dist/coordinator.js +50 -3
- package/dist/extract.d.ts +19 -0
- package/dist/extract.js +133 -5
- package/dist/fold.d.ts +32 -0
- package/dist/fold.js +105 -0
- package/dist/frames.d.ts +5 -1
- package/dist/frames.js +156 -9
- package/dist/inline.d.ts +114 -0
- package/dist/inline.js +276 -13
- package/dist/iteration-admission.d.ts +116 -7
- package/dist/iteration-admission.js +137 -8
- package/dist/iteration-layout.d.ts +60 -0
- package/dist/iteration-layout.js +54 -3
- package/dist/iteration-owner.d.ts +14 -1
- package/dist/iteration-owner.js +2 -2
- package/dist/iteration-worker.js +124 -58
- package/dist/iteration.d.ts +179 -13
- package/dist/iteration.js +845 -70
- package/dist/lease.d.ts +45 -0
- package/dist/lease.js +274 -0
- package/dist/managed.d.ts +72 -0
- package/dist/managed.js +425 -27
- package/dist/memo.d.ts +99 -0
- package/dist/memo.js +250 -0
- package/dist/node.d.ts +43 -1
- package/dist/node.js +87 -11
- 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 +12 -11
- package/dist/transaction.d.ts +10 -0
- package/package.json +40 -4
package/dist/context.d.ts
CHANGED
|
@@ -17,6 +17,15 @@ export interface ContextResetReport {
|
|
|
17
17
|
globals: number;
|
|
18
18
|
/** Table slots whose reference was restored. */
|
|
19
19
|
tableSlots: number;
|
|
20
|
+
/** Bytes inside `overwrite` ranges, neither compared nor restored. */
|
|
21
|
+
skippedBytes: number;
|
|
22
|
+
}
|
|
23
|
+
/** Bytes of one context memory that the caller writes completely before anything reads them. */
|
|
24
|
+
export interface ContextRange {
|
|
25
|
+
/** Index into the plan's memories. The default is 0. */
|
|
26
|
+
memory?: number;
|
|
27
|
+
offset: number;
|
|
28
|
+
length: number;
|
|
20
29
|
}
|
|
21
30
|
/**
|
|
22
31
|
* Captures private Wasm state after initialization. `prepareContext` exposes hidden bindings.
|
|
@@ -28,6 +37,11 @@ export interface ContextResetReport {
|
|
|
28
37
|
export declare class InstanceCheckpoint {
|
|
29
38
|
#private;
|
|
30
39
|
constructor(instance: WebAssembly.Instance, plan: PreparedContext);
|
|
31
|
-
/**
|
|
32
|
-
|
|
40
|
+
/**
|
|
41
|
+
* Restores all mutable bindings. `full` skips page comparison and copies every memory byte.
|
|
42
|
+
* Bytes inside `overwrite` are left as they are: the caller must write every one of them
|
|
43
|
+
* before the program reads it, so a task that rewrites its input region each time does not pay
|
|
44
|
+
* to compare and copy back the previous task's input.
|
|
45
|
+
*/
|
|
46
|
+
restore(strategy?: 'changed' | 'full', overwrite?: ContextRange[]): ContextResetReport;
|
|
33
47
|
}
|
package/dist/context.js
CHANGED
|
@@ -45,8 +45,13 @@ export class InstanceCheckpoint {
|
|
|
45
45
|
};
|
|
46
46
|
});
|
|
47
47
|
}
|
|
48
|
-
/**
|
|
49
|
-
|
|
48
|
+
/**
|
|
49
|
+
* Restores all mutable bindings. `full` skips page comparison and copies every memory byte.
|
|
50
|
+
* Bytes inside `overwrite` are left as they are: the caller must write every one of them
|
|
51
|
+
* before the program reads it, so a task that rewrites its input region each time does not pay
|
|
52
|
+
* to compare and copy back the previous task's input.
|
|
53
|
+
*/
|
|
54
|
+
restore(strategy = 'changed', overwrite = []) {
|
|
50
55
|
if (strategy !== 'changed' && strategy !== 'full')
|
|
51
56
|
throw new TypeError('invalid context reset strategy');
|
|
52
57
|
for (const { memory, bytes } of this.#memories)
|
|
@@ -55,27 +60,38 @@ export class InstanceCheckpoint {
|
|
|
55
60
|
for (const { table, slots } of this.#tables)
|
|
56
61
|
if (table.length !== slots.length)
|
|
57
62
|
throw new ContextResetError('table-size');
|
|
63
|
+
const skipped = this.#ranges(overwrite);
|
|
58
64
|
const report = {
|
|
59
65
|
checkedBytes: 0,
|
|
60
66
|
copiedBytes: 0,
|
|
61
67
|
changedPages: 0,
|
|
62
68
|
globals: 0,
|
|
63
|
-
tableSlots: 0
|
|
69
|
+
tableSlots: 0,
|
|
70
|
+
skippedBytes: 0
|
|
64
71
|
};
|
|
65
|
-
|
|
72
|
+
this.#memories.forEach(({ memory, bytes }, index) => {
|
|
66
73
|
const target = new Uint8Array(memory.buffer);
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
74
|
+
let from = 0;
|
|
75
|
+
for (const [start, end] of [
|
|
76
|
+
...skipped[index],
|
|
77
|
+
[bytes.length, bytes.length]
|
|
78
|
+
]) {
|
|
79
|
+
for (let offset = from; offset < start; offset += 65536) {
|
|
80
|
+
const stop = Math.min(offset + 65536, start);
|
|
81
|
+
const source = bytes.subarray(offset, stop), current = target.subarray(offset, stop);
|
|
82
|
+
if (strategy === 'changed')
|
|
83
|
+
report.checkedBytes += current.length;
|
|
84
|
+
if (strategy === 'full' ||
|
|
85
|
+
!Buffer.from(current.buffer, current.byteOffset, current.length).equals(Buffer.from(source.buffer, source.byteOffset, source.length))) {
|
|
86
|
+
current.set(source);
|
|
87
|
+
report.copiedBytes += current.length;
|
|
88
|
+
report.changedPages++;
|
|
89
|
+
}
|
|
76
90
|
}
|
|
91
|
+
report.skippedBytes += end - start;
|
|
92
|
+
from = end;
|
|
77
93
|
}
|
|
78
|
-
}
|
|
94
|
+
});
|
|
79
95
|
for (const { global, value } of this.#globals) {
|
|
80
96
|
if (!Object.is(global.value, value)) {
|
|
81
97
|
global.value = value;
|
|
@@ -90,4 +106,34 @@ export class InstanceCheckpoint {
|
|
|
90
106
|
}
|
|
91
107
|
return report;
|
|
92
108
|
}
|
|
109
|
+
/** Validates ranges and returns, per memory, sorted and merged [start, end) pairs. */
|
|
110
|
+
#ranges(overwrite) {
|
|
111
|
+
const merged = this.#memories.map(() => []);
|
|
112
|
+
for (const { memory = 0, offset, length } of overwrite) {
|
|
113
|
+
const size = this.#memories[memory]?.bytes.length;
|
|
114
|
+
if (size === undefined)
|
|
115
|
+
throw new RangeError('overwrite range names a missing memory');
|
|
116
|
+
if (!Number.isSafeInteger(offset) ||
|
|
117
|
+
!Number.isSafeInteger(length) ||
|
|
118
|
+
offset < 0 ||
|
|
119
|
+
length < 0)
|
|
120
|
+
throw new RangeError('overwrite range needs a non-negative integer offset and length');
|
|
121
|
+
if (offset + length > size)
|
|
122
|
+
throw new RangeError('overwrite range exceeds the memory');
|
|
123
|
+
if (length)
|
|
124
|
+
merged[memory].push([offset, offset + length]);
|
|
125
|
+
}
|
|
126
|
+
return merged.map((pairs) => {
|
|
127
|
+
pairs.sort((a, b) => a[0] - b[0]);
|
|
128
|
+
const out = [];
|
|
129
|
+
for (const [start, end] of pairs) {
|
|
130
|
+
const last = out[out.length - 1];
|
|
131
|
+
if (last && start <= last[1])
|
|
132
|
+
last[1] = Math.max(last[1], end);
|
|
133
|
+
else
|
|
134
|
+
out.push([start, end]);
|
|
135
|
+
}
|
|
136
|
+
return out;
|
|
137
|
+
});
|
|
138
|
+
}
|
|
93
139
|
}
|
package/dist/coordinator.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { DirtyRuns, runBytes } from "./runs.js";
|
|
1
2
|
function trap(result) {
|
|
2
3
|
if (!result.trap)
|
|
3
4
|
return;
|
|
@@ -134,6 +135,7 @@ export class Coordinator {
|
|
|
134
135
|
if (failure?.status === 'rejected')
|
|
135
136
|
throw failure.reason;
|
|
136
137
|
const dirty = new Set();
|
|
138
|
+
const dirtyRuns = new DirtyRuns();
|
|
137
139
|
const canonical = new Uint8Array(this.#memory.buffer);
|
|
138
140
|
const shared = new Uint8Array(this.#workerMemory.buffer);
|
|
139
141
|
const observe = (result) => {
|
|
@@ -141,8 +143,12 @@ export class Coordinator {
|
|
|
141
143
|
return;
|
|
142
144
|
this.#profile.workerWallMs.push(result.timing.wallMs);
|
|
143
145
|
this.#profile.workerCpuMs.push(result.timing.cpuMs);
|
|
144
|
-
this.#profile.readBytes += result.
|
|
145
|
-
|
|
146
|
+
this.#profile.readBytes += result.readRuns
|
|
147
|
+
? runBytes(result.readRuns)
|
|
148
|
+
: result.reads.length;
|
|
149
|
+
this.#profile.writeBytes += result.writeData
|
|
150
|
+
? result.writeData.length
|
|
151
|
+
: result.writes.length;
|
|
146
152
|
};
|
|
147
153
|
const commit = (result) => {
|
|
148
154
|
const start = this.#profile ? performance.now() : 0;
|
|
@@ -152,6 +158,28 @@ export class Coordinator {
|
|
|
152
158
|
shared[address] = value;
|
|
153
159
|
dirty.add(address);
|
|
154
160
|
}
|
|
161
|
+
if (result.writeRuns) {
|
|
162
|
+
let at = 0;
|
|
163
|
+
for (let i = 0; i < result.writeRuns.length; i += 2) {
|
|
164
|
+
const start = result.writeRuns[i], length = result.writeRuns[i + 1];
|
|
165
|
+
const data = result.writeData;
|
|
166
|
+
const mirror = this.#memory !== this.#workerMemory;
|
|
167
|
+
if (length > 16) {
|
|
168
|
+
const piece = data.subarray(at, at + length);
|
|
169
|
+
canonical.set(piece, start);
|
|
170
|
+
if (mirror)
|
|
171
|
+
shared.set(piece, start);
|
|
172
|
+
}
|
|
173
|
+
else
|
|
174
|
+
for (let k = 0; k < length; k++) {
|
|
175
|
+
canonical[start + k] = data[at + k];
|
|
176
|
+
if (mirror)
|
|
177
|
+
shared[start + k] = data[at + k];
|
|
178
|
+
}
|
|
179
|
+
dirtyRuns.add(start, length);
|
|
180
|
+
at += length;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
155
183
|
if (this.#profile)
|
|
156
184
|
this.#profile.commitMs += performance.now() - start;
|
|
157
185
|
};
|
|
@@ -160,7 +188,13 @@ export class Coordinator {
|
|
|
160
188
|
let result = outcome.value[Math.floor(i / width)];
|
|
161
189
|
observe(result);
|
|
162
190
|
const start = this.#profile ? performance.now() : 0;
|
|
163
|
-
|
|
191
|
+
// a batch's results share one encoding; both paths stay exact if they ever mix
|
|
192
|
+
const stored = dirtyRuns.count > 0;
|
|
193
|
+
const conflict = result.reads.some((address) => dirty.has(address) || (stored && dirtyRuns.has(address))) ||
|
|
194
|
+
(result.readRuns !== undefined &&
|
|
195
|
+
(dirtyRuns.overlaps(result.readRuns) ||
|
|
196
|
+
(dirty.size > 0 &&
|
|
197
|
+
[...dirty].some((address) => overlapsRuns(result.readRuns, address)))));
|
|
164
198
|
if (this.#profile)
|
|
165
199
|
this.#profile.validationMs += performance.now() - start;
|
|
166
200
|
if (result.exhausted || conflict) {
|
|
@@ -189,3 +223,16 @@ export class Coordinator {
|
|
|
189
223
|
}
|
|
190
224
|
}
|
|
191
225
|
}
|
|
226
|
+
function overlapsRuns(runs, address) {
|
|
227
|
+
let low = 0, high = runs.length / 2 - 1;
|
|
228
|
+
while (low <= high) {
|
|
229
|
+
const mid = (low + high) >> 1;
|
|
230
|
+
if (runs[mid * 2] > address)
|
|
231
|
+
high = mid - 1;
|
|
232
|
+
else if (runs[mid * 2] + runs[mid * 2 + 1] <= address)
|
|
233
|
+
low = mid + 1;
|
|
234
|
+
else
|
|
235
|
+
return true;
|
|
236
|
+
}
|
|
237
|
+
return false;
|
|
238
|
+
}
|
package/dist/extract.d.ts
CHANGED
|
@@ -84,15 +84,32 @@ export interface StackRoute {
|
|
|
84
84
|
};
|
|
85
85
|
/** Immutable globals with a constant initializer that the set reads. */
|
|
86
86
|
constants: string[];
|
|
87
|
+
/** Delta globals of an ownership redirect that the set reads; the worker module exports them. */
|
|
88
|
+
redirect?: string[];
|
|
87
89
|
/** The worker set: closed under calls, in module order. */
|
|
88
90
|
functions: string[];
|
|
89
91
|
/** The members that sit on a call cycle. */
|
|
90
92
|
recursive: string[];
|
|
93
|
+
/** Tables a member calls through; every worker holds its own copy, built from the segments. */
|
|
94
|
+
tables: string[];
|
|
91
95
|
/** Static work per member; a recursive member counts one level of its own calls. */
|
|
92
96
|
work: Record<string, Work>;
|
|
93
97
|
/** Why the module is or is not eligible. */
|
|
94
98
|
diagnostics: string[];
|
|
95
99
|
}
|
|
100
|
+
/** Name prefix of the mutable delta globals an ownership redirect adds; the stack route sets them aside. */
|
|
101
|
+
export declare const redirectDelta = "__folio_od";
|
|
102
|
+
/** What an ownership redirect hands the iteration executor: windows with their delta globals and the copy-out. */
|
|
103
|
+
export interface OwnershipPlan {
|
|
104
|
+
/** each window `[low, end)` and the exported mutable global that holds `copy - low` */
|
|
105
|
+
windows: {
|
|
106
|
+
low: number;
|
|
107
|
+
end: number;
|
|
108
|
+
delta: string;
|
|
109
|
+
}[];
|
|
110
|
+
/** byte ranges the last record's copy supplies */
|
|
111
|
+
copyOut: [number, number][];
|
|
112
|
+
}
|
|
96
113
|
/** Computes the stack route of a module. A failure is reported in `diagnostics`, never thrown. */
|
|
97
114
|
export declare function stackRoute(original: Uint8Array, options?: AdmissionOptions): StackRoute;
|
|
98
115
|
/** One local of the owner that the schedule or the outlined body needs. */
|
|
@@ -156,6 +173,8 @@ export interface IterationRoute {
|
|
|
156
173
|
iterate?: string;
|
|
157
174
|
stackExport?: string;
|
|
158
175
|
module?: Uint8Array;
|
|
176
|
+
/** set when `module` carries an ownership redirect: the windows each slot keeps a private copy of */
|
|
177
|
+
ownership?: OwnershipPlan;
|
|
159
178
|
diagnostics: string[];
|
|
160
179
|
}
|
|
161
180
|
/**
|
package/dist/extract.js
CHANGED
|
@@ -1200,6 +1200,64 @@ export function extract(original, prepareTracked, options = {}) {
|
|
|
1200
1200
|
module?.dispose();
|
|
1201
1201
|
}
|
|
1202
1202
|
}
|
|
1203
|
+
// a table is the same in every worker when nothing can change it after instantiation: it is
|
|
1204
|
+
// defined here, not exported, no instruction writes it and every segment is a constant-offset list.
|
|
1205
|
+
// `written` reads the whole module as text, so it runs only when a call needs the answer
|
|
1206
|
+
function tableShapes(m) {
|
|
1207
|
+
const shapes = new Map();
|
|
1208
|
+
let text;
|
|
1209
|
+
const written = () => (text ??= /\btable\.(set|grow|fill|copy|init)\b/.test(m.emitText()));
|
|
1210
|
+
const count = m.getNumTables();
|
|
1211
|
+
if (!count)
|
|
1212
|
+
return { shapes, written };
|
|
1213
|
+
const exported = new Set();
|
|
1214
|
+
for (let i = 0; i < m.getNumExports(); i++) {
|
|
1215
|
+
const e = binaryen.getExportInfo(m.getExportByIndex(i));
|
|
1216
|
+
if (e.kind === binaryen.ExternalTable)
|
|
1217
|
+
exported.add(e.value);
|
|
1218
|
+
}
|
|
1219
|
+
for (let i = 0; i < count; i++) {
|
|
1220
|
+
const t = binaryen.getTableInfo(m.getTableByIndex(i));
|
|
1221
|
+
shapes.set(t.name, {
|
|
1222
|
+
refusal: t.module
|
|
1223
|
+
? 'imported table'
|
|
1224
|
+
: exported.has(t.name)
|
|
1225
|
+
? 'exported table'
|
|
1226
|
+
: undefined,
|
|
1227
|
+
runs: [],
|
|
1228
|
+
functions: new Set()
|
|
1229
|
+
});
|
|
1230
|
+
}
|
|
1231
|
+
for (let i = 0; i < m.getNumElementSegments(); i++) {
|
|
1232
|
+
const s = binaryen.getElementSegmentInfo(m.getElementSegmentByIndex(i));
|
|
1233
|
+
const shape = shapes.get(s.table);
|
|
1234
|
+
if (!shape)
|
|
1235
|
+
continue;
|
|
1236
|
+
const offset = info(s.offset);
|
|
1237
|
+
if (offset.id !== binaryen.ConstId || typeof offset.value !== 'number') {
|
|
1238
|
+
shape.refusal ??= 'element segment offset is not a constant';
|
|
1239
|
+
continue;
|
|
1240
|
+
}
|
|
1241
|
+
let start = 0;
|
|
1242
|
+
for (let k = 0; k <= s.data.length; k++) {
|
|
1243
|
+
if (k < s.data.length && s.data[k])
|
|
1244
|
+
continue;
|
|
1245
|
+
if (k > start)
|
|
1246
|
+
shape.runs.push({
|
|
1247
|
+
name: `${s.name}_${shape.runs.length}`,
|
|
1248
|
+
offset: (offset.value >>> 0) + start,
|
|
1249
|
+
names: s.data.slice(start, k)
|
|
1250
|
+
});
|
|
1251
|
+
start = k + 1;
|
|
1252
|
+
}
|
|
1253
|
+
for (const name of s.data)
|
|
1254
|
+
if (name)
|
|
1255
|
+
shape.functions.add(name);
|
|
1256
|
+
}
|
|
1257
|
+
return { shapes, written };
|
|
1258
|
+
}
|
|
1259
|
+
/** Name prefix of the mutable delta globals an ownership redirect adds; the stack route sets them aside. */
|
|
1260
|
+
export const redirectDelta = '__folio_od';
|
|
1203
1261
|
/** Computes the stack route of a module. A failure is reported in `diagnostics`, never thrown. */
|
|
1204
1262
|
export function stackRoute(original, options = {}) {
|
|
1205
1263
|
const { assumedTrips } = admission(options);
|
|
@@ -1208,6 +1266,7 @@ export function stackRoute(original, options = {}) {
|
|
|
1208
1266
|
constants: [],
|
|
1209
1267
|
functions: [],
|
|
1210
1268
|
recursive: [],
|
|
1269
|
+
tables: [],
|
|
1211
1270
|
work: Object.create(null),
|
|
1212
1271
|
diagnostics: []
|
|
1213
1272
|
};
|
|
@@ -1245,12 +1304,14 @@ export function stackRoute(original, options = {}) {
|
|
|
1245
1304
|
: undefined
|
|
1246
1305
|
});
|
|
1247
1306
|
}
|
|
1307
|
+
const { shapes: tables, written } = tableShapes(module);
|
|
1308
|
+
const refusedTables = new Set();
|
|
1248
1309
|
const bodies = [];
|
|
1249
1310
|
for (let i = 0; i < module.getNumFunctions(); i++) {
|
|
1250
1311
|
const f = binaryen.getFunctionInfo(module.getFunctionByIndex(i));
|
|
1251
1312
|
if (!f.body)
|
|
1252
1313
|
continue;
|
|
1253
|
-
const body = { info: f, targets: new Set(), refs: new Set() };
|
|
1314
|
+
const body = { info: f, targets: new Set(), refs: new Set(), tables: new Set() };
|
|
1254
1315
|
if (![...binaryen.expandType(f.params), ...f.vars].every((t) => numeric.has(t)) ||
|
|
1255
1316
|
!(f.results === binaryen.none || numeric.has(f.results)))
|
|
1256
1317
|
body.cause = 'non-numeric signature';
|
|
@@ -1260,6 +1321,25 @@ export function stackRoute(original, options = {}) {
|
|
|
1260
1321
|
body.cause ??= 'unsupported expression';
|
|
1261
1322
|
else if (v.id === binaryen.GlobalGetId || v.id === binaryen.GlobalSetId)
|
|
1262
1323
|
body.refs.add(v.name);
|
|
1324
|
+
else if (v.id === binaryen.CallIndirectId) {
|
|
1325
|
+
const shape = tables.get(v.table);
|
|
1326
|
+
const refusal = shape
|
|
1327
|
+
? (shape.refusal ?? (written() ? 'the module writes a table' : undefined))
|
|
1328
|
+
: 'no such table';
|
|
1329
|
+
if (!shape || refusal) {
|
|
1330
|
+
body.cause ??= 'indirect call';
|
|
1331
|
+
refusedTables.add(`${v.table}: ${refusal}`);
|
|
1332
|
+
}
|
|
1333
|
+
else {
|
|
1334
|
+
body.tables.add(v.table);
|
|
1335
|
+
// any function of the call's type in the table can run, so each must be a member
|
|
1336
|
+
for (const name of shape.functions) {
|
|
1337
|
+
const g = binaryen.getFunctionInfo(module.getFunction(name));
|
|
1338
|
+
if (g.params === v.params && g.results === v.results)
|
|
1339
|
+
body.targets.add(name);
|
|
1340
|
+
}
|
|
1341
|
+
}
|
|
1342
|
+
}
|
|
1263
1343
|
else if (!pureNodes.has(v.id))
|
|
1264
1344
|
body.cause ??= 'impure expression';
|
|
1265
1345
|
if (v.isAtomic)
|
|
@@ -1280,7 +1360,13 @@ export function stackRoute(original, options = {}) {
|
|
|
1280
1360
|
bodies.push(body);
|
|
1281
1361
|
}
|
|
1282
1362
|
const referenced = new Set(bodies.flatMap((body) => [...body.refs]));
|
|
1283
|
-
const
|
|
1363
|
+
const deltas = [...globals.keys()].filter((name) => name.startsWith(redirectDelta) &&
|
|
1364
|
+
referenced.has(name) &&
|
|
1365
|
+
globals.get(name).mutable &&
|
|
1366
|
+
!globals.get(name).refusal);
|
|
1367
|
+
if (deltas.length)
|
|
1368
|
+
route.redirect = deltas;
|
|
1369
|
+
const named = [...globals.keys()].filter((name) => referenced.has(name) && !deltas.includes(name));
|
|
1284
1370
|
const mutable = named.filter((name) => globals.get(name).mutable);
|
|
1285
1371
|
if (mutable.length > 1) {
|
|
1286
1372
|
for (const name of mutable.slice(1))
|
|
@@ -1317,6 +1403,9 @@ export function stackRoute(original, options = {}) {
|
|
|
1317
1403
|
}
|
|
1318
1404
|
const inSet = bodies.filter((body) => members.has(body.info.name));
|
|
1319
1405
|
route.functions = inSet.map((body) => body.info.name);
|
|
1406
|
+
route.tables = [...new Set(inSet.flatMap((body) => [...body.tables]))];
|
|
1407
|
+
for (const refusal of [...refusedTables].sort())
|
|
1408
|
+
route.diagnostics.push(`refused table ${refusal}`);
|
|
1320
1409
|
const cyclic = recursiveFunctions(new Map(inSet.map((b) => [b.info.name, b.targets])));
|
|
1321
1410
|
route.recursive = route.functions.filter((name) => cyclic.has(name));
|
|
1322
1411
|
const model = workModel(new Map(inSet.map((body) => [body.info.name, body.info])), (name) => members.has(name), assumedTrips);
|
|
@@ -1343,6 +1432,7 @@ export function stackRoute(original, options = {}) {
|
|
|
1343
1432
|
constants: [],
|
|
1344
1433
|
functions: [],
|
|
1345
1434
|
recursive: [],
|
|
1435
|
+
tables: [],
|
|
1346
1436
|
work: Object.create(null),
|
|
1347
1437
|
diagnostics: [
|
|
1348
1438
|
`stack route unavailable: ${error instanceof Error ? error.message : String(error)}`
|
|
@@ -1378,6 +1468,7 @@ export function iterationRoute(original, route) {
|
|
|
1378
1468
|
const stack = route.stack?.name;
|
|
1379
1469
|
const constants = new Set(route.constants);
|
|
1380
1470
|
const refusals = [];
|
|
1471
|
+
let omitted = 0;
|
|
1381
1472
|
for (let i = 0; i < m.getNumFunctions(); i++) {
|
|
1382
1473
|
const f = binaryen.getFunctionInfo(m.getFunctionByIndex(i));
|
|
1383
1474
|
if (!f.body || !callsInLoop(f.body, members))
|
|
@@ -1387,10 +1478,15 @@ export function iterationRoute(original, route) {
|
|
|
1387
1478
|
plan.route.diagnostics.unshift(...refusals);
|
|
1388
1479
|
return plan.route;
|
|
1389
1480
|
}
|
|
1390
|
-
for (const reason of plan.refusals)
|
|
1391
|
-
if (refusals.length <
|
|
1481
|
+
for (const reason of plan.refusals) {
|
|
1482
|
+
if (refusals.length < 64)
|
|
1392
1483
|
refusals.push(`refused loop in ${f.name}: ${reason}`);
|
|
1484
|
+
else
|
|
1485
|
+
omitted++;
|
|
1486
|
+
}
|
|
1393
1487
|
}
|
|
1488
|
+
if (omitted)
|
|
1489
|
+
refusals.push(`${omitted} more refused loops not listed`);
|
|
1394
1490
|
refusals.push('no loop calls into the worker set');
|
|
1395
1491
|
return emptyIteration(refusals);
|
|
1396
1492
|
}
|
|
@@ -1764,10 +1860,31 @@ function outlineOwner(m, f, context) {
|
|
|
1764
1860
|
const g = binaryen.getGlobalInfo(m.getGlobal(name));
|
|
1765
1861
|
w.addGlobal(name, g.type, false, w.copyExpression(g.init));
|
|
1766
1862
|
}
|
|
1863
|
+
for (const name of context.route.redirect ?? []) {
|
|
1864
|
+
w.addGlobal(name, binaryen.i32, true, w.i32.const(0));
|
|
1865
|
+
w.addGlobalExport(name, name);
|
|
1866
|
+
}
|
|
1767
1867
|
for (const name of context.route.functions) {
|
|
1768
1868
|
const g = binaryen.getFunctionInfo(m.getFunction(name));
|
|
1769
1869
|
w.addFunction(name, g.params, g.results, g.vars, w.copyExpression(g.body));
|
|
1770
1870
|
}
|
|
1871
|
+
// same slots as the owner's table; a function outside the set gets a stub that no member
|
|
1872
|
+
// can reach, because the route took every function of a called type into the set
|
|
1873
|
+
const shapes = context.route.tables.length
|
|
1874
|
+
? tableShapes(m).shapes
|
|
1875
|
+
: new Map();
|
|
1876
|
+
for (const name of context.route.tables) {
|
|
1877
|
+
const table = binaryen.getTableInfo(m.getTable(name));
|
|
1878
|
+
w.addTable(name, table.initial, table.max ?? 0xffffffff);
|
|
1879
|
+
for (const run of shapes.get(name).runs) {
|
|
1880
|
+
for (const entry of run.names)
|
|
1881
|
+
if (!w.getFunction(entry)) {
|
|
1882
|
+
const g = binaryen.getFunctionInfo(m.getFunction(entry));
|
|
1883
|
+
w.addFunction(entry, g.params, g.results, [], w.unreachable());
|
|
1884
|
+
}
|
|
1885
|
+
w.addActiveElementSegment(name, run.name, run.names, w.i32.const(run.offset));
|
|
1886
|
+
}
|
|
1887
|
+
}
|
|
1771
1888
|
let iterate = '__folio_iterate';
|
|
1772
1889
|
while (w.getFunction(iterate))
|
|
1773
1890
|
iterate += '_';
|
|
@@ -1780,7 +1897,18 @@ function outlineOwner(m, f, context) {
|
|
|
1780
1897
|
w.addGlobalExport(stack, stackExport);
|
|
1781
1898
|
if (!w.validate())
|
|
1782
1899
|
throw new Error('invalid iteration module');
|
|
1783
|
-
|
|
1900
|
+
// a redirect keeps names so the stack route still sets its delta globals aside
|
|
1901
|
+
const named = Boolean(context.route.redirect?.length);
|
|
1902
|
+
const before = binaryen.getDebugInfo();
|
|
1903
|
+
if (named)
|
|
1904
|
+
binaryen.setDebugInfo(true);
|
|
1905
|
+
let bytes;
|
|
1906
|
+
try {
|
|
1907
|
+
bytes = w.emitBinary();
|
|
1908
|
+
}
|
|
1909
|
+
finally {
|
|
1910
|
+
binaryen.setDebugInfo(before);
|
|
1911
|
+
}
|
|
1784
1912
|
new WebAssembly.Module(Uint8Array.from(bytes));
|
|
1785
1913
|
const trip = entry(counter);
|
|
1786
1914
|
const locals = [
|
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
|
@@ -24,10 +24,14 @@ export interface FrameIdentity {
|
|
|
24
24
|
sequence: number;
|
|
25
25
|
}
|
|
26
26
|
export declare function validateIdentity(value: FrameIdentity): void;
|
|
27
|
-
|
|
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;
|
|
28
30
|
export declare function decodeResults(bytes: Uint8Array, identity: FrameIdentity, options: {
|
|
29
31
|
maximumFrameBytes: number;
|
|
30
32
|
memoryBytes: number;
|
|
31
33
|
maxBytes: number;
|
|
32
34
|
calls: number;
|
|
35
|
+
/** The request asked for run encoding. A reply in addresses still decodes; see `runsVersion`. */
|
|
36
|
+
runs?: boolean;
|
|
33
37
|
}): TransactionResult[];
|