tla-ts 0.0.7 → 0.0.8-dev.8067c48

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.
@@ -41,7 +41,7 @@ import {
41
41
  withLetInstances,
42
42
  } from "./instances";
43
43
  import { overrideDefinitions, type JavaOverride } from "./java-overrides";
44
- import { extractModule, parseModule } from "./parser";
44
+ import { extractModule } from "./parser";
45
45
  import {
46
46
  getArity,
47
47
  getParamArities,
@@ -73,7 +73,8 @@ import { resolveInstanceSubexpressions, resolveModuleSubexpressions } from "./su
73
73
  import { checkModuleLevels, createLevelAnalyzer } from "./levels";
74
74
  import { createInstanceChecker, type InstanceCheck } from "./substitution-levels";
75
75
  import { isEvalException } from "./tlc/messages";
76
- import { createTlcDefinitionOrder, getTlcDeclarationOrder } from "./tlc/order";
76
+ import { createDefinitionFacts } from "./dependencies";
77
+ import { createTlcOrder, type TlcOrder } from "./tlc/order";
77
78
  import {
78
79
  isTlcInteger,
79
80
  createModelValue,
@@ -88,6 +89,8 @@ import type { TlcVersion } from "./tlc/version";
88
89
  interface EnvironmentOptions {
89
90
  source: string;
90
91
  loadModule: ((name: string) => string | undefined) | undefined;
92
+ // The TLC orders of the same spec, when the caller already reads them.
93
+ order?: TlcOrder;
91
94
  loadClass?: (name: string) => Uint8Array | undefined;
92
95
  print: (text: string) => void;
93
96
  files: FileSystem;
@@ -177,9 +180,9 @@ const getDeclarations = (linked: LinkedModule): Declaration[] => [
177
180
 
178
181
  // Parses the module SANY reads from the file name.tla. A module SANY's parser rejects fails as it
179
182
  // does in TLC: SANY's message, then a fatal error that names the module.
180
- const parseModuleFile = (source: string, name: string) => {
183
+ const parseModuleFile = (source: string, name: string, order: TlcOrder) => {
181
184
  try {
182
- return parseModule(source);
185
+ return order.parseModule(source);
183
186
  } catch (error) {
184
187
  if (!(error instanceof TlaSyntaxError)) throw error;
185
188
  const parseError = getSanyParseError(source);
@@ -299,7 +302,8 @@ export const createEnvironment = (
299
302
  config: ModelConfig,
300
303
  options: EnvironmentOptions,
301
304
  ): Specification => {
302
- const root = parseModule(options.source);
305
+ const order = options.order ?? createTlcOrder(options.source, options.loadModule);
306
+ const root = order.parseModule(options.source);
303
307
  const definitions = new Map<string, Definition>();
304
308
  const parameterized = new Map<string, boolean>();
305
309
  // The definition each instance copy came from, and the instance parameters the copy adds.
@@ -417,7 +421,7 @@ export const createEnvironment = (
417
421
  BUNDLED_MODULES.get(name) ??
418
422
  extractModule(options.source, name);
419
423
  if (source === undefined) throw createMissingModuleError(name, importer);
420
- const module = parseModuleFile(source, name);
424
+ const module = parseModuleFile(source, name, order);
421
425
  if (module.name !== name) {
422
426
  throw createSanyError(
423
427
  [
@@ -1222,9 +1226,8 @@ export const createEnvironment = (
1222
1226
  if (unassigned.length > 0 && !options.isUnconfigured) {
1223
1227
  // TLC reports the first constant it finds unassigned in SANY's order of declarations.
1224
1228
  const name =
1225
- getTlcDeclarationOrder(options.source, options.loadModule, "constant").find((constant) =>
1226
- unassigned.includes(constant),
1227
- ) ?? unassigned[0];
1229
+ order.getDeclarationOrder("constant").find((constant) => unassigned.includes(constant)) ??
1230
+ unassigned[0];
1228
1231
  throw new TlaConfigError(
1229
1232
  `The constant parameter ${name} is not assigned a value by the configuration file.`,
1230
1233
  );
@@ -1237,6 +1240,18 @@ export const createEnvironment = (
1237
1240
  });
1238
1241
  }
1239
1242
 
1243
+ const variableIndex = getVariableIndex(
1244
+ variables,
1245
+ linkOrder.flatMap((linked) => linked.module.variables.map((variable) => variable.name)),
1246
+ );
1247
+ const facts = createDefinitionFacts({
1248
+ definitions,
1249
+ substitutions,
1250
+ constants,
1251
+ variableIndex,
1252
+ builtins,
1253
+ variables,
1254
+ });
1240
1255
  const environment: Environment = {
1241
1256
  runtime,
1242
1257
  definitions,
@@ -1245,14 +1260,13 @@ export const createEnvironment = (
1245
1260
  substitutions,
1246
1261
  instanceNames,
1247
1262
  variables,
1248
- variableIndex: getVariableIndex(
1249
- variables,
1250
- linkOrder.flatMap((linked) => linked.module.variables.map((variable) => variable.name)),
1251
- ),
1252
- definitionValues: new Map(),
1253
- constantLevels: new Map(),
1263
+ variableIndex,
1264
+ facts,
1265
+ startupValues: Array.from({ length: facts.ids.size }, (): Value | undefined => undefined),
1254
1266
  substitutedDefinitions,
1255
1267
  evaluators: new WeakMap(),
1268
+ entryEvaluators: new WeakMap(),
1269
+ definitionEvaluators: new WeakMap(),
1256
1270
  };
1257
1271
  // TLC evaluates a substitute at startup and rejects it only when that fails and the level bound it
1258
1272
  // computes is above constant. The bound counts the operators that TLC's Java code overrides with
@@ -1298,7 +1312,7 @@ export const createEnvironment = (
1298
1312
  const instantiated = new Set(
1299
1313
  linkOrder.flatMap((linked) => linked.module.instances.map((instance) => instance.module)),
1300
1314
  );
1301
- const getDefinitionOrder = createTlcDefinitionOrder(options.source, options.loadModule);
1315
+ const getDefinitionOrder = order.createDefinitionOrder();
1302
1316
  return {
1303
1317
  environment,
1304
1318
  assumptions,