Skip to content

Commit

Permalink
Generate unsafe wrappers that return the status instead of throwing.
Browse files Browse the repository at this point in the history
  • Loading branch information
maleadt committed Feb 5, 2020
1 parent 181808f commit 331faf7
Show file tree
Hide file tree
Showing 4 changed files with 1,091 additions and 1,074 deletions.
23 changes: 23 additions & 0 deletions src/error.jl
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,29 @@ end
throw(CuError(res))
end

macro checked(ex)
# parse the function definition
@assert Meta.isexpr(ex, :function)
sig = ex.args[1]
@assert Meta.isexpr(sig, :call)
body = ex.args[2]
@assert Meta.isexpr(body, :block)
@assert length(body.args) == 2 # line number node and a single call

# generate a "safe" version that performs a check
safe_body = Expr(:block, body.args[1], :(@check $(body.args[2])))
safe_def = Expr(:function, sig, safe_body)

# generate a "unsafe" version that returns the error code instead
unsafe_sig = Expr(:call, Symbol("unsafe_$(sig.args[1])"), sig.args[2:end]...)
unsafe_def = Expr(:function, unsafe_sig, body)

return quote
@eval $safe_def
@eval $unsafe_def
end
end

macro check(ex)
fun = Symbol(decode_ccall_function(ex))
init = if !in(fun, preinit_apicalls)
Expand Down
Loading

0 comments on commit 331faf7

Please sign in to comment.