Skip to contents

ffi_callback() makes a C function pointer that calls fun, for a C function that takes one: a comparator for qsort(), a handler a library keeps and calls later. It is made with libffi's closure API (design §4.5).

Usage

ffi_callback(fun, type, lib = NULL, default = NULL)

Arguments

fun

An R function, called with one argument per C parameter.

type

The C type of the function pointer, as text.

lib

NULL, or a handle from ffi_dlopen() that keeps the callback alive until it is closed, and declares the types type names.

default

The value the callback returns when it cannot run fun or fun fails; NULL for the zero of the result type (a null pointer, an all-zero struct). Checked as a result is. A void callback takes NULL only.

Value

A callback: a zuffi_ptr of class zuffi_callback, which a call takes for a function-pointer argument. ffi_cast() to its type makes an R function that calls it through C.

Details

type is the pointer's C type, as a header writes it: "int (*)(const void *, const void *)", or the name of a typedef of one that lib declares. Arguments reach fun as R values by the rules of design §6: numbers and strings as R vectors, pointers as borrowed zuffi_ptr objects (NULL for a null pointer), structs by value as a copy, enums as zuffi_enum integers. What fun returns is converted back exactly, or the conversion fails as a call's argument would: a struct result may also be a named list, an enum result an enumerator's name, and a string or pointer result stays valid until the callback is next called.

Lifetime. Passing a callback to a call keeps it alive for that call. A library that keeps the pointer and calls it later needs it for longer: with lib, the callback lives until ffi_close() closes that library, whatever happens to the R object; without it, the R object must be kept, and a callback collected while C still holds its pointer crashes the process. ffi_free() frees one at once. lib is also where the type names in type are read.

Errors and other jumps. An error, an interrupt or a restart in fun must not jump across the library's frames, which would corrupt the process. So the callback returns default to the library instead, and every later callback of the same call returns default without running R; once the library has returned, the error (or the interrupt, or the restart) carries on as it would have. The library itself keeps running until it returns: qsort() finishes its sort with a comparator that answers default. A library with a way to stop early needs default set to the value that asks for it. A callback that fails outside any call zuffi made, as when a library runs it from a finaliser, turns its error into a warning of class zuffi_callback_warning and returns default.

Threads. A callback runs R only on R's main thread. Called from another thread, it returns default there without touching R, and the next call zuffi makes from R raises a zuffi_thread_error that names it (design D6).

Where the system refuses the executable memory a callback needs, ffi_callback() raises a zuffi_closure_error and calls into C still work; zuffi_info()'s closures says whether callbacks can be made.

Examples

lib <- ffi_dlopen(if (.Platform$OS.type == "windows") "ucrtbase" else "m")
ffi_cdef(lib, "void qsort(void *base, size_t n, size_t size,
                          int (*cmp)(const void *, const void *));")
cmp <- ffi_callback(function(a, b) {
  x <- ffi_cast(a, "double *")[1]
  y <- ffi_cast(b, "double *")[1]
  as.integer(sign(x - y))
}, "int (*)(const void *, const void *)")
x <- ffi_new(lib, "double[4]", c(3, 1, 4, 2))
lib$qsort(x, 4, 8, cmp)
x[]
#> [1] 1 2 3 4
ffi_close(lib)