Describe the C ABI
Bind and call one C function on Linux
The ABI is the set of rules about how a program calls native code. Match the C argument and result types when creating a binding. This example uses glibc and its C abs function.
use ffi
lib = ffi.load('libc.so.6')
try:
absolute = ffi.bind(lib, 'abs', [i32], i32)
print(absolute(-7))
finally:
ffi.close(lib)
7
The binding describes one signed 32-bit input and a signed 32-bit result. Use the right library for your target system.
Loading and binding functions
Load a shared library, then bind a symbol to its C signature. Match the argument and return types to the target ABI.
Native types and structures
Use Atom’s native descriptors for scalar C values. When passing a structure, match its field order, alignment, and representation.
Pointers, callbacks, and variadics
Use the C signature to choose pointer descriptors and fixed arguments. Pay attention to ownership before passing a pointer or keeping a native callback.
FFI uses libffi and the host's default C calling convention. Load an ABI-compatible shared library for the same OS and process architecture. Linux commonly uses .so (possibly with a version suffix), macOS uses .dylib, and Windows uses .dll. Pass an explicit path for a bundled library. A bare name uses the OS loader's search rules. Atom passes the path through to dlopen on Unix or LoadLibraryW on Windows. Use exported C symbol names. C++ libraries can expose an extern "C" wrapper. A caller cannot select a different calling convention through ffi.bind. Create a C wrapper for symbols whose ABI the binder cannot represent.
| C representation | Atom FFI descriptor |
|---|---|
int8_t, int16_t, int32_t, int64_t |
i8, i16, i32, i64 |
uint8_t, uint16_t, uint32_t, uint64_t |
u8, u16, u32, u64 |
float, double |
f32, f64 |
| 8-bit boolean ABI | bool. Match the C parameter's actual ABI. |
| 32-bit Unicode scalar | char. For a C single-byte char use i8 or u8 according to the declared type and target. |
void *, opaque handles, typed data pointers, function pointers |
ptr. Supply null, an appropriate native pointer or packed storage. Only use packed storage for compatible data pointers. |
NUL-terminated UTF-8 const char * |
cstring. Value can be a char, a string or null for ordinary native calls. |
void |
void return only. Atom receives null. |
| Native struct by value | Record layout with scalar and supported nested record fields, matching libffi's reported layout. |
For C long, size_t, intptr_t, enum and system typedefs, select the descriptor using the target C header and ABI. For example, Windows x86-64 C long is 32-bit even though size_t is 64-bit. Native pointer and C string descriptors are signature types, with no direct packed field storage. Use a wrapper for unions, bitfields, pointer-bearing records passed by value, non-default calling conventions and structs whose layout libffi rejects. A packed array with the matching scalar or record layout can be passed behind a pointer along with a count. There is no automatic C-array length argument.
Describe parameters in order and supply a return descriptor. Binds are exact-arity. For a variadic function supply the number of fixed parameters as the fifth argument and include the promoted descriptor of every trailing argument. Usual C promotions include float to double and narrow integers to int where representable. Make a separate binding for each arity/type pattern. Libffi validates supported signature layouts. It cannot determine the symbol's actual C declaration.
A by-value record argument must be a packed record with structurally matching fields, sizes and offsets. A returned record is copied into an owning packed value. Arrays inside records are supported as packed storage but do not form a by-value libffi parameter or result. Use an explicit pointer when a fixed inline array or non-native packing is required by C, verify offsets, and pass a pointer to the storage container.