createRFunc: Generate code to define R function to interface to C routine

Description Usage Arguments Value Author(s) References See Also Examples

Description

This function accepts a resolved description of a C routine and generates the R code that will invoke that routine by coercing any arguments to the correct type and passing control the native routine. It then converts the result back to an R object.

The code can use a closure to store the address of the native symbol and also the call interface (CIF) object describing the routine for the FFI library.

Usage

1
2
createRFunc(routine, name = routine$name, useGlobalCIF = useClosure,
             useClosure = TRUE, returnInputs = NA)

Arguments

routine

a description of the C routine, obtained via resolveType in the RGCCTranslationUnit package

name

the name of the C routine

useGlobalCIF

a logical value indicating whether to use a global variable to store the CIF for this routine, or to inline the CIF creation code or, if useClosure is TRUE, to store the CIF object in the closure environment.

useClosure

a logical value indicating whether to create a separate environment for this function in which the address of the C routine, the CIF and other objects that are used across different calls to this new function cab be stored

returnInputs

the default value for the returnInputs parameter in the function being generated. This is then passed to callCIF and controls whether any mutable parameters should be returned after the call. The caller can specify the value or leave it to the function to determine based on the number and types of the parameters.

Value

A character vector containing R code to create a function

Author(s)

Duncan Temple Lang

References

The RGCCTranslationUnit package

See Also

CIF callCIF

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  tu.file = system.file("TU", "utsname", "utsname.c.001t.tu", package =  "RGCCTUFFI")

  tu = parseTU(tu.file)
  r = getRoutines(tu)
  r.uname = resolveType(r$uname, tu)
  code = createRFunc(r.uname, returnInputs = "FALSE")

  ds = getDataStructures(tu)
  ds.utsname = resolveType(ds$utsname, tu)
  scode = defStructClass(ds.utsname)

  define(list(code, scode))

  u = utsnamePtr()
  uname(u)

  names(u)
  u[]
  u$sysname


  ################ 
  # Alternatively, we can use genTUInterface() to do everything
  # for us in a single call.  But we lose some control, e.g.
  # setting the returnInputs.

  code = genTUInterface(tu.file, pattern = "utsname|uname")

      # now we can evaluate/load/define the code in this R session
      # including the uname() function.
  define(code)

omegahat/RGCCTUFFI documentation built on May 24, 2019, 1:53 p.m.