tla-ts 0.0.8 → 0.0.9-dev.0dd11f695
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 +30 -0
- package/dist/compile.cjs +23 -0
- package/dist/compile.js +23 -0
- package/dist/deciders.cjs +15 -15
- package/dist/deciders.js +40 -40
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +98 -19
- package/dist/index.d.ts +98 -19
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +1 -1
- package/dist/parallel-worker.js +1 -1
- package/dist/prove-worker.cjs +1 -1
- package/dist/prove-worker.js +1 -1
- package/dist/prove.cjs +116 -36
- package/dist/prove.js +116 -36
- package/dist/run.cjs +143 -31
- package/dist/run.js +143 -31
- package/dist/tlapm.cjs +2 -2
- package/dist/tlapm.js +1 -1
- package/dist/transfer.cjs +128 -106
- package/dist/transfer.js +128 -106
- package/dist/worker.cjs +7 -7
- package/dist/worker.js +7 -7
- package/package.json +3 -1
- package/src/ast.ts +7 -21
- package/src/bags.ts +99 -99
- package/src/builtins.ts +44 -75
- package/src/bundled-modules.ts +1 -8
- package/src/check.ts +605 -311
- package/src/cli/options.ts +12 -3
- package/src/cli/run.ts +2 -2
- package/src/cli/runtime-invariants.ts +135 -0
- package/src/cli/shared.ts +20 -9
- package/src/cli/tlc-arguments.ts +646 -0
- package/src/cli/tlc-usage.ts +362 -0
- package/src/cli/tlc.ts +571 -185
- package/src/cli/typescript.ts +26 -0
- package/src/cli/xml-exporter.ts +4 -10
- package/src/codegen.ts +190 -51
- package/src/config.ts +101 -68
- package/src/coverage.ts +855 -0
- package/src/dependencies.ts +340 -24
- package/src/dfid.ts +636 -0
- package/src/environment.ts +289 -109
- package/src/errors.ts +142 -45
- package/src/evaluate.ts +734 -980
- package/src/exit-codes.ts +37 -1
- package/src/index.ts +3 -0
- package/src/instances.ts +85 -9
- package/src/java-overrides.ts +2 -12
- package/src/json-reader.ts +456 -0
- package/src/json.ts +262 -139
- package/src/levels.ts +2 -3
- package/src/lexer.ts +150 -70
- package/src/liveness-checker.ts +10 -18
- package/src/liveness.ts +122 -64
- package/src/parallel-worker.ts +14 -10
- package/src/parallel.ts +41 -17
- package/src/parser.ts +436 -90
- package/src/pcal/ast.ts +7 -28
- package/src/pcal/parse.ts +11 -39
- package/src/pcal/tokenize.ts +4 -20
- package/src/pcal/translate.ts +10 -42
- package/src/pcal/validate.ts +3 -21
- package/src/pretty-print.ts +1 -2
- package/src/prover/smt/solver/solver.ts +31 -27
- package/src/randomization.ts +84 -35
- package/src/resolve.ts +18 -60
- package/src/runtime.ts +11 -4
- package/src/sany/builtins.ts +3 -4
- package/src/sany/generator.ts +128 -176
- package/src/sany/levels.ts +6 -30
- package/src/sany/lexer.ts +7 -23
- package/src/sany/linter.ts +31 -29
- package/src/sany/modules.ts +1 -5
- package/src/sany/nodes.ts +4 -28
- package/src/sany/parse-error.ts +44 -100
- package/src/sany/report.ts +25 -40
- package/src/sany/selectors.ts +8 -22
- package/src/sany/source.ts +8 -15
- package/src/sany/xml.ts +11 -42
- package/src/search.ts +550 -150
- package/src/simulate.ts +694 -0
- package/src/spec-description.ts +167 -49
- package/src/standard-definitions.ts +2 -3
- package/src/standard-modules.ts +264 -68
- package/src/state-graph.ts +51 -18
- package/src/subexpressions.ts +31 -5
- package/src/successors.ts +269 -85
- package/src/symmetry.ts +59 -147
- package/src/tableau.ts +1 -2
- package/src/tlc/fingerprint.ts +226 -151
- package/src/tlc/jar-data.ts +113 -6
- package/src/tlc/messages.ts +122 -37
- package/src/tlc/order.ts +56 -82
- package/src/tlc/random.ts +16 -16
- package/src/tlc/replay.ts +564 -212
- package/src/tlc/summary.ts +2 -4
- package/src/tlc/tlc-operators.ts +81 -36
- package/src/tlc/tlcext-operators.ts +138 -117
- package/src/tlc/trace-spec.ts +273 -0
- package/src/tlc/value-stream.ts +143 -0
- package/src/tlc/version.ts +2 -4
- package/src/typescript/actions.ts +494 -0
- package/src/typescript/collections.ts +615 -0
- package/src/typescript/compile.ts +70 -0
- package/src/typescript/context.ts +106 -0
- package/src/typescript/emit.ts +122 -0
- package/src/typescript/expressions.ts +508 -0
- package/src/typescript/formula.ts +160 -0
- package/src/typescript/functions.ts +295 -0
- package/src/typescript/model.ts +164 -0
- package/src/typescript/names.ts +116 -0
- package/src/typescript/program.ts +51 -0
- package/src/typescript/properties.ts +148 -0
- package/src/typescript/scope.ts +117 -0
- package/src/typescript/steps.ts +175 -0
- package/src/typescript/types.ts +178 -0
- package/src/values.ts +910 -318
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
import { formatValue, type Value } from "../values";
|
|
2
|
+
|
|
3
|
+
interface TraceSpecInput {
|
|
4
|
+
module: string;
|
|
5
|
+
specName: string;
|
|
6
|
+
rawConstants: string[];
|
|
7
|
+
states: Record<string, Value>[];
|
|
8
|
+
loopStart: number | undefined;
|
|
9
|
+
modelValues: string[];
|
|
10
|
+
isDefined: (name: string) => boolean;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
interface TraceState {
|
|
14
|
+
values: [string, string][];
|
|
15
|
+
number: number;
|
|
16
|
+
isStuttering: boolean;
|
|
17
|
+
isBackToState: boolean;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const INDENT = " ";
|
|
21
|
+
const SEPARATOR = "\n----\n";
|
|
22
|
+
const CLOSING_TAG = `${"=".repeat(77)}\n`;
|
|
23
|
+
const LASSO_START = "_TTraceLassoStart";
|
|
24
|
+
const LASSO_END = "_TTraceLassoEnd";
|
|
25
|
+
|
|
26
|
+
const getJavaHash = (text: string) => {
|
|
27
|
+
const hash = text
|
|
28
|
+
.split("")
|
|
29
|
+
.reduce((current, unit) => (Math.imul(current, 31) + unit.charCodeAt(0)) | 0, 0);
|
|
30
|
+
return hash ^ (hash >>> 16);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
// TLC lists the modules and the variables in the order a Java HashSet or HashMap iterates them.
|
|
34
|
+
const getHashOrder = (names: string[]) => {
|
|
35
|
+
const unique = [...new Set(names)];
|
|
36
|
+
const capacity = 2 ** Math.max(4, Math.ceil(Math.log2(unique.length / 0.75)));
|
|
37
|
+
return unique
|
|
38
|
+
.map((name, index) => ({ name, index, bucket: getJavaHash(name) & (capacity - 1) }))
|
|
39
|
+
.sort((left, right) => left.bucket - right.bucket || left.index - right.index)
|
|
40
|
+
.map((entry) => entry.name);
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const formatJavaDate = (date: Date) => {
|
|
44
|
+
const pad = (value: number) => String(value).padStart(2, "0");
|
|
45
|
+
const getPart = (options: Intl.DateTimeFormatOptions, type: Intl.DateTimeFormatPartTypes) =>
|
|
46
|
+
new Intl.DateTimeFormat("en-US", options).formatToParts(date).find((part) => part.type === type)
|
|
47
|
+
?.value;
|
|
48
|
+
return `${getPart({ weekday: "short" }, "weekday")} ${getPart({ month: "short" }, "month")} ${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())} ${getPart({ timeZoneName: "short" }, "timeZoneName")} ${date.getFullYear()}`;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
// Java's String#split drops the empty strings at the end.
|
|
52
|
+
const splitLines = (text: string) => {
|
|
53
|
+
const lines = text.split("\n");
|
|
54
|
+
while (lines.length > 1 && lines[lines.length - 1] === "") lines.pop();
|
|
55
|
+
return lines;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const indent = (text: string, depth: number) => {
|
|
59
|
+
const prefix = INDENT.repeat(depth);
|
|
60
|
+
return prefix + splitLines(text).join(`\n${prefix}`);
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const getPrimer = (module: string, extendedModules: string[]) =>
|
|
64
|
+
`---- MODULE ${module} ----\nEXTENDS ${extendedModules.join(", ")}\n\n`;
|
|
65
|
+
|
|
66
|
+
const getConjunction = (state: TraceState, prefix?: string) =>
|
|
67
|
+
indent(
|
|
68
|
+
[
|
|
69
|
+
...(prefix === undefined ? [] : [prefix]),
|
|
70
|
+
...state.values.map(([name, value]) => `${name} = (${value})`),
|
|
71
|
+
].join("\n/\\\n"),
|
|
72
|
+
2,
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
const getInstance = (name: string, module: string, definition: string) =>
|
|
76
|
+
`${name} ==\n${INDENT}LET ${module} == INSTANCE ${module}\n${INDENT}IN ${module}!${definition}\n----\n`;
|
|
77
|
+
|
|
78
|
+
const getExpressionStub = (specName: string, variables: string[]) => {
|
|
79
|
+
const variable = variables[0];
|
|
80
|
+
const examples =
|
|
81
|
+
variable === undefined
|
|
82
|
+
? ""
|
|
83
|
+
: `\\* ,_${variable}Unchanged |-> ${variable} = ${variable}'
|
|
84
|
+
|
|
85
|
+
\\* Format the \`${variable}\` variable as Json value.
|
|
86
|
+
\\* ,_${variable}Json |->
|
|
87
|
+
\\* LET J == INSTANCE Json
|
|
88
|
+
\\* IN J!ToJson(${variable})
|
|
89
|
+
|
|
90
|
+
\\* Lastly, you may build expressions over arbitrary sets of states by
|
|
91
|
+
\\* leveraging the _TETrace operator. For example, this is how to
|
|
92
|
+
\\* count the number of times a spec variable changed up to the current
|
|
93
|
+
\\* state in the trace.
|
|
94
|
+
\\* ,_${variable}ModCount |->
|
|
95
|
+
\\* LET F[s \\in DOMAIN _TETrace] ==
|
|
96
|
+
\\* IF s = 1 THEN 0
|
|
97
|
+
\\* ELSE IF _TETrace[s].${variable} # _TETrace[s-1].${variable}
|
|
98
|
+
\\* THEN 1 + F[s-1] ELSE F[s-1]
|
|
99
|
+
\\* IN F[_TEPosition - 1]
|
|
100
|
+
`;
|
|
101
|
+
const body = `\\* To hide variables of the \`${specName}\` spec from the error trace,
|
|
102
|
+
\\* remove the variables below. The trace will be written in the order
|
|
103
|
+
\\* of the fields of this record.
|
|
104
|
+
${variables.map((name) => `${name} |-> ${name}`).join("\n,")}
|
|
105
|
+
|
|
106
|
+
\\* Put additional constant-, state-, and action-level expressions here:
|
|
107
|
+
\\* ,_stateNumber |-> _TEPosition
|
|
108
|
+
${examples}`;
|
|
109
|
+
return `expression == \n${INDENT}[\n${indent(body, 2)}\n${INDENT}]\n\n`;
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
export const writeTraceSpec = (input: TraceSpecInput) => {
|
|
113
|
+
const loopStart = input.loopStart;
|
|
114
|
+
const states = input.states.map((state, index): TraceState => ({
|
|
115
|
+
values: getHashOrder(Object.keys(state)).map((name) => [name, formatValue(state[name])]),
|
|
116
|
+
number: index + 1,
|
|
117
|
+
isStuttering: false,
|
|
118
|
+
isBackToState: false,
|
|
119
|
+
}));
|
|
120
|
+
const trace =
|
|
121
|
+
loopStart === undefined
|
|
122
|
+
? states
|
|
123
|
+
: [
|
|
124
|
+
...states,
|
|
125
|
+
loopStart === states.length - 1
|
|
126
|
+
? { ...states[loopStart], isStuttering: true }
|
|
127
|
+
: { ...states[loopStart], isBackToState: true },
|
|
128
|
+
];
|
|
129
|
+
if (trace.length <= 1) return undefined;
|
|
130
|
+
const variables = Object.keys(input.states[0]);
|
|
131
|
+
const specName = input.specName;
|
|
132
|
+
const final = trace[trace.length - 1];
|
|
133
|
+
const isLasso = final.isBackToState;
|
|
134
|
+
|
|
135
|
+
const declaredNames = new Set(input.rawConstants.map((line) => line.split(" = ")[0]));
|
|
136
|
+
const constantLines = [
|
|
137
|
+
...input.rawConstants.map((line) => indent(line, 1)),
|
|
138
|
+
...input.modelValues
|
|
139
|
+
.filter((name) => !declaredNames.has(name))
|
|
140
|
+
.map((name) => indent(`${name} = ${name}`, 1)),
|
|
141
|
+
...(isLasso ? [`${LASSO_START} = ${final.number}`, `${LASSO_END} = ${trace.length - 1}`] : []),
|
|
142
|
+
];
|
|
143
|
+
const teConstants = [
|
|
144
|
+
...input.modelValues.filter((name) => !input.isDefined(name)),
|
|
145
|
+
...(isLasso ? [LASSO_START, LASSO_END] : []),
|
|
146
|
+
];
|
|
147
|
+
const teConstantsModule = `${specName}_TEConstants`;
|
|
148
|
+
const constantModules = getHashOrder([
|
|
149
|
+
"TLC",
|
|
150
|
+
...(teConstants.length > 0 ? [teConstantsModule] : []),
|
|
151
|
+
]);
|
|
152
|
+
const extendedModules = getHashOrder([
|
|
153
|
+
"Toolbox",
|
|
154
|
+
"TLCExt",
|
|
155
|
+
"Naturals",
|
|
156
|
+
"Sequences",
|
|
157
|
+
...constantModules,
|
|
158
|
+
specName,
|
|
159
|
+
]);
|
|
160
|
+
const expressionModule = `${specName}_TEExpression`;
|
|
161
|
+
const traceModule = `${specName}_TETrace`;
|
|
162
|
+
|
|
163
|
+
const property = final.isStuttering
|
|
164
|
+
? {
|
|
165
|
+
config: "\nPROPERTY\n _prop\n",
|
|
166
|
+
definition: `\n_prop ==\n${INDENT}~<>[](\n${getConjunction(trace[trace.length - 2])}\n${INDENT})`,
|
|
167
|
+
}
|
|
168
|
+
: isLasso
|
|
169
|
+
? {
|
|
170
|
+
config: "\nPROPERTY\n _prop\n",
|
|
171
|
+
definition: `\n_prop ==\n${indent(
|
|
172
|
+
`~(([]<>(\n${getConjunction(trace[trace.length - 2])}\n))/\\([]<>(\n${getConjunction(trace[final.number - 1])}\n)))`,
|
|
173
|
+
1,
|
|
174
|
+
)}`,
|
|
175
|
+
}
|
|
176
|
+
: {
|
|
177
|
+
config: "\nINVARIANT\n _inv\n",
|
|
178
|
+
definition: `\n_inv ==\n${INDENT}~(\n${getConjunction(final, 'TLCGet("level") = Len(_TETrace)')}\n${INDENT})`,
|
|
179
|
+
};
|
|
180
|
+
const lassoStep = isLasso
|
|
181
|
+
? `${INDENT}${INDENT} \\/ /\\ i = ${LASSO_END}\n${INDENT}${INDENT} /\\ j = ${LASSO_START}\n`
|
|
182
|
+
: "";
|
|
183
|
+
const next = `_next ==
|
|
184
|
+
/\\ \\E i,j \\in DOMAIN _TETrace:
|
|
185
|
+
/\\ \\/ /\\ j = i + 1
|
|
186
|
+
/\\ i = TLCGet("level")
|
|
187
|
+
${lassoStep}${variables
|
|
188
|
+
.map(
|
|
189
|
+
(name) =>
|
|
190
|
+
` /\\ ${name} = _TETrace[i].${name}\n /\\ ${name}' = _TETrace[j].${name}\n`,
|
|
191
|
+
)
|
|
192
|
+
.join("")}
|
|
193
|
+
\\* Uncomment the ASSUME below to write the states of the error trace
|
|
194
|
+
\\* to the given file in Json format. Note that you can pass any tuple
|
|
195
|
+
\\* to \`JsonSerialize\`. For example, a sub-sequence of _TETrace.
|
|
196
|
+
\\* ASSUME
|
|
197
|
+
\\* LET J == INSTANCE Json
|
|
198
|
+
\\* IN J!JsonSerialize("${input.module}.json", _TETrace)
|
|
199
|
+
|
|
200
|
+
`;
|
|
201
|
+
const statesWithoutLasso = trace.filter((state) => !state.isBackToState && !state.isStuttering);
|
|
202
|
+
// TLC adds a view when a state appears twice before the lasso closes, so that TLCGet("level")
|
|
203
|
+
// tells the copies apart.
|
|
204
|
+
const isViewed =
|
|
205
|
+
isLasso &&
|
|
206
|
+
new Set(statesWithoutLasso.map((state) => JSON.stringify(state.values))).size <
|
|
207
|
+
statesWithoutLasso.length;
|
|
208
|
+
const view = isViewed
|
|
209
|
+
? {
|
|
210
|
+
config: "\nVIEW\n _view\n",
|
|
211
|
+
definition: `\n_view ==\n${INDENT}<<${variables.join(", ")}, IF TLCGet("level") = ${LASSO_END} + 1 THEN ${LASSO_START} ELSE TLCGet("level")>>\n`,
|
|
212
|
+
}
|
|
213
|
+
: { config: "", definition: "" };
|
|
214
|
+
const deserializedTrace = `\n${getPrimer(traceModule, getHashOrder(["IOUtils", ...constantModules, specName]))}trace == IODeserialize("${input.module}.bin", TRUE)\n\n${CLOSING_TAG}`;
|
|
215
|
+
const traceFunction = `${INDENT}<<\n${statesWithoutLasso
|
|
216
|
+
.map(
|
|
217
|
+
(state) =>
|
|
218
|
+
`${INDENT}([${state.values.map(([name, value]) => `${name} |-> ${value}`).join(",")}])`,
|
|
219
|
+
)
|
|
220
|
+
.join(",\n")}\n${INDENT}>>${SEPARATOR}\n`;
|
|
221
|
+
|
|
222
|
+
const tla = [
|
|
223
|
+
getPrimer(input.module, extendedModules),
|
|
224
|
+
`${getInstance("_expression", expressionModule, "expression")}\n`,
|
|
225
|
+
getInstance("_trace", traceModule, "trace"),
|
|
226
|
+
property.definition,
|
|
227
|
+
`${SEPARATOR}\n`,
|
|
228
|
+
"_init ==\n",
|
|
229
|
+
variables.map((name) => `${INDENT}/\\ ${name} = _TETrace[1].${name}\n`).join(""),
|
|
230
|
+
"----\n\n",
|
|
231
|
+
next,
|
|
232
|
+
view.definition,
|
|
233
|
+
CLOSING_TAG,
|
|
234
|
+
"\n",
|
|
235
|
+
` Note that you can extract this module \`${expressionModule}\`\n`,
|
|
236
|
+
" to a dedicated file to reuse `expression` (the module in the \n",
|
|
237
|
+
` dedicated \`${expressionModule}.tla\` file takes precedence \n`,
|
|
238
|
+
` over the module \`${expressionModule}\` below).`,
|
|
239
|
+
`\n\n${getPrimer(expressionModule, extendedModules)}${getExpressionStub(specName, variables)}${CLOSING_TAG}\n\n`,
|
|
240
|
+
"\n",
|
|
241
|
+
"Parsing and semantic processing can take forever if the trace below is long.\n",
|
|
242
|
+
" In this case, it is advised to uncomment the module below to deserialize the\n",
|
|
243
|
+
" trace from a generated binary file.\n",
|
|
244
|
+
`\n\\*${deserializedTrace.replaceAll("\n", "\n\\*")}\n\n`,
|
|
245
|
+
getPrimer(traceModule, getHashOrder([...constantModules, specName])),
|
|
246
|
+
`trace == \n${traceFunction}\n`,
|
|
247
|
+
...(teConstants.length > 0
|
|
248
|
+
? [
|
|
249
|
+
CLOSING_TAG,
|
|
250
|
+
"\n",
|
|
251
|
+
getPrimer(teConstantsModule, [specName]),
|
|
252
|
+
`CONSTANTS ${teConstants.join(", ")}\n\n`,
|
|
253
|
+
]
|
|
254
|
+
: []),
|
|
255
|
+
CLOSING_TAG,
|
|
256
|
+
].join("");
|
|
257
|
+
const config = [
|
|
258
|
+
`\n---- CONFIG ${input.module} ----\n`,
|
|
259
|
+
constantLines.length > 0
|
|
260
|
+
? `CONSTANTS\n${constantLines.map((line) => `${line}\n`).join("")}`
|
|
261
|
+
: "",
|
|
262
|
+
property.config,
|
|
263
|
+
"\nCHECK_DEADLOCK\n \\* CHECK_DEADLOCK off because of PROPERTY or INVARIANT above.\n FALSE",
|
|
264
|
+
"\n\nINIT\n _init\n",
|
|
265
|
+
"\nNEXT\n _next\n",
|
|
266
|
+
view.config,
|
|
267
|
+
"\nCONSTANT\n _TETrace <- _trace\n",
|
|
268
|
+
"\nALIAS\n _expression\n",
|
|
269
|
+
CLOSING_TAG,
|
|
270
|
+
`\\* Generated on ${formatJavaDate(new Date())}`,
|
|
271
|
+
].join("");
|
|
272
|
+
return tla + config;
|
|
273
|
+
};
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { gzipSync } from "node:zlib";
|
|
2
|
+
import { defineStateOperator, getValue, overrideWithJava } from "../builtins";
|
|
3
|
+
import type { Builtin } from "../evaluate";
|
|
4
|
+
import { getPath, type FileSystem } from "../json";
|
|
5
|
+
import {
|
|
6
|
+
getFunctionClass,
|
|
7
|
+
getModelValues,
|
|
8
|
+
isSetValue,
|
|
9
|
+
toFiniteFunction,
|
|
10
|
+
toFiniteSet,
|
|
11
|
+
type Value,
|
|
12
|
+
} from "../values";
|
|
13
|
+
|
|
14
|
+
const TAGS = {
|
|
15
|
+
boolean: 0,
|
|
16
|
+
integer: 1,
|
|
17
|
+
string: 3,
|
|
18
|
+
record: 4,
|
|
19
|
+
set: 5,
|
|
20
|
+
tuple: 7,
|
|
21
|
+
function: 9,
|
|
22
|
+
modelValue: 21,
|
|
23
|
+
interval: 23,
|
|
24
|
+
handle: 26,
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
// TLC fingerprints the value before it writes it, which normalizes every set and function.
|
|
28
|
+
export const serializeValue = (value: Value) => {
|
|
29
|
+
const bytes: number[] = [];
|
|
30
|
+
const handles = {
|
|
31
|
+
objects: new Map<object, number>(),
|
|
32
|
+
names: new Map<string, number>(),
|
|
33
|
+
count: 0,
|
|
34
|
+
};
|
|
35
|
+
const modelValues = getModelValues();
|
|
36
|
+
const writeByte = (byte: number) => bytes.push(byte & 0xff);
|
|
37
|
+
const writeShort = (short: number) => {
|
|
38
|
+
writeByte(short >> 8);
|
|
39
|
+
writeByte(short);
|
|
40
|
+
};
|
|
41
|
+
const writeInt = (int: number) => {
|
|
42
|
+
writeShort(int >> 16);
|
|
43
|
+
writeShort(int);
|
|
44
|
+
};
|
|
45
|
+
const writeNat = (nat: number) => (nat > 0x7fff ? writeInt(-nat) : writeShort(nat));
|
|
46
|
+
// A reader skips the token and the variable location that TLC writes before the characters.
|
|
47
|
+
const writeString = (text: string) => {
|
|
48
|
+
writeByte(TAGS.string);
|
|
49
|
+
writeInt(0);
|
|
50
|
+
writeInt(-1);
|
|
51
|
+
writeInt(text.length);
|
|
52
|
+
text.split("").forEach((unit) => writeByte(unit.charCodeAt(0)));
|
|
53
|
+
};
|
|
54
|
+
const writeHandle = (index: number) => {
|
|
55
|
+
writeByte(TAGS.handle);
|
|
56
|
+
writeNat(index);
|
|
57
|
+
};
|
|
58
|
+
const write = (inner: Value): void => {
|
|
59
|
+
if (typeof inner === "boolean") {
|
|
60
|
+
writeByte(TAGS.boolean);
|
|
61
|
+
writeByte(inner ? 1 : 0);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
if (typeof inner === "number") {
|
|
65
|
+
writeByte(TAGS.integer);
|
|
66
|
+
writeInt(inner);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
if (typeof inner === "string") {
|
|
70
|
+
handles.count++;
|
|
71
|
+
writeString(inner);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
if (inner.kind === "ModelValue") {
|
|
75
|
+
writeByte(TAGS.modelValue);
|
|
76
|
+
writeShort(modelValues.indexOf(inner));
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
if (inner.kind === "Interval") {
|
|
80
|
+
writeByte(TAGS.interval);
|
|
81
|
+
writeInt(inner.low);
|
|
82
|
+
writeInt(inner.high);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
const handle = handles.objects.get(inner);
|
|
86
|
+
if (handle !== undefined) {
|
|
87
|
+
writeHandle(handle);
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
handles.objects.set(inner, handles.count++);
|
|
91
|
+
if (isSetValue(inner)) {
|
|
92
|
+
const elements = toFiniteSet(inner).elements;
|
|
93
|
+
writeByte(TAGS.set);
|
|
94
|
+
writeInt(elements.length);
|
|
95
|
+
elements.forEach(write);
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
const functionValue = toFiniteFunction(inner);
|
|
99
|
+
const functionClass = getFunctionClass(inner);
|
|
100
|
+
if (functionClass === "tuple") {
|
|
101
|
+
writeByte(TAGS.tuple);
|
|
102
|
+
writeNat(functionValue.range.length);
|
|
103
|
+
functionValue.range.forEach(write);
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
if (functionClass === "record") {
|
|
107
|
+
writeByte(TAGS.record);
|
|
108
|
+
writeInt(functionValue.domain.length);
|
|
109
|
+
functionValue.domain.forEach((name, index) => {
|
|
110
|
+
const fieldName = String(name);
|
|
111
|
+
const nameHandle = handles.names.get(fieldName);
|
|
112
|
+
if (nameHandle === undefined) {
|
|
113
|
+
handles.names.set(fieldName, handles.count++);
|
|
114
|
+
writeString(fieldName);
|
|
115
|
+
} else {
|
|
116
|
+
writeHandle(nameHandle);
|
|
117
|
+
}
|
|
118
|
+
write(functionValue.range[index]);
|
|
119
|
+
});
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
writeByte(TAGS.function);
|
|
123
|
+
writeNat(functionValue.domain.length);
|
|
124
|
+
writeByte(1);
|
|
125
|
+
functionValue.domain.forEach((key, index) => {
|
|
126
|
+
write(key);
|
|
127
|
+
write(functionValue.range[index]);
|
|
128
|
+
});
|
|
129
|
+
};
|
|
130
|
+
write(value);
|
|
131
|
+
return gzipSync(Uint8Array.from(bytes));
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
export const createTlcTraceOperators = (files: FileSystem): Builtin[] => [
|
|
135
|
+
overrideWithJava(
|
|
136
|
+
defineStateOperator("_TLCTraceSerialize", 2, (args) => {
|
|
137
|
+
const path = getPath(getValue(args, 1, "_TLCTraceSerialize"));
|
|
138
|
+
files.writeFile?.(path, serializeValue(getValue(args, 0, "_TLCTraceSerialize")));
|
|
139
|
+
return true;
|
|
140
|
+
}),
|
|
141
|
+
"public static final tlc2.value.IValue tlc2.module._TLCTrace.ioSerialize(tlc2.value.IValue,tlc2.value.impl.StringValue) throws java.io.IOException",
|
|
142
|
+
),
|
|
143
|
+
];
|
package/src/tlc/version.ts
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
import { existsSync, readFileSync } from "node:fs";
|
|
2
2
|
|
|
3
|
-
//
|
|
4
|
-
// follows for any other tla2tools.jar.
|
|
3
|
+
// "master" stands for the master builds that tla-ts follows for any other tla2tools.jar.
|
|
5
4
|
export type TlcVersion = "1.7.4" | "master";
|
|
6
5
|
|
|
7
6
|
const MANIFEST = "META-INF/MANIFEST.MF";
|
|
8
7
|
|
|
9
|
-
//
|
|
10
|
-
// node:worker_threads, so only a run with a jar imports it.
|
|
8
|
+
// fflate loads node:worker_threads, so only a run with a jar imports it.
|
|
11
9
|
export const getTlcVersion = async (jar: string | undefined): Promise<TlcVersion> => {
|
|
12
10
|
if (jar === undefined || !existsSync(jar)) return "master";
|
|
13
11
|
const fflate = await import("fflate");
|