Browse Source

Move to autogenerated schemas

main
Tony Garnock-Jones 7 months ago
parent
commit
f7399cb858
  1. 2
      .gitignore
  2. 2
      package.json
  3. 4
      rollup.config.js
  4. 8
      schemas/box-protocol.prs
  5. 8
      schemas/dataspace.prs
  6. 20
      schemas/rewrite.prs
  7. 17
      schemas/wire.prs
  8. 3
      schemas/worker.prs
  9. 10
      src/box-protocol.ts
  10. 2
      src/box.ts
  11. 2
      src/client.ts
  12. 78
      src/gen/box-protocol.ts
  13. 25
      src/gen/dataspace.ts
  14. 227
      src/gen/rewrite.ts
  15. 177
      src/gen/wire.ts
  16. 25
      src/gen/worker.ts
  17. 2
      src/main.ts

2
.gitignore

@ -1,4 +1,4 @@
package-lock.json
node_modules/
lib/
index.js
yarn-error.log

2
package.json

@ -10,6 +10,8 @@
"@preserves/schema": "^0.0.0"
},
"scripts": {
"regenerate": "rm -rf ./src/gen && preserves-schema-ts --module Pointer=./src/actor.ts --output ./src/gen ./schemas/**/*.prs",
"regenerate:watch": "yarn regenerate --watch",
"compile": "tsc",
"compile:watch": "tsc -w",
"rollup": "rollup -c",

4
rollup.config.js

@ -5,9 +5,9 @@ export default {
format: 'umd',
name: 'Main',
globals: {
'preserves': 'Preserves',
'@preserves/core': 'Preserves',
'crypto': 'crypto', // this is a lie, but it lets the detection code in cryptography.ts run
},
},
external: ['preserves', 'crypto'],
external: ['@preserves/core', 'crypto'],
};

8
schemas/box-protocol.prs

@ -0,0 +1,8 @@
version 1 .
pointer Pointer.Ref .
BoxState = <BoxState int>.
SetBox = <SetBox int>.
BoxCap = BoxState / <Observe =SetBox ref>.
ClientCap = SetBox / <Observe =BoxState ref>.

8
schemas/dataspace.prs

@ -0,0 +1,8 @@
version 1 .
;As implemented
Observe = <Observe symbol ref>.
; ;As will be implemented soon
; Observe = <Observe Pattern ref>.
.

20
schemas/rewrite.prs

@ -0,0 +1,20 @@
version 1 .
ConstructorSpec = <rec any int> / <arr int> / <dict>.
Pattern = </[
<_>
<bind symbol Pattern>
<and [Pattern ...]>
<not Pattern>
<lit any>
<compound ConstructorSpec { any: Pattern ...:... }>
]>.
Template = </[
<ref symbol>
<lit any>
<compound ConstructorSpec { any: Template ...:... }>
]>.
Rewrite = <rewrite Pattern Template>.

17
schemas/wire.prs

@ -0,0 +1,17 @@
version 1 .
SturdyRef = <ref any [Caveat ...] bytes>.
Caveat = rewrite.Rewrite / <or [rewrite.Rewrite ...]>.
Handle = int .
Event = Assert / Retract / Message / Sync .
Oid = int .
Turn = [[Oid Event] ...].
Assert = <assert @assertion any @handle Handle>.
Retract = <retract @handle Handle>.
Message = <message @body any>.
Sync = <sync @peer ref>.
; WireRef = [0 Oid] / [1 Oid AttenuationStage ...].
.

3
schemas/worker.prs

@ -0,0 +1,3 @@
version 1 .
Instance = <Instance string any>.

10
src/box-protocol.ts

@ -1,10 +0,0 @@
import { Ref } from "actor";
import { Record } from "@preserves/core";
export const _BoxState = Symbol.for('BoxState');
export const BoxState = Record.makeConstructor<{value: number}, Ref>()(
_BoxState, ['value']);
export const _SetBox = Symbol.for('SetBox');
export const SetBox = Record.makeConstructor<{newValue: number}, Ref>()(
_SetBox, ['newValue']);

2
src/box.ts

@ -1,4 +1,4 @@
import { BoxState, SetBox } from "./box-protocol.js";
import { BoxState, SetBox } from "./gen/box-protocol.js";
import { Assertion, Handle, Ref, Turn } from "./actor.js";
import { Observe } from "./dataspace.js";

2
src/client.ts

@ -1,4 +1,4 @@
import { BoxState, SetBox } from "./box-protocol.js";
import { BoxState, SetBox } from "./gen/box-protocol.js";
import { Observe } from "./dataspace.js";
import { Assertion, Ref, Turn } from "./actor.js";

