t20891.nim 650 B

1234567891011121314151617181920212223242526272829
  1. import macros, tables
  2. var mapping {.compileTime.}: Table[string, NimNode]
  3. macro register(a: static[string], b: typed): untyped =
  4. mapping[a] = b
  5. macro getPtr(a: static[string]): untyped =
  6. result = mapping[a]
  7. proc foo() =
  8. iterator it() {.closure.} =
  9. discard
  10. proc getIterPtr(): pointer {.nimcall.} =
  11. rawProc(it)
  12. register("foo", getIterPtr())
  13. discard getIterPtr() # Comment either this to make it work
  14. foo() # or this
  15. proc bar() =
  16. iterator it() {.closure.} =
  17. discard getPtr("foo") # Or this
  18. discard
  19. proc getIterPtr(): pointer {.nimcall.} =
  20. rawProc(it)
  21. register("bar", getIterPtr())
  22. discard getIterPtr()
  23. bar()