HOME

TheInfoList



OR:

libffi is a
foreign function interface A foreign function interface (FFI) is a mechanism by which a program written in one programming language can call routines or make use of services written in another. Naming The term comes from the specification for Common Lisp, which explicit ...
library. It provides a
C programming language ''The C Programming Language'' (sometimes termed ''K&R'', after its authors' initials) is a computer programming book written by Brian Kernighan and Dennis Ritchie, the latter of whom originally designed and implemented the language, as well as ...
interface for calling natively compiled functions given information about the target
function Function or functionality may refer to: Computing * Function key, a type of key on computer keyboards * Function model, a structured representation of processes in a system * Function object or functor or functionoid, a concept of object-oriente ...
at
run time Run(s) or RUN may refer to: Places * Run (island), one of the Banda Islands in Indonesia * Run (stream), a stream in the Dutch province of North Brabant People * Run (rapper), Joseph Simmons, now known as "Reverend Run", from the hip-hop group ...
instead of
compile time In computer science, compile time (or compile-time) describes the time window during which a computer program is compiled. The term is used as an adjective to describe concepts related to the context of program compilation, as opposed to concept ...
. It also implements the opposite functionality: libffi can produce a pointer to a function that can accept and decode any combination of arguments defined at run time. libffi is most often used as a bridging technology between
compiled In computing, a compiler is a computer program that translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primarily used for programs that ...
and interpreted language implementations. libffi may also be used to implement plug-ins, where the plug-in's function signatures are not known at the time of creating the host application. Notable users include
Python Python may refer to: Snakes * Pythonidae, a family of nonvenomous snakes found in Africa, Asia, and Australia ** ''Python'' (genus), a genus of Pythonidae found in Africa and Asia * Python (mythology), a mythical serpent Computing * Python (pro ...
,
Haskell Haskell () is a general-purpose, statically-typed, purely functional programming language with type inference and lazy evaluation. Designed for teaching, research and industrial applications, Haskell has pioneered a number of programming lang ...
, Dalvik, F-Script,
PyPy PyPy () is an implementation of the Python programming language. PyPy often runs faster than the standard implementation CPython because PyPy uses a just-in-time compiler. Most Python code runs well on PyPy except for code that depends on CPytho ...
,
PyObjC PyObjC is a bidirectional bridge between the Python and Objective-C programming languages, allowing programmers to use and extend existing Objective-C libraries, such as Apple's Cocoa framework, using Python. PyObjC is used to develop macOS appli ...
,
RubyCocoa RubyCocoa is a macOS framework that provides a bridge between the Ruby and the Objective-C programming languages, allowing the user to manipulate Objective-C objects from Ruby, and vice versa. It makes it possible to write a Cocoa application c ...
,
JRuby JRuby is an implementation of the Ruby programming language atop the Java Virtual Machine, written largely in Java. It is free software released under a three-way EPL/GPL/LGPL license. JRuby is tightly integrated with Java to allow the embedding ...
,
Rubinius Rubinius was an alternative Ruby (programming language), Ruby implementation created by Evan Phoenix. Based loosely on the Smalltalk-80 ''Blue Book'' design, Rubinius sought to "provide a rich, high-performance environment for running Ruby code." ...
,
MacRuby MacRuby is a discontinued implementation of the Ruby language that ran on the Objective-C runtime and CoreFoundation framework under development by Apple Inc. which "was supposed to replace RubyCocoa". It targeted Ruby 1.9 and used the high per ...
, gcj,
GNU Smalltalk GNU Smalltalk is an implementation of the Smalltalk programming language by the GNU Project. The implementation, unlike other Smalltalk environments, uses text files for program input and interprets the contents as Smalltalk code. In this way, ...
, IcedTea, Cycript,
Pawn Pawn most often refers to: * Pawn (chess), the weakest and most numerous piece in the game * Pawnbroker or pawnshop, a business that provides loans by taking personal property as collateral Pawn may also refer to: Places * Pawn, Oregon, an his ...
,
Squeak Squeak is an object-oriented, class-based, and reflective programming language. It was derived from Smalltalk-80 by a group that included some of Smalltalk-80's original developers, initially at Apple Computer, then at Walt Disney Imagineering, ...
,
Java Native Access Java Native Access (JNA) is a community-developed library that provides Java programs easy access to native shared libraries without using the Java Native Interface (JNI). JNA's design aims to provide native access in a natural way with a minimum ...
,
Common Lisp Common Lisp (CL) is a dialect of the Lisp programming language, published in ANSI standard document ''ANSI INCITS 226-1994 (S20018)'' (formerly ''X3.226-1994 (R1999)''). The Common Lisp HyperSpec, a hyperlinked HTML version, has been derived fro ...
(via CFFI),
Racket Racket may refer to: * Racket (crime), a systematised element of organized crime ** Protection racket, a scheme whereby a group provides protection to businesses or other groups through violence outside the sanction of the law * Racket (sports equ ...
,
Embeddable Common Lisp Embeddable Common Lisp (ECL) is a small implementation of the ANSI Common Lisp programming language that can be used stand-alone or embedded in extant applications written in C. It creates OS-native executables and libraries (i.e. Executable and ...
and
Mozilla Mozilla (stylized as moz://a) is a free software community founded in 1998 by members of Netscape. The Mozilla community uses, develops, spreads and supports Mozilla products, thereby promoting exclusively free software and open standards, wi ...
. On
Mac OS X macOS (; previously OS X and originally Mac OS X) is a Unix operating system developed and marketed by Apple Inc. since 2001. It is the primary operating system for Apple's Mac (computer), Mac computers. Within the market of ...
, libffi is commonly used with BridgeSupport, which provides
programming language A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language. The description of a programming ...
neutral descriptions of
framework A framework is a generic term commonly referring to an essential supporting structure which other things are built on top of. Framework may refer to: Computing * Application framework, used to implement the structure of an application for an op ...
interfaces, and Nu which binds direct
Objective-C Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. Originally developed by Brad Cox and Tom Love in the early 1980s, it was selected by NeXT for its NeXTS ...
access from
Lisp A lisp is a speech impairment in which a person misarticulates sibilants (, , , , , , , ). These misarticulations often result in unclear speech. Types * A frontal lisp occurs when the tongue is placed anterior to the target. Interdental lisping ...
. libffi has been widely
ported In software engineering, porting is the process of adapting software for the purpose of achieving some form of execution in a computing environment that is different from the one that a given program (meant for such execution) was originally desi ...
and is released under a
MIT license The MIT License is a permissive free software license originating at the Massachusetts Institute of Technology (MIT) in the late 1980s. As a permissive license, it puts only very limited restriction on reuse and has, therefore, high license comp ...
.


Background

Although the C programming language is ubiquitous among platforms, the ways function calls are implemented in machine code – the
calling convention In computer science, a calling convention is an implementation-level (low-level) scheme for how subroutines or functions receive parameters from their caller and how they return a result. When some code calls a function, design choices have been ...
– vary. When one wants to load a subroutine dynamically at run-time, a knowledge of these conventions is required. libffi has knowledge of the calling convention on many platforms (processor–OS combinations). Its C
API An application programming interface (API) is a way for two or more computer programs to communicate with each other. It is a type of software Interface (computing), interface, offering a service to other pieces of software. A document or standa ...
, which is shared on all builds of libffi regardless of platform,
abstracts An abstract is a brief summary of a research article, thesis, review, academic conference, conference proceedings, proceeding, or any in-depth analysis of a particular subject and is often used to help the reader quickly ascertain the paper's purpo ...
over the complexity of loading code on different platforms. In addition to regular functions, it also supports C-style
variadic function In mathematics and in computer programming, a variadic function is a function of indefinite arity, i.e., one which accepts a variable number of arguments. Support for variadic functions differs widely among programming languages. The term ''varia ...
s. The C calling convention is not only used by the C language: due to the amount of existing code written in C, most newer compiled languages also allow writing and calling functions in such a convention. As a result, libffi is able to interact with some functions written in these languages too.


History

libffi, originally developed by Anthony Green, was inspired by the Gencall library from
Silicon Graphics Silicon Graphics, Inc. (stylized as SiliconGraphics before 1999, later rebranded SGI, historically known as Silicon Graphics Computer Systems or SGCS) was an American high-performance computing manufacturer, producing computer hardware and soft ...
. Gencall was developed by Gianni Mariani, then employed by
SGI SGI may refer to: Companies *Saskatchewan Government Insurance *Scientific Games International, a gambling company *Silicon Graphics, Inc., a former manufacturer of high-performance computing products *Silicon Graphics International, formerly Rac ...
, for the purpose of allowing calls to functions by address and creating a call frame for the particular calling convention ( MIPS O32). Anthony Green refined the idea and extended it to other architectures and calling conventions and open sourcing libffi.


Adoption

The libffi library is useful in building a bridge between interpreted and natively compiled code. Some notable users include:


D

:
libffi-d
:: Bindings to the libffi library for D. Primarily used for th
MCI
(Managed Compiler Infrastructure).


F-Script

:; F-Script :: Dynamically generates Cocoa classes written in F-Script.


Guile

:;
GNU Guile GNU Ubiquitous Intelligent Language for Extensions (GNU Guile) is the preferred extension language system for the GNU Project and features an implementation of the programming language Scheme. Its first version was released in 1993. In addition ...
:: libffi is used in Guile 1.9.8 and onwards


Haskell

:; GHC :: libffi has been used for the majority of the FFI performed by the GHC since late 2009.


Java

:;
OpenJDK OpenJDK (Open Java Development Kit) is a free and open-source implementation of the Java Platform, Standard Edition (Java SE). It is the result of an effort Sun Microsystems began in 2006. The implementation is licensed under the GPL-2.0-only wi ...
:: The open-source implementation of the Java Platform Standard Edition uses libffi to bridge between the interpreter and native code for Zero-Assembly port. :; Dalvik :: Dalvik is the virtual machine which runs the Java platform on
Android Android may refer to: Science and technology * Android (robot), a humanoid robot or synthetic organism designed to imitate a human * Android (operating system), Google's mobile operating system ** Bugdroid, a Google mascot sometimes referred to ...
mobile devices. libffi is used on Android ports for which no custom bridging code has been written. :;
Java Native Access Java Native Access (JNA) is a community-developed library that provides Java programs easy access to native shared libraries without using the Java Native Interface (JNI). JNA's design aims to provide native access in a natural way with a minimum ...
(JNA) :: The JNI-free way to call native code from Java. :; gcj :: The runtime library for the GNU Compiler for the Java Programming Language uses libffi to handle calls back and forth between interpreted and natively compiled code. gcj is part of the GCC, the
GNU Compiler Collection The GNU Compiler Collection (GCC) is an optimizing compiler produced by the GNU Project supporting various programming languages, hardware architectures and operating systems. The Free Software Foundation (FSF) distributes GCC as free software ...
.


JavaScript

:
JSCocoa
:: Call Objective-C code from javascript on Mac OS X and the iPhone (via the libffi-iphone port). :;
Mozilla Mozilla (stylized as moz://a) is a free software community founded in 1998 by members of Netscape. The Mozilla community uses, develops, spreads and supports Mozilla products, thereby promoting exclusively free software and open standards, wi ...
:: libffi is used in the js-ctypes library (previously known as ctypes.jsm) to call C functions within
JavaScript JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of Website, websites use JavaScript on the Client (computing), client side ...
code (available in
XULRunner XULRunner is a discontinued, packaged version of the Mozilla platform to enable standalone desktop application development using XUL, developed by Mozilla. It replaced the ''Gecko Runtime Environment'', a stalled project with a similar purpose. T ...
applications,
Firefox Mozilla Firefox, or simply Firefox, is a free and open-source web browser developed by the Mozilla Foundation and its subsidiary, the Mozilla Corporation. It uses the Gecko rendering engine to display web pages, which implements current and ...
extensions etc).https://developer.mozilla.org/en/js-ctypesjs-ctypes reference
/ref> :
node-ffi
:: A
Node.js Node.js is an open-source server environment. Node.js is cross-platform and runs on Windows, Linux, Unix, and macOS. Node.js is a back-end JavaScript runtime environment. Node.js runs on the V8 JavaScript Engine and executes JavaScript code ou ...
addon for loading and calling dynamic libraries from JavaScript.


Lisp

:; cffi-libffi :: The standard foreign function interface library for Common Lis
CFFI
includes the cffi-libffi system to include support for passing and returning structure arguments by value. :
pixie
:: pixie (a
Clojure Clojure (, like ''closure'') is a dynamic and functional dialect of the Lisp programming language on the Java platform. Like other Lisp dialects, Clojure treats code as data and has a Lisp macro system. The current development process is comm ...
inspired Lisp dialect) uses libffi as the default foreign function interface mechanism.


Perl

:
FFI::Raw
:: A thin wrapper around libffi. :
FFI::Platypus
:: Newer, more convenient wrapper around libffi.


Python

:;
CPython CPython is the reference implementation of the Python (programming language), Python programming language. Written in C (programming language), C and Python, CPython is the default and most widely used implementation of the Python language. CP ...
:: The default, most-widely used implementation of the Python programming language uses libffi in the standard ctypes library. :;
PyObjC PyObjC is a bidirectional bridge between the Python and Objective-C programming languages, allowing programmers to use and extend existing Objective-C libraries, such as Apple's Cocoa framework, using Python. PyObjC is used to develop macOS appli ...
:: Call Objective-C code from Python on Mac OS X.


Racket

:;
Racket Racket may refer to: * Racket (crime), a systematised element of organized crime ** Protection racket, a scheme whereby a group provides protection to businesses or other groups through violence outside the sanction of the law * Racket (sports equ ...
:: Call C code from this popular Scheme implementation (also used as the implementation platform for Paul Graham's new Lisp, Arc).


Ruby

:
Fiddle
:: A libffi wrapper in the Ruby Standard Library :
Ruby-FFI
:: A Foreign Function Interface extension for Ruby. :;
RubyCocoa RubyCocoa is a macOS framework that provides a bridge between the Ruby and the Objective-C programming languages, allowing the user to manipulate Objective-C objects from Ruby, and vice versa. It makes it possible to write a Cocoa application c ...
:: Call Objective-C code from Ruby on Mac OS X.


References


External links

*{{official website
Anthony Green's blog
C (programming language) libraries Free computer libraries