ffi.callback()
Creates an executable callback address.
Signature
ffi.callback(entry, parameters, result)
Parameters
Atom function, array of up to 254 native parameter descriptors, native result descriptor. Callback result cstring is unavailable.
Returns
Owned pointer to callable machine code.
Behavior
Keep the pointer live. Atom reentry is allowed only within an active synchronous FFI call on the right VM and thread. Callback errors propagate out of the enclosing native call.
Errors
Unsupported descriptor or executable closure failure. Invocations in another context return zero to C.
Example
Build the native C fixture and pass its shared library path after --.
use ffi
use os
func increment(value):
return value + 1
lib = ffi.load(os.args[0])
try:
apply = ffi.bind(lib, 'apply', [ptr, i32], i32)
handler = ffi.callback(increment, [i32], i32)
try:
assert(apply(handler, 7) == 8)
finally:
ffi.free(handler)
finally:
ffi.close(lib)