<s>
A	O
foreign	B-Application
function	I-Application
interface	I-Application
(	O
FFI	O
)	O
is	O
a	O
mechanism	O
by	O
which	O
a	O
program	O
written	O
in	O
one	O
programming	O
language	O
can	O
call	O
routines	O
or	O
make	O
use	O
of	O
services	O
written	O
or	O
compiled	O
in	O
another	O
one	O
.	O
</s>
<s>
The	O
term	O
comes	O
from	O
the	O
specification	O
for	O
Common	B-Language
Lisp	I-Language
,	O
which	O
explicitly	O
refers	O
to	O
the	O
programming	O
language	O
feature	O
enabling	O
for	O
inter-language	O
calls	O
as	O
such	O
;	O
the	O
term	O
is	O
also	O
often	O
used	O
officially	O
by	O
the	O
Haskell	B-Language
,	O
Rust	B-Application
,	O
Python	B-Language
,	O
and	O
LuaJIT	B-Application
(	O
Lua	B-Language
)	O
interpreter	O
and	O
compiler	O
documentations	O
.	O
</s>
<s>
Other	O
languages	O
use	O
other	O
terminology	O
:	O
the	O
Ada	B-Language
programming	I-Language
language	I-Language
talks	O
about	O
"	O
language	B-Application
bindings	I-Application
"	O
,	O
while	O
Java	B-Language
refers	O
to	O
its	O
FFI	O
as	O
the	O
JNI	B-Language
(	O
Java	B-Language
Native	I-Language
Interface	I-Language
)	O
or	O
JNA	B-Language
(	O
Java	B-Language
Native	I-Language
Access	I-Language
)	O
.	O
</s>
<s>
Foreign	B-Application
function	I-Application
interface	I-Application
has	O
become	O
generic	O
terminology	O
for	O
mechanisms	O
which	O
provide	O
such	O
services	O
.	O
</s>
<s>
The	O
primary	O
function	O
of	O
a	O
foreign	B-Application
function	I-Application
interface	I-Application
is	O
to	O
mate	O
the	O
semantics	O
and	O
calling	O
conventions	O
of	O
one	O
programming	O
language	O
(	O
the	O
host	O
language	O
,	O
or	O
the	O
language	O
which	O
defines	O
the	O
FFI	O
)	O
,	O
with	O
the	O
semantics	O
and	O
conventions	O
of	O
another	O
(	O
the	O
guest	O
language	O
)	O
.	O
</s>
<s>
This	O
process	O
must	O
also	O
take	O
into	O
consideration	O
the	O
runtime	B-Device
environments	I-Device
and/or	O
application	B-Operating_System
binary	I-Operating_System
interfaces	I-Operating_System
of	O
both	O
.	O
</s>
<s>
Use	O
of	O
a	O
tool	O
to	O
automatically	O
"	O
wrap	O
"	O
guest-language	O
functions	O
with	O
appropriate	O
glue	B-General_Concept
code	I-General_Concept
,	O
which	O
performs	O
any	O
necessary	O
translation	O
.	O
</s>
<s>
For	O
example	O
,	O
C++	B-Language
functions	O
called	O
from	O
C	B-Language
may	O
not	O
(	O
in	O
general	O
)	O
include	O
reference	O
parameters	O
or	O
throw	O
exceptions	O
.	O
</s>
<s>
If	O
one	O
language	O
supports	O
garbage	B-General_Concept
collection	I-General_Concept
(	O
GC	O
)	O
and	O
the	O
other	O
does	O
not	O
;	O
care	O
must	O
be	O
taken	O
that	O
the	O
non-GC	O
language	O
code	O
does	O
nothing	O
to	O
cause	O
GC	O
in	O
the	O
other	O
to	O
fail	O
.	O
</s>
<s>
In	O
JNI	B-Language
,	O
for	O
example	O
,	O
C	B-Language
code	O
which	O
"	O
holds	O
on	O
to	O
"	O
object	O
references	O
that	O
it	O
receives	O
from	O
Java	B-Language
must	O
"	O
register	O
"	O
this	O
fact	O
with	O
the	O
Java	B-Language
runtime	I-Language
environment	I-Language
(	O
JRE	O
)	O
;	O
otherwise	O
,	O
Java	B-Language
may	O
delete	O
objects	O
before	O
C	B-Language
has	O
finished	O
with	O
them	O
.	O
</s>
<s>
(	O
The	O
C	B-Language
code	O
must	O
also	O
explicitly	O
release	O
its	O
link	O
to	O
any	O
such	O
object	O
once	O
C	B-Language
has	O
no	O
further	O
need	O
of	O
that	O
object	O
.	O
)	O
</s>
<s>
One	O
or	O
both	O
languages	O
may	O
be	O
running	O
on	O
a	O
virtual	B-Architecture
machine	I-Architecture
(	O
VM	O
)	O
;	O
moreover	O
,	O
if	O
both	O
are	O
,	O
these	O
will	O
probably	O
be	O
different	O
VMs	O
.	O
</s>
<s>
Cross-language	O
inheritance	B-Language
and	O
other	O
differences	O
,	O
such	O
as	O
between	O
type	O
systems	O
or	O
between	O
object-composition	B-Language
models	I-Language
,	O
may	O
be	O
especially	O
difficult	O
.	O
</s>
<s>
Ada	B-Language
language	I-Language
bindings	O
,	O
allowing	O
not	O
only	O
to	O
call	O
foreign	O
functions	O
but	O
also	O
to	O
export	O
its	O
functions	O
and	O
methods	O
to	O
be	O
called	O
from	O
non-Ada	O
code	O
.	O
</s>
<s>
C++	B-Language
has	O
a	O
trivial	O
FFI	O
with	O
C	B-Language
,	O
as	O
the	O
languages	O
share	O
a	O
significant	O
common	O
subset	O
.	O
</s>
<s>
The	O
primary	O
effect	O
of	O
the	O
declaration	O
in	O
C++	B-Language
is	O
to	O
disable	O
C++	B-Language
name	B-Language
mangling	I-Language
.	O
</s>
<s>
Clean	B-Operating_System
provides	O
a	O
bidirectional	O
FFI	O
with	O
all	O
languages	O
following	O
C	B-Language
or	O
the	O
stdcall	O
calling	O
convention	O
.	O
</s>
<s>
CNI	O
,	O
alternative	O
to	O
JNI	B-Language
used	O
in	O
the	O
GNU	O
compiler	O
environment	O
.	O
</s>
<s>
One	O
of	O
the	O
bases	O
of	O
the	O
Component	B-Application
Object	I-Application
Model	I-Application
is	O
a	O
common	O
interface	O
format	O
,	O
which	O
natively	O
uses	O
the	O
same	O
types	O
as	O
Visual	B-Language
Basic	I-Language
for	O
strings	O
and	O
arrays	O
.	O
</s>
<s>
Dynamic	B-Language
languages	I-Language
,	O
such	O
as	O
Python	B-Language
,	O
Perl	B-Language
,	O
Tcl	B-Operating_System
,	O
and	O
Ruby	B-Language
,	O
all	O
provide	O
easy	O
access	O
to	O
native	O
code	O
written	O
in	O
C/C	O
++	O
(	O
or	O
any	O
other	O
language	O
obeying	O
C/C	O
++	O
calling	O
conventions	O
)	O
.	O
</s>
<s>
Factor	B-Application
has	O
FFIs	O
for	O
C	B-Language
,	O
,	O
,	O
and	O
;	O
all	O
of	O
these	O
enable	O
importing	O
and	O
calling	O
arbitrary	O
shared	O
libraries	O
dynamically	O
.	O
</s>
<s>
Fortran	B-Application
2003	O
has	O
a	O
module	O
ISO_C_BINDING	O
which	O
provides	O
interoperable	O
data	O
types	O
(	O
both	O
intrinsic	O
types	O
and	O
POD	O
structs	O
)	O
,	O
interoperable	O
pointers	O
,	O
interoperable	O
global	O
data	O
stores	O
,	O
and	O
mechanisms	O
for	O
calling	O
C	B-Language
from	O
Fortran	B-Application
and	O
for	O
calling	O
Fortran	B-Application
from	O
C	B-Language
.	O
It	O
has	O
been	O
improved	O
in	O
the	O
Fortran	B-Application
2018	O
standard	O
.	O
</s>
<s>
Go	B-Application
can	O
call	O
C	B-Language
code	O
directly	O
via	O
the	O
"	O
C	B-Language
"	O
pseudo-package	O
.	O
</s>
<s>
GWT	B-Language
,	O
in	O
which	O
Java	B-Language
is	O
compiled	O
to	O
JavaScript	O
,	O
has	O
an	O
FFI	O
called	O
JSNI	B-Language
which	O
allows	O
Java	B-Language
source	O
to	O
call	O
arbitrary	O
JavaScript	O
functions	O
,	O
and	O
for	O
JavaScript	O
to	O
call	O
back	O
into	O
Java	B-Language
.	O
</s>
<s>
JNI	B-Language
,	O
which	O
provides	O
an	O
interface	O
between	O
Java	B-Language
and	O
C/C	O
++	O
,	O
the	O
preferred	O
systems	O
languages	O
on	O
most	O
systems	O
where	O
Java	B-Language
is	O
deployed	O
.	O
</s>
<s>
JNA	B-Language
provides	O
an	O
interface	O
with	O
native	O
libraries	O
without	O
having	O
to	O
write	O
glue	B-General_Concept
code	I-General_Concept
.	O
</s>
<s>
LuaJIT	B-Application
,	O
a	O
just-in-time	O
implementation	O
of	O
Lua	B-Language
,	O
has	O
an	O
FFI	O
that	O
allows	O
"	O
calling	O
external	O
C	B-Language
functions	O
and	O
using	O
C	B-Language
data	O
structures	O
from	O
pure	O
Lua	B-Language
code	O
"	O
.	O
</s>
<s>
Nim	B-Application
has	O
an	O
FFI	O
which	O
enables	O
it	O
to	O
use	O
source	O
from	O
C	B-Language
,	O
C++	B-Language
,	O
and	O
Objective-C	B-Language
.	O
</s>
<s>
JavaScript	O
:	O
JavaScript	O
is	O
usually	O
run	O
inside	O
web	B-Application
browser	I-Application
runtimes	O
that	O
do	O
n't	O
provide	O
direct	O
access	O
to	O
system	O
libraries	O
or	O
commands	O
to	O
run	O
,	O
but	O
there	O
are	O
few	O
exceptions	O
:	O
</s>
<s>
Node.js	B-Language
provides	O
functions	O
to	O
open	O
precompiled	O
modules	O
that	O
in	O
turn	O
may	O
provide	O
access	O
to	O
non-builtin	O
resources	O
.	O
</s>
<s>
Deno	B-Application
,	O
provides	O
kind	O
of	O
FFI	O
interface	O
via	O
functions	O
.	O
</s>
<s>
Julia	B-Application
has	O
ccall	O
keyword	O
to	O
call	O
C	B-Language
(	O
and	O
other	O
languages	O
,	O
e.g.	O
</s>
<s>
Fortran	B-Application
)	O
;	O
while	O
packages	O
,	O
providing	O
similar	O
no-boilerplate	O
support	O
,	O
are	O
available	O
for	O
some	O
languages	O
e.g.	O
</s>
<s>
for	O
Python	B-Language
(	O
to	O
e.g.	O
</s>
<s>
provide	O
OO	O
support	O
and	O
GC	O
support	O
)	O
,	O
Java	B-Language
(	O
and	O
supports	O
other	O
JDK-languages	O
,	O
such	O
as	O
Scala	O
)	O
and	O
R	O
.	O
Interactive	O
use	O
with	O
C++	B-Language
is	O
also	O
possible	O
with	O
Cxx.jl	O
package	O
.	O
</s>
<s>
PhoneGap	B-Protocol
(	O
was	O
called	O
by	O
the	O
name	O
Apache	B-Protocol
Callback	I-Protocol
,	O
but	O
now	O
Apache	B-Protocol
Cordova	I-Protocol
)	O
is	O
a	O
platform	O
for	O
building	O
native	O
mobile	O
applications	O
using	O
HTML	O
,	O
CSS	O
and	O
JavaScript	O
.	O
</s>
<s>
PHP	B-Application
provides	O
FFI	O
to	O
C	B-Language
.	O
</s>
<s>
Python	B-Language
provides	O
the	O
and	O
modules	O
.	O
</s>
<s>
For	O
example	O
,	O
the	O
ctypes	B-Application
module	O
can	O
load	O
C	B-Language
functions	O
from	O
shared	O
libraries/DLLs	O
on-the-fly	O
and	O
translate	O
simple	O
data	O
types	O
automatically	O
between	O
Python	B-Language
and	O
C	B-Language
semantics	O
as	O
follows	O
:	O
</s>
<s>
Racket	B-Operating_System
has	O
a	O
native	O
FFI	O
based	O
heavily	O
on	O
macros	O
that	O
enables	O
importing	O
arbitrary	O
shared	O
libraries	O
dynamically	O
.	O
</s>
<s>
Ruby	B-Language
provides	O
FFI	O
either	O
through	O
the	O
gem	O
,	O
or	O
through	O
the	O
standard	O
library	O
.	O
</s>
<s>
Rust	B-Application
defines	O
a	O
foreign	B-Application
function	I-Application
interface	I-Application
to	O
functions	O
with	O
various	O
standard	O
ABIs	O
.	O
</s>
<s>
There	O
is	O
also	O
a	O
library	O
for	O
interfacing	O
with	O
Elixir	B-Language
,	O
.	O
</s>
<s>
Visual	B-Language
Basic	I-Language
has	O
a	O
declarative	O
syntax	O
that	O
allows	O
it	O
to	O
call	O
non-Unicode	O
C	B-Language
functions	O
.	O
</s>
<s>
Wolfram	B-Language
Language	I-Language
provides	O
a	O
technology	O
called	O
WSTP	O
(	O
Wolfram	B-Language
Symbolic	O
Transfer	O
Protocol	O
)	O
which	O
enables	O
bidirectional	O
calling	O
of	O
code	O
between	O
other	O
languages	O
with	O
bindings	O
for	O
C++	B-Language
,	O
Java	B-Language
,	O
.NET	O
and	O
other	O
languages	O
.	O
</s>
<s>
Zig	B-Application
provides	O
FFI	O
to	O
c	B-Language
using	O
the	O
builtin	O
function	O
.	O
</s>
<s>
In	O
addition	O
,	O
many	O
FFIs	O
can	O
be	O
generated	O
automatically	O
:	O
for	O
example	O
,	O
SWIG	B-Application
.	O
</s>
<s>
However	O
,	O
in	O
the	O
case	O
of	O
an	O
extension	B-Language
language	I-Language
a	O
semantic	O
inversion	O
of	O
the	O
relationship	O
of	O
guest	O
and	O
host	O
can	O
occur	O
,	O
when	O
a	O
smaller	O
body	O
of	O
extension	B-Language
language	I-Language
is	O
the	O
guest	O
invoking	O
services	O
in	O
the	O
larger	O
body	O
of	O
host	O
language	O
,	O
such	O
as	O
writing	O
a	O
small	O
plugin	O
for	O
GIMP	O
.	O
</s>
<s>
Some	O
FFIs	O
are	O
restricted	O
to	O
free	O
standing	O
functions	O
,	O
while	O
others	O
also	O
allow	O
calls	O
of	O
functions	O
embedded	O
in	O
an	O
object	O
or	O
class	O
(	O
often	O
called	O
method	B-Language
calls	I-Language
)	O
;	O
some	O
even	O
permit	O
migration	O
of	O
complex	O
datatypes	O
and/or	O
objects	O
across	O
the	O
language	O
boundary	O
.	O
</s>
<s>
In	O
most	O
cases	O
,	O
an	O
FFI	O
is	O
defined	O
by	O
a	O
"	O
higher-level	O
"	O
language	O
,	O
so	O
that	O
it	O
may	O
employ	O
services	O
defined	O
and	O
implemented	O
in	O
a	O
lower	O
level	O
language	O
,	O
typically	O
a	O
systems	O
language	O
like	O
C	B-Language
or	O
C++	B-Language
.	O
</s>
<s>
This	O
is	O
typically	O
done	O
to	O
either	O
access	O
OS	O
services	O
in	O
the	O
language	O
in	O
which	O
the	O
OS	O
 '	O
