Switch to proper schema

This commit is contained in:
Tony Garnock-Jones 2023-05-28 12:03:45 +02:00
parent 021fdf06bb
commit 2a2b7da4bc
3 changed files with 43 additions and 42 deletions

View File

@ -0,0 +1,14 @@
version 1 .
embeddedType EntityRef.Cap .
ActionDescription =
/ @spawnActor <spawn-actor @detail OptionalAny @initialAssertions #{protocol.Handle}>
/ @stopActor <stop-actor @error OptionalAny>
/ @inertCheck <inert-check>
/ <assert @target #!any @handle protocol.Handle @assertion any>
/ <retract @target #!any @handle protocol.Handle>
/ <message @target #!any @assertion any>
/ <sync @target #!any @callback #!any>
.
OptionalAny = <none> / <some @value any> .

View File

@ -1,12 +1,13 @@
/// SPDX-License-Identifier: GPL-3.0-or-later /// SPDX-License-Identifier: GPL-3.0-or-later
/// SPDX-FileCopyrightText: Copyright © 2016-2023 Tony Garnock-Jones <tonyg@leastfixedpoint.com> /// SPDX-FileCopyrightText: Copyright © 2016-2023 Tony Garnock-Jones <tonyg@leastfixedpoint.com>
import { IdentitySet, Value, embeddedId, is, fromJS, stringify, Dictionary } from '@preserves/core'; import { IdentitySet, Value, embeddedId, is, fromJS, stringify, Dictionary, KeyedSet } from '@preserves/core';
import { Cell, Field, Graph } from './dataflow.js'; import { Cell, Field, Graph } from './dataflow.js';
import { Caveat, runRewrites } from './rewrite.js'; import { Caveat, runRewrites } from './rewrite.js';
import { ActorSpace } from './space.js'; import { ActorSpace } from './space.js';
import type { ActionDescription, StructuredTask, TaskAction } from './task.js'; import { ActionDescription, StructuredTask, TaskAction } from './task.js';
import { randomId } from './randomid.js'; import { randomId } from './randomid.js';
import * as Q from '../gen/queuedTasks.js';
export type AnyValue = Value<Ref>; export type AnyValue = Value<Ref>;
@ -352,7 +353,7 @@ export class Turn {
// ^ we trust initialAssertions, so can use `!` safely // ^ we trust initialAssertions, so can use `!` safely
const newActor = Actor.__unsafeNew(this.activeFacet.actor.space, newOutbound); const newActor = Actor.__unsafeNew(this.activeFacet.actor.space, newOutbound);
const detail = 'detail' in bootProc ? bootProc.detail : void 0; const detail: Q.OptionalAny<Ref> = 'detail' in bootProc ? Q.OptionalAny.some(bootProc.detail) : Q.OptionalAny.none();
const spawningFacet = this.activeFacet; const spawningFacet = this.activeFacet;
this.enqueue(spawningFacet, this.enqueue(spawningFacet,
() => { () => {
@ -362,7 +363,11 @@ export class Turn {
describe() { return { type: 'bootActor', detail }; }, describe() { return { type: 'bootActor', detail }; },
}); });
}, },
{ type: 'spawnActor', detail, initialAssertions }); () => {
const a = new KeyedSet<number, Ref>();
initialAssertions.forEach(h => a.add(h));
return Q.ActionDescription.spawnActor({ detail, initialAssertions: a });
});
return newActor; return newActor;
} }
@ -382,19 +387,16 @@ export class Turn {
stopActor(): void { stopActor(): void {
this.enqueue(this.activeFacet.actor.root, this.enqueue(this.activeFacet.actor.root,
() => this.activeFacet.actor._terminateWith({ ok: true }), () => this.activeFacet.actor._terminateWith({ ok: true }),
{ type: 'stopActor', err: void 0 }); () => Q.ActionDescription.stopActor(Q.OptionalAny.none()));
} }
crash(err: Error): void { crash(err: Error): void {
this.enqueue(this.activeFacet.actor.root, this.enqueue(this.activeFacet.actor.root,
() => this.activeFacet.actor._terminateWith({ ok: false, err }), () => this.activeFacet.actor._terminateWith({ ok: false, err }),
{ () => Q.ActionDescription.stopActor(Q.OptionalAny.some(Dictionary.fromJS({
type: 'stopActor', message: err.message,
err: Dictionary.fromJS({ stack: err.stack ? err.stack : false,
message: err.message, }))));
stack: err.stack ? err.stack : false,
}),
});
} }
field<V>(initial: V, name?: string): Field<V> { field<V>(initial: V, name?: string): Field<V> {
@ -448,12 +450,11 @@ export class Turn {
e.established = true; e.established = true;
ref.target.assert?.(a, h); ref.target.assert?.(a, h);
}, },
{ () => Q.ActionDescription.assert({
type: 'assert',
target: ref, target: ref,
handle: h, handle: h,
assertion, assertion,
}); }));
} }
} }
@ -482,11 +483,10 @@ export class Turn {
e.peer.target.retract?.(e.handle); e.peer.target.retract?.(e.handle);
} }
}, },
{ () => Q.ActionDescription.retract({
type: 'retract',
target: e.peer, target: e.peer,
handle: e.handle, handle: e.handle,
}); }));
} }
sync(ref: Ref): Promise<void> { sync(ref: Ref): Promise<void> {
@ -496,11 +496,10 @@ export class Turn {
_sync(ref: Ref, peer: Ref): void { _sync(ref: Ref, peer: Ref): void {
this.enqueue(ref.relay, this.enqueue(ref.relay,
() => _sync_impl(ref.target, peer), () => _sync_impl(ref.target, peer),
{ () => Q.ActionDescription.sync({
type: 'sync',
target: ref, target: ref,
callback: peer, callback: peer,
}); }));
} }
message(ref: Ref, assertable: Assertable): void { message(ref: Ref, assertable: Assertable): void {
@ -509,11 +508,10 @@ export class Turn {
if (a !== null) { if (a !== null) {
this.enqueue(ref.relay, this.enqueue(ref.relay,
() => ref.target.message?.(assertion), () => ref.target.message?.(assertion),
{ () => Q.ActionDescription.message({
type: 'message',
target: ref, target: ref,
assertion, assertion,
}); }));
} }
} }
@ -541,13 +539,13 @@ export class Turn {
}, delayMilliseconds); }, delayMilliseconds);
} }
enqueue(relay: Facet, a0: LocalAction, detail: ActionDescription): void { enqueue(relay: Facet, a0: LocalAction, detail: () => ActionDescription): void {
if (this.queues === null) { if (this.queues === null) {
throw new Error("Attempt to reuse a committed Turn"); throw new Error("Attempt to reuse a committed Turn");
} }
const a: StructuredTask<TaskAction> = { const a: StructuredTask<TaskAction> = {
perform() { Turn.active._inFacet(relay, a0); }, perform() { Turn.active._inFacet(relay, a0); },
describe() { return { targetFacet: relay, action: detail }; }, describe() { return { targetFacet: relay, action: detail() }; },
}; };
this.queues.get(relay.actor)?.push(a) ?? this.queues.set(relay.actor, [a]); this.queues.get(relay.actor)?.push(a) ?? this.queues.set(relay.actor, [a]);
} }
@ -572,6 +570,6 @@ function stopIfInertAfter(a: LocalAction): LocalAction {
Turn.active.stop(facet); Turn.active.stop(facet);
} }
}, },
{ type: 'inertCheck' }); Q.ActionDescription.inertCheck);
}; };
} }

