@mark1russell7/coeval 0.11.0 → 0.12.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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package",
3
3
  "name": "@mark1russell7/coeval",
4
- "version": "0.11.0",
4
+ "version": "0.12.0",
5
5
  "description": "coeval: the kernel of declarative languages. Documents in envelopes, a ladder that finds the evaluator, kind contracts, compiled plans, a static kit, a formula kit and a run side with capabilities.",
6
6
  "type": "module",
7
7
  "main": "./build/index.js",
package/src/deref.ts CHANGED
@@ -363,6 +363,8 @@ export function context(parts: Partial<Context> = {}): Context {
363
363
  }
364
364
 
365
365
  const MAX_COLLAPSE = 64;
366
+ /** The reason of a resolution that reaches the bound of collapses without a cycle that it found (DEREF.CHAIN-REASON). It is a plain string, thus a bundle drops it (PKG.PURE). */
367
+ const CHAIN_REASON = "The path follows more than 64 references. One resolution follows at most 64.";
366
368
  const NO_HOPS: readonly Hop[] = /* @__PURE__ */ Object.freeze([]);
367
369
 
368
370
  /** A value that can be a reference document: a record with the type `ref`. The full check comes after. */
@@ -560,7 +562,8 @@ function walkersOf(nav: Navigator, path: readonly unknown[]): KnownWalkers | und
560
562
  * - `#NAME?`: the root `names` has no such name.
561
563
  * - `#N/A`: a step reaches nothing.
562
564
  * - `#STEP?`: a step is not a key and not a step document.
563
- * - `#CYCLE!`: the references form a cycle.
565
+ * - `#CYCLE!`: the references form a cycle, or the resolution follows more than 64 references. The reason tells
566
+ * which (DEREF.CHAIN-REASON).
564
567
  *
565
568
  * @param nav - The world and the code of the evaluators.
566
569
  * @param ctx - The context.
