From 1bd65cd28380ba683e83d9b30d52a88b01c32c6c Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Tue, 23 Mar 2021 12:18:57 +0100 Subject: [PATCH] Update to new schema compiler backend --- schemas/box-protocol.prs | 8 +- schemas/dataspace.prs | 2 +- schemas/protocol.prs | 3 +- schemas/sturdy.prs | 13 +- schemas/worker.prs | 2 +- src/actor.ts | 6 +- src/bag.ts | 4 +- src/box.ts | 10 +- src/client.ts | 14 +- src/dataspace.ts | 34 +- src/gen/box-protocol.ts | 86 +-- src/gen/dataspace.ts | 48 +- src/gen/protocol.ts | 445 ++++++------ src/gen/sturdy.ts | 1417 ++++++++++++++++---------------------- src/gen/worker.ts | 48 +- src/main.ts | 36 +- src/protocol.ts | 33 +- src/relay.ts | 51 +- src/rewrite.ts | 121 ++-- src/sandbox.ts | 6 +- src/server.ts | 17 +- src/sturdy-demo.ts | 30 +- src/sturdy.ts | 14 +- src/tools/attenuate.ts | 11 +- 24 files changed, 1110 insertions(+), 1349 deletions(-) diff --git a/schemas/box-protocol.prs b/schemas/box-protocol.prs index 5560c2e..74bdd7e 100644 --- a/schemas/box-protocol.prs +++ b/schemas/box-protocol.prs @@ -1,9 +1,9 @@ version 1 . pointer Actor.Ref . -BoxState = . -SetBox = . +BoxState = . +SetBox = . -; BoxCap = BoxState / . -; ClientCap = SetBox / . +; BoxCap = BoxState / . +; ClientCap = SetBox / . . diff --git a/schemas/dataspace.prs b/schemas/dataspace.prs index bd5caa7..04196e8 100644 --- a/schemas/dataspace.prs +++ b/schemas/dataspace.prs @@ -5,5 +5,5 @@ pointer Actor.Ref . Observe = . ; ;As will be implemented soon -; Observe = . +; Observe = . . diff --git a/schemas/protocol.prs b/schemas/protocol.prs index 1abd6d7..27abc63 100644 --- a/schemas/protocol.prs +++ b/schemas/protocol.prs @@ -5,7 +5,8 @@ Assertion = any . Handle = int . Event = Assert / Retract / Message / Sync . Oid = int . -Turn = [[Oid Event] ...]. +Turn = [TurnEvent ...]. +TurnEvent = [@oid Oid @event Event]. Assert = . Retract = . diff --git a/schemas/sturdy.prs b/schemas/sturdy.prs index 6e2316b..5a64456 100644 --- a/schemas/sturdy.prs +++ b/schemas/sturdy.prs @@ -3,7 +3,7 @@ pointer Actor.Ref . ; Each Attenuation is a stage. The sequence of Attenuations is run RIGHT-TO-LEFT. ; That is, the newest Attenuations are at the right. -SturdyRef = . +SturdyRef = . ; An individual Attenuation is run RIGHT-TO-LEFT. ; That is, the newest Caveats are at the right. @@ -13,7 +13,7 @@ Attenuation = [Caveat ...]. ; can add 3rd-party caveats and richer predicates later Caveat = Rewrite / Alts . Rewrite = . -Alts = . +Alts = . Resolve = . @@ -29,10 +29,13 @@ Lit = . Pattern = PDiscard / PBind / PAnd / PNot / Lit / PCompound . PDiscard = <_>. PBind = . -PAnd = . +PAnd = . PNot = . -PCompound = . +PCompound = . +PCompoundMembers = { any: Pattern ...:... }. Template = TRef / Lit / TCompound . TRef = . -TCompound = . +TCompound = . +TCompoundMembers = { any: Template ...:... }. + diff --git a/schemas/worker.prs b/schemas/worker.prs index 24bc57c..a1598f6 100644 --- a/schemas/worker.prs +++ b/schemas/worker.prs @@ -1,3 +1,3 @@ version 1 . -Instance = . +Instance = . diff --git a/src/actor.ts b/src/actor.ts index 63a9119..741cfa0 100644 --- a/src/actor.ts +++ b/src/actor.ts @@ -1,4 +1,4 @@ -import { DecodeError, IdentitySet, TypedDecoder, Value } from '@preserves/core'; +import { DecodeError, IdentitySet, Value } from '@preserves/core'; import { Attenuation, runRewrites } from './rewrite.js'; import { queueTask } from './task.js'; @@ -32,8 +32,8 @@ export function isRef(v: any): v is Ref { return 'relay' in v && v.relay instanceof Actor && 'target' in v; } -export function decodeRef(_d: TypedDecoder): Ref | undefined { - throw new DecodeError("Cannot decode Ref"); +export function toRef(_v: any): Ref | undefined { + return isRef(_v) ? _v : void 0; } let nextActorId = 0; diff --git a/src/bag.ts b/src/bag.ts index 1c660b4..d00acbe 100644 --- a/src/bag.ts +++ b/src/bag.ts @@ -10,7 +10,7 @@ export enum ChangeDescription { } export class Bag { - _items: Dictionary; + _items: Dictionary; constructor(s?: Set) { this._items = new Dictionary(); @@ -65,7 +65,7 @@ export class Bag { this._items.forEach(f); } - snapshot(): Dictionary { + snapshot(): Dictionary { return this._items.clone(); } diff --git a/src/box.ts b/src/box.ts index 42f18e2..21f6667 100644 --- a/src/box.ts +++ b/src/box.ts @@ -1,5 +1,5 @@ -import { BoxState, SetBox } from "./gen/box-protocol.js"; -import { Observe } from "./gen/dataspace.js"; +import { BoxState, $SetBox, fromBoxState } from "./gen/box-protocol.js"; +import { fromObserve, Observe } from "./gen/dataspace.js"; import { Assertion, Handle, Ref, Turn } from "./actor.js"; let startTime = Date.now(); @@ -12,10 +12,10 @@ export default function (t: Turn, arg: Assertion) { console.log('Spawning Box', LIMIT, REPORT_EVERY); let valueHandle: Handle | undefined; function setValue(t: Turn, value: number) { - valueHandle = t.replace(ds, valueHandle, BoxState(value)); + valueHandle = t.replace(ds, valueHandle, fromBoxState(BoxState(value))); } setValue(t, 0); - t.assert(ds, Observe(SetBox.constructorInfo.label, t.ref({ + t.assert(ds, fromObserve(Observe($SetBox, t.ref({ message(t: Turn, [newValue]: [number]): void { // console.log(`Box ${t.actor.id}: got ${newValue}`); if (newValue % REPORT_EVERY === 0) { @@ -29,5 +29,5 @@ export default function (t: Turn, arg: Assertion) { if (newValue === LIMIT) t.quit(); setValue(t, newValue); } - }))); + })))); } diff --git a/src/client.ts b/src/client.ts index 012f1d7..b70e8d9 100644 --- a/src/client.ts +++ b/src/client.ts @@ -1,17 +1,17 @@ -import { BoxState, SetBox } from "./gen/box-protocol.js"; -import { Observe } from "./gen/dataspace.js"; +import { $BoxState, fromSetBox, SetBox } from "./gen/box-protocol.js"; +import { fromObserve, Observe } from "./gen/dataspace.js"; import { Assertion, Ref, Turn } from "./actor.js"; export default function (t: Turn, ds: Ref) { console.log('Spawning Client'); let count = 0; - t.assert(ds, Observe(BoxState.constructorInfo.label, t.ref({ + t.assert(ds, fromObserve(Observe($BoxState, t.ref({ assert(t: Turn, [currentValue]: [number]): void { // console.log(`Client ${t.actor.id}: got ${currentValue}`); - t.message(ds, SetBox(currentValue + 1)); + t.message(ds, fromSetBox(SetBox(currentValue + 1))); } - }))); - t.assert(ds, Observe(BoxState.constructorInfo.label, t.ref({ + })))); + t.assert(ds, fromObserve(Observe($BoxState, t.ref({ assert(_t: Turn, _assertion: Assertion): void { count++; // console.log('inc to', count, _assertion); @@ -23,5 +23,5 @@ export default function (t: Turn, ds: Ref) { } // console.log('dec to', count); }, - }))); + })))); } diff --git a/src/dataspace.ts b/src/dataspace.ts index 962b43e..e141cd7 100644 --- a/src/dataspace.ts +++ b/src/dataspace.ts @@ -2,7 +2,7 @@ import { Assertion, Entity, Handle, Ref, Turn } from 'actor'; import { Dictionary, IdentityMap, is, Record, Tuple } from '@preserves/core'; import { Bag, ChangeDescription } from './bag'; -import { Observe } from './gen/dataspace'; +import { toObserve } from './gen/dataspace'; export * from './gen/dataspace'; // Q. Why keep "Observe"? Why not do the clever trick of asserting the @@ -38,22 +38,23 @@ export * from './gen/dataspace'; export class Dataspace implements Partial { readonly handleMap: IdentityMap> = new IdentityMap(); readonly assertions = new Bag(); - readonly subscriptions = new Dictionary>, Ref>(); + readonly subscriptions = new Dictionary>>(); assert(turn: Turn, rec: Assertion, handle: Handle): void { // console.log(preserves`ds ${turn.actor.id} assert ${rec} ${handle}`); if (!Record.isRecord, Ref>(rec)) return; this.handleMap.set(handle, rec); if (this.assertions.change(rec, +1) !== ChangeDescription.ABSENT_TO_PRESENT) return; - if (Observe.isClassOf(rec)) { - const label = Observe._.label(rec)!; - const observer = Observe._.observer(rec); - const seen = new Dictionary(); - if (!this.subscriptions.has(label)) this.subscriptions.set(label, new Map()); - this.subscriptions.get(label)!.set(observer, seen); - this.assertions.forEach((_count, prev) => - is((prev as Record).label, label) - && seen.set(prev, turn.assert(observer, prev))); + { + const o = toObserve(rec); + if (o !== void 0) { + const seen = new Dictionary(); + if (!this.subscriptions.has(o.label)) this.subscriptions.set(o.label, new Map()); + this.subscriptions.get(o.label)!.set(o.observer, seen); + this.assertions.forEach((_count, prev) => + is((prev as Record).label, o.label) + && seen.set(prev, turn.assert(o.observer, prev))); + } } this.subscriptions.get(rec.label)?.forEach((seen, peer) => seen.has(rec) || seen.set(rec, turn.assert(peer, rec))); @@ -69,10 +70,13 @@ export class Dataspace implements Partial { turn.retract(seen.get(rec)); seen.delete(rec); }); - if (Observe.isClassOf(rec)) { - let peerMap = this.subscriptions.get(Observe._.label(rec)!)!; - peerMap.delete(Observe._.observer(rec) as Ref); - if (peerMap.size === 0) this.subscriptions.delete(Observe._.label(rec)!); + { + const o = toObserve(rec); + if (o !== void 0) { + let peerMap = this.subscriptions.get(o.label)!; + peerMap.delete(o.observer); + if (peerMap.size === 0) this.subscriptions.delete(o.label); + } } } diff --git a/src/gen/box-protocol.ts b/src/gen/box-protocol.ts index 04302f3..25eba68 100644 --- a/src/gen/box-protocol.ts +++ b/src/gen/box-protocol.ts @@ -4,72 +4,62 @@ import * as _i_Actor from "../actor"; export const $BoxState = Symbol.for("BoxState"); export const $SetBox = Symbol.for("SetBox"); -export const BoxState = _.Record.makeConstructor<{"_field0": number}, _ptr>()($BoxState, ["_field0"]); - -export type BoxState = _.Record<(typeof $BoxState), [number], _ptr>; - -export const SetBox = _.Record.makeConstructor<{"_field0": number}, _ptr>()($SetBox, ["_field0"]); - -export type SetBox = _.Record<(typeof $SetBox), [number], _ptr>; - export type _ptr = _i_Actor.Ref; export type _val = _.Value<_ptr>; +export type BoxState = {"value": number}; -export function isBoxState(v: any): v is BoxState { - return ( - _.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) && - _.is(v.label, $BoxState) && - ((v.length === 1) && typeof v[0] === 'number') - ); +export type SetBox = {"value": number}; + + +export const _toPtr = (v: _val) => {let result: undefined | _ptr; result = _i_Actor.toRef(v); return result;}; + +export function BoxState(value: number): BoxState {return {value};} + +export function SetBox(value: number): SetBox {return {value};} + +export function asBoxState(v: _val): BoxState { + let result = toBoxState(v); + if (result === void 0) throw new TypeError(`Invalid BoxState: ${_.stringify(v)}`); + return result; } -export function asBoxState(v: any): BoxState { - if (!isBoxState(v)) {throw new TypeError(`Invalid BoxState: ${_.stringify(v)}`);} else {return v;}; -} - -export function decodeBoxState(d: _.TypedDecoder<_ptr>): BoxState | undefined { - let result; - if (d.openRecord()) { - let _tmp0: any; - _tmp0 = _.asLiteral(d.nextSymbol(), $BoxState); +export function toBoxState(v: _val): undefined | BoxState { + let result: undefined | BoxState; + if (_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v)) { + let _tmp0: (null) | undefined; + _tmp0 = _.is(v.label, $BoxState) ? null : void 0; if (_tmp0 !== void 0) { - let _tmp1, _tmp2: any; - _tmp2 = d.nextSignedInteger(); - if (_tmp2 !== void 0) {if (d.closeCompound()) _tmp1 = [_tmp2];}; - if (_tmp1 !== void 0) result = _.Record<(typeof $BoxState), [number]>(_tmp0 as any, _tmp1 as any); + let _tmp1: (number) | undefined; + _tmp1 = typeof v[0] === 'number' ? v[0] : void 0; + if (_tmp1 !== void 0) {result = {"value": _tmp1};}; }; }; return result; } -export function isSetBox(v: any): v is SetBox { - return ( - _.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) && - _.is(v.label, $SetBox) && - ((v.length === 1) && typeof v[0] === 'number') - ); +export function fromBoxState(_v: BoxState): _val {return _.Record($BoxState, [_v["value"]]);} + +export function asSetBox(v: _val): SetBox { + let result = toSetBox(v); + if (result === void 0) throw new TypeError(`Invalid SetBox: ${_.stringify(v)}`); + return result; } -export function asSetBox(v: any): SetBox { - if (!isSetBox(v)) {throw new TypeError(`Invalid SetBox: ${_.stringify(v)}`);} else {return v;}; -} - -export function decodeSetBox(d: _.TypedDecoder<_ptr>): SetBox | undefined { - let result; - if (d.openRecord()) { - let _tmp3: any; - _tmp3 = _.asLiteral(d.nextSymbol(), $SetBox); - if (_tmp3 !== void 0) { - let _tmp4, _tmp5: any; - _tmp5 = d.nextSignedInteger(); - if (_tmp5 !== void 0) {if (d.closeCompound()) _tmp4 = [_tmp5];}; - if (_tmp4 !== void 0) result = _.Record<(typeof $SetBox), [number]>(_tmp3 as any, _tmp4 as any); +export function toSetBox(v: _val): undefined | SetBox { + let result: undefined | SetBox; + if (_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v)) { + let _tmp0: (null) | undefined; + _tmp0 = _.is(v.label, $SetBox) ? null : void 0; + if (_tmp0 !== void 0) { + let _tmp1: (number) | undefined; + _tmp1 = typeof v[0] === 'number' ? v[0] : void 0; + if (_tmp1 !== void 0) {result = {"value": _tmp1};}; }; }; return result; } -export const _decodePtr = (d: _.TypedDecoder<_ptr>) => {let result; result = _i_Actor.decodeRef(d); return result;}; +export function fromSetBox(_v: SetBox): _val {return _.Record($SetBox, [_v["value"]]);} diff --git a/src/gen/dataspace.ts b/src/gen/dataspace.ts index cfd1b62..f1243c5 100644 --- a/src/gen/dataspace.ts +++ b/src/gen/dataspace.ts @@ -3,44 +3,40 @@ import * as _i_Actor from "../actor"; export const $Observe = Symbol.for("Observe"); -export const Observe = _.Record.makeConstructor<{"label": symbol, "observer": _ptr}, _ptr>()($Observe, ["label","observer"]); - -export type Observe = _.Record<(typeof $Observe), [symbol, _ptr], _ptr>; - export type _ptr = _i_Actor.Ref; export type _val = _.Value<_ptr>; +export type Observe = {"label": symbol, "observer": _ptr}; -export function isObserve(v: any): v is Observe { - return ( - _.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) && - _.is(v.label, $Observe) && - ((v.length === 2) && typeof v[0] === 'symbol' && _.isPointer(v[1])) - ); + +export const _toPtr = (v: _val) => {let result: undefined | _ptr; result = _i_Actor.toRef(v); return result;}; + +export function Observe(label: symbol, observer: _ptr): Observe {return {label, observer};} + +export function asObserve(v: _val): Observe { + let result = toObserve(v); + if (result === void 0) throw new TypeError(`Invalid Observe: ${_.stringify(v)}`); + return result; } -export function asObserve(v: any): Observe { - if (!isObserve(v)) {throw new TypeError(`Invalid Observe: ${_.stringify(v)}`);} else {return v;}; -} - -export function decodeObserve(d: _.TypedDecoder<_ptr>): Observe | undefined { - let result; - if (d.openRecord()) { - let _tmp0: any; - _tmp0 = _.asLiteral(d.nextSymbol(), $Observe); +export function toObserve(v: _val): undefined | Observe { + let result: undefined | Observe; + if (_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v)) { + let _tmp0: (null) | undefined; + _tmp0 = _.is(v.label, $Observe) ? null : void 0; if (_tmp0 !== void 0) { - let _tmp1, _tmp2, _tmp3: any; - _tmp2 = d.nextSymbol(); - if (_tmp2 !== void 0) { - _tmp3 = _decodePtr(d); - if (_tmp3 !== void 0) {if (d.closeCompound()) _tmp1 = [_tmp2, _tmp3];}; + let _tmp1: (symbol) | undefined; + _tmp1 = typeof v[0] === 'symbol' ? v[0] : void 0; + if (_tmp1 !== void 0) { + let _tmp2: (_ptr) | undefined; + _tmp2 = _toPtr(v[1]); + if (_tmp2 !== void 0) {result = {"label": _tmp1, "observer": _tmp2};}; }; - if (_tmp1 !== void 0) result = _.Record<(typeof $Observe), [symbol, _ptr]>(_tmp0 as any, _tmp1 as any); }; }; return result; } -export const _decodePtr = (d: _.TypedDecoder<_ptr>) => {let result; result = _i_Actor.decodeRef(d); return result;}; +export function fromObserve(_v: Observe): _val {return _.Record($Observe, [_v["label"], _v["observer"]]);} diff --git a/src/gen/protocol.ts b/src/gen/protocol.ts index 1689e56..32bbda5 100644 --- a/src/gen/protocol.ts +++ b/src/gen/protocol.ts @@ -6,280 +6,291 @@ export const $message = Symbol.for("message"); export const $retract = Symbol.for("retract"); export const $sync = Symbol.for("sync"); -export type Assertion = _val; - -export type Handle = number; - -export type Event = (Assert | Retract | Message | Sync); - -export type Oid = number; - -export type Turn = Array<[Oid, Event]>; - -export const Assert = _.Record.makeConstructor<{"assertion": Assertion, "handle": Handle}, _ptr>()($assert, ["assertion","handle"]); - -export type Assert = _.Record<(typeof $assert), [Assertion, Handle], _ptr>; - -export const Retract = _.Record.makeConstructor<{"handle": Handle}, _ptr>()($retract, ["handle"]); - -export type Retract = _.Record<(typeof $retract), [Handle], _ptr>; - -export const Message = _.Record.makeConstructor<{"body": Assertion}, _ptr>()($message, ["body"]); - -export type Message = _.Record<(typeof $message), [Assertion], _ptr>; - -export const Sync = _.Record.makeConstructor<{"peer": _ptr}, _ptr>()($sync, ["peer"]); - -export type Sync = _.Record<(typeof $sync), [_ptr], _ptr>; - export type _ptr = _i_Protocol.WireRef; export type _val = _.Value<_ptr>; +export type Assertion = _val; -export function isAssertion(v: any): v is Assertion {return true;} +export type Handle = number; -export function asAssertion(v: any): Assertion { - if (!isAssertion(v)) {throw new TypeError(`Invalid Assertion: ${_.stringify(v)}`);} else {return v;}; +export type Event = ( + {"_variant": "Assert", "value": Assert} | + {"_variant": "Retract", "value": Retract} | + {"_variant": "Message", "value": Message} | + {"_variant": "Sync", "value": Sync} +); + +export type Oid = number; + +export type Turn = Array; + +export type TurnEvent = {"oid": Oid, "event": Event}; + +export type Assert = {"assertion": Assertion, "handle": Handle}; + +export type Retract = {"handle": Handle}; + +export type Message = {"body": Assertion}; + +export type Sync = {"peer": _ptr}; + + +export const _toPtr = (v: _val) => { + let result: undefined | _ptr; + result = _i_Protocol.toWireRef(v); + return result; +}; + +export function Assertion(value: _val): Assertion {return value;} + +export function Handle(value: number): Handle {return value;} + +export namespace Event { + export function Assert(value: Assert): Event {return {"_variant": "Assert", value};}; + export function Retract(value: Retract): Event {return {"_variant": "Retract", value};}; + export function Message(value: Message): Event {return {"_variant": "Message", value};}; + export function Sync(value: Sync): Event {return {"_variant": "Sync", value};}; } -export function decodeAssertion(d: _.TypedDecoder<_ptr>): Assertion | undefined {let result; result = d.next(); return result;} +export function Oid(value: number): Oid {return value;} -export function isHandle(v: any): v is Handle {return typeof v === 'number';} +export function Turn(value: Array): Turn {return value;} -export function asHandle(v: any): Handle { - if (!isHandle(v)) {throw new TypeError(`Invalid Handle: ${_.stringify(v)}`);} else {return v;}; +export function TurnEvent(oid: Oid, event: Event): TurnEvent {return {oid, event};} + +export function Assert(assertion: Assertion, handle: Handle): Assert {return {assertion, handle};} + +export function Retract(handle: Handle): Retract {return {handle};} + +export function Message(body: Assertion): Message {return {body};} + +export function Sync(peer: _ptr): Sync {return {peer};} + +export function asAssertion(v: _val): Assertion { + let result = toAssertion(v); + if (result === void 0) throw new TypeError(`Invalid Assertion: ${_.stringify(v)}`); + return result; } -export function decodeHandle(d: _.TypedDecoder<_ptr>): Handle | undefined {let result; result = d.nextSignedInteger(); return result;} - -export function isEvent(v: any): v is Event { - return _.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) && ( - ( - ( - _.is(v.label, $assert) && ((v.length === 2) && isAssertion(v[0]) && isHandle(v[1])) - ) || - (_.is(v.label, $retract) && ((v.length === 1) && isHandle(v[0]))) || - (_.is(v.label, $message) && ((v.length === 1) && isAssertion(v[0]))) || - (_.is(v.label, $sync) && ((v.length === 1) && _.isPointer(v[0]))) - ) - ); +export function toAssertion(v: _val): undefined | Assertion { + let _tmp0: (_val) | undefined; + let result: undefined | Assertion; + _tmp0 = v; + if (_tmp0 !== void 0) {result = _tmp0;}; + return result; } -export function asEvent(v: any): Event { - if (!isEvent(v)) {throw new TypeError(`Invalid Event: ${_.stringify(v)}`);} else {return v;}; +export function fromAssertion(_v: Assertion): _val {return _v;} + +export function asHandle(v: _val): Handle { + let result = toHandle(v); + if (result === void 0) throw new TypeError(`Invalid Handle: ${_.stringify(v)}`); + return result; } -export function decodeEvent(d: _.TypedDecoder<_ptr>): Event | undefined { - let result; - if (d.openRecord()) { - let _tmp0, _tmp1: any; - _tmp0 = d.next(); - _tmp1 = d.mark(); - if (_.is(_tmp0, $assert)) { - let _tmp2, _tmp3, _tmp4: any; - _tmp3 = decodeAssertion(d); - if (_tmp3 !== void 0) { - _tmp4 = decodeHandle(d); - if (_tmp4 !== void 0) {if (d.closeCompound()) _tmp2 = [_tmp3, _tmp4];}; - }; - if (_tmp2 !== void 0) result = _.Record<(typeof $assert), [Assertion, Handle]>(_tmp0 as any, _tmp2 as any); - }; +export function toHandle(v: _val): undefined | Handle { + let _tmp0: (number) | undefined; + let result: undefined | Handle; + _tmp0 = typeof v === 'number' ? v : void 0; + if (_tmp0 !== void 0) {result = _tmp0;}; + return result; +} + +export function fromHandle(_v: Handle): _val {return _v;} + +export function asEvent(v: _val): Event { + let result = toEvent(v); + if (result === void 0) throw new TypeError(`Invalid Event: ${_.stringify(v)}`); + return result; +} + +export function toEvent(v: _val): undefined | Event { + let _tmp0: (Assert) | undefined; + let result: undefined | Event; + _tmp0 = toAssert(v); + if (_tmp0 !== void 0) {result = {"_variant": "Assert", "value": _tmp0};}; + if (result === void 0) { + let _tmp1: (Retract) | undefined; + _tmp1 = toRetract(v); + if (_tmp1 !== void 0) {result = {"_variant": "Retract", "value": _tmp1};}; if (result === void 0) { - d.restoreMark(_tmp1); - if (_.is(_tmp0, $retract)) { - let _tmp5, _tmp6: any; - _tmp6 = decodeHandle(d); - if (_tmp6 !== void 0) {if (d.closeCompound()) _tmp5 = [_tmp6];}; - if (_tmp5 !== void 0) result = _.Record<(typeof $retract), [Handle]>(_tmp0 as any, _tmp5 as any); - }; + let _tmp2: (Message) | undefined; + _tmp2 = toMessage(v); + if (_tmp2 !== void 0) {result = {"_variant": "Message", "value": _tmp2};}; if (result === void 0) { - d.restoreMark(_tmp1); - if (_.is(_tmp0, $message)) { - let _tmp7, _tmp8: any; - _tmp8 = decodeAssertion(d); - if (_tmp8 !== void 0) {if (d.closeCompound()) _tmp7 = [_tmp8];}; - if (_tmp7 !== void 0) result = _.Record<(typeof $message), [Assertion]>(_tmp0 as any, _tmp7 as any); - }; - if (result === void 0) { - d.restoreMark(_tmp1); - if (_.is(_tmp0, $sync)) { - let _tmp9, _tmp10: any; - _tmp10 = _decodePtr(d); - if (_tmp10 !== void 0) {if (d.closeCompound()) _tmp9 = [_tmp10];}; - if (_tmp9 !== void 0) result = _.Record<(typeof $sync), [_ptr]>(_tmp0 as any, _tmp9 as any); - }; - }; + let _tmp3: (Sync) | undefined; + _tmp3 = toSync(v); + if (_tmp3 !== void 0) {result = {"_variant": "Sync", "value": _tmp3};}; }; }; }; return result; } -export function isOid(v: any): v is Oid {return typeof v === 'number';} - -export function asOid(v: any): Oid { - if (!isOid(v)) {throw new TypeError(`Invalid Oid: ${_.stringify(v)}`);} else {return v;}; +export function fromEvent(_v: Event): _val { + switch (_v._variant) { + case "Assert": {return fromAssert(_v.value);}; + case "Retract": {return fromRetract(_v.value);}; + case "Message": {return fromMessage(_v.value);}; + case "Sync": {return fromSync(_v.value);}; + }; } -export function decodeOid(d: _.TypedDecoder<_ptr>): Oid | undefined {let result; result = d.nextSignedInteger(); return result;} - -export function isTurn(v: any): v is Turn { - return ( - _.Array.isArray(v) && - !_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) && - (v.length >= 0) && - v.every(v => ( - ( - _.Array.isArray(v) && - !_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) && - (v.length === 2) && - isOid(v[0]) && - isEvent(v[1]) - ) - )) - ); +export function asOid(v: _val): Oid { + let result = toOid(v); + if (result === void 0) throw new TypeError(`Invalid Oid: ${_.stringify(v)}`); + return result; } -export function asTurn(v: any): Turn { - if (!isTurn(v)) {throw new TypeError(`Invalid Turn: ${_.stringify(v)}`);} else {return v;}; +export function toOid(v: _val): undefined | Oid { + let _tmp0: (number) | undefined; + let result: undefined | Oid; + _tmp0 = typeof v === 'number' ? v : void 0; + if (_tmp0 !== void 0) {result = _tmp0;}; + return result; } -export function decodeTurn(d: _.TypedDecoder<_ptr>): Turn | undefined { - let result; - if (d.openSequence()) { - let _tmp11: any; - { - let vN: Array<[Oid, Event]> | undefined = []; - while (!d.closeCompound()) { - let _tmp12, _tmp13: any; - _tmp11 = void 0; - if (d.openSequence()) { - _tmp12 = decodeOid(d); - if (_tmp12 !== void 0) { - _tmp13 = decodeEvent(d); - if (_tmp13 !== void 0) {if (d.closeCompound()) _tmp11 = [_tmp12, _tmp13];}; - }; - }; - if (_tmp11 === void 0) {vN = void 0; break;}; - vN.push(_tmp11); +export function fromOid(_v: Oid): _val {return _v;} + +export function asTurn(v: _val): Turn { + let result = toTurn(v); + if (result === void 0) throw new TypeError(`Invalid Turn: ${_.stringify(v)}`); + return result; +} + +export function toTurn(v: _val): undefined | Turn { + let result: undefined | Turn; + if (_.Array.isArray(v)) { + let _tmp0: (Array) | undefined; + _tmp0 = []; + for (const _tmp1 of v) { + let _tmp2: (TurnEvent) | undefined; + _tmp2 = toTurnEvent(_tmp1); + if (_tmp2 !== void 0) {_tmp0.push(_tmp2); continue;}; + _tmp0 = void 0; + break; + }; + if (_tmp0 !== void 0) {result = _tmp0;}; + }; + return result; +} + +export function fromTurn(_v: Turn): _val {return _v.map(v => fromTurnEvent(v));} + +export function asTurnEvent(v: _val): TurnEvent { + let result = toTurnEvent(v); + if (result === void 0) throw new TypeError(`Invalid TurnEvent: ${_.stringify(v)}`); + return result; +} + +export function toTurnEvent(v: _val): undefined | TurnEvent { + let result: undefined | TurnEvent; + if (_.Array.isArray(v) && v.length === 2) { + let _tmp0: (Oid) | undefined; + _tmp0 = toOid(v[0]); + if (_tmp0 !== void 0) { + let _tmp1: (Event) | undefined; + _tmp1 = toEvent(v[1]); + if (_tmp1 !== void 0) {result = {"oid": _tmp0, "event": _tmp1};}; + }; + }; + return result; +} + +export function fromTurnEvent(_v: TurnEvent): _val {return [fromOid(_v["oid"]), fromEvent(_v["event"])];} + +export function asAssert(v: _val): Assert { + let result = toAssert(v); + if (result === void 0) throw new TypeError(`Invalid Assert: ${_.stringify(v)}`); + return result; +} + +export function toAssert(v: _val): undefined | Assert { + let result: undefined | Assert; + if (_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v)) { + let _tmp0: (null) | undefined; + _tmp0 = _.is(v.label, $assert) ? null : void 0; + if (_tmp0 !== void 0) { + let _tmp1: (Assertion) | undefined; + _tmp1 = toAssertion(v[0]); + if (_tmp1 !== void 0) { + let _tmp2: (Handle) | undefined; + _tmp2 = toHandle(v[1]); + if (_tmp2 !== void 0) {result = {"assertion": _tmp1, "handle": _tmp2};}; }; - result = vN; }; }; return result; } -export function isAssert(v: any): v is Assert { - return ( - _.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) && - _.is(v.label, $assert) && - ((v.length === 2) && isAssertion(v[0]) && isHandle(v[1])) - ); +export function fromAssert(_v: Assert): _val { + return _.Record($assert, [fromAssertion(_v["assertion"]), fromHandle(_v["handle"])]); } -export function asAssert(v: any): Assert { - if (!isAssert(v)) {throw new TypeError(`Invalid Assert: ${_.stringify(v)}`);} else {return v;}; +export function asRetract(v: _val): Retract { + let result = toRetract(v); + if (result === void 0) throw new TypeError(`Invalid Retract: ${_.stringify(v)}`); + return result; } -export function decodeAssert(d: _.TypedDecoder<_ptr>): Assert | undefined { - let result; - if (d.openRecord()) { - let _tmp14: any; - _tmp14 = _.asLiteral(d.nextSymbol(), $assert); - if (_tmp14 !== void 0) { - let _tmp15, _tmp16, _tmp17: any; - _tmp16 = decodeAssertion(d); - if (_tmp16 !== void 0) { - _tmp17 = decodeHandle(d); - if (_tmp17 !== void 0) {if (d.closeCompound()) _tmp15 = [_tmp16, _tmp17];}; - }; - if (_tmp15 !== void 0) result = _.Record<(typeof $assert), [Assertion, Handle]>(_tmp14 as any, _tmp15 as any); +export function toRetract(v: _val): undefined | Retract { + let result: undefined | Retract; + if (_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v)) { + let _tmp0: (null) | undefined; + _tmp0 = _.is(v.label, $retract) ? null : void 0; + if (_tmp0 !== void 0) { + let _tmp1: (Handle) | undefined; + _tmp1 = toHandle(v[0]); + if (_tmp1 !== void 0) {result = {"handle": _tmp1};}; }; }; return result; } -export function isRetract(v: any): v is Retract { - return ( - _.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) && - _.is(v.label, $retract) && - ((v.length === 1) && isHandle(v[0])) - ); +export function fromRetract(_v: Retract): _val {return _.Record($retract, [fromHandle(_v["handle"])]);} + +export function asMessage(v: _val): Message { + let result = toMessage(v); + if (result === void 0) throw new TypeError(`Invalid Message: ${_.stringify(v)}`); + return result; } -export function asRetract(v: any): Retract { - if (!isRetract(v)) {throw new TypeError(`Invalid Retract: ${_.stringify(v)}`);} else {return v;}; -} - -export function decodeRetract(d: _.TypedDecoder<_ptr>): Retract | undefined { - let result; - if (d.openRecord()) { - let _tmp18: any; - _tmp18 = _.asLiteral(d.nextSymbol(), $retract); - if (_tmp18 !== void 0) { - let _tmp19, _tmp20: any; - _tmp20 = decodeHandle(d); - if (_tmp20 !== void 0) {if (d.closeCompound()) _tmp19 = [_tmp20];}; - if (_tmp19 !== void 0) result = _.Record<(typeof $retract), [Handle]>(_tmp18 as any, _tmp19 as any); +export function toMessage(v: _val): undefined | Message { + let result: undefined | Message; + if (_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v)) { + let _tmp0: (null) | undefined; + _tmp0 = _.is(v.label, $message) ? null : void 0; + if (_tmp0 !== void 0) { + let _tmp1: (Assertion) | undefined; + _tmp1 = toAssertion(v[0]); + if (_tmp1 !== void 0) {result = {"body": _tmp1};}; }; }; return result; } -export function isMessage(v: any): v is Message { - return ( - _.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) && - _.is(v.label, $message) && - ((v.length === 1) && isAssertion(v[0])) - ); +export function fromMessage(_v: Message): _val {return _.Record($message, [fromAssertion(_v["body"])]);} + +export function asSync(v: _val): Sync { + let result = toSync(v); + if (result === void 0) throw new TypeError(`Invalid Sync: ${_.stringify(v)}`); + return result; } -export function asMessage(v: any): Message { - if (!isMessage(v)) {throw new TypeError(`Invalid Message: ${_.stringify(v)}`);} else {return v;}; -} - -export function decodeMessage(d: _.TypedDecoder<_ptr>): Message | undefined { - let result; - if (d.openRecord()) { - let _tmp21: any; - _tmp21 = _.asLiteral(d.nextSymbol(), $message); - if (_tmp21 !== void 0) { - let _tmp22, _tmp23: any; - _tmp23 = decodeAssertion(d); - if (_tmp23 !== void 0) {if (d.closeCompound()) _tmp22 = [_tmp23];}; - if (_tmp22 !== void 0) result = _.Record<(typeof $message), [Assertion]>(_tmp21 as any, _tmp22 as any); +export function toSync(v: _val): undefined | Sync { + let result: undefined | Sync; + if (_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v)) { + let _tmp0: (null) | undefined; + _tmp0 = _.is(v.label, $sync) ? null : void 0; + if (_tmp0 !== void 0) { + let _tmp1: (_ptr) | undefined; + _tmp1 = _toPtr(v[0]); + if (_tmp1 !== void 0) {result = {"peer": _tmp1};}; }; }; return result; } -export function isSync(v: any): v is Sync { - return ( - _.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) && - _.is(v.label, $sync) && - ((v.length === 1) && _.isPointer(v[0])) - ); -} - -export function asSync(v: any): Sync { - if (!isSync(v)) {throw new TypeError(`Invalid Sync: ${_.stringify(v)}`);} else {return v;}; -} - -export function decodeSync(d: _.TypedDecoder<_ptr>): Sync | undefined { - let result; - if (d.openRecord()) { - let _tmp24: any; - _tmp24 = _.asLiteral(d.nextSymbol(), $sync); - if (_tmp24 !== void 0) { - let _tmp25, _tmp26: any; - _tmp26 = _decodePtr(d); - if (_tmp26 !== void 0) {if (d.closeCompound()) _tmp25 = [_tmp26];}; - if (_tmp25 !== void 0) result = _.Record<(typeof $sync), [_ptr]>(_tmp24 as any, _tmp25 as any); - }; - }; - return result; -} - -export const _decodePtr = (d: _.TypedDecoder<_ptr>) => {let result; result = _i_Protocol.decodeWireRef(d); return result;}; +export function fromSync(_v: Sync): _val {return _.Record($sync, [_v["peer"]]);} diff --git a/src/gen/sturdy.ts b/src/gen/sturdy.ts index 261eadc..afe1549 100644 --- a/src/gen/sturdy.ts +++ b/src/gen/sturdy.ts @@ -1,7 +1,7 @@ import * as _ from "@preserves/core"; import * as _i_Actor from "../actor"; -export const $__ = Symbol.for("_"); +export const $_ = Symbol.for("_"); export const $and = Symbol.for("and"); export const $arr = Symbol.for("arr"); export const $bind = Symbol.for("bind"); @@ -15,630 +15,473 @@ export const $ref = Symbol.for("ref"); export const $resolve = Symbol.for("resolve"); export const $rewrite = Symbol.for("rewrite"); -export const SturdyRef = _.Record.makeConstructor<{"oid": _val, "caveatChain": Array, "sig": _.Bytes}, _ptr>()($ref, ["oid","caveatChain","sig"]); - -export type SturdyRef = _.Record<(typeof $ref), [_val, Array, _.Bytes], _ptr>; - -export type Attenuation = Array; - -export type Caveat = (Rewrite | Alts); - -export const Rewrite = _.Record.makeConstructor<{"pattern": Pattern, "template": Template}, _ptr>()($rewrite, ["pattern","template"]); - -export type Rewrite = _.Record<(typeof $rewrite), [Pattern, Template], _ptr>; - -export const Alts = _.Record.makeConstructor<{"alternatives": Array}, _ptr>()($or, ["alternatives"]); - -export type Alts = _.Record<(typeof $or), [Array], _ptr>; - -export const Resolve = _.Record.makeConstructor<{"sturdyref": SturdyRef, "observer": _ptr}, _ptr>()($resolve, ["sturdyref","observer"]); - -export type Resolve = _.Record<(typeof $resolve), [SturdyRef, _ptr], _ptr>; - -export type ConstructorSpec = (CRec | CArr | CDict); - -export const CRec = _.Record.makeConstructor<{"label": _val, "arity": number}, _ptr>()($rec, ["label","arity"]); - -export type CRec = _.Record<(typeof $rec), [_val, number], _ptr>; - -export const CArr = _.Record.makeConstructor<{"arity": number}, _ptr>()($arr, ["arity"]); - -export type CArr = _.Record<(typeof $arr), [number], _ptr>; - -export const CDict = _.Record.makeConstructor<{}, _ptr>()($dict, []); - -export type CDict = _.Record<(typeof $dict), [], _ptr>; - -export const Lit = _.Record.makeConstructor<{"value": _val}, _ptr>()($lit, ["value"]); - -export type Lit = _.Record<(typeof $lit), [_val], _ptr>; - -export type Pattern = (PDiscard | PBind | PAnd | PNot | Lit | PCompound); - -export const PDiscard = _.Record.makeConstructor<{}, _ptr>()($__, []); - -export type PDiscard = _.Record<(typeof $__), [], _ptr>; - -export const PBind = _.Record.makeConstructor<{"name": symbol, "pattern": Pattern}, _ptr>()($bind, ["name","pattern"]); - -export type PBind = _.Record<(typeof $bind), [symbol, Pattern], _ptr>; - -export const PAnd = _.Record.makeConstructor<{"patterns": Array}, _ptr>()($and, ["patterns"]); - -export type PAnd = _.Record<(typeof $and), [Array], _ptr>; - -export const PNot = _.Record.makeConstructor<{"pattern": Pattern}, _ptr>()($not, ["pattern"]); - -export type PNot = _.Record<(typeof $not), [Pattern], _ptr>; - -export const PCompound = _.Record.makeConstructor<{"ctor": ConstructorSpec, "members": _.KeyedDictionary<_val, Pattern, _ptr>}, _ptr>()($compound, ["ctor","members"]); - -export type PCompound = _.Record< - (typeof $compound), - [ConstructorSpec, _.KeyedDictionary<_val, Pattern, _ptr>], - _ptr ->; - -export type Template = (TRef | Lit | TCompound); - -export const TRef = _.Record.makeConstructor<{"name": symbol}, _ptr>()($ref, ["name"]); - -export type TRef = _.Record<(typeof $ref), [symbol], _ptr>; - -export const TCompound = _.Record.makeConstructor<{"ctor": ConstructorSpec, "members": _.KeyedDictionary<_val, Template, _ptr>}, _ptr>()($compound, ["ctor","members"]); - -export type TCompound = _.Record< - (typeof $compound), - [ConstructorSpec, _.KeyedDictionary<_val, Template, _ptr>], - _ptr ->; - export type _ptr = _i_Actor.Ref; export type _val = _.Value<_ptr>; +export type SturdyRef = {"oid": _val, "caveatChain": Array, "sig": _.Bytes}; -export function isSturdyRef(v: any): v is SturdyRef { - return ( - _.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) && - _.is(v.label, $ref) && - ( - (v.length === 3) && - true && - ( - _.Array.isArray(v[1]) && - !_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v[1]) && - (v[1].length >= 0) && - v[1].every(v => (isAttenuation(v))) - ) && - _.Bytes.isBytes(v[2]) - ) - ); +export type Attenuation = Array; + +export type Caveat = ( + {"_variant": "Rewrite", "value": Rewrite} | + {"_variant": "Alts", "value": Alts} +); + +export type Rewrite = {"pattern": Pattern, "template": Template}; + +export type Alts = {"alternatives": Array}; + +export type Resolve = {"sturdyref": SturdyRef, "observer": _ptr}; + +export type ConstructorSpec = ( + {"_variant": "CRec", "value": CRec} | + {"_variant": "CArr", "value": CArr} | + {"_variant": "CDict", "value": CDict} +); + +export type CRec = {"label": _val, "arity": number}; + +export type CArr = {"arity": number}; + +export type CDict = null; + +export type Lit = {"value": _val}; + +export type Pattern = ( + {"_variant": "PDiscard", "value": PDiscard} | + {"_variant": "PBind", "value": PBind} | + {"_variant": "PAnd", "value": PAnd} | + {"_variant": "PNot", "value": PNot} | + {"_variant": "Lit", "value": Lit} | + {"_variant": "PCompound", "value": PCompound} +); + +export type PDiscard = null; + +export type PBind = {"name": symbol, "pattern": Pattern}; + +export type PAnd = {"patterns": Array}; + +export type PNot = {"pattern": Pattern}; + +export type PCompound = {"ctor": ConstructorSpec, "members": PCompoundMembers}; + +export type PCompoundMembers = _.KeyedDictionary<_val, Pattern, _ptr>; + +export type Template = ( + {"_variant": "TRef", "value": TRef} | + {"_variant": "Lit", "value": Lit} | + {"_variant": "TCompound", "value": TCompound} +); + +export type TRef = {"name": symbol}; + +export type TCompound = {"ctor": ConstructorSpec, "members": TCompoundMembers}; + +export type TCompoundMembers = _.KeyedDictionary<_val, Template, _ptr>; + + +export const _toPtr = (v: _val) => {let result: undefined | _ptr; result = _i_Actor.toRef(v); return result;}; + +export function SturdyRef(oid: _val, caveatChain: Array, sig: _.Bytes): SturdyRef {return {oid, caveatChain, sig};} + +export function Attenuation(value: Array): Attenuation {return value;} + +export namespace Caveat { + export function Rewrite(value: Rewrite): Caveat {return {"_variant": "Rewrite", value};}; + export function Alts(value: Alts): Caveat {return {"_variant": "Alts", value};}; } -export function asSturdyRef(v: any): SturdyRef { - if (!isSturdyRef(v)) {throw new TypeError(`Invalid SturdyRef: ${_.stringify(v)}`);} else {return v;}; +export function Rewrite(pattern: Pattern, template: Template): Rewrite {return {pattern, template};} + +export function Alts(alternatives: Array): Alts {return {alternatives};} + +export function Resolve(sturdyref: SturdyRef, observer: _ptr): Resolve {return {sturdyref, observer};} + +export namespace ConstructorSpec { + export function CRec(value: CRec): ConstructorSpec {return {"_variant": "CRec", value};}; + export function CArr(value: CArr): ConstructorSpec {return {"_variant": "CArr", value};}; + export function CDict(value: CDict): ConstructorSpec {return {"_variant": "CDict", value};}; } -export function decodeSturdyRef(d: _.TypedDecoder<_ptr>): SturdyRef | undefined { - let result; - if (d.openRecord()) { - let _tmp0: any; - _tmp0 = _.asLiteral(d.nextSymbol(), $ref); +export function CRec(label: _val, arity: number): CRec {return {label, arity};} + +export function CArr(arity: number): CArr {return {arity};} + +export function CDict(): CDict {return null;} + +export function Lit(value: _val): Lit {return {value};} + +export namespace Pattern { + export function PDiscard(value: PDiscard): Pattern {return {"_variant": "PDiscard", value};}; + export function PBind(value: PBind): Pattern {return {"_variant": "PBind", value};}; + export function PAnd(value: PAnd): Pattern {return {"_variant": "PAnd", value};}; + export function PNot(value: PNot): Pattern {return {"_variant": "PNot", value};}; + export function Lit(value: Lit): Pattern {return {"_variant": "Lit", value};}; + export function PCompound(value: PCompound): Pattern {return {"_variant": "PCompound", value};}; +} + +export function PDiscard(): PDiscard {return null;} + +export function PBind(name: symbol, pattern: Pattern): PBind {return {name, pattern};} + +export function PAnd(patterns: Array): PAnd {return {patterns};} + +export function PNot(pattern: Pattern): PNot {return {pattern};} + +export function PCompound(ctor: ConstructorSpec, members: PCompoundMembers): PCompound {return {ctor, members};} + +export function PCompoundMembers(value: _.KeyedDictionary<_val, Pattern, _ptr>): PCompoundMembers {return value;} + +export namespace Template { + export function TRef(value: TRef): Template {return {"_variant": "TRef", value};}; + export function Lit(value: Lit): Template {return {"_variant": "Lit", value};}; + export function TCompound(value: TCompound): Template {return {"_variant": "TCompound", value};}; +} + +export function TRef(name: symbol): TRef {return {name};} + +export function TCompound(ctor: ConstructorSpec, members: TCompoundMembers): TCompound {return {ctor, members};} + +export function TCompoundMembers(value: _.KeyedDictionary<_val, Template, _ptr>): TCompoundMembers {return value;} + +export function asSturdyRef(v: _val): SturdyRef { + let result = toSturdyRef(v); + if (result === void 0) throw new TypeError(`Invalid SturdyRef: ${_.stringify(v)}`); + return result; +} + +export function toSturdyRef(v: _val): undefined | SturdyRef { + let result: undefined | SturdyRef; + if (_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v)) { + let _tmp0: (null) | undefined; + _tmp0 = _.is(v.label, $ref) ? null : void 0; if (_tmp0 !== void 0) { - let _tmp1, _tmp2, _tmp3, _tmp4: any; - _tmp2 = d.next(); - if (_tmp2 !== void 0) { - if (d.openSequence()) { - let _tmp5: any; + let _tmp1: (_val) | undefined; + _tmp1 = v[0]; + if (_tmp1 !== void 0) { + if (_.Array.isArray(v[1])) { + let _tmp2: (Array<_val>) | undefined; + let _tmp3: (Array) | undefined; + _tmp2 = v[1]; { - let vN: Array | undefined = []; - while (!d.closeCompound()) { - _tmp5 = void 0; - _tmp5 = decodeAttenuation(d); - if (_tmp5 === void 0) {vN = void 0; break;}; - vN.push(_tmp5); + _tmp3 = []; + for (const _tmp4 of _tmp2) { + let _tmp5: (Attenuation) | undefined; + _tmp5 = toAttenuation(_tmp4); + if (_tmp5 !== void 0) {_tmp3.push(_tmp5); continue;}; + _tmp3 = void 0; + break; + }; + if (_tmp3 !== void 0) { + let _tmp6: (_.Bytes) | undefined; + _tmp6 = _.Bytes.isBytes(v[2]) ? v[2] : void 0; + if (_tmp6 !== void 0) {result = {"oid": _tmp1, "caveatChain": _tmp3, "sig": _tmp6};}; }; - _tmp3 = vN; }; }; - if (_tmp3 !== void 0) { - _tmp4 = d.nextByteString(); - if (_tmp4 !== void 0) {if (d.closeCompound()) _tmp1 = [_tmp2, _tmp3, _tmp4];}; - }; - }; - if (_tmp1 !== void 0) result = _.Record<(typeof $ref), [_val, Array, _.Bytes]>(_tmp0 as any, _tmp1 as any); - }; - }; - return result; -} - -export function isAttenuation(v: any): v is Attenuation { - return ( - _.Array.isArray(v) && - !_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) && - (v.length >= 0) && - v.every(v => (isCaveat(v))) - ); -} - -export function asAttenuation(v: any): Attenuation { - if (!isAttenuation(v)) {throw new TypeError(`Invalid Attenuation: ${_.stringify(v)}`);} else {return v;}; -} - -export function decodeAttenuation(d: _.TypedDecoder<_ptr>): Attenuation | undefined { - let result; - if (d.openSequence()) { - let _tmp6: any; - { - let vN: Array | undefined = []; - while (!d.closeCompound()) { - _tmp6 = void 0; - _tmp6 = decodeCaveat(d); - if (_tmp6 === void 0) {vN = void 0; break;}; - vN.push(_tmp6); - }; - result = vN; - }; - }; - return result; -} - -export function isCaveat(v: any): v is Caveat { - return _.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) && ( - ( - ( - _.is(v.label, $rewrite) && ((v.length === 2) && isPattern(v[0]) && isTemplate(v[1])) - ) || - ( - _.is(v.label, $or) && ( - (v.length === 1) && - ( - _.Array.isArray(v[0]) && - !_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v[0]) && - (v[0].length >= 0) && - v[0].every(v => (isRewrite(v))) - ) - ) - ) - ) - ); -} - -export function asCaveat(v: any): Caveat { - if (!isCaveat(v)) {throw new TypeError(`Invalid Caveat: ${_.stringify(v)}`);} else {return v;}; -} - -export function decodeCaveat(d: _.TypedDecoder<_ptr>): Caveat | undefined { - let result; - if (d.openRecord()) { - let _tmp7, _tmp8: any; - _tmp7 = d.next(); - _tmp8 = d.mark(); - if (_.is(_tmp7, $rewrite)) { - let _tmp9, _tmp10, _tmp11: any; - _tmp10 = decodePattern(d); - if (_tmp10 !== void 0) { - _tmp11 = decodeTemplate(d); - if (_tmp11 !== void 0) {if (d.closeCompound()) _tmp9 = [_tmp10, _tmp11];}; - }; - if (_tmp9 !== void 0) result = _.Record<(typeof $rewrite), [Pattern, Template]>(_tmp7 as any, _tmp9 as any); - }; - if (result === void 0) { - d.restoreMark(_tmp8); - if (_.is(_tmp7, $or)) { - let _tmp12, _tmp13: any; - if (d.openSequence()) { - let _tmp14: any; - { - let vN: Array | undefined = []; - while (!d.closeCompound()) { - _tmp14 = void 0; - _tmp14 = decodeRewrite(d); - if (_tmp14 === void 0) {vN = void 0; break;}; - vN.push(_tmp14); - }; - _tmp13 = vN; - }; - }; - if (_tmp13 !== void 0) {if (d.closeCompound()) _tmp12 = [_tmp13];}; - if (_tmp12 !== void 0) result = _.Record<(typeof $or), [Array]>(_tmp7 as any, _tmp12 as any); }; }; }; return result; } -export function isRewrite(v: any): v is Rewrite { - return ( - _.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) && - _.is(v.label, $rewrite) && - ((v.length === 2) && isPattern(v[0]) && isTemplate(v[1])) - ); +export function fromSturdyRef(_v: SturdyRef): _val { + return _.Record($ref, [_v["oid"], _v["caveatChain"].map(v => fromAttenuation(v)), _v["sig"]]); } -export function asRewrite(v: any): Rewrite { - if (!isRewrite(v)) {throw new TypeError(`Invalid Rewrite: ${_.stringify(v)}`);} else {return v;}; +export function asAttenuation(v: _val): Attenuation { + let result = toAttenuation(v); + if (result === void 0) throw new TypeError(`Invalid Attenuation: ${_.stringify(v)}`); + return result; } -export function decodeRewrite(d: _.TypedDecoder<_ptr>): Rewrite | undefined { - let result; - if (d.openRecord()) { - let _tmp15: any; - _tmp15 = _.asLiteral(d.nextSymbol(), $rewrite); - if (_tmp15 !== void 0) { - let _tmp16, _tmp17, _tmp18: any; - _tmp17 = decodePattern(d); - if (_tmp17 !== void 0) { - _tmp18 = decodeTemplate(d); - if (_tmp18 !== void 0) {if (d.closeCompound()) _tmp16 = [_tmp17, _tmp18];}; +export function toAttenuation(v: _val): undefined | Attenuation { + let result: undefined | Attenuation; + if (_.Array.isArray(v)) { + let _tmp0: (Array) | undefined; + _tmp0 = []; + for (const _tmp1 of v) { + let _tmp2: (Caveat) | undefined; + _tmp2 = toCaveat(_tmp1); + if (_tmp2 !== void 0) {_tmp0.push(_tmp2); continue;}; + _tmp0 = void 0; + break; + }; + if (_tmp0 !== void 0) {result = _tmp0;}; + }; + return result; +} + +export function fromAttenuation(_v: Attenuation): _val {return _v.map(v => fromCaveat(v));} + +export function asCaveat(v: _val): Caveat { + let result = toCaveat(v); + if (result === void 0) throw new TypeError(`Invalid Caveat: ${_.stringify(v)}`); + return result; +} + +export function toCaveat(v: _val): undefined | Caveat { + let _tmp0: (Rewrite) | undefined; + let result: undefined | Caveat; + _tmp0 = toRewrite(v); + if (_tmp0 !== void 0) {result = {"_variant": "Rewrite", "value": _tmp0};}; + if (result === void 0) { + let _tmp1: (Alts) | undefined; + _tmp1 = toAlts(v); + if (_tmp1 !== void 0) {result = {"_variant": "Alts", "value": _tmp1};}; + }; + return result; +} + +export function fromCaveat(_v: Caveat): _val { + switch (_v._variant) { + case "Rewrite": {return fromRewrite(_v.value);}; + case "Alts": {return fromAlts(_v.value);}; + }; +} + +export function asRewrite(v: _val): Rewrite { + let result = toRewrite(v); + if (result === void 0) throw new TypeError(`Invalid Rewrite: ${_.stringify(v)}`); + return result; +} + +export function toRewrite(v: _val): undefined | Rewrite { + let result: undefined | Rewrite; + if (_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v)) { + let _tmp0: (null) | undefined; + _tmp0 = _.is(v.label, $rewrite) ? null : void 0; + if (_tmp0 !== void 0) { + let _tmp1: (Pattern) | undefined; + _tmp1 = toPattern(v[0]); + if (_tmp1 !== void 0) { + let _tmp2: (Template) | undefined; + _tmp2 = toTemplate(v[1]); + if (_tmp2 !== void 0) {result = {"pattern": _tmp1, "template": _tmp2};}; }; - if (_tmp16 !== void 0) result = _.Record<(typeof $rewrite), [Pattern, Template]>(_tmp15 as any, _tmp16 as any); }; }; return result; } -export function isAlts(v: any): v is Alts { - return ( - _.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) && - _.is(v.label, $or) && - ( - (v.length === 1) && - ( - _.Array.isArray(v[0]) && - !_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v[0]) && - (v[0].length >= 0) && - v[0].every(v => (isRewrite(v))) - ) - ) - ); +export function fromRewrite(_v: Rewrite): _val { + return _.Record($rewrite, [fromPattern(_v["pattern"]), fromTemplate(_v["template"])]); } -export function asAlts(v: any): Alts { - if (!isAlts(v)) {throw new TypeError(`Invalid Alts: ${_.stringify(v)}`);} else {return v;}; +export function asAlts(v: _val): Alts { + let result = toAlts(v); + if (result === void 0) throw new TypeError(`Invalid Alts: ${_.stringify(v)}`); + return result; } -export function decodeAlts(d: _.TypedDecoder<_ptr>): Alts | undefined { - let result; - if (d.openRecord()) { - let _tmp19: any; - _tmp19 = _.asLiteral(d.nextSymbol(), $or); - if (_tmp19 !== void 0) { - let _tmp20, _tmp21: any; - if (d.openSequence()) { - let _tmp22: any; +export function toAlts(v: _val): undefined | Alts { + let result: undefined | Alts; + if (_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v)) { + let _tmp0: (null) | undefined; + _tmp0 = _.is(v.label, $or) ? null : void 0; + if (_tmp0 !== void 0) { + if (_.Array.isArray(v[0])) { + let _tmp1: (Array<_val>) | undefined; + let _tmp2: (Array) | undefined; + _tmp1 = v[0]; { - let vN: Array | undefined = []; - while (!d.closeCompound()) { - _tmp22 = void 0; - _tmp22 = decodeRewrite(d); - if (_tmp22 === void 0) {vN = void 0; break;}; - vN.push(_tmp22); + _tmp2 = []; + for (const _tmp3 of _tmp1) { + let _tmp4: (Rewrite) | undefined; + _tmp4 = toRewrite(_tmp3); + if (_tmp4 !== void 0) {_tmp2.push(_tmp4); continue;}; + _tmp2 = void 0; + break; }; - _tmp21 = vN; + if (_tmp2 !== void 0) {result = {"alternatives": _tmp2};}; }; }; - if (_tmp21 !== void 0) {if (d.closeCompound()) _tmp20 = [_tmp21];}; - if (_tmp20 !== void 0) result = _.Record<(typeof $or), [Array]>(_tmp19 as any, _tmp20 as any); }; }; return result; } -export function isResolve(v: any): v is Resolve { - return ( - _.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) && - _.is(v.label, $resolve) && - ((v.length === 2) && isSturdyRef(v[0]) && _.isPointer(v[1])) - ); +export function fromAlts(_v: Alts): _val {return _.Record($or, [_v["alternatives"].map(v => fromRewrite(v))]);} + +export function asResolve(v: _val): Resolve { + let result = toResolve(v); + if (result === void 0) throw new TypeError(`Invalid Resolve: ${_.stringify(v)}`); + return result; } -export function asResolve(v: any): Resolve { - if (!isResolve(v)) {throw new TypeError(`Invalid Resolve: ${_.stringify(v)}`);} else {return v;}; -} - -export function decodeResolve(d: _.TypedDecoder<_ptr>): Resolve | undefined { - let result; - if (d.openRecord()) { - let _tmp23: any; - _tmp23 = _.asLiteral(d.nextSymbol(), $resolve); - if (_tmp23 !== void 0) { - let _tmp24, _tmp25, _tmp26: any; - _tmp25 = decodeSturdyRef(d); - if (_tmp25 !== void 0) { - _tmp26 = _decodePtr(d); - if (_tmp26 !== void 0) {if (d.closeCompound()) _tmp24 = [_tmp25, _tmp26];}; +export function toResolve(v: _val): undefined | Resolve { + let result: undefined | Resolve; + if (_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v)) { + let _tmp0: (null) | undefined; + _tmp0 = _.is(v.label, $resolve) ? null : void 0; + if (_tmp0 !== void 0) { + let _tmp1: (SturdyRef) | undefined; + _tmp1 = toSturdyRef(v[0]); + if (_tmp1 !== void 0) { + let _tmp2: (_ptr) | undefined; + _tmp2 = _toPtr(v[1]); + if (_tmp2 !== void 0) {result = {"sturdyref": _tmp1, "observer": _tmp2};}; }; - if (_tmp24 !== void 0) result = _.Record<(typeof $resolve), [SturdyRef, _ptr]>(_tmp23 as any, _tmp24 as any); }; }; return result; } -export function isConstructorSpec(v: any): v is ConstructorSpec { - return _.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) && ( - ( - (_.is(v.label, $rec) && ((v.length === 2) && true && typeof v[1] === 'number')) || - (_.is(v.label, $arr) && ((v.length === 1) && typeof v[0] === 'number')) || - (_.is(v.label, $dict) && ((v.length === 0))) - ) - ); +export function fromResolve(_v: Resolve): _val {return _.Record($resolve, [fromSturdyRef(_v["sturdyref"]), _v["observer"]]);} + +export function asConstructorSpec(v: _val): ConstructorSpec { + let result = toConstructorSpec(v); + if (result === void 0) throw new TypeError(`Invalid ConstructorSpec: ${_.stringify(v)}`); + return result; } -export function asConstructorSpec(v: any): ConstructorSpec { - if (!isConstructorSpec(v)) {throw new TypeError(`Invalid ConstructorSpec: ${_.stringify(v)}`);} else {return v;}; -} - -export function decodeConstructorSpec(d: _.TypedDecoder<_ptr>): ConstructorSpec | undefined { - let result; - if (d.openRecord()) { - let _tmp27, _tmp28: any; - _tmp27 = d.next(); - _tmp28 = d.mark(); - if (_.is(_tmp27, $rec)) { - let _tmp29, _tmp30, _tmp31: any; - _tmp30 = d.next(); - if (_tmp30 !== void 0) { - _tmp31 = d.nextSignedInteger(); - if (_tmp31 !== void 0) {if (d.closeCompound()) _tmp29 = [_tmp30, _tmp31];}; - }; - if (_tmp29 !== void 0) result = _.Record<(typeof $rec), [_val, number]>(_tmp27 as any, _tmp29 as any); - }; +export function toConstructorSpec(v: _val): undefined | ConstructorSpec { + let _tmp0: (CRec) | undefined; + let result: undefined | ConstructorSpec; + _tmp0 = toCRec(v); + if (_tmp0 !== void 0) {result = {"_variant": "CRec", "value": _tmp0};}; + if (result === void 0) { + let _tmp1: (CArr) | undefined; + _tmp1 = toCArr(v); + if (_tmp1 !== void 0) {result = {"_variant": "CArr", "value": _tmp1};}; if (result === void 0) { - d.restoreMark(_tmp28); - if (_.is(_tmp27, $arr)) { - let _tmp32, _tmp33: any; - _tmp33 = d.nextSignedInteger(); - if (_tmp33 !== void 0) {if (d.closeCompound()) _tmp32 = [_tmp33];}; - if (_tmp32 !== void 0) result = _.Record<(typeof $arr), [number]>(_tmp27 as any, _tmp32 as any); - }; - if (result === void 0) { - d.restoreMark(_tmp28); - if (_.is(_tmp27, $dict)) { - let _tmp34: any; - if (d.closeCompound()) _tmp34 = []; - if (_tmp34 !== void 0) result = _.Record<(typeof $dict), []>(_tmp27 as any, _tmp34 as any); - }; + let _tmp2: (CDict) | undefined; + _tmp2 = toCDict(v); + if (_tmp2 !== void 0) {result = {"_variant": "CDict", "value": _tmp2};}; + }; + }; + return result; +} + +export function fromConstructorSpec(_v: ConstructorSpec): _val { + switch (_v._variant) { + case "CRec": {return fromCRec(_v.value);}; + case "CArr": {return fromCArr(_v.value);}; + case "CDict": {return fromCDict(_v.value);}; + }; +} + +export function asCRec(v: _val): CRec { + let result = toCRec(v); + if (result === void 0) throw new TypeError(`Invalid CRec: ${_.stringify(v)}`); + return result; +} + +export function toCRec(v: _val): undefined | CRec { + let result: undefined | CRec; + if (_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v)) { + let _tmp0: (null) | undefined; + _tmp0 = _.is(v.label, $rec) ? null : void 0; + if (_tmp0 !== void 0) { + let _tmp1: (_val) | undefined; + _tmp1 = v[0]; + if (_tmp1 !== void 0) { + let _tmp2: (number) | undefined; + _tmp2 = typeof v[1] === 'number' ? v[1] : void 0; + if (_tmp2 !== void 0) {result = {"label": _tmp1, "arity": _tmp2};}; }; }; }; return result; } -export function isCRec(v: any): v is CRec { - return ( - _.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) && - _.is(v.label, $rec) && - ((v.length === 2) && true && typeof v[1] === 'number') - ); +export function fromCRec(_v: CRec): _val {return _.Record($rec, [_v["label"], _v["arity"]]);} + +export function asCArr(v: _val): CArr { + let result = toCArr(v); + if (result === void 0) throw new TypeError(`Invalid CArr: ${_.stringify(v)}`); + return result; } -export function asCRec(v: any): CRec { - if (!isCRec(v)) {throw new TypeError(`Invalid CRec: ${_.stringify(v)}`);} else {return v;}; -} - -export function decodeCRec(d: _.TypedDecoder<_ptr>): CRec | undefined { - let result; - if (d.openRecord()) { - let _tmp35: any; - _tmp35 = _.asLiteral(d.nextSymbol(), $rec); - if (_tmp35 !== void 0) { - let _tmp36, _tmp37, _tmp38: any; - _tmp37 = d.next(); - if (_tmp37 !== void 0) { - _tmp38 = d.nextSignedInteger(); - if (_tmp38 !== void 0) {if (d.closeCompound()) _tmp36 = [_tmp37, _tmp38];}; - }; - if (_tmp36 !== void 0) result = _.Record<(typeof $rec), [_val, number]>(_tmp35 as any, _tmp36 as any); +export function toCArr(v: _val): undefined | CArr { + let result: undefined | CArr; + if (_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v)) { + let _tmp0: (null) | undefined; + _tmp0 = _.is(v.label, $arr) ? null : void 0; + if (_tmp0 !== void 0) { + let _tmp1: (number) | undefined; + _tmp1 = typeof v[0] === 'number' ? v[0] : void 0; + if (_tmp1 !== void 0) {result = {"arity": _tmp1};}; }; }; return result; } -export function isCArr(v: any): v is CArr { - return ( - _.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) && - _.is(v.label, $arr) && - ((v.length === 1) && typeof v[0] === 'number') - ); +export function fromCArr(_v: CArr): _val {return _.Record($arr, [_v["arity"]]);} + +export function asCDict(v: _val): CDict { + let result = toCDict(v); + if (result === void 0) throw new TypeError(`Invalid CDict: ${_.stringify(v)}`); + return result; } -export function asCArr(v: any): CArr { - if (!isCArr(v)) {throw new TypeError(`Invalid CArr: ${_.stringify(v)}`);} else {return v;}; +export function toCDict(v: _val): undefined | CDict { + let result: undefined | CDict; + if (_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v)) { + let _tmp0: (null) | undefined; + _tmp0 = _.is(v.label, $dict) ? null : void 0; + if (_tmp0 !== void 0) {result = null;}; + }; + return result; } -export function decodeCArr(d: _.TypedDecoder<_ptr>): CArr | undefined { - let result; - if (d.openRecord()) { - let _tmp39: any; - _tmp39 = _.asLiteral(d.nextSymbol(), $arr); - if (_tmp39 !== void 0) { - let _tmp40, _tmp41: any; - _tmp41 = d.nextSignedInteger(); - if (_tmp41 !== void 0) {if (d.closeCompound()) _tmp40 = [_tmp41];}; - if (_tmp40 !== void 0) result = _.Record<(typeof $arr), [number]>(_tmp39 as any, _tmp40 as any); +export function fromCDict(_v: CDict): _val {return _.Record($dict, []);} + +export function asLit(v: _val): Lit { + let result = toLit(v); + if (result === void 0) throw new TypeError(`Invalid Lit: ${_.stringify(v)}`); + return result; +} + +export function toLit(v: _val): undefined | Lit { + let result: undefined | Lit; + if (_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v)) { + let _tmp0: (null) | undefined; + _tmp0 = _.is(v.label, $lit) ? null : void 0; + if (_tmp0 !== void 0) { + let _tmp1: (_val) | undefined; + _tmp1 = v[0]; + if (_tmp1 !== void 0) {result = {"value": _tmp1};}; }; }; return result; } -export function isCDict(v: any): v is CDict { - return ( - _.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) && - _.is(v.label, $dict) && - ((v.length === 0)) - ); -} +export function fromLit(_v: Lit): _val {return _.Record($lit, [_v["value"]]);} -export function asCDict(v: any): CDict { - if (!isCDict(v)) {throw new TypeError(`Invalid CDict: ${_.stringify(v)}`);} else {return v;}; -} - -export function decodeCDict(d: _.TypedDecoder<_ptr>): CDict | undefined { - let result; - if (d.openRecord()) { - let _tmp42: any; - _tmp42 = _.asLiteral(d.nextSymbol(), $dict); - if (_tmp42 !== void 0) { - let _tmp43: any; - if (d.closeCompound()) _tmp43 = []; - if (_tmp43 !== void 0) result = _.Record<(typeof $dict), []>(_tmp42 as any, _tmp43 as any); - }; - }; +export function asPattern(v: _val): Pattern { + let result = toPattern(v); + if (result === void 0) throw new TypeError(`Invalid Pattern: ${_.stringify(v)}`); return result; } -export function isLit(v: any): v is Lit { - return ( - _.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) && - _.is(v.label, $lit) && - ((v.length === 1) && true) - ); -} - -export function asLit(v: any): Lit { - if (!isLit(v)) {throw new TypeError(`Invalid Lit: ${_.stringify(v)}`);} else {return v;}; -} - -export function decodeLit(d: _.TypedDecoder<_ptr>): Lit | undefined { - let result; - if (d.openRecord()) { - let _tmp44: any; - _tmp44 = _.asLiteral(d.nextSymbol(), $lit); - if (_tmp44 !== void 0) { - let _tmp45, _tmp46: any; - _tmp46 = d.next(); - if (_tmp46 !== void 0) {if (d.closeCompound()) _tmp45 = [_tmp46];}; - if (_tmp45 !== void 0) result = _.Record<(typeof $lit), [_val]>(_tmp44 as any, _tmp45 as any); - }; - }; - return result; -} - -export function isPattern(v: any): v is Pattern { - return _.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) && ( - ( - (_.is(v.label, $__) && ((v.length === 0))) || - ( - _.is(v.label, $bind) && ((v.length === 2) && typeof v[0] === 'symbol' && isPattern(v[1])) - ) || - ( - _.is(v.label, $and) && ( - (v.length === 1) && - ( - _.Array.isArray(v[0]) && - !_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v[0]) && - (v[0].length >= 0) && - v[0].every(v => (isPattern(v))) - ) - ) - ) || - (_.is(v.label, $not) && ((v.length === 1) && isPattern(v[0]))) || - (_.is(v.label, $lit) && ((v.length === 1) && true)) || - ( - _.is(v.label, $compound) && ( - (v.length === 2) && - isConstructorSpec(v[0]) && - ( - _.Dictionary.isDictionary<_val, _ptr>(v[1]) && - ((() => { - for (const e of v[1]) {if (!(true)) return false; if (!(isPattern(e[1]))) return false;}; - return true; - })()) - ) - ) - ) - ) - ); -} - -export function asPattern(v: any): Pattern { - if (!isPattern(v)) {throw new TypeError(`Invalid Pattern: ${_.stringify(v)}`);} else {return v;}; -} - -export function decodePattern(d: _.TypedDecoder<_ptr>): Pattern | undefined { - let result; - if (d.openRecord()) { - let _tmp47, _tmp48: any; - _tmp47 = d.next(); - _tmp48 = d.mark(); - if (_.is(_tmp47, $__)) { - let _tmp49: any; - if (d.closeCompound()) _tmp49 = []; - if (_tmp49 !== void 0) result = _.Record<(typeof $__), []>(_tmp47 as any, _tmp49 as any); - }; +export function toPattern(v: _val): undefined | Pattern { + let _tmp0: (PDiscard) | undefined; + let result: undefined | Pattern; + _tmp0 = toPDiscard(v); + if (_tmp0 !== void 0) {result = {"_variant": "PDiscard", "value": _tmp0};}; + if (result === void 0) { + let _tmp1: (PBind) | undefined; + _tmp1 = toPBind(v); + if (_tmp1 !== void 0) {result = {"_variant": "PBind", "value": _tmp1};}; if (result === void 0) { - d.restoreMark(_tmp48); - if (_.is(_tmp47, $bind)) { - let _tmp50, _tmp51, _tmp52: any; - _tmp51 = d.nextSymbol(); - if (_tmp51 !== void 0) { - _tmp52 = decodePattern(d); - if (_tmp52 !== void 0) {if (d.closeCompound()) _tmp50 = [_tmp51, _tmp52];}; - }; - if (_tmp50 !== void 0) result = _.Record<(typeof $bind), [symbol, Pattern]>(_tmp47 as any, _tmp50 as any); - }; + let _tmp2: (PAnd) | undefined; + _tmp2 = toPAnd(v); + if (_tmp2 !== void 0) {result = {"_variant": "PAnd", "value": _tmp2};}; if (result === void 0) { - d.restoreMark(_tmp48); - if (_.is(_tmp47, $and)) { - let _tmp53, _tmp54: any; - if (d.openSequence()) { - let _tmp55: any; - { - let vN: Array | undefined = []; - while (!d.closeCompound()) { - _tmp55 = void 0; - _tmp55 = decodePattern(d); - if (_tmp55 === void 0) {vN = void 0; break;}; - vN.push(_tmp55); - }; - _tmp54 = vN; - }; - }; - if (_tmp54 !== void 0) {if (d.closeCompound()) _tmp53 = [_tmp54];}; - if (_tmp53 !== void 0) result = _.Record<(typeof $and), [Array]>(_tmp47 as any, _tmp53 as any); - }; + let _tmp3: (PNot) | undefined; + _tmp3 = toPNot(v); + if (_tmp3 !== void 0) {result = {"_variant": "PNot", "value": _tmp3};}; if (result === void 0) { - d.restoreMark(_tmp48); - if (_.is(_tmp47, $not)) { - let _tmp56, _tmp57: any; - _tmp57 = decodePattern(d); - if (_tmp57 !== void 0) {if (d.closeCompound()) _tmp56 = [_tmp57];}; - if (_tmp56 !== void 0) result = _.Record<(typeof $not), [Pattern]>(_tmp47 as any, _tmp56 as any); - }; + let _tmp4: (Lit) | undefined; + _tmp4 = toLit(v); + if (_tmp4 !== void 0) {result = {"_variant": "Lit", "value": _tmp4};}; if (result === void 0) { - d.restoreMark(_tmp48); - if (_.is(_tmp47, $lit)) { - let _tmp58, _tmp59: any; - _tmp59 = d.next(); - if (_tmp59 !== void 0) {if (d.closeCompound()) _tmp58 = [_tmp59];}; - if (_tmp58 !== void 0) result = _.Record<(typeof $lit), [_val]>(_tmp47 as any, _tmp58 as any); - }; - if (result === void 0) { - d.restoreMark(_tmp48); - if (_.is(_tmp47, $compound)) { - let _tmp60, _tmp61, _tmp62: any; - _tmp61 = decodeConstructorSpec(d); - if (_tmp61 !== void 0) { - if (d.openDictionary()) { - let r: _.KeyedDictionary<_val, Pattern, _ptr> | undefined = new _.KeyedDictionary(); - while (!d.closeCompound()) { - let K: undefined | _val = void 0; - K = d.next(); - if (K === void 0) { r = void 0; break; }; - let V: undefined | Pattern = void 0; - V = decodePattern(d); - if (V === void 0) { r = void 0; break; }; - r.set(K, V); - }; - _tmp62 = r; - }; - if (_tmp62 !== void 0) {if (d.closeCompound()) _tmp60 = [_tmp61, _tmp62];}; - }; - if (_tmp60 !== void 0) result = _.Record<(typeof $compound), [ConstructorSpec, _.KeyedDictionary<_val, Pattern, _ptr>]>(_tmp47 as any, _tmp60 as any); - }; - }; + let _tmp5: (PCompound) | undefined; + _tmp5 = toPCompound(v); + if (_tmp5 !== void 0) {result = {"_variant": "PCompound", "value": _tmp5};}; }; }; }; @@ -647,341 +490,293 @@ export function decodePattern(d: _.TypedDecoder<_ptr>): Pattern | undefined { return result; } -export function isPDiscard(v: any): v is PDiscard { - return ( - _.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) && - _.is(v.label, $__) && - ((v.length === 0)) - ); +export function fromPattern(_v: Pattern): _val { + switch (_v._variant) { + case "PDiscard": {return fromPDiscard(_v.value);}; + case "PBind": {return fromPBind(_v.value);}; + case "PAnd": {return fromPAnd(_v.value);}; + case "PNot": {return fromPNot(_v.value);}; + case "Lit": {return fromLit(_v.value);}; + case "PCompound": {return fromPCompound(_v.value);}; + }; } -export function asPDiscard(v: any): PDiscard { - if (!isPDiscard(v)) {throw new TypeError(`Invalid PDiscard: ${_.stringify(v)}`);} else {return v;}; +export function asPDiscard(v: _val): PDiscard { + let result = toPDiscard(v); + if (result === void 0) throw new TypeError(`Invalid PDiscard: ${_.stringify(v)}`); + return result; } -export function decodePDiscard(d: _.TypedDecoder<_ptr>): PDiscard | undefined { - let result; - if (d.openRecord()) { - let _tmp63: any; - _tmp63 = _.asLiteral(d.nextSymbol(), $__); - if (_tmp63 !== void 0) { - let _tmp64: any; - if (d.closeCompound()) _tmp64 = []; - if (_tmp64 !== void 0) result = _.Record<(typeof $__), []>(_tmp63 as any, _tmp64 as any); - }; +export function toPDiscard(v: _val): undefined | PDiscard { + let result: undefined | PDiscard; + if (_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v)) { + let _tmp0: (null) | undefined; + _tmp0 = _.is(v.label, $_) ? null : void 0; + if (_tmp0 !== void 0) {result = null;}; }; return result; } -export function isPBind(v: any): v is PBind { - return ( - _.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) && - _.is(v.label, $bind) && - ((v.length === 2) && typeof v[0] === 'symbol' && isPattern(v[1])) - ); +export function fromPDiscard(_v: PDiscard): _val {return _.Record($_, []);} + +export function asPBind(v: _val): PBind { + let result = toPBind(v); + if (result === void 0) throw new TypeError(`Invalid PBind: ${_.stringify(v)}`); + return result; } -export function asPBind(v: any): PBind { - if (!isPBind(v)) {throw new TypeError(`Invalid PBind: ${_.stringify(v)}`);} else {return v;}; -} - -export function decodePBind(d: _.TypedDecoder<_ptr>): PBind | undefined { - let result; - if (d.openRecord()) { - let _tmp65: any; - _tmp65 = _.asLiteral(d.nextSymbol(), $bind); - if (_tmp65 !== void 0) { - let _tmp66, _tmp67, _tmp68: any; - _tmp67 = d.nextSymbol(); - if (_tmp67 !== void 0) { - _tmp68 = decodePattern(d); - if (_tmp68 !== void 0) {if (d.closeCompound()) _tmp66 = [_tmp67, _tmp68];}; +export function toPBind(v: _val): undefined | PBind { + let result: undefined | PBind; + if (_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v)) { + let _tmp0: (null) | undefined; + _tmp0 = _.is(v.label, $bind) ? null : void 0; + if (_tmp0 !== void 0) { + let _tmp1: (symbol) | undefined; + _tmp1 = typeof v[0] === 'symbol' ? v[0] : void 0; + if (_tmp1 !== void 0) { + let _tmp2: (Pattern) | undefined; + _tmp2 = toPattern(v[1]); + if (_tmp2 !== void 0) {result = {"name": _tmp1, "pattern": _tmp2};}; }; - if (_tmp66 !== void 0) result = _.Record<(typeof $bind), [symbol, Pattern]>(_tmp65 as any, _tmp66 as any); }; }; return result; } -export function isPAnd(v: any): v is PAnd { - return ( - _.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) && - _.is(v.label, $and) && - ( - (v.length === 1) && - ( - _.Array.isArray(v[0]) && - !_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v[0]) && - (v[0].length >= 0) && - v[0].every(v => (isPattern(v))) - ) - ) - ); +export function fromPBind(_v: PBind): _val {return _.Record($bind, [_v["name"], fromPattern(_v["pattern"])]);} + +export function asPAnd(v: _val): PAnd { + let result = toPAnd(v); + if (result === void 0) throw new TypeError(`Invalid PAnd: ${_.stringify(v)}`); + return result; } -export function asPAnd(v: any): PAnd { - if (!isPAnd(v)) {throw new TypeError(`Invalid PAnd: ${_.stringify(v)}`);} else {return v;}; -} - -export function decodePAnd(d: _.TypedDecoder<_ptr>): PAnd | undefined { - let result; - if (d.openRecord()) { - let _tmp69: any; - _tmp69 = _.asLiteral(d.nextSymbol(), $and); - if (_tmp69 !== void 0) { - let _tmp70, _tmp71: any; - if (d.openSequence()) { - let _tmp72: any; +export function toPAnd(v: _val): undefined | PAnd { + let result: undefined | PAnd; + if (_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v)) { + let _tmp0: (null) | undefined; + _tmp0 = _.is(v.label, $and) ? null : void 0; + if (_tmp0 !== void 0) { + if (_.Array.isArray(v[0])) { + let _tmp1: (Array<_val>) | undefined; + let _tmp2: (Array) | undefined; + _tmp1 = v[0]; { - let vN: Array | undefined = []; - while (!d.closeCompound()) { - _tmp72 = void 0; - _tmp72 = decodePattern(d); - if (_tmp72 === void 0) {vN = void 0; break;}; - vN.push(_tmp72); + _tmp2 = []; + for (const _tmp3 of _tmp1) { + let _tmp4: (Pattern) | undefined; + _tmp4 = toPattern(_tmp3); + if (_tmp4 !== void 0) {_tmp2.push(_tmp4); continue;}; + _tmp2 = void 0; + break; }; - _tmp71 = vN; + if (_tmp2 !== void 0) {result = {"patterns": _tmp2};}; }; }; - if (_tmp71 !== void 0) {if (d.closeCompound()) _tmp70 = [_tmp71];}; - if (_tmp70 !== void 0) result = _.Record<(typeof $and), [Array]>(_tmp69 as any, _tmp70 as any); }; }; return result; } -export function isPNot(v: any): v is PNot { - return ( - _.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) && - _.is(v.label, $not) && - ((v.length === 1) && isPattern(v[0])) - ); +export function fromPAnd(_v: PAnd): _val {return _.Record($and, [_v["patterns"].map(v => fromPattern(v))]);} + +export function asPNot(v: _val): PNot { + let result = toPNot(v); + if (result === void 0) throw new TypeError(`Invalid PNot: ${_.stringify(v)}`); + return result; } -export function asPNot(v: any): PNot { - if (!isPNot(v)) {throw new TypeError(`Invalid PNot: ${_.stringify(v)}`);} else {return v;}; -} - -export function decodePNot(d: _.TypedDecoder<_ptr>): PNot | undefined { - let result; - if (d.openRecord()) { - let _tmp73: any; - _tmp73 = _.asLiteral(d.nextSymbol(), $not); - if (_tmp73 !== void 0) { - let _tmp74, _tmp75: any; - _tmp75 = decodePattern(d); - if (_tmp75 !== void 0) {if (d.closeCompound()) _tmp74 = [_tmp75];}; - if (_tmp74 !== void 0) result = _.Record<(typeof $not), [Pattern]>(_tmp73 as any, _tmp74 as any); +export function toPNot(v: _val): undefined | PNot { + let result: undefined | PNot; + if (_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v)) { + let _tmp0: (null) | undefined; + _tmp0 = _.is(v.label, $not) ? null : void 0; + if (_tmp0 !== void 0) { + let _tmp1: (Pattern) | undefined; + _tmp1 = toPattern(v[0]); + if (_tmp1 !== void 0) {result = {"pattern": _tmp1};}; }; }; return result; } -export function isPCompound(v: any): v is PCompound { - return ( - _.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) && - _.is(v.label, $compound) && - ( - (v.length === 2) && - isConstructorSpec(v[0]) && - ( - _.Dictionary.isDictionary<_val, _ptr>(v[1]) && - ((() => { - for (const e of v[1]) {if (!(true)) return false; if (!(isPattern(e[1]))) return false;}; - return true; - })()) - ) - ) - ); +export function fromPNot(_v: PNot): _val {return _.Record($not, [fromPattern(_v["pattern"])]);} + +export function asPCompound(v: _val): PCompound { + let result = toPCompound(v); + if (result === void 0) throw new TypeError(`Invalid PCompound: ${_.stringify(v)}`); + return result; } -export function asPCompound(v: any): PCompound { - if (!isPCompound(v)) {throw new TypeError(`Invalid PCompound: ${_.stringify(v)}`);} else {return v;}; -} - -export function decodePCompound(d: _.TypedDecoder<_ptr>): PCompound | undefined { - let result; - if (d.openRecord()) { - let _tmp76: any; - _tmp76 = _.asLiteral(d.nextSymbol(), $compound); - if (_tmp76 !== void 0) { - let _tmp77, _tmp78, _tmp79: any; - _tmp78 = decodeConstructorSpec(d); - if (_tmp78 !== void 0) { - if (d.openDictionary()) { - let r: _.KeyedDictionary<_val, Pattern, _ptr> | undefined = new _.KeyedDictionary(); - while (!d.closeCompound()) { - let K: undefined | _val = void 0; - K = d.next(); - if (K === void 0) { r = void 0; break; }; - let V: undefined | Pattern = void 0; - V = decodePattern(d); - if (V === void 0) { r = void 0; break; }; - r.set(K, V); - }; - _tmp79 = r; - }; - if (_tmp79 !== void 0) {if (d.closeCompound()) _tmp77 = [_tmp78, _tmp79];}; +export function toPCompound(v: _val): undefined | PCompound { + let result: undefined | PCompound; + if (_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v)) { + let _tmp0: (null) | undefined; + _tmp0 = _.is(v.label, $compound) ? null : void 0; + if (_tmp0 !== void 0) { + let _tmp1: (ConstructorSpec) | undefined; + _tmp1 = toConstructorSpec(v[0]); + if (_tmp1 !== void 0) { + let _tmp2: (PCompoundMembers) | undefined; + _tmp2 = toPCompoundMembers(v[1]); + if (_tmp2 !== void 0) {result = {"ctor": _tmp1, "members": _tmp2};}; }; - if (_tmp77 !== void 0) result = _.Record<(typeof $compound), [ConstructorSpec, _.KeyedDictionary<_val, Pattern, _ptr>]>(_tmp76 as any, _tmp77 as any); }; }; return result; } -export function isTemplate(v: any): v is Template { - return _.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) && ( - ( - (_.is(v.label, $ref) && ((v.length === 1) && typeof v[0] === 'symbol')) || - (_.is(v.label, $lit) && ((v.length === 1) && true)) || - ( - _.is(v.label, $compound) && ( - (v.length === 2) && - isConstructorSpec(v[0]) && - ( - _.Dictionary.isDictionary<_val, _ptr>(v[1]) && - ((() => { - for (const e of v[1]) {if (!(true)) return false; if (!(isTemplate(e[1]))) return false;}; - return true; - })()) - ) - ) - ) - ) +export function fromPCompound(_v: PCompound): _val { + return _.Record( + $compound, + [fromConstructorSpec(_v["ctor"]), fromPCompoundMembers(_v["members"])] ); } -export function asTemplate(v: any): Template { - if (!isTemplate(v)) {throw new TypeError(`Invalid Template: ${_.stringify(v)}`);} else {return v;}; +export function asPCompoundMembers(v: _val): PCompoundMembers { + let result = toPCompoundMembers(v); + if (result === void 0) throw new TypeError(`Invalid PCompoundMembers: ${_.stringify(v)}`); + return result; } -export function decodeTemplate(d: _.TypedDecoder<_ptr>): Template | undefined { - let result; - if (d.openRecord()) { - let _tmp80, _tmp81: any; - _tmp80 = d.next(); - _tmp81 = d.mark(); - if (_.is(_tmp80, $ref)) { - let _tmp82, _tmp83: any; - _tmp83 = d.nextSymbol(); - if (_tmp83 !== void 0) {if (d.closeCompound()) _tmp82 = [_tmp83];}; - if (_tmp82 !== void 0) result = _.Record<(typeof $ref), [symbol]>(_tmp80 as any, _tmp82 as any); +export function toPCompoundMembers(v: _val): undefined | PCompoundMembers { + let _tmp0: (_.KeyedDictionary<_val, Pattern, _ptr>) | undefined; + let result: undefined | PCompoundMembers; + if (_.Dictionary.isDictionary<_ptr>(v)) { + _tmp0 = new _.KeyedDictionary(); + for (const [_tmp2, _tmp1] of v) { + let _tmp3: (_val) | undefined; + _tmp3 = _tmp2; + if (_tmp3 !== void 0) { + let _tmp4: (Pattern) | undefined; + _tmp4 = toPattern(_tmp1); + if (_tmp4 !== void 0) {_tmp0.set(_tmp3, _tmp4); continue;}; + }; + _tmp0 = void 0; + break; }; + if (_tmp0 !== void 0) {result = _tmp0;}; + }; + return result; +} + +export function fromPCompoundMembers(_v: PCompoundMembers): _val { + return new _.Dictionary<_ptr>(_.Array.from(_v.entries()).map(([k, v]) => [k, fromPattern(v)])); +} + +export function asTemplate(v: _val): Template { + let result = toTemplate(v); + if (result === void 0) throw new TypeError(`Invalid Template: ${_.stringify(v)}`); + return result; +} + +export function toTemplate(v: _val): undefined | Template { + let _tmp0: (TRef) | undefined; + let result: undefined | Template; + _tmp0 = toTRef(v); + if (_tmp0 !== void 0) {result = {"_variant": "TRef", "value": _tmp0};}; + if (result === void 0) { + let _tmp1: (Lit) | undefined; + _tmp1 = toLit(v); + if (_tmp1 !== void 0) {result = {"_variant": "Lit", "value": _tmp1};}; if (result === void 0) { - d.restoreMark(_tmp81); - if (_.is(_tmp80, $lit)) { - let _tmp84, _tmp85: any; - _tmp85 = d.next(); - if (_tmp85 !== void 0) {if (d.closeCompound()) _tmp84 = [_tmp85];}; - if (_tmp84 !== void 0) result = _.Record<(typeof $lit), [_val]>(_tmp80 as any, _tmp84 as any); - }; - if (result === void 0) { - d.restoreMark(_tmp81); - if (_.is(_tmp80, $compound)) { - let _tmp86, _tmp87, _tmp88: any; - _tmp87 = decodeConstructorSpec(d); - if (_tmp87 !== void 0) { - if (d.openDictionary()) { - let r: _.KeyedDictionary<_val, Template, _ptr> | undefined = new _.KeyedDictionary(); - while (!d.closeCompound()) { - let K: undefined | _val = void 0; - K = d.next(); - if (K === void 0) { r = void 0; break; }; - let V: undefined | Template = void 0; - V = decodeTemplate(d); - if (V === void 0) { r = void 0; break; }; - r.set(K, V); - }; - _tmp88 = r; - }; - if (_tmp88 !== void 0) {if (d.closeCompound()) _tmp86 = [_tmp87, _tmp88];}; - }; - if (_tmp86 !== void 0) result = _.Record<(typeof $compound), [ConstructorSpec, _.KeyedDictionary<_val, Template, _ptr>]>(_tmp80 as any, _tmp86 as any); - }; + let _tmp2: (TCompound) | undefined; + _tmp2 = toTCompound(v); + if (_tmp2 !== void 0) {result = {"_variant": "TCompound", "value": _tmp2};}; + }; + }; + return result; +} + +export function fromTemplate(_v: Template): _val { + switch (_v._variant) { + case "TRef": {return fromTRef(_v.value);}; + case "Lit": {return fromLit(_v.value);}; + case "TCompound": {return fromTCompound(_v.value);}; + }; +} + +export function asTRef(v: _val): TRef { + let result = toTRef(v); + if (result === void 0) throw new TypeError(`Invalid TRef: ${_.stringify(v)}`); + return result; +} + +export function toTRef(v: _val): undefined | TRef { + let result: undefined | TRef; + if (_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v)) { + let _tmp0: (null) | undefined; + _tmp0 = _.is(v.label, $ref) ? null : void 0; + if (_tmp0 !== void 0) { + let _tmp1: (symbol) | undefined; + _tmp1 = typeof v[0] === 'symbol' ? v[0] : void 0; + if (_tmp1 !== void 0) {result = {"name": _tmp1};}; + }; + }; + return result; +} + +export function fromTRef(_v: TRef): _val {return _.Record($ref, [_v["name"]]);} + +export function asTCompound(v: _val): TCompound { + let result = toTCompound(v); + if (result === void 0) throw new TypeError(`Invalid TCompound: ${_.stringify(v)}`); + return result; +} + +export function toTCompound(v: _val): undefined | TCompound { + let result: undefined | TCompound; + if (_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v)) { + let _tmp0: (null) | undefined; + _tmp0 = _.is(v.label, $compound) ? null : void 0; + if (_tmp0 !== void 0) { + let _tmp1: (ConstructorSpec) | undefined; + _tmp1 = toConstructorSpec(v[0]); + if (_tmp1 !== void 0) { + let _tmp2: (TCompoundMembers) | undefined; + _tmp2 = toTCompoundMembers(v[1]); + if (_tmp2 !== void 0) {result = {"ctor": _tmp1, "members": _tmp2};}; }; }; }; return result; } -export function isTRef(v: any): v is TRef { - return ( - _.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) && - _.is(v.label, $ref) && - ((v.length === 1) && typeof v[0] === 'symbol') +export function fromTCompound(_v: TCompound): _val { + return _.Record( + $compound, + [fromConstructorSpec(_v["ctor"]), fromTCompoundMembers(_v["members"])] ); } -export function asTRef(v: any): TRef { - if (!isTRef(v)) {throw new TypeError(`Invalid TRef: ${_.stringify(v)}`);} else {return v;}; -} - -export function decodeTRef(d: _.TypedDecoder<_ptr>): TRef | undefined { - let result; - if (d.openRecord()) { - let _tmp89: any; - _tmp89 = _.asLiteral(d.nextSymbol(), $ref); - if (_tmp89 !== void 0) { - let _tmp90, _tmp91: any; - _tmp91 = d.nextSymbol(); - if (_tmp91 !== void 0) {if (d.closeCompound()) _tmp90 = [_tmp91];}; - if (_tmp90 !== void 0) result = _.Record<(typeof $ref), [symbol]>(_tmp89 as any, _tmp90 as any); - }; - }; +export function asTCompoundMembers(v: _val): TCompoundMembers { + let result = toTCompoundMembers(v); + if (result === void 0) throw new TypeError(`Invalid TCompoundMembers: ${_.stringify(v)}`); return result; } -export function isTCompound(v: any): v is TCompound { - return ( - _.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) && - _.is(v.label, $compound) && - ( - (v.length === 2) && - isConstructorSpec(v[0]) && - ( - _.Dictionary.isDictionary<_val, _ptr>(v[1]) && - ((() => { - for (const e of v[1]) {if (!(true)) return false; if (!(isTemplate(e[1]))) return false;}; - return true; - })()) - ) - ) - ); -} - -export function asTCompound(v: any): TCompound { - if (!isTCompound(v)) {throw new TypeError(`Invalid TCompound: ${_.stringify(v)}`);} else {return v;}; -} - -export function decodeTCompound(d: _.TypedDecoder<_ptr>): TCompound | undefined { - let result; - if (d.openRecord()) { - let _tmp92: any; - _tmp92 = _.asLiteral(d.nextSymbol(), $compound); - if (_tmp92 !== void 0) { - let _tmp93, _tmp94, _tmp95: any; - _tmp94 = decodeConstructorSpec(d); - if (_tmp94 !== void 0) { - if (d.openDictionary()) { - let r: _.KeyedDictionary<_val, Template, _ptr> | undefined = new _.KeyedDictionary(); - while (!d.closeCompound()) { - let K: undefined | _val = void 0; - K = d.next(); - if (K === void 0) { r = void 0; break; }; - let V: undefined | Template = void 0; - V = decodeTemplate(d); - if (V === void 0) { r = void 0; break; }; - r.set(K, V); - }; - _tmp95 = r; - }; - if (_tmp95 !== void 0) {if (d.closeCompound()) _tmp93 = [_tmp94, _tmp95];}; +export function toTCompoundMembers(v: _val): undefined | TCompoundMembers { + let _tmp0: (_.KeyedDictionary<_val, Template, _ptr>) | undefined; + let result: undefined | TCompoundMembers; + if (_.Dictionary.isDictionary<_ptr>(v)) { + _tmp0 = new _.KeyedDictionary(); + for (const [_tmp2, _tmp1] of v) { + let _tmp3: (_val) | undefined; + _tmp3 = _tmp2; + if (_tmp3 !== void 0) { + let _tmp4: (Template) | undefined; + _tmp4 = toTemplate(_tmp1); + if (_tmp4 !== void 0) {_tmp0.set(_tmp3, _tmp4); continue;}; }; - if (_tmp93 !== void 0) result = _.Record<(typeof $compound), [ConstructorSpec, _.KeyedDictionary<_val, Template, _ptr>]>(_tmp92 as any, _tmp93 as any); + _tmp0 = void 0; + break; }; + if (_tmp0 !== void 0) {result = _tmp0;}; }; return result; } -export const _decodePtr = (d: _.TypedDecoder<_ptr>) => {let result; result = _i_Actor.decodeRef(d); return result;}; +export function fromTCompoundMembers(_v: TCompoundMembers): _val { + return new _.Dictionary<_ptr>(_.Array.from(_v.entries()).map(([k, v]) => [k, fromTemplate(v)])); +} diff --git a/src/gen/worker.ts b/src/gen/worker.ts index 92f2a43..c378db3 100644 --- a/src/gen/worker.ts +++ b/src/gen/worker.ts @@ -2,44 +2,40 @@ import * as _ from "@preserves/core"; export const $Instance = Symbol.for("Instance"); -export const Instance = _.Record.makeConstructor<{"_field0": string, "_field1": _val}, _ptr>()($Instance, ["_field0","_field1"]); - -export type Instance = _.Record<(typeof $Instance), [string, _val], _ptr>; - export type _ptr = never; export type _val = _.Value<_ptr>; +export type Instance = {"name": string, "argument": _val}; -export function isInstance(v: any): v is Instance { - return ( - _.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) && - _.is(v.label, $Instance) && - ((v.length === 2) && typeof v[0] === 'string' && true) - ); + +export const _toPtr = () => { throw new _.DecodeError("Pointers forbidden"); }; + +export function Instance(name: string, argument: _val): Instance {return {name, argument};} + +export function asInstance(v: _val): Instance { + let result = toInstance(v); + if (result === void 0) throw new TypeError(`Invalid Instance: ${_.stringify(v)}`); + return result; } -export function asInstance(v: any): Instance { - if (!isInstance(v)) {throw new TypeError(`Invalid Instance: ${_.stringify(v)}`);} else {return v;}; -} - -export function decodeInstance(d: _.TypedDecoder<_ptr>): Instance | undefined { - let result; - if (d.openRecord()) { - let _tmp0: any; - _tmp0 = _.asLiteral(d.nextSymbol(), $Instance); +export function toInstance(v: _val): undefined | Instance { + let result: undefined | Instance; + if (_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v)) { + let _tmp0: (null) | undefined; + _tmp0 = _.is(v.label, $Instance) ? null : void 0; if (_tmp0 !== void 0) { - let _tmp1, _tmp2, _tmp3: any; - _tmp2 = d.nextString(); - if (_tmp2 !== void 0) { - _tmp3 = d.next(); - if (_tmp3 !== void 0) {if (d.closeCompound()) _tmp1 = [_tmp2, _tmp3];}; + let _tmp1: (string) | undefined; + _tmp1 = typeof v[0] === 'string' ? v[0] : void 0; + if (_tmp1 !== void 0) { + let _tmp2: (_val) | undefined; + _tmp2 = v[1]; + if (_tmp2 !== void 0) {result = {"name": _tmp1, "argument": _tmp2};}; }; - if (_tmp1 !== void 0) result = _.Record<(typeof $Instance), [string, _val]>(_tmp0 as any, _tmp1 as any); }; }; return result; } -export const _decodePtr = () => { throw new _.DecodeError("Pointers forbidden"); }; +export function fromInstance(_v: Instance): _val {return _.Record($Instance, [_v["name"], _v["argument"]]);} diff --git a/src/main.ts b/src/main.ts index 6a2f70c..655dd56 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,12 +1,13 @@ import { Actor, Assertion, Ref, Turn } from './actor.js'; import { Dictionary, Record } from '@preserves/core'; -import { Dataspace, Observe } from './dataspace.js'; +import { Dataspace } from './dataspace.js'; import { Worker } from 'worker_threads'; import { Relay, spawnRelay } from './relay.js'; -import { attenuate, CRec, Lit, Pattern, PCompound, rfilter } from './rewrite.js'; import path from 'path'; -import { BoxState, SetBox } from './gen/box-protocol.js'; +import { attenuate, CRec, Lit, Pattern, PCompound, rfilter, ConstructorSpec } from './rewrite.js'; +import { $BoxState, $SetBox } from './gen/box-protocol.js'; +import { $Observe } from './gen/dataspace.js'; const Instance = Record.makeConstructor<{moduleName: string, arg: Assertion}>()( Symbol.for('Instance'), ['moduleName', 'arg']); @@ -56,30 +57,25 @@ Turn.for(new Actor(), async (t: Turn) => { const ds_for_box = attenuate( ds, - rfilter(PCompound(CRec(BoxState.constructorInfo.label, - BoxState.constructorInfo.arity), - new Dictionary()), - PCompound(CRec(Observe.constructorInfo.label, - Observe.constructorInfo.arity), - new Dictionary([ - [0, Lit(SetBox.constructorInfo.label)]])))); + rfilter( + Pattern.PCompound(PCompound(ConstructorSpec.CRec(CRec($BoxState, 1)), new Dictionary())), + Pattern.PCompound(PCompound(ConstructorSpec.CRec(CRec($Observe, 2)), + new Dictionary([ + [0, Pattern.Lit(Lit($SetBox))]]))))); const ds_for_client = attenuate( ds_unproxied, - rfilter(PCompound(CRec(SetBox.constructorInfo.label, - SetBox.constructorInfo.arity), - new Dictionary()), - PCompound(CRec(Observe.constructorInfo.label, - Observe.constructorInfo.arity), - new Dictionary([ - [0, Lit(BoxState.constructorInfo.label)]])))); - + rfilter( + Pattern.PCompound(PCompound(ConstructorSpec.CRec(CRec($SetBox, 1)), new Dictionary())), + Pattern.PCompound(PCompound(ConstructorSpec.CRec(CRec($Observe, 2)), + new Dictionary([ + [0, Pattern.Lit(Lit($BoxState))]]))))); const boxpath = path.join(__dirname, 'box.js'); const clientpath = path.join(__dirname, 'client.js'); - // spawnModule(t, boxpath, [ds_for_box, 500000, 25000]); - spawnWorker(t, boxpath, [ds_for_box, 50000, 2500]); + spawnModule(t, boxpath, [ds_for_box, 500000, 25000]); + // spawnWorker(t, boxpath, [ds_for_box, 50000, 2500]); spawnModule(t, clientpath, ds_for_client); // spawnWorker(t, clientpath, ds_for_client); diff --git a/src/protocol.ts b/src/protocol.ts index b0281df..2d81a30 100644 --- a/src/protocol.ts +++ b/src/protocol.ts @@ -1,7 +1,8 @@ -import { Attenuation, decodeAttenuation } from './gen/sturdy.js'; +import { Attenuation, toAttenuation } from './gen/sturdy.js'; import * as IO from './gen/protocol.js'; -import { decodeRef, Ref } from './actor.js'; -import { TypedDecoder } from '@preserves/core'; +import { Assertion, Ref } from './actor.js'; +import { mapPointers } from '@preserves/core'; +import { pointerNotAllowed } from './sturdy.js'; export type WireSymbol = { oid: IO.Oid, ref: Ref, count: number }; @@ -17,27 +18,19 @@ export function yourRef(oid: IO.Oid, attenuation: Attenuation): WireRef & { loc: return { loc: 'your', oid, attenuation }; } -export function decodeWireRef(d: TypedDecoder): WireRef | undefined { - if (d.openSequence()) { - switch (d.nextSignedInteger()) { +export function toWireRef(v: IO._val): WireRef | undefined { + if (Array.isArray(v) && v.length >= 2) { + switch (v[0]) { case 0: { - const oid = d.nextSignedInteger(); - if (oid !== void 0) { - if (d.closeCompound()) { - return myRef(oid); - } - } + const oid = v[1]; + if (typeof oid === 'number') return myRef(oid); break; } case 1: { - const oid = d.nextSignedInteger(); - if (oid !== void 0) { - const attenuation = d.withPointerDecoder(decodeRef, d => decodeAttenuation(d)); - if (attenuation !== void 0) { - if (d.closeCompound()) { - return yourRef(oid, attenuation); - } - } + const oid = v[1]; + if (typeof oid === 'number') { + const attenuation = toAttenuation(mapPointers(v.slice(2), pointerNotAllowed) as Array); + if (attenuation !== void 0) return yourRef(oid, attenuation); } break; } diff --git a/src/relay.ts b/src/relay.ts index 6aef43a..42f3232 100644 --- a/src/relay.ts +++ b/src/relay.ts @@ -1,10 +1,10 @@ import { Actor, Assertion, Entity, Handle, Ref, Turn } from './actor.js'; import { BytesLike, canonicalString, Decoder, encode, FlexMap, IdentityMap, mapPointers, underlying, Value } from '@preserves/core'; import * as IO from './gen/protocol.js'; -import { decodeWireRef, myRef, WireRef, WireSymbol, yourRef } from './protocol.js'; +import { myRef, toWireRef, WireRef, WireSymbol, yourRef } from './protocol.js'; import { queueTask } from './task.js'; import { attenuate } from './rewrite.js'; -import { Attenuation } from './gen/sturdy.js'; +import { Attenuation, fromAttenuation } from './gen/sturdy.js'; import { pointerNotAllowed } from './sturdy.js'; export class SyncPeerEntity implements Entity { @@ -52,16 +52,16 @@ export class RelayEntity implements Entity { } assert(_turn: Turn, assertion: Assertion, handle: Handle): void { - this.send(IO.Assert(this.relay.register(assertion, handle), handle)); + this.send(IO.Event.Assert(IO.Assert(this.relay.register(assertion, handle), handle))) } retract(_turn: Turn, handle: Handle): void { this.relay.deregister(handle); - this.send(IO.Retract(handle)); + this.send(IO.Event.Retract(IO.Retract(handle))); } message(_turn: Turn, body: Assertion): void { - this.send(IO.Message(this.relay.register(body, null))); + this.send(IO.Event.Message(IO.Message(this.relay.register(body, null)))); } sync(turn: Turn, peer: Ref): void { @@ -69,7 +69,7 @@ export class RelayEntity implements Entity { const exported: Array = []; const ior = this.relay.rewriteRefOut(turn.ref(peerEntity), false, exported); peerEntity.e = exported[0]; - this.send(IO.Sync(ior)); + this.send(IO.Event.Sync(IO.Sync(ior))); } } @@ -135,7 +135,7 @@ export class Relay { trustPeer: boolean; readonly decoder = new Decoder(void 0, { includeAnnotations: false }) - .replacePointerDecoder(decodeWireRef); + .replacePointerDecoder(d => toWireRef(d.next())); constructor(t: Turn, options: RelayOptions) { this.actor = t.actor; @@ -227,20 +227,20 @@ export class Relay { if (this.pendingTurn.length === 0) { queueTask(() => { if (this.debug) console.log('OUT', this.pendingTurn.asPreservesText()); - this.w(underlying(encode(this.pendingTurn, { + this.w(underlying(encode(IO.fromTurn(this.pendingTurn), { canonical: true, encodePointer: (e, n) => { switch (n.loc) { case 'mine': return e.push([0, n.oid]); case 'your': return e.push([1, n.oid, ... mapPointers( - n.attenuation, pointerNotAllowed) as Array]); + fromAttenuation(n.attenuation), pointerNotAllowed) as Array]); } }, }))); this.pendingTurn = []; }); } - this.pendingTurn.push([remoteOid, m]); + this.pendingTurn.push(IO.TurnEvent(remoteOid, m)); } lookupLocal(localOid: IO.Oid): Ref { @@ -251,14 +251,13 @@ export class Relay { Turn.for(this.actor, t => { this.decoder.write(bs); while (true) { - const wireTurn = this.decoder.shortGuard( - () => IO.decodeTurn(this.decoder) ?? false, - () => void 0); - if (wireTurn === void 0) break; - if (wireTurn === false) throw new Error("Bad IO.Turn"); + const rawTurn = this.decoder.try_next(); + if (rawTurn === void 0) break; + const wireTurn = IO.toTurn(rawTurn); + if (wireTurn === void 0) throw new Error("Bad IO.Turn"); if (this.debug) console.log('IN', wireTurn.asPreservesText()); wireTurn.forEach(v => { - const [localOid, m] = v; + const { oid: localOid, event: m } = v; this.handle(t, this.lookupLocal(localOid), m); }); } @@ -266,17 +265,17 @@ export class Relay { } handle(t: Turn, r: Ref, m: IO.Event) { - switch (m.label) { - case IO.$assert: { - const [a, imported] = this.rewriteIn(t, IO.Assert._.assertion(m)); - this.inboundAssertions.set(IO.Assert._.handle(m), { + switch (m._variant) { + case 'Assert': { + const [a, imported] = this.rewriteIn(t, m.value.assertion); + this.inboundAssertions.set(m.value.handle, { localHandle: t.assert(r, a), imported, }); break; } - case IO.$retract: { - const remoteHandle = IO.Retract._.handle(m); + case 'Retract': { + const remoteHandle = m.value.handle; const h = this.inboundAssertions.get(remoteHandle); if (h === void 0) throw new Error(`Peer retracted invalid handle ${remoteHandle}`); this.inboundAssertions.delete(remoteHandle); @@ -284,15 +283,15 @@ export class Relay { t.retract(h.localHandle); break; } - case IO.$message: { - const [a, imported] = this.rewriteIn(t, IO.Message._.body(m)); + case 'Message': { + const [a, imported] = this.rewriteIn(t, m.value.body); if (imported.length > 0) throw new Error("Cannot receive transient reference"); t.message(r, a); break; } - case IO.$sync: { + case 'Sync': { const imported: Array = []; - const k = this.rewriteRefIn(t, IO.Sync._.peer(m), imported); + const k = this.rewriteRefIn(t, m.value.peer, imported); t.sync(r).then(t => { t.message(k, true); imported.forEach(e => this.imported.drop(e)); diff --git a/src/rewrite.ts b/src/rewrite.ts index e5680e1..01f4b98 100644 --- a/src/rewrite.ts +++ b/src/rewrite.ts @@ -1,34 +1,7 @@ import type { Assertion, Handle, Ref, Turn } from "./actor.js"; import { Dictionary, IdentityMap, is, Record, Tuple } from "@preserves/core"; -import { - $__, - $and, - $arr, - $bind, - $compound, - $dict, - $lit, - $not, - $or, - $rec, - $ref, - Alts, - Attenuation, - CArr, - CRec, - Caveat, - Lit, - PAnd, - PBind, - PCompound, - PNot, - Pattern, - Rewrite, - TCompound, - TRef, - Template, -} from './gen/sturdy.js'; +import { Alts, Attenuation, Caveat, PBind, Pattern, Rewrite, TRef, Template } from './gen/sturdy.js'; export * from './gen/sturdy.js'; export type Bindings = { [name: string]: Assertion }; @@ -37,52 +10,52 @@ export function match(p: Pattern, v: Assertion): Bindings | null { let bindings: Bindings = {}; function walk(p: Pattern, v: Assertion): boolean { - switch (p.label) { - case $__: + switch (p._variant) { + case 'PDiscard': return true; - case $bind: - if (walk(PBind._.pattern(p), v)) { - bindings[PBind._.name(p).asPreservesText()] = v; + case 'PBind': + if (walk(p.value.pattern, v)) { + bindings[p.value.name.asPreservesText()] = v; return true; } return false; - case $and: - for (const pp of PAnd._.patterns(p)) { + case 'PAnd': + for (const pp of p.value.patterns) { if (!walk(pp, v)) return false; } return true; - case $not: { + case 'PNot': { const savedBindings = bindings; bindings = {}; - const result = !walk(PNot._.pattern(p), v) + const result = !walk(p.value.pattern, v) bindings = savedBindings; return result; } - case $lit: - return is(Lit._.value(p), v); - case $compound: { - const ctor = PCompound._.ctor(p); - const members = PCompound._.members(p); - switch (ctor.label) { - case $rec: + case 'Lit': + return is(p.value.value, v); + case 'PCompound': { + const ctor = p.value.ctor; + const members = p.value.members; + switch (ctor._variant) { + case 'CRec': if (!Record.isRecord, Ref>(v)) return false; - if (!is(CRec._.label(ctor), v.label)) return false; - if (CRec._.arity(ctor) !== v.length) return false; + if (!is(ctor.value.label, v.label)) return false; + if (ctor.value.arity !== v.length) return false; for (const [key, pp] of members) { if (typeof key !== 'number') return false; if (!walk(pp, v[key])) return false; } return true; - case $arr: + case 'CArr': if (!Array.isArray(v)) return false; if ('label' in v) return false; - if (CArr._.arity(ctor) !== v.length) return false; + if (ctor.value.arity !== v.length) return false; for (const [key, pp] of members) { if (typeof key !== 'number') return false; if (!walk(pp, v[key])) return false; } return true; - case $dict: + case 'CDict': if (!Dictionary.isDictionary(v)) return false; for (const [key, pp] of members) { const vv = v.get(key as Assertion); @@ -100,40 +73,38 @@ export function match(p: Pattern, v: Assertion): Bindings | null { export function instantiate(t: Template, b: Bindings): Assertion { function walk(t: Template): Assertion { - switch (t.label) { - case $ref: { - const n = TRef._.name(t).asPreservesText() + switch (t._variant) { + case 'TRef': { + const n = t.value.name.asPreservesText() const v = b[n]; if (v === void 0) throw new Error(`Unbound reference: ${n}`); return v; } - case $lit: - return Lit._.value(t) as Assertion; - case $compound: { - const ctor = TCompound._.ctor(t); - const members = TCompound._.members(t); - switch (ctor.label) { - case $rec: { - const v = Record( - CRec._.label(ctor) as Assertion, - [] as Assertion[], - ); - v.length = CRec._.arity(ctor); + case 'Lit': + return t.value.value as Assertion; + case 'TCompound': { + const ctor = t.value.ctor; + const members = t.value.members; + switch (ctor._variant) { + case 'CRec': { + const v = [] as unknown as Record; + v.length = ctor.value.arity; + v.label = ctor.value.label; for (const [key, tt] of members) { v[key as number] = walk(tt); } return v; } - case $arr: { + case 'CArr': { const v = []; - v.length = CArr._.arity(ctor); + v.length = ctor.value.arity; for (const [key, tt] of members) { v[key as number] = walk(tt); } return v; } - case $dict: { - const v = new Dictionary(); + case 'CDict': { + const v = new Dictionary(); for (const [key, tt] of members) { v.set(key as Assertion, walk(tt)); } @@ -148,20 +119,20 @@ export function instantiate(t: Template, b: Bindings): Assertion { } export function rewrite(r: Rewrite, v: Assertion): Assertion | null { - const bindings = match(Rewrite._.pattern(r), v); + const bindings = match(r.pattern, v); if (bindings === null) return null; - return instantiate(Rewrite._.template(r), bindings); + return instantiate(r.template, bindings); } export function examineAlternatives(cav: Caveat, v: Assertion): Assertion | null { - if (cav.label === $or) { - for (const r of Alts._.alternatives(cav)) { + if (cav._variant === 'Alts') { + for (const r of cav.value.alternatives) { const w = rewrite(r, v); if (w !== null) return w; } return null; } else { - return rewrite(cav, v); + return rewrite(cav.value, v); } } @@ -179,8 +150,8 @@ export function runRewrites(a: Attenuation | undefined, v: Assertion): Assertion const _a = Symbol.for('a'); export function rfilter(... patterns: Pattern[]): Caveat { - const ps = patterns.map(p => Rewrite(PBind(_a, p), TRef(_a))); - return ps.length === 1 ? ps[0] : Alts(ps); + const ps = patterns.map(p => Rewrite(Pattern.PBind(PBind(_a, p)), Template.TRef(TRef(_a)))); + return ps.length === 1 ? Caveat.Rewrite(ps[0]) : Caveat.Alts(Alts(ps)); } export function attenuate(ref: Ref, ... a: Attenuation): Ref { diff --git a/src/sandbox.ts b/src/sandbox.ts index a3dd7b5..51035b4 100644 --- a/src/sandbox.ts +++ b/src/sandbox.ts @@ -1,7 +1,7 @@ import { Actor, Ref, Turn } from "./actor"; import { Relay, spawnRelay } from "./relay"; import { sturdyDecode } from "./sturdy"; -import { Resolve, asSturdyRef } from "./gen/sturdy"; +import { Resolve, asSturdyRef, fromResolve } from "./gen/sturdy"; import * as net from 'net'; import { Bytes } from "@preserves/core"; @@ -29,11 +29,11 @@ const socket = net.createConnection({ port: 5999, host: 'localhost' }, () => { // debug: true, }).then(gatekeeper => import(moduleName).then(m => t.freshen(t => { t.assert(shutdownRef, true); - t.assert(gatekeeper, Resolve(asSturdyRef(cap), t.ref({ + t.assert(gatekeeper, fromResolve(Resolve(asSturdyRef(cap), t.ref({ assert(t, ds) { m.default(t, ds); } - }))); + })))); }))); }); }); diff --git a/src/server.ts b/src/server.ts index fa5e982..119c05e 100644 --- a/src/server.ts +++ b/src/server.ts @@ -3,16 +3,16 @@ import { Dataspace } from './dataspace.js'; import { Relay, spawnRelay } from './relay.js'; import * as net from 'net'; -import { mint, sturdyEncode, SturdyRef, validate, Resolve } from './sturdy.js'; +import { mint, sturdyEncode, validate } from './sturdy.js'; import { KEY_LENGTH } from './cryptography.js'; import { attenuate } from './rewrite.js'; import { Bytes, IdentityMap } from '@preserves/core'; -import { Attenuation, isResolve } from './gen/sturdy.js'; +import { Attenuation, fromSturdyRef, toResolve } from './gen/sturdy.js'; const secretKey = new Bytes(KEY_LENGTH); mint('syndicate', secretKey).then(v => { console.log(v.asPreservesText()); - console.log(sturdyEncode(v).toHex()); + console.log(sturdyEncode(fromSturdyRef(v)).toHex()); }); Turn.for(new Actor(), t => { @@ -32,19 +32,20 @@ Turn.for(new Actor(), t => { }, initialRef: t.ref({ handleMap: new IdentityMap(), - async assert(t, a, h) { - if (!isResolve(a)) return; - const r = Resolve._.sturdyref(a); + async assert(t, a0, h) { + const a = toResolve(a0); + if (a === void 0) return; + const r = a.sturdyref; if (!await validate(r, secretKey)) { console.warn(`Invalid SturdyRef: ${r.asPreservesText()}`); return; } const cavs: Attenuation = []; - SturdyRef._.caveatChain(r).forEach(cs => cavs.push(... cs)); + r.caveatChain.forEach(cs => cavs.push(... cs)); const attenuated_ds = attenuate(ds, ... cavs); t.freshen(t => this.handleMap.set( h, - t.assert(Resolve._.observer(a), attenuated_ds))); + t.assert(a.observer, attenuated_ds))); }, retract(t, h) { t.retract(this.handleMap.get(h)); diff --git a/src/sturdy-demo.ts b/src/sturdy-demo.ts index 00b3f61..6e3f9c8 100644 --- a/src/sturdy-demo.ts +++ b/src/sturdy-demo.ts @@ -1,32 +1,34 @@ import { newKey } from './cryptography.js'; -import { attenuate, KEY_LENGTH, mint, Rewrite, sturdyEncode, validate } from './sturdy.js'; +import { attenuate, KEY_LENGTH, mint, Caveat, Rewrite, sturdyEncode, validate } from './sturdy.js'; import * as RW from './rewrite.js'; import { Bytes, Dictionary } from '@preserves/core'; import { Ref } from 'actor.js'; async function main() { const m1 = await mint('hello world', new Bytes(KEY_LENGTH)); - console.log(m1.asPreservesText()); - const m2 = await attenuate(m1, Rewrite( - RW.PBind(Symbol.for('a'), - RW.PCompound(RW.CRec(Symbol.for('says'), 2), - new Dictionary([ - [0, RW.Lit('Tony')]]))), - RW.TRef(Symbol.for('a')))); - console.log(m2.asPreservesText()); + console.log(m1); + const m2 = await attenuate(m1, Caveat.Rewrite(Rewrite( + RW.Pattern.PBind(RW.PBind( + Symbol.for('a'), + RW.Pattern.PCompound(RW.PCompound( + RW.ConstructorSpec.CRec(RW.CRec(Symbol.for('says'), 2)), + new Dictionary([ + [0, RW.Pattern.Lit(RW.Lit('Tony'))]]))))), + RW.Template.TRef(RW.TRef(Symbol.for('a')))))); + console.log(m2); console.log('should be true:', await validate(m1, new Bytes(KEY_LENGTH))); console.log('should be true:', await validate(m2, new Bytes(KEY_LENGTH))); console.log('should be false:', await validate(m2, Bytes.of(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1))); - m2[0] = 'hello world2'; - console.log(m2.asPreservesText()); + m2.oid = 'hello world2'; + console.log(m2); console.log('should be false:', await validate(m2, new Bytes(KEY_LENGTH))); - m2[0] = 'hello world'; - console.log(m2.asPreservesText()); + m2.oid = 'hello world'; + console.log(m2); console.log('should be true:', await validate(m2, new Bytes(KEY_LENGTH))); console.log('should be false:', await validate(m2, await newKey())); console.log((await newKey()).asPreservesText()); console.log((await newKey()).asPreservesText()); - console.log(sturdyEncode(m2).asPreservesText()); + console.log(sturdyEncode(RW.fromSturdyRef(m2)).asPreservesText()); } main(); diff --git a/src/sturdy.ts b/src/sturdy.ts index e42132e..16eaca7 100644 --- a/src/sturdy.ts +++ b/src/sturdy.ts @@ -40,15 +40,15 @@ export async function mint(oid: SturdyValue, secretKey: Bytes): Promise { return S.SturdyRef( - S.SturdyRef._.oid(r), - [... S.SturdyRef._.caveatChain(r), a], - await mac(S.SturdyRef._.sig(r), sturdyEncode(a)) + r.oid, + [... r.caveatChain, a], + await mac(r.sig, sturdyEncode(S.fromAttenuation(a))) ); } export async function validate(r: S.SturdyRef, secretKey: Bytes): Promise { - const sig = await S.SturdyRef._.caveatChain(r).reduce( - async (sig, a) => mac(await sig, sturdyEncode(a)), - mac(secretKey, sturdyEncode(S.SturdyRef._.oid(r)))); - return is(sig, S.SturdyRef._.sig(r)); + const sig = await r.caveatChain.reduce( + async (sig, a) => mac(await sig, sturdyEncode(S.fromAttenuation(a))), + mac(secretKey, sturdyEncode(r.oid))); + return is(sig, r.sig); } diff --git a/src/tools/attenuate.ts b/src/tools/attenuate.ts index 952e585..f98f75b 100644 --- a/src/tools/attenuate.ts +++ b/src/tools/attenuate.ts @@ -1,10 +1,13 @@ import { Bytes, Reader } from '@preserves/core'; -import { attenuate, Caveat, sturdyDecode, sturdyEncode, SturdyRef } from '../sturdy.js'; +import { fromSturdyRef, toSturdyRef, toCaveat, attenuate, sturdyDecode, sturdyEncode } from '../sturdy.js'; const [ base, pat ] = process.argv.slice(2); -const baseCap = sturdyDecode(Bytes.fromHex(base ?? '')) as SturdyRef; -const cs = new Reader(pat).next() as Array; +const baseCap = toSturdyRef(sturdyDecode(Bytes.fromHex(base ?? ''))); +if (baseCap === void 0) throw new Error("Cannot decode sturdyref"); +const cs0 = new Reader(pat).next(); +if (!Array.isArray(cs0)) throw new Error("Expected array of caveats"); +const cs = cs0.map(c => toCaveat(c) ?? (()=>{ throw new Error("Cannot decode caveat"); })()); attenuate(baseCap, ... cs).then(derived => { console.log(derived.asPreservesText()); - console.log(sturdyEncode(derived).toHex()); + console.log(sturdyEncode(fromSturdyRef(derived)).toHex()); });