Aforeign function interface (FFI) is a mechanism by which a program written in oneprogramming language can call routines or make use of services written or compiled in another one. An FFI is often used in contexts where calls are made into a binarydynamic-link library.
The term comes from the specification forCommon Lisp, which explicitly refers to the programming language feature enabling for inter-language calls as such;[1][citation needed] the term is also often used officially by theinterpreter andcompilerdocumentation forHaskell,[2]Rust,[3]PHP,[4]Python, andLuaJIT (Lua).[5][6]: 35 [7] Other languages use other terminology:Ada haslanguage bindings, whileJava hasJava Native Interface (JNI),Java Native Access (JNA), or since Java 22,Foreign Function and Memory API. Foreign function interface has become generic terminology for mechanisms which provide such services.
The primary function of a foreign function interface is to mate the semantics andcalling conventions of one programming language (thehost language, or the language which defines the FFI), with the semantics and conventions of another (theguest language). This process must also take into consideration theruntime environments andapplication binary interfaces of both. This can be done in several ways:
FFIs may be complicated by the following considerations:

Many FFIs can be generated automatically: for example,SWIG. However, in the case of anextension language a semantic inversion of the relationship of guest and host can occur, when a smaller body of extension language is the guest invoking services in the larger body of host language, such as writing a small plugin[8] for GIMP.[9]
Some FFIs are restricted to free standingfunctions, while others also allow calls of functions embedded in an object or class (often calledmethod calls); some even permit migration of complex datatypes or objects across the language boundary.
In most cases, an FFI is defined by ahigher-level language, so that it may employ services defined and implemented in alower-level language, typically asystem programming language likeC orC++. This is typically done to either accessoperating system (OS) services in the language in which the OS API is defined, or for performance goals.
Many FFIs also provide the means for the called language to invoke services in the host language also.
The term foreign function interface is generally not used to describe multi-lingual runtimes such as the MicrosoftCommon Language Runtime, where a commonsubstrate is provided which enables any CLR-compliant language to use services defined in any other. (However, in this case the CLR does include an FFI,P/Invoke, to call outside the runtime.) In addition, many distributed computing architectures such as theJava remote method invocation (RMI), RPC,CORBA,SOAP andD-Bus permit different services to be written in different languages; such architectures are generally not considered FFIs.
There are some special cases, in which the languages compile into the same bytecode VM, likeClojure andJava, as well asElixir andErlang. Since there is no interface, it is not an FFI, strictly speaking, while it offers the same functions to the user.
Haskell's FFI is used to call functions from other languages (basically C at this point), and for C to call Haskell functions.
This module provides utilities to handle data across non-Rust interfaces, like other programming languages and the underlying operating system. It is mainly of use for FFI (Foreign Function Interface) bindings and code that needs to exchange C-like strings with other languages.
Defined C variables are made available as properties of the FFI instance.
C Foreign Function Interface for Python. The goal is to provide a convenient and reliable way to call compiled C code from Python using interface declarations written in C.