macros: inject more getCurrentFacet

This commit is contained in:
Emery Hemingway 2021-06-30 10:47:54 +02:00
parent 1e78cd600d
commit 274f0429de
1 changed files with 11 additions and 7 deletions

View File

@ -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)