@gmitch215/folio 0.0.0-stage → 1.0.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/LICENSE +21 -0
- package/README.md +482 -2
- package/dist/bake.d.ts +174 -0
- package/dist/bake.js +701 -0
- package/dist/browser-worker.d.ts +9 -0
- package/dist/browser-worker.js +37 -0
- package/dist/browser.d.ts +68 -0
- package/dist/browser.js +274 -0
- package/dist/compiler.d.ts +43 -0
- package/dist/compiler.js +505 -0
- package/dist/coordinator.d.ts +27 -0
- package/dist/coordinator.js +189 -0
- package/dist/extract.d.ts +71 -0
- package/dist/extract.js +1176 -0
- package/dist/frames.d.ts +19 -0
- package/dist/frames.js +111 -0
- package/dist/guards.d.ts +12 -0
- package/dist/guards.js +202 -0
- package/dist/host.d.ts +60 -0
- package/dist/host.js +374 -0
- package/dist/import-order.d.ts +2 -0
- package/dist/import-order.js +114 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +2 -0
- package/dist/managed.d.ts +86 -0
- package/dist/managed.js +398 -0
- package/dist/memory.d.ts +10 -0
- package/dist/memory.js +89 -0
- package/dist/node.d.ts +124 -0
- package/dist/node.js +831 -0
- package/dist/pages.d.ts +109 -0
- package/dist/pages.js +339 -0
- package/dist/remote.d.ts +86 -0
- package/dist/remote.js +255 -0
- package/dist/tasks.d.ts +107 -0
- package/dist/tasks.js +317 -0
- package/dist/transaction.d.ts +23 -0
- package/dist/transaction.js +93 -0
- package/dist/values.d.ts +3 -0
- package/dist/values.js +25 -0
- package/dist/worker.d.ts +1 -0
- package/dist/worker.js +86 -0
- package/package.json +126 -5
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export declare class SpeculationLimit extends Error {
|
|
2
|
+
}
|
|
3
|
+
export interface TransactionResult {
|
|
4
|
+
value?: unknown;
|
|
5
|
+
reads: number[];
|
|
6
|
+
writes: [number, number][];
|
|
7
|
+
trap?: string;
|
|
8
|
+
errorKind?: string;
|
|
9
|
+
exhausted?: boolean;
|
|
10
|
+
timing?: {
|
|
11
|
+
wallMs: number;
|
|
12
|
+
cpuMs: number;
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
export declare function transaction(memory: WebAssembly.Memory, maxBytes: number): {
|
|
16
|
+
load(address: number, offset: number, width: number, signed: number): number;
|
|
17
|
+
store(address: number, value: number, offset: number, width: number): void;
|
|
18
|
+
load64(address: number, offset: number, width: number, signed: number): bigint;
|
|
19
|
+
store64(address: number, value: bigint, offset: number, width: number): void;
|
|
20
|
+
copy(destination: number, source: number, size: number): void;
|
|
21
|
+
fill(destination: number, value: number, size: number): void;
|
|
22
|
+
result(value?: unknown, error?: unknown): TransactionResult;
|
|
23
|
+
};
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
export class SpeculationLimit extends Error {
|
|
2
|
+
}
|
|
3
|
+
export function transaction(memory, maxBytes) {
|
|
4
|
+
const bytes = new Uint8Array(memory.buffer);
|
|
5
|
+
const reads = new Set();
|
|
6
|
+
const writes = new Map();
|
|
7
|
+
function bounds(address, offset, width) {
|
|
8
|
+
const start = (address >>> 0) + (offset >>> 0);
|
|
9
|
+
if (start + width > bytes.length)
|
|
10
|
+
throw new WebAssembly.RuntimeError('memory access out of bounds');
|
|
11
|
+
return start;
|
|
12
|
+
}
|
|
13
|
+
function limit() {
|
|
14
|
+
if (reads.size + writes.size > maxBytes)
|
|
15
|
+
throw new SpeculationLimit('transaction byte budget');
|
|
16
|
+
}
|
|
17
|
+
function read(at) {
|
|
18
|
+
const own = writes.get(at);
|
|
19
|
+
if (own === undefined)
|
|
20
|
+
reads.add(at);
|
|
21
|
+
return own ?? bytes[at];
|
|
22
|
+
}
|
|
23
|
+
return {
|
|
24
|
+
load(address, offset, width, signed) {
|
|
25
|
+
const start = bounds(address, offset, width);
|
|
26
|
+
let value = 0;
|
|
27
|
+
for (let i = 0; i < width; i++) {
|
|
28
|
+
const at = start + i;
|
|
29
|
+
value |= read(at) << (i * 8);
|
|
30
|
+
}
|
|
31
|
+
limit();
|
|
32
|
+
return signed && width < 4 ? (value << (32 - width * 8)) >> (32 - width * 8) : value;
|
|
33
|
+
},
|
|
34
|
+
store(address, value, offset, width) {
|
|
35
|
+
const start = bounds(address, offset, width);
|
|
36
|
+
for (let i = 0; i < width; i++)
|
|
37
|
+
writes.set(start + i, (value >>> (i * 8)) & 255);
|
|
38
|
+
limit();
|
|
39
|
+
},
|
|
40
|
+
load64(address, offset, width, signed) {
|
|
41
|
+
const start = bounds(address, offset, width);
|
|
42
|
+
let value = 0n;
|
|
43
|
+
for (let i = 0; i < width; i++)
|
|
44
|
+
value |= BigInt(read(start + i)) << BigInt(i * 8);
|
|
45
|
+
limit();
|
|
46
|
+
return signed || width === 8 ? BigInt.asIntN(width * 8, value) : value;
|
|
47
|
+
},
|
|
48
|
+
store64(address, value, offset, width) {
|
|
49
|
+
const start = bounds(address, offset, width);
|
|
50
|
+
for (let i = 0; i < width; i++)
|
|
51
|
+
writes.set(start + i, Number((value >> BigInt(i * 8)) & 255n));
|
|
52
|
+
limit();
|
|
53
|
+
},
|
|
54
|
+
copy(destination, source, size) {
|
|
55
|
+
const width = size >>> 0;
|
|
56
|
+
const dest = bounds(destination, 0, width);
|
|
57
|
+
const src = bounds(source, 0, width);
|
|
58
|
+
if (width > maxBytes)
|
|
59
|
+
throw new SpeculationLimit('transaction byte budget');
|
|
60
|
+
const backwards = dest > src && dest < src + width;
|
|
61
|
+
for (let index = 0; index < width; index++) {
|
|
62
|
+
const i = backwards ? width - 1 - index : index;
|
|
63
|
+
writes.set(dest + i, read(src + i));
|
|
64
|
+
limit();
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
fill(destination, value, size) {
|
|
68
|
+
const width = size >>> 0;
|
|
69
|
+
const dest = bounds(destination, 0, width);
|
|
70
|
+
if (width > maxBytes)
|
|
71
|
+
throw new SpeculationLimit('transaction byte budget');
|
|
72
|
+
for (let i = 0; i < width; i++) {
|
|
73
|
+
writes.set(dest + i, value & 255);
|
|
74
|
+
limit();
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
result(value, error) {
|
|
78
|
+
return {
|
|
79
|
+
value,
|
|
80
|
+
reads: [...reads],
|
|
81
|
+
writes: [...writes],
|
|
82
|
+
...(error instanceof SpeculationLimit
|
|
83
|
+
? { exhausted: true }
|
|
84
|
+
: error
|
|
85
|
+
? {
|
|
86
|
+
trap: error instanceof Error ? error.message : String(error),
|
|
87
|
+
errorKind: error instanceof Error ? error.name : 'Error'
|
|
88
|
+
}
|
|
89
|
+
: {})
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
}
|
package/dist/values.d.ts
ADDED
package/dist/values.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// Structured clone canonicalizes NaNs. Keep the original numeric ABI bits in
|
|
2
|
+
// side metadata so a caller can reinterpret a returned Number without losing them.
|
|
3
|
+
function bits(value) {
|
|
4
|
+
if (typeof value !== 'number' || !Number.isNaN(value))
|
|
5
|
+
return;
|
|
6
|
+
const bytes = new DataView(new ArrayBuffer(8));
|
|
7
|
+
bytes.setFloat64(0, value, true);
|
|
8
|
+
return bytes.getBigUint64(0, true);
|
|
9
|
+
}
|
|
10
|
+
export function numberBits(value) {
|
|
11
|
+
if (!Array.isArray(value))
|
|
12
|
+
return bits(value);
|
|
13
|
+
const values = value.map(bits);
|
|
14
|
+
return values.some((value) => value !== undefined) ? values : undefined;
|
|
15
|
+
}
|
|
16
|
+
export function restoreNumbers(value, encoded) {
|
|
17
|
+
const number = (encoded) => {
|
|
18
|
+
const bytes = new DataView(new ArrayBuffer(8));
|
|
19
|
+
bytes.setBigUint64(0, encoded, true);
|
|
20
|
+
return bytes.getFloat64(0, true);
|
|
21
|
+
};
|
|
22
|
+
return typeof encoded === 'bigint'
|
|
23
|
+
? number(encoded)
|
|
24
|
+
: value.map((value, index) => encoded[index] === undefined ? value : number(encoded[index]));
|
|
25
|
+
}
|
package/dist/worker.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/worker.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { parentPort, threadId, workerData } from 'node:worker_threads';
|
|
2
|
+
import { SpeculationLimit, transaction } from "./transaction.js";
|
|
3
|
+
import { numberBits, restoreNumbers } from "./values.js";
|
|
4
|
+
const { module, nativeModule, memory, importModule, importName } = workerData;
|
|
5
|
+
const nativeInstance = new WebAssembly.Instance(nativeModule, {
|
|
6
|
+
[importModule]: { [importName]: memory }
|
|
7
|
+
});
|
|
8
|
+
let active;
|
|
9
|
+
const instance = new WebAssembly.Instance(module, {
|
|
10
|
+
[importModule]: { [importName]: memory },
|
|
11
|
+
folio: {
|
|
12
|
+
...(importModule === 'folio' ? { [importName]: memory } : {}),
|
|
13
|
+
load: (...args) => active.load(...args),
|
|
14
|
+
store: (...args) => active.store(...args),
|
|
15
|
+
load64: (...args) => active.load64(...args),
|
|
16
|
+
store64: (...args) => active.store64(...args),
|
|
17
|
+
copy: (...args) => active.copy(...args),
|
|
18
|
+
fill: (...args) => active.fill(...args),
|
|
19
|
+
exhausted: () => {
|
|
20
|
+
throw new SpeculationLimit('speculative instruction budget');
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
function execute(call, argumentBits, fuel, maxBytes, native, profile) {
|
|
25
|
+
if (argumentBits !== undefined)
|
|
26
|
+
call.args = restoreNumbers(call.args, argumentBits);
|
|
27
|
+
const start = profile ? performance.now() : 0;
|
|
28
|
+
const cpuStart = profile ? process.threadCpuUsage() : undefined;
|
|
29
|
+
if (!native)
|
|
30
|
+
active = transaction(memory, maxBytes);
|
|
31
|
+
let value;
|
|
32
|
+
let error;
|
|
33
|
+
try {
|
|
34
|
+
if (native) {
|
|
35
|
+
value = nativeInstance.exports[call.export](...call.args);
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
instance.exports.__folio_set_fuel(fuel);
|
|
39
|
+
value = instance.exports[call.export](...call.args);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
catch (caught) {
|
|
43
|
+
error = caught;
|
|
44
|
+
}
|
|
45
|
+
const result = native
|
|
46
|
+
? { value, reads: [], writes: [], ...(error ? { trap: String(error) } : {}) }
|
|
47
|
+
: active.result(value, error);
|
|
48
|
+
const cpu = profile ? process.threadCpuUsage(cpuStart) : undefined;
|
|
49
|
+
const timing = cpu
|
|
50
|
+
? { wallMs: performance.now() - start, cpuMs: (cpu.user + cpu.system) / 1000 }
|
|
51
|
+
: undefined;
|
|
52
|
+
const encoded = native ? undefined : numberBits(value);
|
|
53
|
+
return {
|
|
54
|
+
...result,
|
|
55
|
+
...(encoded === undefined ? {} : { numberBits: encoded }),
|
|
56
|
+
...(timing ? { timing } : {})
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
parentPort.on('message', ({ id, call, calls, argumentBits, fuel, maxBytes, native, profile }) => {
|
|
60
|
+
if (calls) {
|
|
61
|
+
let remaining = maxBytes;
|
|
62
|
+
const results = calls.map((call, index) => {
|
|
63
|
+
if (remaining <= 0)
|
|
64
|
+
return {
|
|
65
|
+
reads: [],
|
|
66
|
+
writes: [],
|
|
67
|
+
exhausted: true,
|
|
68
|
+
...(profile ? { timing: { wallMs: 0, cpuMs: 0 } } : {})
|
|
69
|
+
};
|
|
70
|
+
const result = execute(call, argumentBits?.[index], fuel, remaining, false, profile);
|
|
71
|
+
remaining -= result.reads.length + result.writes.length;
|
|
72
|
+
return result;
|
|
73
|
+
});
|
|
74
|
+
parentPort.postMessage({ id, results });
|
|
75
|
+
}
|
|
76
|
+
else
|
|
77
|
+
parentPort.postMessage({
|
|
78
|
+
id,
|
|
79
|
+
...execute(call, argumentBits, fuel, maxBytes, native, profile)
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
parentPort.postMessage({
|
|
83
|
+
ready: true,
|
|
84
|
+
threadId,
|
|
85
|
+
shared: memory.buffer instanceof SharedArrayBuffer
|
|
86
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,127 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "@gmitch215/folio",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Shared WASM memory and ordered speculative execution across isolates",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"webassembly",
|
|
7
|
+
"wasm",
|
|
8
|
+
"parallel",
|
|
9
|
+
"worker_threads",
|
|
10
|
+
"sharedarraybuffer",
|
|
11
|
+
"cloudflare-workers",
|
|
12
|
+
"workerd"
|
|
13
|
+
],
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"author": "Gregory Mitchell <me@gmitch215.xyz>",
|
|
16
|
+
"type": "module",
|
|
17
|
+
"sideEffects": false,
|
|
18
|
+
"main": "./dist/index.js",
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"types": "./dist/index.d.ts",
|
|
23
|
+
"import": "./dist/index.js"
|
|
24
|
+
},
|
|
25
|
+
"./node": {
|
|
26
|
+
"types": "./dist/node.d.ts",
|
|
27
|
+
"import": "./dist/node.js"
|
|
28
|
+
},
|
|
29
|
+
"./compiler": {
|
|
30
|
+
"types": "./dist/compiler.d.ts",
|
|
31
|
+
"import": "./dist/compiler.js"
|
|
32
|
+
},
|
|
33
|
+
"./remote": {
|
|
34
|
+
"types": "./dist/remote.d.ts",
|
|
35
|
+
"import": "./dist/remote.js"
|
|
36
|
+
},
|
|
37
|
+
"./host": {
|
|
38
|
+
"types": "./dist/host.d.ts",
|
|
39
|
+
"import": "./dist/host.js"
|
|
40
|
+
},
|
|
41
|
+
"./managed": {
|
|
42
|
+
"types": "./dist/managed.d.ts",
|
|
43
|
+
"import": "./dist/managed.js"
|
|
44
|
+
},
|
|
45
|
+
"./browser": {
|
|
46
|
+
"types": "./dist/browser.d.ts",
|
|
47
|
+
"import": "./dist/browser.js"
|
|
48
|
+
},
|
|
49
|
+
"./pages": {
|
|
50
|
+
"types": "./dist/pages.d.ts",
|
|
51
|
+
"import": "./dist/pages.js"
|
|
52
|
+
},
|
|
53
|
+
"./bake": {
|
|
54
|
+
"types": "./dist/bake.d.ts",
|
|
55
|
+
"import": "./dist/bake.js"
|
|
56
|
+
},
|
|
57
|
+
"./tasks": {
|
|
58
|
+
"types": "./dist/tasks.d.ts",
|
|
59
|
+
"import": "./dist/tasks.js"
|
|
60
|
+
},
|
|
61
|
+
"./package.json": "./package.json"
|
|
62
|
+
},
|
|
63
|
+
"files": [
|
|
64
|
+
"dist",
|
|
65
|
+
"LICENSE",
|
|
66
|
+
"README.md"
|
|
67
|
+
],
|
|
68
|
+
"engines": {
|
|
69
|
+
"node": ">=26"
|
|
70
|
+
},
|
|
71
|
+
"scripts": {
|
|
72
|
+
"build": "tsc -p tsconfig.build.json",
|
|
73
|
+
"build:c": "bun scripts/build/fixtures.ts",
|
|
74
|
+
"build:prepared": "bun scripts/build/prepare.ts",
|
|
75
|
+
"build:experiments": "node experiments/managed/prepare.ts && node experiments/browser/prepare.ts && node experiments/browser/extraction/prepare.ts && node experiments/consumer/prepare.ts kernel-control",
|
|
76
|
+
"typecheck": "bun run build:c && bun run build:experiments && tsc --noEmit -p tsconfig.json && tsc --noEmit -p experiments/cloud/tsconfig.json && tsc --noEmit -p experiments/managed/tsconfig.json && tsc --noEmit -p experiments/browser/tsconfig.json && tsc --noEmit -p experiments/consumer/tsconfig.json",
|
|
77
|
+
"typecheck:php": "node experiments/consumer/php/prepare.ts && tsc --noEmit -p experiments/consumer/php/tsconfig.json",
|
|
78
|
+
"typecheck:bootimage": "node experiments/bootimage/prepare.ts && tsc --noEmit -p experiments/bootimage/tsconfig.json",
|
|
79
|
+
"test": "vitest run --project unit",
|
|
80
|
+
"test:watch": "vitest --project unit",
|
|
81
|
+
"test:integration": "vitest run --project integration",
|
|
82
|
+
"test:coverage": "vitest run --project unit --project integration --coverage",
|
|
83
|
+
"test:report": "vitest run --project unit --project integration --coverage --reporter=default --reporter=junit --outputFile=test-report.junit.xml",
|
|
84
|
+
"test:e2e": "vitest run --project e2e",
|
|
85
|
+
"example": "node examples/basic.ts",
|
|
86
|
+
"analyze": "bun scripts/analyze/run.ts",
|
|
87
|
+
"docs:build": "typedoc",
|
|
88
|
+
"docs:serve": "typedoc --watch",
|
|
89
|
+
"prettier": "prettier --write .",
|
|
90
|
+
"prettier:check": "prettier --check .",
|
|
91
|
+
"prepare": "husky",
|
|
92
|
+
"prepublishOnly": "bun run build"
|
|
93
|
+
},
|
|
94
|
+
"dependencies": {
|
|
95
|
+
"binaryen": "132.0.0"
|
|
96
|
+
},
|
|
97
|
+
"devDependencies": {
|
|
98
|
+
"@cloudflare/playwright": "1.3.6",
|
|
99
|
+
"@cloudflare/workers-types": "^5.20260925.1",
|
|
100
|
+
"@types/node": "^26.0.0",
|
|
101
|
+
"@vitest/coverage-v8": "^5.0.1",
|
|
102
|
+
"esbuild": "0.28.2",
|
|
103
|
+
"husky": "^9.1.7",
|
|
104
|
+
"lint-staged": "^17.6.0",
|
|
105
|
+
"prettier": "^3.9.10",
|
|
106
|
+
"prettier-plugin-organize-imports": "^4.3.0",
|
|
107
|
+
"prettier-plugin-sh": "^0.20.2",
|
|
108
|
+
"typedoc": "^0.28.20",
|
|
109
|
+
"typescript": "^5.9.3",
|
|
110
|
+
"vitest": "^5.0.1",
|
|
111
|
+
"wrangler": "^4.139.0"
|
|
112
|
+
},
|
|
113
|
+
"publishConfig": {
|
|
114
|
+
"access": "public"
|
|
115
|
+
},
|
|
116
|
+
"repository": {
|
|
117
|
+
"type": "git",
|
|
118
|
+
"url": "git+https://github.com/gmitch215/folio.git"
|
|
119
|
+
},
|
|
120
|
+
"homepage": "https://github.com/gmitch215/folio",
|
|
121
|
+
"bugs": {
|
|
122
|
+
"url": "https://github.com/gmitch215/folio/issues"
|
|
123
|
+
},
|
|
124
|
+
"lint-staged": {
|
|
125
|
+
"*.{ts,js,mjs,json,jsonc,md,yml,sh}": "prettier --write"
|
|
126
|
+
}
|
|
127
|
+
}
|