78
src/gen/box-protocol.ts

@ -0,0 +1,78 @@
import * as _ from "@preserves/core";
import * as _i_Pointer from "../actor";
export const $BoxState = Symbol.for("BoxState");
export const $Observe = Symbol.for("Observe");
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 BoxCap = (BoxState | _.Record<(typeof $Observe), [(typeof $SetBox), _ptr], _ptr>);
export type ClientCap = (SetBox | _.Record<(typeof $Observe), [(typeof $BoxState), _ptr], _ptr>);
export type _ptr = _i_Pointer.Ref;
export type _val = _.Value<_ptr>;
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 function asBoxState(v: any): BoxState {
if (!isBoxState(v)) {throw new TypeError(`Invalid BoxState: ${_.stringify(v)}`);} else {return v;};
}
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 asSetBox(v: any): SetBox {
if (!isSetBox(v)) {throw new TypeError(`Invalid SetBox: ${_.stringify(v)}`);} else {return v;};
}
export function isBoxCap(v: any): v is BoxCap {
return (
isBoxState(v) ||
(
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
_.is(v.label, $Observe) &&
((v.length === 2) && _.is(v[0], $SetBox) && _.isPointer(v[1]))
)
);
}
export function asBoxCap(v: any): BoxCap {
if (!isBoxCap(v)) {throw new TypeError(`Invalid BoxCap: ${_.stringify(v)}`);} else {return v;};
}
export function isClientCap(v: any): v is ClientCap {
return (
isSetBox(v) ||
(
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
_.is(v.label, $Observe) &&
((v.length === 2) && _.is(v[0], $BoxState) && _.isPointer(v[1]))
)
);
}
export function asClientCap(v: any): ClientCap {
if (!isClientCap(v)) {throw new TypeError(`Invalid ClientCap: ${_.stringify(v)}`);} else {return v;};
}

25
src/gen/dataspace.ts

@ -0,0 +1,25 @@
import * as _ from "@preserves/core";
export const $Observe = Symbol.for("Observe");
export const Observe = _.Record.makeConstructor<{"_field0": symbol, "_field1": _ptr}, _ptr>()($Observe, ["_field0","_field1"]);
export type Observe = _.Record<(typeof $Observe), [symbol, _ptr], _ptr>;
export type _ptr = never;
export type _val = _.Value<_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 function asObserve(v: any): Observe {
if (!isObserve(v)) {throw new TypeError(`Invalid Observe: ${_.stringify(v)}`);} else {return v;};
}

227
src/gen/rewrite.ts