View File

@ -1,10 +1,9 @@
/// SPDX-License-Identifier: GPL-3.0-or-later /// SPDX-License-Identifier: GPL-3.0-or-later
/// SPDX-FileCopyrightText: Copyright © 2023 Tony Garnock-Jones <tonyg@leastfixedpoint.com> /// SPDX-FileCopyrightText: Copyright © 2023 Tony Garnock-Jones <tonyg@leastfixedpoint.com>
// Each Turn executes one Task import type { Facet } from './actor.js';
import type { AnyValue, Handle, Ref, Facet } from './actor.js'; import * as q from '../gen/queuedTasks.js';
import type { IdentitySet } from '@preserves/core';
export type Task = StructuredTask<TaskDescription>; export type Task = StructuredTask<TaskDescription>;
@ -14,19 +13,9 @@ export interface StructuredTask<T> {
} }
export type TaskDescription = export type TaskDescription =
| { type: 'bootActor', detail?: AnyValue } | { type: 'bootActor', detail: q.OptionalAny }
| { type: 'turn', tasks: TaskAction[] } | { type: 'turn', tasks: TaskAction[] }
; ;
export type TaskAction = { targetFacet: Facet, action: ActionDescription }; export type TaskAction = { targetFacet: Facet, action: ActionDescription };
export type ActionDescription = q.ActionDescription;
export type ActionDescription =
| { type: 'spawnActor', detail?: AnyValue, initialAssertions: IdentitySet<Handle> }
| { type: 'stopActor', err?: AnyValue }
| { type: 'inertCheck' }
| { type: 'assert', target: Ref, handle: Handle, assertion: AnyValue }
| { type: 'retract', target: Ref, handle: Handle }
| { type: 'message', target: Ref, assertion: AnyValue }
| { type: 'sync', target: Ref, callback: Ref }
;