API	B-Application
is	O
defined	O
,	O
or	O
for	O
performance	O
considerations	O
.	O
</s>
<s>
The	O
term	O
foreign	B-Application
function	I-Application
interface	I-Application
is	O
generally	O
not	O
used	O
to	O
describe	O
multi-lingual	O
runtimes	O
such	O
as	O
the	O
Microsoft	O
Common	O
Language	O
Runtime	O
,	O
where	O
a	O
common	O
"	O
substrate	O
"	O
is	O
provided	O
which	O
enables	O
any	O
CLR-compliant	O
language	O
to	O
use	O
services	O
defined	O
in	O
any	O
other	O
.	O
</s>
<s>
In	O
addition	O
,	O
many	O
distributed	O
computing	O
architectures	O
such	O
as	O
the	O
Java	B-Language
remote	I-Language
method	I-Language
invocation	I-Language
(	O
RMI	O
)	O
,	O
RPC	O
,	O
CORBA	B-Architecture
,	O
SOAP	B-Application
and	O
D-Bus	B-Language
permit	O
different	O
services	O
to	O
be	O
written	O
in	O
different	O
languages	O
;	O
such	O
architectures	O
are	O
generally	O
not	O
considered	O
FFIs	O
.	O
</s>
<s>
There	O
are	O
some	O
special	O
cases	O
,	O
in	O
which	O
the	O
languages	O
compile	O
into	O
the	O
same	O
bytecode	O
VM	O
,	O
like	O
Clojure	B-Language
and	O
Java	B-Language
,	O
as	O
well	O
as	O
Elixir	B-Language
and	O
Erlang	B-Operating_System
.	O
</s>
