Skip to content

Commit

Permalink
fix call block (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
bung87 authored Oct 10, 2024
1 parent 88a1380 commit 6df874d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
7 changes: 4 additions & 3 deletions darwin/objc/blocks.nim
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,18 @@ proc toBlock*[T: proc](v: T): auto =
convertToClosure(v)

proc getInvokeType(procType: NimNode): NimNode =
result = copyNimTree(procType)
result = copyNimTree(getTypeImpl(procType))
let pragmas = result[1]


var i = 0
while i < pragmas.len:
if pragmas[i].kind in {nnkSym, nnkIdent} and $pragmas[i] == "closure":
pragmas.del(i)
else:
inc i

pragmas.add(ident"cdecl")
result.addPragma(ident"cdecl")

result.params.insert(1, newIdentDefs(ident"<theblock>", ident"pointer"))

Expand Down
11 changes: 11 additions & 0 deletions tests/tarrays.nim
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ proc test() =

test()

type MyBlock = proc(a:int): void

proc testBlock() =
var v = 5
let bl = toBlock() do(a: int):
Expand All @@ -39,3 +41,12 @@ proc testBlock() =
doAssert(v == 128)

testBlock()

proc testBlock2(cb: Block[MyBlock]) =
cb.call(123)

var v = 5
let bl = toBlock() do(a: int):
v = v + a
testBlock2(bl)
doAssert(v == 128)

0 comments on commit 6df874d

Please sign in to comment.