0
|
1 \name{callback}
|
|
2 \alias{new.callback}
|
|
3 \alias{callback}
|
|
4 \alias{dyncallback}
|
|
5 \title{Dynamic wrapping of R functions as C callbacks}
|
|
6 \description{
|
|
7 Function to wrap R functions as C function pointers.
|
|
8 }
|
|
9 \usage{
|
|
10 new.callback( signature, fun, envir = new.env() )
|
|
11 }
|
|
12 \arguments{
|
|
13 \item{signature}{character string specifying the \link[=signature]{call signature} of the C function callback type.}
|
|
14 \item{fun}{R function to be wrapped as a C function pointer.}
|
|
15 \item{envir}{the environment in which to evaluate the call to \code{fun}. }
|
|
16 }
|
|
17 \details{
|
|
18 Callbacks are user-defined functions that are registered in a foreign library and that are executed at a later time from within that library.
|
|
19 Examples include user-interface event handlers that are registered in GUI toolkits, and, comparison functions for custom data types to be passed to generic sort algorithm.
|
|
20
|
|
21 The function \code{new.callback} wraps an R function \code{fun} as a C function pointer and returns an external pointer. The foreign C function type of the wrapped
|
|
22 R function is specified by a \link{call signature} given by \code{signature}.
|
|
23
|
|
24 When the C function pointer is called, a global callback handler (implemented in C) is executed first, that dynamically creates an R call expression to \code{fun}
|
|
25 using the arguments, passed from C and converted to R, according to the \emph{argument types signature} within the \link{call signature} specified. See
|
|
26 \code{\link{.dyncall}} for details on the format.
|
|
27 Finally, the handler evaluates the R call expression within the environment given by \code{envir}.
|
|
28 On return, the R return value of \code{fun} is coerced to the C value, according to the return type signature specified in \code{signature}.
|
|
29 If an error occurs during the evaluation, the callback will be disabled for further invocations. (This behaviour might change in the future.)
|
|
30
|
|
31 }
|
|
32 \value{
|
|
33 \code{new.callback} returns an external pointer to a synthetically generated C function.
|
|
34 }
|
|
35
|
|
36 \section{Portability}{
|
|
37 The implementation is based on the \emph{dyncallback} library (part of the DynCall project).
|
|
38
|
|
39 The following processor architectures are supported: X86, X64, ARM (including Thumb) and partial stable support for PowerPC 32-bit; The library
|
|
40 has been built and tested to work on various OSs: Linux, Mac OS X, Windows 32/64-bit, BSDs, Haiku, Nexenta/Open Solaris, Minix and Plan9,
|
|
41 as well as embedded platforms such as Linux/ARM (OpenMoko, Beagleboard, Gumstix, Efika MX, Raspberry Pi), Nintendo DS (ARM), Sony Playstation Portable (MIPS 32-bit/eabi) and iOS (ARM - armv6 mode ok, armv7 unstable).
|
|
42 Special notes for PowerPC 32-Bit: Callbacks for System V (Linux/BSD) are unstable in this release; MacOS X/Darwin works fine.
|
|
43 In the context of R, dyncallback has currently no support for callbacks on MIPS, SPARC and PowerPC 64-Bit.
|
|
44 Using dyncallback to implement non-default calling conventions is not supported yet. (e.g. Window Procedures on Win32/X86).
|
|
45 }
|
|
46 \note{
|
|
47 The call signature \strong{MUST} match the foreign C callback function type, otherwise an activated callback call from C can lead to a \strong{fatal R process crash}.
|
|
48
|
|
49 A small amount of memory is allocated with each wrapper.
|
|
50 A finalizer function that frees the allocated memory is registered at the external pointer.
|
|
51 If the external callback function pointer is registered in a C library, a reference should also be held in R as long as the callback can be activated from a foreign C run-time context,
|
|
52 otherwise the garbage collector might call the finalizer and the next invocation of the callback could lead to a \strong{fatal R process crash} as well.
|
|
53 }
|
|
54
|
|
55 \references{
|
|
56 Adler, D. (2012) \dQuote{Foreign Library Interface}, \emph{The R Journal}, \bold{4(1)}, 30--40, June 2012.
|
|
57 \url{http://journal.r-project.org/archive/2012-1/RJournal_2012-1_Adler.pdf}
|
|
58
|
|
59 Adler, D., Philipp, T. (2008) \emph{DynCall Project}.
|
|
60 \url{http://dyncall.org}
|
|
61 }
|
|
62 \seealso{
|
|
63 See \code{\link{signature}} for details on call signatures,
|
|
64 \code{\link{reg.finalizer}} for details on finalizers.
|
|
65 }
|
|
66 \examples{
|
|
67 \donttest{
|
|
68 # Create a function, wrap it to a callback and call it via .dyncall:
|
|
69 f <- function(x,y) x+y
|
|
70 cb <- new.callback("ii)i", f)
|
|
71 r <- .dyncall(cb, "ii)i", 20, 3)
|
|
72
|
|
73 # Sort vectors directly via 'qsort' C library function using an R callback:
|
|
74 dynbind(c("msvcrt","c","c.so.6"), "qsort(piip)v;")
|
|
75 cb <- new.callback("pp)i",function(px,py){
|
|
76 x <- .unpack(px, 0, "d")
|
|
77 y <- .unpack(py, 0, "d")
|
|
78 if (x > y) return(1) else if (x == y) return(0) else return(-1)
|
|
79 })
|
|
80 x <- rnorm(100)
|
|
81 qsort(x,length(x),8,cb)
|
|
82 }
|
|
83 }
|
|
84 \keyword{programming}
|
|
85 \keyword{interface}
|