@@ -590,15 +593,21 @@ function resolveFully(nav: Navigator, ctx: Context, path: readonly unknown[], ho
590
593
  const done = hops ?? NO_HOPS;
591
594
  const failed = (code: ResolveCode | ErrorCode, reason: string): Resolution => ({ ok: false, code, reason, hops: done });
592
595
  let collapses = 0;
596
+ // The references whose paths are in evaluation. A reference that is in this list again closes a cycle.
597
+ let open: object[] | undefined;
593
598
  const adopt = nav.adopt ?? ((value: unknown): Thing => value);
594
599
 
595
600
  const collapse = (thing: Thing): Resolution => {
596
601
  let here = thing;
597
602
  if (nav.plain === true) return { ok: true, thing: here, hops: done };
598
603
  while (isRef(here)) {
604
+ if (open?.includes(here) === true) return failed("#CYCLE!", "The references form a cycle.");
599
605
  collapses += 1;
600
- if (collapses > MAX_COLLAPSE) return failed("#CYCLE!", "The references form a cycle.");
606
+ if (collapses > MAX_COLLAPSE) return failed("#CYCLE!", CHAIN_REASON);
607
+ open ??= [];
608
+ open.push(here);
601
609
  const inner = walk(here.value, undefined);
610
+ open.pop();
602
611
  if (!inner.ok) return inner;
603
612
  here = adopt(inner.thing);
604
613
  hops?.push({ step: "collapse", thing: here, by: "collapse" });
package/src/evaluate.ts CHANGED
@@ -156,6 +156,22 @@ function granted(ctx: Context): Context {
156
156
  return out;
157
157
  }
158
158
 
159
+ /**
160
+ * This function gives the context in which the runtime resolves a path of compiled code (CAP.CAPS-GRANT). A path from
161
+ * the root `caps` reads the capabilities that both the context and its grant permit, and a context without a grant
162
+ * reads none. Thus a context that compiled code widened gives the capabilities of its document, and a context that it
163
+ * narrowed keeps its own list. Each other path, and each context whose grant is its own list, keeps the same context.
164
+ */
165
+ function readable(ctx: Context, path: readonly unknown[]): Context {
166
+ if (path[0] !== "caps") return ctx;
167
+ const grant = (ctx as Granted)[GRANT];
168
+ if (grant === ctx.caps) return ctx;
169
+ const caps = grant === undefined ? NO_CAPS : intersectCaps(ctx.caps, grant);
170
+ if (caps === ctx.caps) return ctx;
171
+ const out: Granted = { ...ctx, caps, [GRANT]: caps };
172
+ return out;
173
+ }
174
+
159
175
  /**
160
176
  * This function gives the context of a part of a function evaluator. It is the context that the evaluator gives,
161
177
  * narrowed by the context of the document (CAP.NARROW), with the grant of its capabilities.
@@ -896,15 +912,18 @@ class Session implements Runtime {
896
912
  return this.reading !== undefined;
897
913
  }
898
914
 
915
+ /** This function resolves a path. A path from the root `caps` reads the capabilities through the grant (CAP.CAPS-GRANT). */
899
916
  resolve(ctx: Context, path: readonly unknown[], options?: ResolveOptions): Resolution {
900
- return this.read(ctx, path, undefined, options);
917
+ return this.read(readable(ctx, path), path, undefined, options);
901
918
  }
902
919
 
903
920
  /**
904
921
  * This function resolves a compiled path (PLAN.PATH). Without a list of reads and without a trace, the compiled
905
- * path answers at once. Each other resolution records its read, as `resolve` does.
922
+ * path answers at once. Each other resolution records its read, as `resolve` does. A path from the root `caps` reads
923
+ * the capabilities through the grant (CAP.CAPS-GRANT).
906
924
  */
907
- resolvePath(ctx: Context, compiled: CompiledPath): Resolution {
925
+ resolvePath(given: Context, compiled: CompiledPath): Resolution {
926
+ const ctx = readable(given, compiled.path);
908
927
  if (this.reading === undefined && !this.traced && compiled.nav === this.machine) return compiled.resolve(ctx);
909
928
  return this.read(ctx, compiled.path, compiled, undefined);
910
929
  }
package/src/world.ts CHANGED
@@ -270,10 +270,28 @@ export interface World {
270
270
  readonly fallback?: Kind;
271
271
  }
272
272
 
273
- /** The result of a lookup of a type in the world. */
273
+ /**
274
+ * The result of a lookup of a type in the world. The reasons of a failure:
275
+ *
276
+ * - `unknown`: the world has no entry for a key, and no fallback kind.
277
+ * - `cycle`: the aliases form a cycle. The lookup reached a path a second time.
278
+ * - `too-long`: the lookup followed more than 32 aliases without a path a second time (DEREF.CHAIN-REASON).
279
+ * - `not-a-kind`: the path reaches an entry of the wrong sort. An example is an alias whose target is not in the world.
280
+ * - `computed`: the type is a computed type, thus it has no keys.
281
+ */
274
282
  export type Lookup =
275
283
  | { readonly ok: true; readonly kind: Kind; readonly path: readonly Key[]; readonly aliases: number }
276
- | { readonly ok: false; readonly reason: "unknown" | "cycle" | "not-a-kind" | "computed"; readonly path: readonly Key[] };
284
+ | { readonly ok: false; readonly reason: "unknown" | "cycle" | "too-long" | "not-a-kind" | "computed"; readonly path: readonly Key[] };
285
+
286
+ /** The most aliases that one lookup follows (WORLD.ALIAS-LIMIT). */
287
+ const MAX_ALIASES = 32;
288
+
289
+ /** This function tells if two paths of a lookup have the same keys. */
290
+ function samePath(a: readonly Key[], b: readonly Key[]): boolean {
291
+ if (a.length !== b.length) return false;
292
+ for (let index = 0; index < a.length; index += 1) if (a[index] !== b[index]) return false;
293
+ return true;
294
+ }
277
295
 
278
296
  /**
279
297
  * This function tells if an entry of the world is a kind.
@@ -312,7 +330,8 @@ function entryAt(entries: Readonly<Record<string, WorldEntry>>, key: Key): World
312
330
 
313
331
  /**
314
332
  * This function finds the kind that a type names. It walks the path from the root of the world, one key at a
315
- * time. An alias on the way is followed until a kind answers (collapse). A cycle of aliases stops the lookup. In a
333
+ * time. An alias on the way is followed until a kind answers (collapse). A cycle of aliases stops the lookup, and so
334
+ * does a chain of more than 32 aliases. In a
316
335
  * namespace with a fallback, a key that the namespace does not have gets the fallback kind. In an open world, a first
317
336
  * key that the world does not have gets the fallback kind of the world.
318
337
  *
@@ -325,6 +344,8 @@ export function lookupKind(world: World, type: TypeRef): Lookup {
325
344
  if (start === undefined) return { ok: false, reason: "computed", path: [] };
326
345
  let path: readonly Key[] = start;
327
346
  let aliases = 0;
347
+ // The paths before each alias. The same path again closes a cycle, because the lookup of a path is always the same.
348
+ let seen: (readonly Key[])[] | undefined;
328
349
  for (;;) {
329
350
  let entries = world.kinds;
330
351
  let fallback: Kind | undefined = world.fallback;
@@ -339,12 +360,16 @@ export function lookupKind(world: World, type: TypeRef): Lookup {
339
360
  }
340
361
  if (isAlias(entry)) {
341
362
  aliases += 1;
342
- if (aliases > 32) return { ok: false, reason: "cycle", path };
363
+ if (aliases > MAX_ALIASES) return { ok: false, reason: "too-long", path };
343
364
  const target = entry.value.slice(1);
344
365
  if (entry.value[0] !== "world" || !target.every((item) => typeof item === "string" || typeof item === "number")) {
345
366
  return { ok: false, reason: "not-a-kind", path };
346
367
  }
368
+ seen ??= [];
369
+ seen.push(path);
347
370
  path = [...(target as readonly Key[]), ...path.slice(index + 1)];
371
+ const next = path;
372
+ if (seen.some((before) => samePath(before, next))) return { ok: false, reason: "cycle", path };
348
373
  followed = true;
349
374
  break;
350
375
  }