@ -0,0 +1,227 @@
import * as _ from "@preserves/core";
export const $__ = Symbol.for("_");
export const $and = Symbol.for("and");
export const $arr = Symbol.for("arr");
export const $bind = Symbol.for("bind");
export const $compound = Symbol.for("compound");
export const $dict = Symbol.for("dict");
export const $lit = Symbol.for("lit");
export const $not = Symbol.for("not");
export const $rec = Symbol.for("rec");
export const $ref = Symbol.for("ref");
export const $rewrite = Symbol.for("rewrite");
export const __lit3 = Symbol.for("/");
export type ConstructorSpec = (
_.Record<(typeof $rec), [_val, number], _ptr> |
_.Record<(typeof $arr), [number], _ptr> |
_.Record<(typeof $dict), [], _ptr>
);
export const Pattern = _.Record.makeConstructor<{
"_field0": [
_.Record<(typeof $__), [], _ptr>,
_.Record<(typeof $bind), [symbol, Pattern], _ptr>,
_.Record<(typeof $and), [Array<Pattern>], _ptr>,
_.Record<(typeof $not), [Pattern], _ptr>,
_.Record<(typeof $lit), [_val], _ptr>,
_.Record<(typeof $compound), [ConstructorSpec, _.KeyedDictionary<_val, Pattern>], _ptr>
]
}, _ptr>()(__lit3, ["_field0"]);
export type Pattern = _.Record<
(typeof __lit3),
[
[
_.Record<(typeof $__), [], _ptr>,
_.Record<(typeof $bind), [symbol, Pattern], _ptr>,
_.Record<(typeof $and), [Array<Pattern>], _ptr>,
_.Record<(typeof $not), [Pattern], _ptr>,
_.Record<(typeof $lit), [_val], _ptr>,
_.Record<(typeof $compound), [ConstructorSpec, _.KeyedDictionary<_val, Pattern>], _ptr>
]
],
_ptr
>;
export const Template = _.Record.makeConstructor<{
"_field0": [
_.Record<(typeof $ref), [symbol], _ptr>,
_.Record<(typeof $lit), [_val], _ptr>,
_.Record<(typeof $compound), [ConstructorSpec, _.KeyedDictionary<_val, Template>], _ptr>
]
}, _ptr>()(__lit3, ["_field0"]);
export type Template = _.Record<
(typeof __lit3),
[
[
_.Record<(typeof $ref), [symbol], _ptr>,
_.Record<(typeof $lit), [_val], _ptr>,
_.Record<(typeof $compound), [ConstructorSpec, _.KeyedDictionary<_val, Template>], _ptr>
]
],
_ptr
>;
export const Rewrite = _.Record.makeConstructor<{"_field0": Pattern, "_field1": Template}, _ptr>()($rewrite, ["_field0","_field1"]);
export type Rewrite = _.Record<(typeof $rewrite), [Pattern, Template], _ptr>;
export type _ptr = never;
export type _val = _.Value<_ptr>;
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')
) ||
(
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
_.is(v.label, $arr) &&
((v.length === 1) && typeof v[0] === 'number')
) ||
(
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
_.is(v.label, $dict) &&
((v.length === 0))
)
);
}
export function asConstructorSpec(v: any): ConstructorSpec {
if (!isConstructorSpec(v)) {throw new TypeError(`Invalid ConstructorSpec: ${_.stringify(v)}`);} else {return v;};
}
export function isPattern(v: any): v is Pattern {
return (
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
_.is(v.label, __lit3) &&
(
(v.length === 1) &&
(
_.Array.isArray(v[0]) &&
!_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v[0]) &&
(v[0].length === 6) &&
(
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v[0][0]) &&
_.is(v[0][0].label, $__) &&
((v[0][0].length === 0))
) &&
(
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v[0][1]) &&
_.is(v[0][1].label, $bind) &&
(
(v[0][1].length === 2) &&
typeof v[0][1][0] === 'symbol' &&
isPattern(v[0][1][1])
)
) &&
(
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v[0][2]) &&
_.is(v[0][2].label, $and) &&
(
(v[0][2].length === 1) &&
(
_.Array.isArray(v[0][2][0]) &&
!_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v[0][2][0]) &&
(v[0][2][0].length >= 0) &&
v[0][2][0].slice(0).every(v => (isPattern(v)))
)
)
) &&
(
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v[0][3]) &&
_.is(v[0][3].label, $not) &&
((v[0][3].length === 1) && isPattern(v[0][3][0]))
) &&
(
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v[0][4]) &&
_.is(v[0][4].label, $lit) &&
((v[0][4].length === 1) && true)
) &&
(
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v[0][5]) &&
_.is(v[0][5].label, $compound) &&
(
(v[0][5].length === 2) &&
isConstructorSpec(v[0][5][0]) &&
(
_.Dictionary.isDictionary<_val, _ptr>(v[0][5][1]) &&
((() => {
for (const e of v[0][5][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 isTemplate(v: any): v is Template {
return (
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
_.is(v.label, __lit3) &&
(
(v.length === 1) &&
(
_.Array.isArray(v[0]) &&
!_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v[0]) &&
(v[0].length === 3) &&
(
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v[0][0]) &&
_.is(v[0][0].label, $ref) &&
((v[0][0].length === 1) && typeof v[0][0][0] === 'symbol')
) &&
(
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v[0][1]) &&
_.is(v[0][1].label, $lit) &&
((v[0][1].length === 1) && true)
) &&
(
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v[0][2]) &&
_.is(v[0][2].label, $compound) &&
(
(v[0][2].length === 2) &&
isConstructorSpec(v[0][2][0]) &&
(
_.Dictionary.isDictionary<_val, _ptr>(v[0][2][1]) &&
((() => {
for (const e of v[0][2][1]) {if (!(true)) return false; if (!(isTemplate(e[1]))) return false;};
return true;
})())
)
)
)
)
)
);
}
export function asTemplate(v: any): Template {
if (!isTemplate(v)) {throw new TypeError(`Invalid Template: ${_.stringify(v)}`);} else {return v;};
}
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 asRewrite(v: any): Rewrite {
if (!isRewrite(v)) {throw new TypeError(`Invalid Rewrite: ${_.stringify(v)}`);} else {return v;};
}

177
src/gen/wire.ts

@ -0,0 +1,177 @@
import * as _ from "@preserves/core";
import * as _i_rewrite from "./rewrite";
export const $assert = Symbol.for("assert");
export const $message = Symbol.for("message");
export const $or = Symbol.for("or");
export const $ref = Symbol.for("ref");
export const $retract = Symbol.for("retract");
export const $sync = Symbol.for("sync");
export const SturdyRef = _.Record.makeConstructor<{"_field0": _val, "_field1": Array<Caveat>, "_field2": _.Bytes}, _ptr>()($ref, ["_field0","_field1","_field2"]);
export type SturdyRef = _.Record<(typeof $ref), [_val, Array<Caveat>, _.Bytes], _ptr>;
export type Caveat = (_i_rewrite.Rewrite | _.Record<(typeof $or), [Array<_i_rewrite.Rewrite>], _ptr>);
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": _val, "handle": Handle}, _ptr>()($assert, ["assertion","handle"]);
export type Assert = _.Record<(typeof $assert), [_val, 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": _val}, _ptr>()($message, ["body"]);
export type Message = _.Record<(typeof $message), [_val], _ptr>;
export const Sync = _.Record.makeConstructor<{"peer": _ptr}, _ptr>()($sync, ["peer"]);
export type Sync = _.Record<(typeof $sync), [_ptr], _ptr>;
export type _ptr = never;
export type _val = _.Value<_ptr>;
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].slice(0).every(v => (isCaveat(v)))
) &&
_.Bytes.isBytes(v[2])
)
);
}
export function asSturdyRef(v: any): SturdyRef {
if (!isSturdyRef(v)) {throw new TypeError(`Invalid SturdyRef: ${_.stringify(v)}`);} else {return v;};
}
export function isCaveat(v: any): v is Caveat {
return (
_i_rewrite.isRewrite(v) ||
(
_.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].slice(0).every(v => (_i_rewrite.isRewrite(v)))
)
)
)
);
}
export function asCaveat(v: any): Caveat {
if (!isCaveat(v)) {throw new TypeError(`Invalid Caveat: ${_.stringify(v)}`);} else {return v;};
}
export function isHandle(v: any): v is Handle {return typeof v === 'number';}
export function asHandle(v: any): Handle {
if (!isHandle(v)) {throw new TypeError(`Invalid Handle: ${_.stringify(v)}`);} else {return v;};
}
export function isEvent(v: any): v is Event {return (isAssert(v) || isRetract(v) || isMessage(v) || isSync(v));}
export function asEvent(v: any): Event {
if (!isEvent(v)) {throw new TypeError(`Invalid Event: ${_.stringify(v)}`);} else {return v;};
}
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 isTurn(v: any): v is Turn {
return (
_.Array.isArray(v) &&
!_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
(v.length >= 0) &&
v.slice(0).every(v => (
(
_.Array.isArray(v) &&
!_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
(v.length === 2) &&
isOid(v[0]) &&
isEvent(v[1])
)
))
);
}
export function asTurn(v: any): Turn {
if (!isTurn(v)) {throw new TypeError(`Invalid Turn: ${_.stringify(v)}`);} else {return v;};
}
export function isAssert(v: any): v is Assert {
return (
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
_.is(v.label, $assert) &&
((v.length === 2) && true && isHandle(v[1]))
);
}
export function asAssert(v: any): Assert {
if (!isAssert(v)) {throw new TypeError(`Invalid Assert: ${_.stringify(v)}`);} else {return v;};
}
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 asRetract(v: any): Retract {
if (!isRetract(v)) {throw new TypeError(`Invalid Retract: ${_.stringify(v)}`);} else {return v;};
}
export function isMessage(v: any): v is Message {
return (
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
_.is(v.label, $message) &&
((v.length === 1) && true)
);
}
export function asMessage(v: any): Message {
if (!isMessage(v)) {throw new TypeError(`Invalid Message: ${_.stringify(v)}`);} else {return v;};
}
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;};
}

25
src/gen/worker.ts

@ -0,0 +1,25 @@
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 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 function asInstance(v: any): Instance {
if (!isInstance(v)) {throw new TypeError(`Invalid Instance: ${_.stringify(v)}`);} else {return v;};
}

2
src/main.ts

@ -3,7 +3,7 @@ import { Dictionary, Record } from '@preserves/core';
import { Dataspace, Observe } from './dataspace.js';
import { Worker } from 'worker_threads';
import { Relay, spawnRelay } from './relay.js';
import { BoxState, SetBox } from './box-protocol.js';
import { BoxState, SetBox } from './gen/box-protocol.js';
import { attenuate, CRec, Lit, Pattern, PCompound, rfilter } from './rewrite.js';
import path from 'path';

Loading…
Cancel
Save