From 274f0429de281db447507ba2e9433acf68cd82f7 Mon Sep 17 00:00:00 2001 From: Emery Hemingway Date: Wed, 30 Jun 2021 10:47:54 +0200 Subject: [PATCH] macros: inject more getCurrentFacet --- src/syndicate/macros.nim | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/syndicate/macros.nim b/src/syndicate/macros.nim index a48f0e4..a47b220 100644 --- a/src/syndicate/macros.nim +++ b/src/syndicate/macros.nim @@ -39,6 +39,7 @@ template stopIf*(cond, body: untyped): untyped = discard getCurrentFacet().addDataflow do (facet: Facet): if cond: facet.stop do (facet: Facet): + proc getCurrentFacet(): Facet {.inject, used.} = facet body template send*(class: RecordClass; fields: varargs[Preserve, @@ -62,11 +63,9 @@ proc assertionForRecord(class: RecordClass; doHandler: NimNode): NimNode = result.add newCall("init", ident"Capture", newCall("init", ident"Discard")) -proc callbackForEvent(event: EventKind; class: RecordClass; doHandler: NimNode; - assertion: NimNode): NimNode = +proc callbackForEvent(event: EventKind; class: RecordClass; doHandler: NimNode): NimNode = ## Generate a procedure that checks an event kind, unpacks the fields of `class` to match the ## parameters of `doHandler`, and calls the body of `doHandler`. - # TODO: the assertion parameter is just for tracing. let formalArgs = doHandler[3] if formalArgs.len.pred != class.arity: error($formalArgs.repr & " does not match record class " & $class, doHandler) @@ -105,6 +104,11 @@ proc callbackForEvent(event: EventKind; class: RecordClass; doHandler: NimNode; body = newStmtList( newCall("assert", infix(newCall("len", recSym), "==", newLit(captureCount))), + newProc( + name = ident"getCurrentFacet", + params = [ ident"Facet" ], + body = scriptFacetSym, + pragmas = newNimNode(nnkPragma).add(ident"inject").add(ident"used")), letSection, doHandler[6] ) @@ -131,7 +135,7 @@ proc callbackForEvent(event: EventKind; class: RecordClass; doHandler: NimNode; proc onEvent(event: EventKind; class: RecordClass; doHandler: NimNode): NimNode = let assertion = assertionForRecord(class, doHandler) - handler = callbackForEvent(event, class, doHandler, assertion) + handler = callbackForEvent(event, class, doHandler) handlerSym = handler[0] result = quote do: `handler` @@ -154,7 +158,7 @@ macro onMessage*(class: static[RecordClass]; doHandler: untyped) = template assert*(class: RecordClass; field: untyped): untyped = mixin getCurrentFacet let facet = getCurrentFacet() - discard facet.addEndpoint do (facet: Facet) -> EndpointSpec: + discard facet.addEndpoint do (_: Facet) -> EndpointSpec: let a = init(class, getPreserve(field)) result.assertion = some(a) @@ -168,11 +172,11 @@ template field*(F: untyped; T: typedesc; initial: T): untyped = template spawn*(name: string; spawnBody: untyped): untyped = mixin getCurrentFacet spawn(getCurrentFacet(), name) do (spawnFacet: Facet): - proc getCurrentFacet(): Facet {.inject.} = spawnFacet + proc getCurrentFacet(): Facet {.inject, used.} = spawnFacet spawnBody template syndicate*(name: string; dataspaceBody: untyped): untyped = proc bootProc(rootFacet: Facet) = - proc getCurrentFacet(): Facet {.inject.} = rootFacet + proc getCurrentFacet(): Facet {.inject, used.} = rootFacet dataspaceBody waitFor bootModule(name, bootProc)