Home

FFI

Foreign Function Interface (FFI) is a mechanism that allows code written in one programming language to call routines or use services written in another language. It provides a bridge between languages, handling symbol resolution, data type conversion (marshalling), and calling conventions. FFI can be provided by language runtimes, compilers, or external libraries, and can operate at compile time or at runtime.

Implementation approaches include static bindings generated at compile time or dynamic bindings resolved at runtime. A

FFI is widely used to access system APIs, to reuse performance-critical native libraries, or to interoperate

common
tool
is
libffi,
a
portable
library
that
enables
calling
into
functions
whose
interface
is
not
known
until
runtime.
Languages
expose
FFI
bindings
to
C
libraries,
such
as
Python's
ctypes
and
cffi,
Ruby's
FFI,
LuaJIT
FFI,
and
Rust's
extern
"C"
interfaces.
Data
types
must
be
mapped
between
languages,
including
integers,
floating
point
numbers,
pointers,
and
structs;
memory
ownership
and
lifetimes
must
be
managed
to
avoid
leaks
or
undefined
behavior.
with
legacy
code.
It
enables
language
ecosystems
to
leverage
existing
code
without
full
reimplementation,
but
it
can
introduce
portability
challenges
due
to
differences
in
ABIs,
alignment,
and
platform
conventions.
Proper
use
requires
attention
to
error
translation,
threading
models,
and
security
implications.