everystate 0.0.0-stage → 0.3.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 +149 -2
- package/bin/everystate.mjs +35 -0
- package/native/darwin-arm64/everystate +0 -0
- package/native/darwin-x64/everystate +0 -0
- package/native/linux-arm64/everystate +0 -0
- package/native/linux-x64/everystate +0 -0
- package/native/win32-x64/everystate.exe +0 -0
- package/package.json +50 -4
- package/wasm/cli.mjs +26 -0
- package/wasm/everystate.d.ts +33 -0
- package/wasm/everystate.mjs +112 -0
- package/wasm/everystate.wasm +0 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Matt Carey
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,150 @@
|
|
|
1
|
-
#
|
|
1
|
+
# everystate
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A TLA+ model checker with no Java dependency, written in Rust. It reads ordinary `.tla` specs and
|
|
4
|
+
TLC `.cfg` files, checks every reachable state, and reports results in TLC's format.
|
|
5
|
+
|
|
6
|
+
- Safety and liveness checking, fairness, symmetry, `VIEW`, `ALIAS`.
|
|
7
|
+
- TLC-compatible text output, exit codes, `-tool` framing and `-dump`.
|
|
8
|
+
- The standard and community modules are built in.
|
|
9
|
+
- One native binary, or WebAssembly for Node, Bun and workers.
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
npm install -g everystate # prebuilt binary for Linux, macOS and Windows
|
|
15
|
+
cargo install everystate # or build from source
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Example
|
|
19
|
+
|
|
20
|
+
`examples/Counter.tla`:
|
|
21
|
+
|
|
22
|
+
```tla
|
|
23
|
+
---- MODULE Counter ----
|
|
24
|
+
EXTENDS Naturals
|
|
25
|
+
VARIABLE n
|
|
26
|
+
|
|
27
|
+
Init == n = 0
|
|
28
|
+
Next == n < 5 /\ n' = n + 1
|
|
29
|
+
Spec == Init /\ [][Next]_n
|
|
30
|
+
|
|
31
|
+
TypeOK == n \in 0..5
|
|
32
|
+
NeverFour == n /= 4
|
|
33
|
+
====
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
`examples/Counter.cfg`:
|
|
37
|
+
|
|
38
|
+
```
|
|
39
|
+
SPECIFICATION Spec
|
|
40
|
+
INVARIANT TypeOK
|
|
41
|
+
INVARIANT NeverFour
|
|
42
|
+
CHECK_DEADLOCK FALSE
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
```sh
|
|
46
|
+
everystate examples/Counter.tla
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
```
|
|
50
|
+
Error: Invariant NeverFour is violated.
|
|
51
|
+
Error: The behavior up to this point is:
|
|
52
|
+
State 1: <Initial predicate>
|
|
53
|
+
n = 0
|
|
54
|
+
|
|
55
|
+
State 2: <Next line 6, col 9 to line 6, col 27 of module Counter>
|
|
56
|
+
n = 1
|
|
57
|
+
...
|
|
58
|
+
State 5: <Next line 6, col 9 to line 6, col 27 of module Counter>
|
|
59
|
+
n = 4
|
|
60
|
+
|
|
61
|
+
5 states generated, 5 distinct states found, 0 states left on queue.
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Exit status is 12, as with TLC.
|
|
65
|
+
|
|
66
|
+
### For scripts and agents
|
|
67
|
+
|
|
68
|
+
```sh
|
|
69
|
+
everystate -json examples/Counter.tla
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
```json
|
|
73
|
+
{"status":"invariant","name":"NeverFour","generated":5,"distinct":5,"queue":0,"depth":5,
|
|
74
|
+
"trace":[{"action":null,"state":{"n":"0"}},{"action":"Next","state":{"n":"1"}}, ...]}
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### From JavaScript (WebAssembly)
|
|
78
|
+
|
|
79
|
+
The npm package also exports the engine compiled to WebAssembly:
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
import { check } from 'everystate';
|
|
83
|
+
|
|
84
|
+
const { result } = check({
|
|
85
|
+
root: 'Counter',
|
|
86
|
+
modules: { Counter: tlaSource },
|
|
87
|
+
config: 'SPECIFICATION Spec\nINVARIANT NeverFour\nCHECK_DEADLOCK FALSE',
|
|
88
|
+
});
|
|
89
|
+
result.status; // 'invariant'
|
|
90
|
+
result.name; // 'NeverFour'
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
`checkAsync` runs the same check in a worker with a large stack, for deeply recursive specs.
|
|
94
|
+
The WebAssembly build is about 2x slower than native and cannot use the file-based community
|
|
95
|
+
operators.
|
|
96
|
+
|
|
97
|
+
### In place of Java TLC
|
|
98
|
+
|
|
99
|
+
`everystate java ...` (or `everystate-java ...` from npm) accepts the `java -cp tla2tools.jar tlc2.TLC ...` command line, so tools that
|
|
100
|
+
shell out to TLC can point at it instead of a JVM. The pstack `statechart` script runs unmodified
|
|
101
|
+
this way.
|
|
102
|
+
|
|
103
|
+
## Options
|
|
104
|
+
|
|
105
|
+
```
|
|
106
|
+
everystate [options] SPEC[.tla]
|
|
107
|
+
-config FILE configuration file (default: SPEC.cfg)
|
|
108
|
+
-deadlock do not check for deadlock
|
|
109
|
+
-json one JSON result object
|
|
110
|
+
-tool TLC's machine-readable message framing
|
|
111
|
+
-dump dot,actionlabels FILE write the state graph
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
Exit status: 0 success, 10 assumption, 11 deadlock, 12 safety, 13 liveness, 150 parse error,
|
|
115
|
+
151 config error.
|
|
116
|
+
|
|
117
|
+
## How it is checked
|
|
118
|
+
|
|
119
|
+
Real TLC is the reference (details in [CONFORMANCE.md](CONFORMANCE.md)):
|
|
120
|
+
|
|
121
|
+
- [tlaplus/Examples](https://github.com/tlaplus/Examples), models up to 5 s: 122 of 122 match on
|
|
122
|
+
result and exact state counts.
|
|
123
|
+
- TLC's own `test-model` suite, run side by side with `tla2tools.jar`: 384 of 414 match.
|
|
124
|
+
- pstack `statechart` corpus: 33 of 33 outputs byte-identical.
|
|
125
|
+
|
|
126
|
+
Faster than single-worker Java TLC on all 12 benchmark models (1.1x to 4.6x, thin on three of
|
|
127
|
+
them), with near-instant start-up: [BENCHMARK.md](BENCHMARK.md).
|
|
128
|
+
|
|
129
|
+
## Not supported
|
|
130
|
+
|
|
131
|
+
Simulation mode, multiple workers, disk-backed state storage (everything is in memory, so a few
|
|
132
|
+
million distinct states is the practical limit), the PlusCal translator (already-translated specs
|
|
133
|
+
work), Java module overrides, labels and subexpression selectors, and TLAPS proof checking (proofs
|
|
134
|
+
are parsed and skipped).
|
|
135
|
+
|
|
136
|
+
## Development
|
|
137
|
+
|
|
138
|
+
```sh
|
|
139
|
+
npm run build # native binary and wasm/everystate.wasm
|
|
140
|
+
npm test # WASM API, pretty-printer and community-module tests
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
Releases: bump the version in `package.json` and `Cargo.toml` and push to `main`;
|
|
144
|
+
`.github/workflows/release.yml` builds the binaries and publishes to npm (trusted publishing, no token).
|
|
145
|
+
|
|
146
|
+
An earlier pure-TypeScript engine is in the git history at commit `e8c67ce` (npm `everystate@0.1.0`).
|
|
147
|
+
|
|
148
|
+
## License
|
|
149
|
+
|
|
150
|
+
MIT
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Launcher: runs the prebuilt native binary for this platform (shipped in ../native/<platform>-<arch>/),
|
|
3
|
+
// or falls back to the WebAssembly build, which only prints a JSON result.
|
|
4
|
+
import { spawnSync } from 'node:child_process';
|
|
5
|
+
import fs from 'node:fs';
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
|
|
8
|
+
const exe = process.platform === 'win32' ? 'everystate.exe' : 'everystate';
|
|
9
|
+
|
|
10
|
+
function nativeBinary() {
|
|
11
|
+
if (process.env.EVERYSTATE_BINARY) return process.env.EVERYSTATE_BINARY;
|
|
12
|
+
const file = path.join(import.meta.dirname, '..', 'native', `${process.platform}-${process.arch}`, exe);
|
|
13
|
+
if (!fs.existsSync(file)) return null;
|
|
14
|
+
try { fs.accessSync(file, fs.constants.X_OK); } catch { try { fs.chmodSync(file, 0o755); } catch {} }
|
|
15
|
+
return file;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// `everystate-java ...` is `everystate java ...`: the stand-in for `java -cp tla2tools.jar tlc2.TLC`.
|
|
19
|
+
const asJava = path.basename(process.argv[1] ?? '').startsWith('everystate-java');
|
|
20
|
+
const args = asJava ? ['java', ...process.argv.slice(2)] : process.argv.slice(2);
|
|
21
|
+
|
|
22
|
+
const bin = nativeBinary();
|
|
23
|
+
if (bin) {
|
|
24
|
+
const r = spawnSync(bin, args, { stdio: 'inherit' });
|
|
25
|
+
if (r.error) { console.error(`everystate: cannot run ${bin}: ${r.error.message}`); process.exit(1); }
|
|
26
|
+
if (r.signal) process.kill(process.pid, r.signal);
|
|
27
|
+
process.exit(r.status ?? 1);
|
|
28
|
+
} else if (asJava) {
|
|
29
|
+
console.error(`everystate: no prebuilt binary for ${process.platform}-${process.arch}; the Java stand-in needs one (cargo install everystate).`);
|
|
30
|
+
process.exit(1);
|
|
31
|
+
} else {
|
|
32
|
+
console.error(`everystate: no prebuilt binary for ${process.platform}-${process.arch}; using the WebAssembly build (JSON output only).`);
|
|
33
|
+
process.argv = [process.argv[0], process.argv[1], ...args];
|
|
34
|
+
await import('../wasm/cli.mjs');
|
|
35
|
+
}
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,6 +1,52 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "everystate",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "A TLA+ model checker with no Java dependency (TLC-compatible). Prebuilt native binaries, with a WebAssembly build for use from JavaScript.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"main": "wasm/everystate.mjs",
|
|
8
|
+
"types": "wasm/everystate.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./wasm/everystate.d.ts",
|
|
12
|
+
"default": "./wasm/everystate.mjs"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"bin": {
|
|
16
|
+
"everystate": "bin/everystate.mjs",
|
|
17
|
+
"everystate-java": "bin/everystate.mjs"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"bin/everystate.mjs",
|
|
21
|
+
"native",
|
|
22
|
+
"wasm/everystate.mjs",
|
|
23
|
+
"wasm/everystate.d.ts",
|
|
24
|
+
"wasm/everystate.wasm",
|
|
25
|
+
"wasm/cli.mjs",
|
|
26
|
+
"README.md",
|
|
27
|
+
"LICENSE"
|
|
28
|
+
],
|
|
29
|
+
"scripts": {
|
|
30
|
+
"build": "cargo build --release && sh wasm/build.sh",
|
|
31
|
+
"test": "node --experimental-strip-types --no-warnings --test test/*.test.ts && node test/community.mjs",
|
|
32
|
+
"conformance:examples": "node scripts/conformance.mjs",
|
|
33
|
+
"conformance:tlc": "node scripts/diff-tlc.mjs",
|
|
34
|
+
"conformance:statechart": "node scripts/statechart-corpus.mjs",
|
|
35
|
+
"bench": "node scripts/bench.mjs"
|
|
36
|
+
},
|
|
37
|
+
"engines": {
|
|
38
|
+
"node": ">=20"
|
|
39
|
+
},
|
|
40
|
+
"keywords": [
|
|
41
|
+
"tla",
|
|
42
|
+
"tlaplus",
|
|
43
|
+
"tlc",
|
|
44
|
+
"model-checker",
|
|
45
|
+
"formal-verification",
|
|
46
|
+
"wasm"
|
|
47
|
+
],
|
|
48
|
+
"repository": {
|
|
49
|
+
"type": "git",
|
|
50
|
+
"url": "git+https://github.com/mattzcarey/everystate.git"
|
|
51
|
+
}
|
|
52
|
+
}
|
package/wasm/cli.mjs
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Minimal command line for the WebAssembly build (used by bench.mjs): reads the spec, the modules next
|
|
3
|
+
// to it and the config from disk, runs check() and prints one JSON line like `everystate -json`.
|
|
4
|
+
// usage: node wasm/cli.mjs [-config FILE] [-deadlock] SPEC.tla
|
|
5
|
+
import fs from 'node:fs';
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
import { check } from './everystate.mjs';
|
|
8
|
+
const argv = process.argv.slice(2);
|
|
9
|
+
let config = null, deadlock = true, spec = null;
|
|
10
|
+
for (let i = 0; i < argv.length; i++) {
|
|
11
|
+
if (argv[i] === '-config') config = argv[++i];
|
|
12
|
+
else if (argv[i] === '-deadlock') deadlock = false;
|
|
13
|
+
else if (!argv[i].startsWith('-')) spec = argv[i];
|
|
14
|
+
}
|
|
15
|
+
if (!spec) { console.error('usage: node wasm/cli.mjs [-config FILE] [-deadlock] SPEC.tla'); process.exit(1); }
|
|
16
|
+
const dir = path.dirname(path.resolve(spec)), root = path.basename(spec, '.tla');
|
|
17
|
+
const modules = {};
|
|
18
|
+
for (const f of fs.readdirSync(dir)) if (f.endsWith('.tla')) modules[f.slice(0, -4)] = fs.readFileSync(path.join(dir, f), 'utf8');
|
|
19
|
+
const cfg = fs.readFileSync(config ?? path.join(dir, root + '.cfg'), 'utf8');
|
|
20
|
+
try {
|
|
21
|
+
const { result } = check({ root, modules, config: cfg, deadlock, print: l => console.log(l) });
|
|
22
|
+
console.log(JSON.stringify({ status: result.status, name: result.name, message: result.message, generated: result.generated, distinct: result.distinct, queue: result.queue, depth: result.depth }));
|
|
23
|
+
} catch (e) {
|
|
24
|
+
console.log(JSON.stringify({ status: 'setup-error', message: e.message }));
|
|
25
|
+
process.exit(150);
|
|
26
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export interface CheckInput {
|
|
2
|
+
/** Name of the root module. */
|
|
3
|
+
root: string;
|
|
4
|
+
/** Module sources by module name (the root and any modules it EXTENDS/INSTANCEs that are not bundled). */
|
|
5
|
+
modules: Record<string, string>;
|
|
6
|
+
/** Contents of the TLC configuration (.cfg) file. */
|
|
7
|
+
config: string;
|
|
8
|
+
/** Check for deadlock (default: true unless the config says CHECK_DEADLOCK FALSE). */
|
|
9
|
+
deadlock?: boolean;
|
|
10
|
+
/** Receives output of Print/PrintT. */
|
|
11
|
+
print?: (line: string) => void;
|
|
12
|
+
}
|
|
13
|
+
export interface TraceStep { action: { name: string } | null; state: string[] }
|
|
14
|
+
export interface CheckResult {
|
|
15
|
+
status: 'ok' | 'invariant' | 'invariant-init' | 'deadlock' | 'action-property' | 'implied-init' | 'liveness' | 'error' | 'assumption' | 'incomplete' | 'init-error';
|
|
16
|
+
name?: string;
|
|
17
|
+
message?: string;
|
|
18
|
+
code?: number;
|
|
19
|
+
generated: number;
|
|
20
|
+
distinct: number;
|
|
21
|
+
queue: number;
|
|
22
|
+
depth: number;
|
|
23
|
+
/** liveness: index (0-based) of the state the lasso returns to; -1 = stuttering */
|
|
24
|
+
loopTo?: number;
|
|
25
|
+
loopAction?: { name: string } | null;
|
|
26
|
+
trace?: TraceStep[];
|
|
27
|
+
}
|
|
28
|
+
export class TlaError extends Error { kind: string }
|
|
29
|
+
export function init(source?: WebAssembly.Module | BufferSource | Response | Promise<Response>): Promise<WebAssembly.Module>;
|
|
30
|
+
export function check(input: CheckInput): { result: CheckResult; model: { vars: { name: string }[] } };
|
|
31
|
+
export function fmt(value: unknown): string;
|
|
32
|
+
/** Under Node: runs `check` in a worker thread with a `stackSizeMb` call stack (default 256). */
|
|
33
|
+
export function checkAsync(input: CheckInput, options?: { stackSizeMb?: number }): Promise<{ result: CheckResult; model: { vars: { name: string }[] } }>;
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
// everystate as WebAssembly: the same `check({ root, modules, config, deadlock?, print? })` API as the
|
|
2
|
+
// TypeScript package's index.ts, backed by the Rust engine. Works in Node and Bun out of the box (the
|
|
3
|
+
// .wasm file next to this module is read synchronously); in a browser or worker call `init()` first
|
|
4
|
+
// with the compiled module, its bytes, or a fetch() Response.
|
|
5
|
+
//
|
|
6
|
+
// Differences from the TypeScript API: a trace state is an array of already formatted values (one
|
|
7
|
+
// string per variable, in `model.vars` order), so `fmt` is the identity on them; `model` only carries
|
|
8
|
+
// `vars`. Every call runs in a fresh WebAssembly instance (string ordering starts from scratch, as
|
|
9
|
+
// `resetInterning()` does in TypeScript) and its memory is released afterwards.
|
|
10
|
+
|
|
11
|
+
let compiled = null;
|
|
12
|
+
|
|
13
|
+
/** Optional: provide the compiled module (WebAssembly.Module, bytes, or a Response / Promise of one). */
|
|
14
|
+
export async function init(source) {
|
|
15
|
+
source = await source;
|
|
16
|
+
if (source instanceof WebAssembly.Module) compiled = source;
|
|
17
|
+
else if (typeof Response !== 'undefined' && source instanceof Response) compiled = await WebAssembly.compile(await source.arrayBuffer());
|
|
18
|
+
else if (source) compiled = await WebAssembly.compile(source);
|
|
19
|
+
else compiled = load();
|
|
20
|
+
return compiled;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function load() {
|
|
24
|
+
if (compiled) return compiled;
|
|
25
|
+
const url = new URL('./everystate.wasm', import.meta.url);
|
|
26
|
+
let bytes;
|
|
27
|
+
if (typeof process !== 'undefined' && process.getBuiltinModule) bytes = process.getBuiltinModule('node:fs').readFileSync(url);
|
|
28
|
+
else if (typeof Bun !== 'undefined' || typeof process !== 'undefined') bytes = globalThis.require?.('node:fs').readFileSync(url);
|
|
29
|
+
if (!bytes) throw new Error('everystate: call `await init(wasmModuleOrBytes)` before check() in this environment.');
|
|
30
|
+
compiled = new WebAssembly.Module(bytes);
|
|
31
|
+
return compiled;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const enc = new TextEncoder(), dec = new TextDecoder();
|
|
35
|
+
|
|
36
|
+
export class TlaError extends Error {
|
|
37
|
+
constructor(message, kind) { super(message); this.kind = kind; }
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Formats a value of a trace state (they are already strings in TLC syntax). */
|
|
41
|
+
export const fmt = v => String(v);
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Model-check a specification held in memory.
|
|
45
|
+
* @param {{root: string, modules: Record<string,string>, config: string, deadlock?: boolean, print?: (line: string) => void}} input
|
|
46
|
+
*/
|
|
47
|
+
export function check(input) {
|
|
48
|
+
const module = load();
|
|
49
|
+
let memory;
|
|
50
|
+
const print = input.print ?? (() => {});
|
|
51
|
+
const instance = new WebAssembly.Instance(module, {
|
|
52
|
+
env: {
|
|
53
|
+
es_print: (ptr, len) => print(dec.decode(new Uint8Array(memory.buffer, ptr, len))),
|
|
54
|
+
es_now: () => Date.now(),
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
const ex = instance.exports;
|
|
58
|
+
memory = ex.memory;
|
|
59
|
+
const put = s => {
|
|
60
|
+
const b = enc.encode(s);
|
|
61
|
+
const p = ex.es_alloc(b.length);
|
|
62
|
+
new Uint8Array(memory.buffer, p, b.length).set(b);
|
|
63
|
+
return [p, b.length];
|
|
64
|
+
};
|
|
65
|
+
for (const [name, src] of Object.entries(input.modules)) ex.es_add_module(...put(name), ...put(src));
|
|
66
|
+
let json;
|
|
67
|
+
try {
|
|
68
|
+
const p = ex.es_check(...put(input.root), ...put(input.config), (input.deadlock ?? true) ? 1 : 0);
|
|
69
|
+
json = JSON.parse(dec.decode(new Uint8Array(memory.buffer, p, ex.es_result_len())));
|
|
70
|
+
} catch (e) {
|
|
71
|
+
// The engine's call stack (RangeError) or the 64 MiB linear-memory stack (RuntimeError) ran out.
|
|
72
|
+
if (e instanceof RangeError || (typeof WebAssembly !== 'undefined' && e instanceof WebAssembly.RuntimeError)) {
|
|
73
|
+
return {
|
|
74
|
+
result: { status: 'error', message: 'Stack overflow while evaluating the specification (recursion too deep). Re-run with a larger stack.', generated: 0, distinct: 0, queue: 0, depth: 0 },
|
|
75
|
+
model: { vars: [] },
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
throw e;
|
|
79
|
+
}
|
|
80
|
+
if (json.status === 'setup-error') throw new TlaError(json.message, json.kind);
|
|
81
|
+
const vars = json.vars ?? [];
|
|
82
|
+
const act = name => (name === null || name === undefined ? null : { name });
|
|
83
|
+
const result = {
|
|
84
|
+
status: json.status, name: json.name, message: json.message, code: json.code,
|
|
85
|
+
generated: json.generated, distinct: json.distinct, queue: json.queue, depth: json.depth,
|
|
86
|
+
loopTo: json.loopTo, loopAction: json.status === 'liveness' ? act(json.loopAction) : undefined,
|
|
87
|
+
trace: json.trace?.map(t => ({ action: act(t.action), state: vars.map(v => t.state[v]) })),
|
|
88
|
+
};
|
|
89
|
+
return { result, model: { vars: vars.map(name => ({ name })) } };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Like `check`, but (under Node) runs in a worker thread with a large call stack, because WebAssembly
|
|
94
|
+
* recursion uses the JavaScript engine's stack: the default ~1 MB allows roughly 500-1000 nested TLA+
|
|
95
|
+
* operator applications, 256 MB allows the evaluator's own limit of 6000. Elsewhere it falls back to `check`.
|
|
96
|
+
*/
|
|
97
|
+
export async function checkAsync(input, { stackSizeMb = 256 } = {}) {
|
|
98
|
+
const isNode = typeof process !== 'undefined' && process.versions?.node && !process.versions.bun && typeof Deno === 'undefined';
|
|
99
|
+
if (!isNode) return check(input);
|
|
100
|
+
const { Worker } = await import('node:worker_threads');
|
|
101
|
+
const { print, ...plain } = input;
|
|
102
|
+
const code = `
|
|
103
|
+
import { parentPort, workerData } from 'node:worker_threads';
|
|
104
|
+
const { check } = await import(workerData.url);
|
|
105
|
+
const out = check({ ...workerData.input, print: line => parentPort.postMessage({ line }) });
|
|
106
|
+
parentPort.postMessage({ out });`;
|
|
107
|
+
return new Promise((resolve, reject) => {
|
|
108
|
+
const w = new Worker(code, { eval: true, type: 'module', workerData: { url: import.meta.url, input: plain }, resourceLimits: { stackSizeMb } });
|
|
109
|
+
w.on('message', m => { if (m.line !== undefined) print?.(m.line); else resolve(m.out); });
|
|
110
|
+
w.on('error', reject);
|
|
111
|
+
});
|
|
112
|
+
}
|
|
Binary file
|