ffi_dlopen() opens a shared library by path or by stem, with each
platform's naming and never the working directory; ffi_dlsym() looks
up a symbol and ffi_close() closes the handle.
ffi_close() keeps the library loaded unless it was opened with
unload = TRUE (ffi_dlopen(), uniffi_load()): a library whose
threads run on after a call, such as a C thread pool or a Rust crate
on tokio, crashed R when its code was unloaded under them. The handle
prints its setting. A library that is still loaded but whose file has
changed since (a rebuild) refuses to open again, with a
zuffi_load_error saying to restart R, rather than run the old code;
and a failed uniffi_load() always releases the library it opened
(#54).
ffi_close() no longer frees the callbacks made with lib = while
the library stays loaded, which since #54 is the default: a library
that kept a callback's address and called it after the close called
freed memory. They are released only by a close that unloads the
library, or by ffi_free() (#57).
?ffi_callback says how to free a callback made with lib = before R
exits: keep it, and call ffi_free() once the library has dropped the
pointer. ffi_free() of such a callback now also lets go of its
function and the environment it captured, which the library's handle
kept alive until the handle was collected (#57).
A failed ffi_include() given a library's name or path closes the
library it opened and gives it back to the system, as a failed
uniffi_load() does; before, the handle was lost with its reference
(#58).
A character vector with a class, such as htmltools::HTML() or
glue::glue() output, passes as its strings to a const char *
argument, a UniFFI string or an enum's name, where before it needed
unclass(). A factor is still refused, now with a message saying to
use as.character() (#51).
Errors converting a UniFFI argument or result name its UniFFI type
(string, i32) rather than the C type it crosses as (#51).
The environment uniffi_load() returns has class uniffi_module and
prints the crate's name, library, contract version, functions with
their arguments, objects with their constructors and methods, and
records, enums, errors and custom types, a custom type with the type
it crosses as. A UniFFI function or method that returns
() returns NULL invisibly. ?uniffi_load says that a HashMap
arrives in Rust's iteration order, not sorted (#51).
Calls to scalar C functions through a vendored libffi 3.8.2: every
integer type, float, double, bool and const char *, converted
exactly or with a classed zuffi_error. 64-bit and 128-bit integers can
arrive as zuint values (ffi_dlopen(int64 = "zuint")).
Variadic functions take typed arguments made with ffi_arg().
ffi_errno() and ffi_last_error() return errno and Windows'
GetLastError() as the latest declared call left them.
Pointers are zuffi_ptr objects carrying their C type, their size when
known and who frees them. ffi_new() allocates C memory R owns;
p$x and p[i] read and write members and elements within the known
size; ffi_cast(), ffi_string(), ffi_own() and ffi_free() work
on any pointer. A freed pointer is cleared, a forked child frees
nothing, and ffi_close() refuses while an owned pointer still needs
the library.
Structs, unions, fixed arrays and nested structs are laid out as the
C compiler lays them out, packed, aligned and #pragma pack
included; ffi_sizeof() and ffi_offsetof() report the layout.
Structs cross by value as arguments and results; unions and packed
structs do not.
R vectors pass as C buffers: a const pointer reads the vector in
place, and ffi_in(), ffi_out(), ffi_inout() and ffi_owned()
give other pointers a direction or an owner. Integer buffers are
always checked for NA.
ffi_cdef() declares functions, structs, unions, enums, typedefs and
#define constants from C text, as a header writes them, and lays out
structs as the C compiler does. It does not preprocess. What zuffi
cannot lay out or call (a bit-field, _Complex, _Atomic, a function
with a body, long double by value) is declared anyway and refused
where it is used, with the reason; ffi_functions() lists every
function and whether it can be called. An error gives its line and
column and leaves the handle as it was.
A #define with a long double operand is not bound, on any platform;
ffi_constants() lists it as skipped with the reason (#44).
Enum results and members are integers of class zuffi_enum, printed
by name; enum arguments take an integer or an enumerator's name.
zuffi_info() reports the package and libffi versions, libffi's
target and the UniFFI contract version the runtime reads (33).
A runtime for Rust crates built with UniFFI 0.33: its C half lowers R
values into UniFFI's wire format and lifts results out of it, exactly
or with a classed error, for scalars, strings, owned and borrowed
bytes, Option, Vec, HashMap with string keys, records, enums,
errors, custom types and objects. Rust errors arrive as conditions of
class <crate>_<Error> and uniffi_error, and panics as
zuffi_panic_error. A library whose contract version or checksums
differ from its bindings' is refused at load. Objects hold their
handle whole, clone it for every call and free it once; ffi_free()
frees one at once.
uniffi_load() loads a UniFFI crate's library with the R bindings that
uniffi-bindgen-r
generates from it, found by default beside the library and named after
the crate. A library and bindings from different builds refuse to load,
naming the functions whose checksums differ. Rust and cargo build the
crate and its bindings; R needs neither.
ffi_callback() makes an R function a C function pointer, for a
qsort() comparator or a handler a library keeps (lib = keeps it
while the library stays loaded). Its arguments and result convert as
a call's do.
An error, interrupt or restart in it never jumps across the library's
frames: the callback returns default, and the condition carries on
once the library has returned. A callback called from another thread
returns default without touching R, and the next call raises a
zuffi_thread_error. ffi_cast() to a function-pointer type makes an
R function that calls the pointer. zuffi_info()$closures says whether
callbacks can be made.
ffi_include() declares a library from its header: the C compiler R
builds packages with (R CMD config CC) preprocesses it, with
include directories and defines, and the ffi_cdef() parser reads
the result. Every object-like macro the header defines is expanded and
bound as a constant where it is one. ffi_constants() lists the
enumerators and constants a handle bound, and the macros it skipped
with the reason. Macros of a file the compiler includes on its own,
such as glibc's <stdc-predef.h>, are not the header's and are left
out. Preprocessed headers are cached
for the session. Without a compiler, or with a header missing, the
error says what is missing. zuffi_info()$preprocessors lists the
preprocessors found. Each type's text is read once a session, so
ffi_include("sqlite3.h") takes about 390 ms the first time, down
from 650 (#37).
ffi_cdef() reads Apple's block pointers (making what holds them
incomplete), enums with a fixed underlying type, typeof and
aarch64's vector types (as unsupported), does not check a static
assertion on a type it does not lay out, gives a bare
__attribute__((aligned)) 16 bytes wherever zuffi was built, and binds a 64-bit
constant that a double holds exactly, such as LLONG_MIN, instead of
refusing it.
A typedef with its own aligned attribute has that alignment, as C's
_Alignof gives it: ffi_new() allocates at it, where before it used
the alignment of the type the typedef names. An array whose element
size is not a multiple of its alignment is refused, as gcc and clang
refuse it (#33).
A README for a first-time reader, and a Get started vignette that runs:
declarations, refused conversions, structs, a qsort() callback and
a header, with the Rust crate shown but not run.
zuffi passes R CMD check on platforms without a libffi target
(Windows on ARM, WebAssembly): the examples, vignette chunks and tests
that call C are skipped there, and the rest run.
On R 4.6 and older, a regular expression that runs out of memory while
matching only warns, and returns a wrong value. Wherever zuffi reads
text with one (type text, member paths, library names, headers, UniFFI
bindings, and the version checks of loading zuint) that warning is now
a zuffi_memory_error. Before, int[3] could read with two
dimensions, or a pointer type with the wrong depth, and stay so for
the session, and ffi_dlopen("./libm.so.6") could open a library of
that name in another directory (#49).
Initial CRAN submission.