Skip to content

Commit

Permalink
Minor tweaks, cosmetics
Browse files Browse the repository at this point in the history
  • Loading branch information
yglukhov committed Oct 12, 2024
1 parent 3669431 commit cee50a4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- devel
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Cache choosenim
id: cache-choosenim
Expand All @@ -47,4 +47,3 @@ jobs:

- name: test
run: nimble test

23 changes: 11 additions & 12 deletions darwin/objc/blocks.nim
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ proc createBlockDescriptor(T: typedesc): BlockDescriptor =
)

proc getBlockDescriptor(T: typedesc): ptr BlockDescriptor =
var bd {.global.} = createBlockDescriptor(T)
const bd = createBlockDescriptor(T)
addr bd

var NSConcreteStackBlock {.importc: "_NSConcreteStackBlock".}: ptr pointer
Expand Down Expand Up @@ -116,19 +116,19 @@ macro convertToClosure(v: typed): untyped =

var hasClosurePragma = false
for n in t.pragma:
if t.kind in {nnkIdent, nnkSym} and $t == "closure":
if t.kind in {nnkIdent, nnkSym} and eqIdent($t, "closure"):
hasClosurePragma = true
break

let params = t[0]
let newParams = newTree(nnkFormalParams, params[0])
if not hasClosurePragma:
t.addPragma(ident"closure")

let params = t.params
let newParams = newTree(nnkFormalParams, params[0])
for _, n, t, _ in arguments(params):
newParams.add(newIdentDefs(ident($n), t))
t[0] = newParams
t.params = newParams

if not hasClosurePragma:
t.addPragma(ident"closure")
result = quote do:
var a: `t` = `v`
toBlockFromClosure(a)
Expand All @@ -139,12 +139,11 @@ proc toBlock*[T: proc](v: T): auto =

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


var i = 0
while i < pragmas.len:
if pragmas[i].kind in {nnkSym, nnkIdent} and $pragmas[i] == "closure":
if pragmas[i].kind in {nnkSym, nnkIdent} and eqIdent($pragmas[i], "closure"):
pragmas.del(i)
else:
inc i
Expand All @@ -157,8 +156,8 @@ macro invokeAux(b: untyped, procType: typedesc, f: pointer, args: varargs[untype
let procT = getTypeImpl(procType)[1]
# echo treeRepr procT
result = newCall(newTree(nnkCast, getInvokeType(procT), f), b)
for s in args:
result.add(s)
for a in args:
result.add(a)
# echo "invokeAux: ", repr result

template call*[T](b: Block[T], args: varargs[untyped]): untyped =
Expand Down
1 change: 0 additions & 1 deletion tests/tblocks.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import ../darwin/foundation
import ../darwin/objc/blocks

####
Expand Down

0 comments on commit cee50a4

Please sign in to comment.