CClosure.cclosureMethod
cclosure(callable, n, ReturnType, (ArgumentTypes...)) -> func_ptr, Ref(callable)

Accepts arguments as @cfunction plus an extra context pointer at position n.

Return func_ptr that expects the context_ptr to be passed as at position n and a reference to callable, which must be GC.@preserved if not directly used in ccall.

Examples

julia> f(x, y) = 
    let left, right
        func, ctx = cclosure(3, Int, (Int, Int)) do a, b
            left = a
            right = b
            a + b
        end
        sum = @ccall $func(x::Int, y::Int, ctx::Ptr{Cvoid})::Int
        left, right, sum
    end
f (generic function with 1 method)

julia> f(1, 2)
(1, 2, 3)