sigildocs

(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.0

Exports

Open a shared library by name. Pass #f to open the current process (libc, etc.).

Close a shared library handle.

Look up a symbol in a library, returning a pointer or #f.

Bind a C function for calling through the FFI.

%ffi-callprocedure

Call a bound FFI function with marshaling.

Return the size of an FFI type in bytes.

Return the alignment of an FFI type in bytes.

Create a pointer from an integer address.

Return the integer address of a pointer.

%ffi-pointer?procedure

Check if a value is an FFI pointer.

Read a typed value at a byte offset from a pointer.

Write a typed value at a byte offset into a pointer.

%ffi-allocprocedure

Allocate zeroed memory. Returns a pointer.

%ffi-freeprocedure

Free allocated memory.

Convert a Scheme string to a malloc'd null-terminated C string pointer.

Convert a C string pointer to a Scheme string.

%ffi-errnoprocedure

Return the current value of errno.

%ffi-strerrorprocedure

Convert an errno value to its string description.

Set a finalizer function on a pointer.

Copy pointer data to a new bytevector.

Get a pointer to a bytevector's data.

%ffi-library?procedure

Check if a value is an FFI library handle.

Check if a value is an FFI function binding.

Create a C-callable function pointer from a Scheme procedure.

Release a callback slot for reuse.

c-libraryprocedure

Open 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))

Close a library handle and unload the shared library.

c-library?procedure

Check if a value is a library handle.

c-symbolprocedure

Look up a symbol in a library, returning a pointer. Raises an error if the symbol is not found.

c-symbol?procedure

Look up a symbol, returning #f if not found.

c-functionprocedure

Create 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?procedure

Check if a value is an FFI function binding.

c-sizeofprocedure

Return the size of an FFI type in bytes.

Examples:

(c-sizeof ffi/double)   ; => 8
(c-sizeof ffi/pointer)  ; => 8
c-alignofprocedure

Return the alignment of an FFI type in bytes.

make-pointerprocedure

Create a pointer from an integer address.

Return the integer address of a pointer.

pointer+procedure

Return a new pointer offset by N bytes.

pointer?procedure

Pointer type predicate.

null-pointer?procedure

Test whether a pointer is null (#f).

pointer-refprocedure

Read 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 8
pointer-set!procedure

Write a value of the given FFI type at a byte offset from a pointer.

Convert a Scheme string to a null-terminated UTF-8 C string. Returns a pointer. The caller must free the memory with c-free.

Convert a null-terminated C string pointer to a Scheme string. Optionally specify a length for strings containing nulls.

Copy pointer memory to a new bytevector.

Get a pointer to a bytevector's data (no copy, aliased).

c-allocprocedure

Allocate SIZE bytes of zeroed memory. Returns a pointer.

c-freeprocedure

Free memory allocated by c-alloc.

Register a C function to be called when a pointer is garbage collected.

c-errnoprocedure

Return the current value of errno.

c-strerrorprocedure

Convert an errno value to its string description.

c-callbackprocedure

Create 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)))))

Release a callback slot for reuse.

After calling this, the pointer must not be used again.

Define 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-sizeprocedure

Return the total size of a struct layout in bytes.

Return the byte offset of a field within a struct.

c-struct-refprocedure

Read a struct field through a pointer.

Examples:

(c-struct-ref ptr point-layout x:)  ; => 1.0
c-struct-set!procedure

Write a struct field through a pointer.

Read all fields of a struct into a dict.

Examples:

(c-struct->dict ptr point-layout)
; => #{ x: 1.0 y: 2.0 z: 3.0 }

Allocate a struct and populate from a dict.

Fields present in the dict are written; missing fields are zeroed.

Allocate memory for one instance of a struct layout, zeroed.

with-c-allocprocedure

Call 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)

(No description)

(No description)