tfuncobj.nim 703 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. discard """
  2. outputsub: '''f1
  3. f2
  4. f3'''
  5. cmd: "nim c --gc:orc $file"
  6. valgrind: true
  7. """
  8. type
  9. FuncObj = object
  10. fn: proc (env: pointer) {.cdecl.}
  11. env: pointer
  12. proc `=destroy`(x: var FuncObj) =
  13. GC_unref(cast[RootRef](x.env))
  14. proc `=copy`(x: var FuncObj, y: FuncObj) {.error.}
  15. # bug #18433
  16. proc main =
  17. var fs: seq[FuncObj]
  18. proc wrap(p: proc()) =
  19. proc closeOver() = p()
  20. let env = rawEnv closeOver
  21. GC_ref(cast[RootRef](env))
  22. fs.add(FuncObj(fn: cast[proc(env: pointer){.cdecl.}](rawProc closeOver), env: env))
  23. wrap(proc() {.closure.} = echo "f1")
  24. wrap(proc() {.closure.} = echo "f2")
  25. wrap(proc() {.closure.} = echo "f3")
  26. for a in fs:
  27. a.fn(a.env)
  28. main()