Skip to contents

zuffi 0.0.0.9000

  • 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() unloads the library.

  • 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 until ffi_close()). 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.