fixed .component macro

This commit is contained in:
Andreas Rumpf 2017-04-14 20:33:03 +02:00
commit 5d71662617
4 changed files with 18 additions and 13 deletions

View file

@ -78,7 +78,9 @@ macro component*(prc: untyped): untyped =
isvirtual = ComponentKind.Node isvirtual = ComponentKind.Node
else: else:
error "component must return VNode or Node" error "component must return VNode or Node"
n[0] = ident("inner" & $name) let realName = if name.kind == nnkPostfix: name[1] else: name
let nn = $realName
n[0] = ident("inner" & nn)
var unpackCall = newCall(n[0]) var unpackCall = newCall(n[0])
var counter = 0 var counter = 0
for i in 1.. <params.len: for i in 1.. <params.len:
@ -89,27 +91,28 @@ macro component*(prc: untyped): untyped =
unpackCall.add unpack(typ, counter) unpackCall.add unpack(typ, counter)
inc counter inc counter
template vwrapper(wname, nameStrLit, unpackCall) {.dirty.} = template vwrapper(pname, cname, nameStrLit, unpackCall) {.dirty.} =
proc wname*(args: seq[VNode]): VNode = proc pname(args: seq[VNode]): VNode =
unpackCall unpackCall
vcomponents[cstring(nameStrLit)] = wname vcomponents[cstring(nameStrLit)] = cname
template dwrapper(wname, nameStrLit, unpackCall) {.dirty.} = template dwrapper(pname, cname, nameStrLit, unpackCall) {.dirty.} =
proc wname(args: seq[VNode]): Node = proc pname(args: seq[VNode]): Node =
unpackCall unpackCall
dcomponents[cstring(nameStrLit)] = wname dcomponents[cstring(nameStrLit)] = cname
result = newTree(nnkStmtList, n) result = newTree(nnkStmtList, n)
if isvirtual == ComponentKind.VNode: if isvirtual == ComponentKind.VNode:
result.add getAst(vwrapper(newname name, newLit($name), unpackCall)) result.add getAst(vwrapper(newname name, realName, newLit(nn), unpackCall))
else: else:
result.add getAst(dwrapper(newname name, newLit($name), unpackCall)) result.add getAst(dwrapper(newname name, realName, newLit(nn), unpackCall))
allcomponents[$name] = isvirtual allcomponents[nn] = isvirtual
when defined(debugKaraxDsl): when defined(debugKaraxDsl):
echo repr result echo repr result
when isMainModule: when isMainModule:
proc foo(x, y: int, b: bool; s: cstring): VNode {.component.} = proc public*(x, y: int, b: bool; s: cstring): VNode {.component.} =
discard
proc private(x, y: int, b: bool; s: cstring): VNode {.component.} =
discard discard

View file

@ -51,7 +51,6 @@ type
Timeout* = ref object Timeout* = ref object
var var
document* {.importc.}: Document
toFocus: Node toFocus: Node
toFocusV: VNode toFocusV: VNode

3
src/karaxprelude.nim Normal file
View file

@ -0,0 +1,3 @@
## Include file that contains the common imports for the Karax framework.
import karax, karaxdsl, vdom, components