diff --git a/packages/core/src/runtime/actor.ts b/packages/core/src/runtime/actor.ts index 0ea54c2..62e92a9 100644 --- a/packages/core/src/runtime/actor.ts +++ b/packages/core/src/runtime/actor.ts @@ -1,7 +1,7 @@ /// SPDX-License-Identifier: GPL-3.0-or-later /// SPDX-FileCopyrightText: Copyright © 2016-2024 Tony Garnock-Jones -import { IdentitySet, Value, embeddedId, is, fromJS, stringify, Dictionary, KeyedSet, Tuple, Embeddable } from '@preserves/core'; +import { IdentitySet, Value, embeddedId, is, fromJS, stringify, KeyedSet, Tuple, Embeddable, IsEmbedded } from '@preserves/core'; import { Cell, Field, Graph } from './dataflow.js'; import { Caveat, runRewrites } from './rewrite.js'; import { ActorSpace } from './space.js'; @@ -40,6 +40,8 @@ export interface Entity { export type Cap = Ref; export class Ref { + get [IsEmbedded](): true { return true; } + readonly relay: Facet; readonly target: Partial; readonly attenuation?: Caveat[]; @@ -64,7 +66,7 @@ export class Ref { } static __from_preserve__(v: AnyValue): undefined | Ref { - return typeof v === 'object' && 'relay' in v ? v : void 0; + return typeof v === 'object' && IsEmbedded in v && 'relay' in v ? v : void 0; } } @@ -401,7 +403,7 @@ export class Turn { }); }, () => { - const a = new KeyedSet(); + const a = new KeyedSet(); initialAssertions.forEach(h => a.add(h)); return Q.ActionDescription.spawnActor({ detail, initialAssertions: a }); }); @@ -430,10 +432,10 @@ export class Turn { crash(err: Error): void { this.enqueue(this.activeFacet.actor.root, () => this.activeFacet.actor._terminateWith({ ok: false, err }), - () => Q.ActionDescription.stopActor(Q.OptionalAny.some(Dictionary.fromJS({ + () => Q.ActionDescription.stopActor(Q.OptionalAny.some({ message: err.message, stack: err.stack ? err.stack : false, - })))); + }))); } field(initial: V, name?: string): Field { diff --git a/packages/core/src/runtime/bag.ts b/packages/core/src/runtime/bag.ts index 9611c11..b3e016c 100644 --- a/packages/core/src/runtime/bag.ts +++ b/packages/core/src/runtime/bag.ts @@ -13,9 +13,9 @@ export enum ChangeDescription { } export class Bag = Value> { - _items: KeyedDictionary; + _items: KeyedDictionary; - constructor(s?: KeyedSet) { + constructor(s?: KeyedSet) { this._items = new KeyedDictionary(); if (s) s.forEach((v) => this._items.set(v, 1)); } @@ -68,7 +68,7 @@ export class Bag = Value> { this._items.forEach((c, v) => f(c, v)); } - snapshot(): KeyedDictionary { + snapshot(): KeyedDictionary { return this._items.clone(); } diff --git a/packages/core/src/runtime/dataspace.ts b/packages/core/src/runtime/dataspace.ts index c4ada0d..1d32b0b 100644 --- a/packages/core/src/runtime/dataspace.ts +++ b/packages/core/src/runtime/dataspace.ts @@ -16,7 +16,7 @@ export type DataspaceOptions = { }; export class DataspaceObserver implements IndexObserver { - readonly captureMap = new KeyedDictionary, Handle, Ref>(); + readonly captureMap = new KeyedDictionary, Handle>(); constructor( public readonly target: Ref, diff --git a/packages/core/src/runtime/pattern.ts b/packages/core/src/runtime/pattern.ts index 14ea6c1..0178509 100644 --- a/packages/core/src/runtime/pattern.ts +++ b/packages/core/src/runtime/pattern.ts @@ -1,7 +1,7 @@ /// SPDX-License-Identifier: GPL-3.0-or-later /// SPDX-FileCopyrightText: Copyright © 2016-2024 Tony Garnock-Jones -import { canonicalString, KeyedDictionary, Dictionary, is, Record, RecordConstructorInfo, Value, CompoundKey, _iterMap } from '@preserves/core'; +import { canonicalString, KeyedDictionary, is, Record, RecordConstructorInfo, Value, _iterMap, DictionaryMap, Dictionary } from '@preserves/core'; import { AnyValue, Ref } from './actor.js'; import * as P from '../gen/dataspacePatterns.js'; @@ -14,7 +14,7 @@ export function classOfValue(v: any): Shape | null { return constructorInfoSignature(Record.constructorInfo(v)); } else if (Array.isArray(v)) { return '' + v.length; - } else if (Map.isMap(v)) { + } else if (Dictionary.isDictionary(v)) { return '{}'; } else { return null; @@ -38,8 +38,9 @@ export function constructorInfoSignature(ci: RecordConstructorInfo): stri } export function step(v: AnyValue, index: AnyValue): AnyValue | undefined { - if (Map.isMap(v)) { - return v.get(index); + const vMap = Dictionary.asMap(v); + if (vMap) { + return vMap.get(index); } else { return (v as Array /* includes Record! */)[index as number]; } @@ -169,7 +170,7 @@ export function withoutCaptures(p: P.Pattern): P.Pattern { case 'arr': return P.Pattern.DCompound(P.DCompound.arr(p.value.items.map(walk))); case 'dict': { - const newDict = new KeyedDictionary, P.Pattern, Ref>(); + const newDict = new KeyedDictionary, P.Pattern>(); for (const [k, v] of p.value.entries) { newDict.set(k, walk(v)); } @@ -204,14 +205,20 @@ export function lit(v: AnyValue): P.Pattern { } else { return P.Pattern.DCompound(P.DCompound.arr(v.map(lit))); } - } else if (Map.isMap(v)) { - return P.Pattern.DCompound(P.DCompound.dict(v.mapEntries( - e => [e[0], lit(e[1])]))); - } else if (Set.isSet(v)) { - throw new Error("Cannot express literal set in pattern"); - } else { - return P.Pattern.DLit(P.DLit(P.asAnyAtom(v))); } + + const vMap = Dictionary.asMap(v); + if (vMap) { + const r = new KeyedDictionary(); + vMap.forEach((val, key) => r.set(key, lit(val))); + return P.Pattern.DCompound(P.DCompound.dict(r)); + } + + if (Set.isSet(v)) { + throw new Error("Cannot express literal set in pattern"); + } + + return P.Pattern.DLit(P.DLit(P.asAnyAtom(v))); } export function drop_lit(p: P.Pattern): AnyValue | null { @@ -229,9 +236,9 @@ export function drop_lit(p: P.Pattern): AnyValue | null { case 'arr': return p.value.items.map(walk); case 'dict': { - const v = new Dictionary(); + const v = new DictionaryMap(); p.value.entries.forEach((pp, key) => v.set(key, walk(pp))); - return v; + return v.simplifiedValue(); } } case 'DLit': @@ -257,5 +264,6 @@ export function arr(... patterns: P.Pattern[]): P.Pattern { } export function dict(... entries: [AnyValue, P.Pattern][]): P.Pattern { - return P.Pattern.DCompound(P.DCompound.dict(new Dictionary(entries))); + return P.Pattern.DCompound(P.DCompound.dict( + new KeyedDictionary(entries))); } diff --git a/packages/core/src/runtime/quasivalue.ts b/packages/core/src/runtime/quasivalue.ts index 366e63d..317a9ab 100644 --- a/packages/core/src/runtime/quasivalue.ts +++ b/packages/core/src/runtime/quasivalue.ts @@ -4,7 +4,7 @@ import { AnyValue, Ref } from './actor.js'; import { Pattern, toPattern } from '../gen/dataspacePatterns.js'; import * as P from './pattern.js'; -import { RecordConstructorInfo, is, Record } from '@preserves/core'; +import { RecordConstructorInfo, is, Record, JsDictionary } from '@preserves/core'; import { Meta, Type, GenType, SchemaDefinition } from '@preserves/schema'; export type QuasiValueConstructorInfo = @@ -100,7 +100,7 @@ export function ctor(info: QuasiValueConstructorInfo, ... items: QuasiValue[]): } else if ('schema' in info) { const definfo = info.schema(); const schema = Meta.asSchema(definfo.schema); - const def = schema.definitions.get(definfo.definitionName)!; + const def = JsDictionary.get(schema.definitions, definfo.definitionName)!; const defNameStr = definfo.definitionName.description!; const ctorArgs = items.slice(); diff --git a/packages/core/src/runtime/rewrite.ts b/packages/core/src/runtime/rewrite.ts index 0150518..90f2499 100644 --- a/packages/core/src/runtime/rewrite.ts +++ b/packages/core/src/runtime/rewrite.ts @@ -2,7 +2,7 @@ /// SPDX-FileCopyrightText: Copyright © 2016-2024 Tony Garnock-Jones import { Turn, Ref } from "./actor.js"; -import { Bytes, Dictionary, DoubleFloat, IdentityMap, is, isEmbedded, Record, Tuple, stringify } from "@preserves/core"; +import { Bytes, Dictionary, DictionaryMap, DoubleFloat, IdentityMap, KeyedDictionary, is, isEmbedded, Record, Tuple, stringify } from "@preserves/core"; import type { Assertion, Handle } from "./actor.js"; import type { SturdyValue } from "../transport/sturdy.js"; @@ -89,9 +89,10 @@ export function match(p: Pattern, v: Assertion): Bindings | null { return true; } case 'dict':{ - if (!Dictionary.isDictionary(v)) return false; + const vMap = Dictionary.asMap(v); + if (!vMap) return false; for (const [key, pp] of p.value.entries.entries()) { - const vv = v.get(key); + const vv = vMap.get(key); if (vv === void 0) return false; if (!walk(pp, vv)) return false; } @@ -136,9 +137,9 @@ export function instantiate(t: Template, b: Bindings): Assertion { case 'arr': return t.value.items.map(walk); case 'dict': { - const v = new Dictionary(); + const v = new DictionaryMap(); t.value.entries.forEach((tt, key) => v.set(key, walk(tt))); - return v; + return v.simplifiedValue(); } } } @@ -243,7 +244,7 @@ export function pArr(... items: Array): Pattern { } export function pDict(... entries: [SturdyValue, Pattern][]): Pattern { - return Pattern.PCompound(PCompound.dict(new Dictionary<_embedded, Pattern>(entries))); + return Pattern.PCompound(PCompound.dict(new KeyedDictionary<_embedded, SturdyValue, Pattern>(entries))); } export function pLit(value: SturdyValue): Pattern { @@ -303,7 +304,7 @@ export function tArr(... items: Array