(sigil ffi)
(sigil ffi) - Dynamic Foreign Function Interface
Provides dynamic FFI so Sigil programs can call C library functions at runtime without writing C glue code.
Examples:
(import (sigil ffi))
(define libm (c-library "libm"))
(define cbrt (c-function libm "cbrt" (list ffi/double) ffi/double))
(cbrt 27.0) ; => 3.0Exports
%ffi-open-libraryprocedureOpen a shared library by name. Pass #f to open the current process (libc, etc.).
%ffi-close-libraryprocedureClose a shared library handle.
%ffi-lookup-symbolprocedureLook up a symbol in a library, returning a pointer or #f.
%ffi-bind-functionprocedureBind a C function for calling through the FFI.
%ffi-callprocedureCall a bound FFI function with marshaling.
%ffi-type-sizeprocedureReturn the size of an FFI type in bytes.
%ffi-type-alignmentprocedureReturn the alignment of an FFI type in bytes.
%ffi-make-pointerprocedureCreate a pointer from an integer address.
%ffi-pointer-addressprocedureReturn the integer address of a pointer.
%ffi-pointer?procedureCheck if a value is an FFI pointer.
%ffi-pointer-refprocedureRead a typed value at a byte offset from a pointer.
%ffi-pointer-set!procedureWrite a typed value at a byte offset into a pointer.
%ffi-allocprocedureAllocate zeroed memory. Returns a pointer.
%ffi-freeprocedureFree allocated memory.
%ffi-string->pointerprocedureConvert a Scheme string to a malloc'd null-terminated C string pointer.
%ffi-pointer->stringprocedureConvert a C string pointer to a Scheme string.
%ffi-errnoprocedureReturn the current value of errno.
%ffi-strerrorprocedureConvert an errno value to its string description.
%ffi-set-finalizer!procedureSet a finalizer function on a pointer.
%ffi-pointer->bytevectorprocedureCopy pointer data to a new bytevector.
%ffi-bytevector->pointerprocedureGet a pointer to a bytevector's data.
%ffi-library?procedureCheck if a value is an FFI library handle.
%ffi-function?procedureCheck if a value is an FFI function binding.
%ffi-callback-createprocedureCreate a C-callable function pointer from a Scheme procedure.
%ffi-callback-releaseprocedureRelease a callback slot for reuse.
c-libraryprocedureOpen a shared library by name.
The name is resolved by the platform's dynamic linker. On Linux, "libm" finds libm.so; on macOS, libm.dylib. Pass #f to open the current process (libc, etc.).
Examples:
(define libm (c-library "libm"))
(define libc (c-library #f))c-library-closeprocedureClose a library handle and unload the shared library.
c-library?procedureCheck if a value is a library handle.
c-symbolprocedureLook up a symbol in a library, returning a pointer. Raises an error if the symbol is not found.
c-symbol?procedureLook up a symbol, returning #f if not found.
c-functionprocedureCreate a callable Scheme procedure from a C function.
Looks up SYMBOL in LIBRARY and returns a procedure that marshals arguments and calls the C function via dyncall. Struct layouts can be used as arg/return types for struct-by-value passing.
Examples:
(define cbrt (c-function libm "cbrt" (list ffi/double) ffi/double))
(cbrt 27.0) ; => 3.0
(define color-layout (c-struct-layout r: ffi/uint8 g: ffi/uint8 b: ffi/uint8 a: ffi/uint8))
(define clear-bg (c-function lib "ClearBackground" (list color-layout) ffi/void))
(clear-bg (dict r: 245 g: 245 b: 245 a: 255))ffi-function?procedureCheck if a value is an FFI function binding.
c-sizeofprocedureReturn the size of an FFI type in bytes.
Examples:
(c-sizeof ffi/double) ; => 8
(c-sizeof ffi/pointer) ; => 8c-alignofprocedureReturn the alignment of an FFI type in bytes.
make-pointerprocedureCreate a pointer from an integer address.
pointer-addressprocedureReturn the integer address of a pointer.
pointer+procedureReturn a new pointer offset by N bytes.
pointer?procedurePointer type predicate.
null-pointer?procedureTest whether a pointer is null (#f).
pointer-refprocedureRead a value of the given FFI type at a byte offset from a pointer.
Examples:
(pointer-ref ptr ffi/int32 0) ; read int32 at offset 0
(pointer-ref ptr ffi/double 8) ; read double at offset 8pointer-set!procedureWrite a value of the given FFI type at a byte offset from a pointer.
string->pointerprocedureConvert a Scheme string to a null-terminated UTF-8 C string. Returns a pointer. The caller must free the memory with c-free.
pointer->stringprocedureConvert a null-terminated C string pointer to a Scheme string. Optionally specify a length for strings containing nulls.
pointer->bytevectorprocedureCopy pointer memory to a new bytevector.
bytevector->pointerprocedureGet a pointer to a bytevector's data (no copy, aliased).
c-allocprocedureAllocate SIZE bytes of zeroed memory. Returns a pointer.
c-freeprocedureFree memory allocated by c-alloc.
set-pointer-finalizer!procedureRegister a C function to be called when a pointer is garbage collected.
c-errnoprocedureReturn the current value of errno.
c-strerrorprocedureConvert an errno value to its string description.
c-callbackprocedureCreate a C-callable function pointer from a Scheme procedure.
ARG-TYPES is a list of FFI type constants for the callback's parameters. RETURN-TYPE is the FFI type for the return value. PROC is the Scheme procedure to call.
Returns a pointer that can be passed to C functions expecting a function pointer (e.g., qsort comparator).
Examples:
(define cmp (c-callback (list ffi/pointer ffi/pointer) ffi/int
(lambda (a b)
(- (pointer-ref a ffi/int32 0) (pointer-ref b ffi/int32 0)))))c-callback-releaseprocedureRelease a callback slot for reuse.
After calling this, the pointer must not be used again.
c-struct-layoutprocedureDefine a struct layout from field names and types.
Fields are specified as alternating keyword-type pairs. Computes field offsets, alignment, and padding following the platform's default struct layout rules.
Examples:
(define point-layout
(c-struct-layout
x: ffi/double
y: ffi/double
z: ffi/double))c-struct-sizeprocedureReturn the total size of a struct layout in bytes.
c-struct-offsetprocedureReturn the byte offset of a field within a struct.
c-struct-refprocedureRead a struct field through a pointer.
Examples:
(c-struct-ref ptr point-layout x:) ; => 1.0c-struct-set!procedureWrite a struct field through a pointer.
c-struct->dictprocedureRead all fields of a struct into a dict.
Examples:
(c-struct->dict ptr point-layout)
; => #{ x: 1.0 y: 2.0 z: 3.0 }dict->c-structprocedureAllocate a struct and populate from a dict.
Fields present in the dict are written; missing fields are zeroed.
c-alloc-structprocedureAllocate memory for one instance of a struct layout, zeroed.
with-c-allocprocedureCall PROC with a temporarily allocated struct, freeing it after.
Examples:
(with-c-alloc point-layout
(lambda (ptr)
(c-struct-set! ptr point-layout x: 1.0)
(c-struct-ref ptr point-layout x:)))ffi/voidvariable(No description)
ffi/boolvariable(No description)
ffi/int8variable(No description)
ffi/uint8variable(No description)
ffi/int16variable(No description)
ffi/uint16variable(No description)
ffi/int32variable(No description)
ffi/uint32variable(No description)
ffi/int64variable(No description)
ffi/uint64variable(No description)
ffi/floatvariable(No description)
ffi/doublevariable(No description)
ffi/pointervariable(No description)
ffi/stringvariable(No description)
ffi/intvariable(No description)
ffi/uintvariable(No description)
ffi/longvariable(No description)
ffi/ulongvariable(No description)
ffi/size-tvariable(No description)
define-c-libraryvariable(No description)
symbol->c-namevariable(No description)