diff --git a/packages/core/src/dataspace.js b/packages/core/src/dataspace.js index 8074614..f9c29cc 100644 --- a/packages/core/src/dataspace.js +++ b/packages/core/src/dataspace.js @@ -544,6 +544,9 @@ Message.prototype.perform = function (ds, ac) { }; Dataspace.send = function (body) { + if (!Dataspace._inScript) { + throw new Error("Cannot `send` during facet setup; are you missing an `on start { ... }`?"); + } Dataspace._currentFacet.enqueueScriptAction(new Message(body)); }; @@ -558,6 +561,9 @@ Spawn.prototype.perform = function (ds, ac) { }; Dataspace.spawn = function (name, bootProc, initialAssertions) { + if (!Dataspace._inScript) { + throw new Error("Cannot `spawn` during facet setup; are you missing an `on start { ... }`?"); + } Dataspace._currentFacet.enqueueScriptAction(new Spawn(name, bootProc, initialAssertions)); }; @@ -579,6 +585,9 @@ DeferredTurn.prototype.perform = function (ds, ac) { }; Dataspace.deferTurn = function (continuation) { + if (!Dataspace._inScript) { + throw new Error("Cannot defer turn during facet setup; are you missing an `on start { ... }`?"); + } Dataspace._currentFacet.enqueueScriptAction(new DeferredTurn(Dataspace.wrap(continuation))); }; @@ -697,7 +706,17 @@ Facet.prototype.stop = function (continuation) { }); }; +Facet.prototype.addStartScript = function (s) { + if (Dataspace._inScript) { + throw new Error("Cannot `on start` outside facet setup"); + } + this.actor.scheduleScript(s); +}; + Facet.prototype.addStopScript = function (s) { + if (Dataspace._inScript) { + throw new Error("Cannot `on stop` outside facet setup"); + } this.stopScripts = this.stopScripts.push(s); }; diff --git a/packages/core/src/relay.js b/packages/core/src/relay.js index 94a2e8a..240ff7f 100644 --- a/packages/core/src/relay.js +++ b/packages/core/src/relay.js @@ -139,7 +139,9 @@ function inNestedDataspace(bootProc) { const outerFacet = Dataspace.currentFacet(); outerFacet.addDataflow(function () {}); // ^ eww! Dummy endpoint to keep the root facet of the relay alive. - const innerDs = new NestedDataspace(outerFacet, bootProc); + const innerDs = new NestedDataspace(outerFacet, function () { + Dataspace.currentFacet().addStartScript(() => bootProc.call(this)); + }); innerDs.start(); }; } diff --git a/packages/driver-streams-node/src/buffer.js b/packages/driver-streams-node/src/buffer.js index 60447d7..f6464ab 100644 --- a/packages/driver-streams-node/src/buffer.js +++ b/packages/driver-streams-node/src/buffer.js @@ -25,8 +25,19 @@ export { PacketRequest, }; +export function onStartSpawnBufferStream() { + const id = genUuid('buffer-stream'); + on start _spawnBufferStream(id); + return id; +} + export function spawnBufferStream() { const id = genUuid('buffer-stream'); + _spawnBufferStream(id); + return id; +} + +function _spawnBufferStream(id) { spawn named id { stop on retracted Observe(S.Duplex(id)); assert S.Duplex(id); diff --git a/packages/syntax-playground/src/server.js b/packages/syntax-playground/src/server.js index 92c53d0..2a5652b 100644 --- a/packages/syntax-playground/src/server.js +++ b/packages/syntax-playground/src/server.js @@ -30,7 +30,7 @@ assertion type Counter(id); function counter() { const id = genUuid(); - spawn named ['counter', id] { + on start spawn named ['counter', id] { const rootFacet = currentFacet(); assert Counter(id); diff --git a/packages/syntax-playground/src/socks.js b/packages/syntax-playground/src/socks.js index 98db1ef..736c630 100644 --- a/packages/syntax-playground/src/socks.js +++ b/packages/syntax-playground/src/socks.js @@ -17,7 +17,7 @@ spawn named 'socks-server' { stop on retracted S.Duplex(conn); - const buf = S.spawnBufferStream(); + const buf = S.onStartSpawnBufferStream(); field this.bufferWanted = true; on start react { stop on (!this.bufferWanted); @@ -210,8 +210,7 @@ spawn named 'remap-service' { err.errno = err.code = 'ENOTFOUND'; err.hostname = err.host = host; err.port = port; - // TODO: should error because no 'on start': - send S.ConnectionRejected(id, err); + on start send S.ConnectionRejected(id, err); } } else { assert S.OutgoingConnection(id, S.TcpAddress(host, port)); diff --git a/packages/syntax/src/plugin.js b/packages/syntax/src/plugin.js index 81212bf..9626b9a 100644 --- a/packages/syntax/src/plugin.js +++ b/packages/syntax/src/plugin.js @@ -439,7 +439,7 @@ export default declare((api, options) => { PseudoEventHandler(path, state) { const { node } = path; if (node.triggerType === "start") { - path.replaceWith(template(`DATASPACE._currentFacet.actor.scheduleScript(() => { + path.replaceWith(template(`DATASPACE._currentFacet.addStartScript(() => { BODY; });`)({ DATASPACE: state.DataspaceID,