Libffi

Last updated
libffi
Developer(s) Anthony Green
Initial releaseOctober 7, 1996;28 years ago (1996-10-07)
Stable release
3.4.6 [1]   OOjs UI icon edit-ltr-progressive.svg / 18 February 2024;9 months ago (18 February 2024)
Repository
Written in C, Assembly language
Operating system Unix-like, Microsoft Windows, OS X, iOS, bare metal
Type Runtime library
License MIT License [2]
Website sourceware.org/libffi/   OOjs UI icon edit-ltr-progressive.svg

libffi is a foreign function interface library. It provides a C programming language interface for calling natively compiled functions given information about the target function at run time instead of compile time. 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.

Contents

libffi is most often used as a bridging technology between compiled 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, Haskell, Dalvik, F-Script, PyPy, PyObjC, RubyCocoa, JRuby, Rubinius, MacRuby, gcj, GNU Smalltalk, IcedTea, Cycript, Pawn, Java Native Access, Common Lisp (via CFFI), Racket, [3] Embeddable Common Lisp and Mozilla. [4]

On Mac OS X, libffi is commonly used with BridgeSupport, which provides programming language neutral descriptions of framework interfaces, and Nu which binds direct Objective-C access from Lisp.

libffi has been widely ported and is released under a MIT license.

Background

Although the C programming language is ubiquitous among platforms, the ways function calls are implemented in machine code the calling convention 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 (processorOS combinations). Its C API, which is shared on all builds of libffi regardless of platform, abstracts over the complexity of loading code on different platforms. In addition to regular functions, it also supports C-style variadic functions.

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. Gencall was developed by Gianni Mariani, then employed by SGI, 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 the MCI (Managed Compiler Infrastructure).

F-Script

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

Guile

GNU Guile
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
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 mobile devices. libffi is used on Android ports for which no custom bridging code has been written.
Java Native Access (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 was a part of the GCC, the GNU Compiler Collection.

JavaScript

JSCocoa
Call Objective-C code from javascript on Mac OS X and the iPhone (via the libffi-iphone port).
Mozilla
libffi is used in the js-ctypes library (previously known as ctypes.jsm) to call C functions within JavaScript code (available in XULRunner applications, Firefox extensions etc). [5]
node-ffi
A Node.js addon for loading and calling dynamic libraries from JavaScript.

Lisp

cffi-libffi
The standard foreign function interface library for Common Lisp CFFI includes the cffi-libffi system to include support for passing and returning structure arguments by value.
pixie
pixie (a Clojure 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
The default, most-widely used implementation of the Python programming language uses libffi in the standard ctypes library.
PyObjC
Call Objective-C code from Python on Mac OS X.

Racket

Racket
Call C code from this popular Scheme implementation.

Ruby

Fiddle
A libffi wrapper in the Ruby Standard Library
Ruby-FFI
A Foreign Function Interface extension for Ruby.
RubyCocoa
Call Objective-C code from Ruby on Mac OS X.

Related Research Articles

<span class="mw-page-title-main">Emacs Lisp</span> Dialect of Lisp in the Emacs text editor

Emacs Lisp is a Lisp dialect made for Emacs. It is used for implementing most of the editing functionality built into Emacs, the remainder being written in C, as is the Lisp interpreter.

An integrated development environment (IDE) is a software application that provides comprehensive facilities for software development. An IDE normally consists of at least a source-code editor, build automation tools, and a debugger. Some IDEs, such as IntelliJ IDEA, Eclipse and Lazarus contain the necessary compiler, interpreter or both; others, such as SharpDevelop and NetBeans, do not.

Cocoa is Apple's native object-oriented application programming interface (API) for its desktop operating system macOS.

<span class="mw-page-title-main">Application binary interface</span> Binary interface between two program units

In computer software, an application binary interface (ABI) is an interface between two binary program modules. Often, one of these modules is a library or operating system facility, and the other is a program that is being run by a user.

In computing, cross-platform software is computer software that is designed to work in several computing platforms. Some cross-platform software requires a separate build for each platform, but some can be directly run on any platform without special preparation, being written in an interpreted language or compiled to portable bytecode for which the interpreters or run-time packages are common or standard components of all supported platforms.

Bytecode is a form of instruction set designed for efficient execution by a software interpreter. Unlike human-readable source code, bytecodes are compact numeric codes, constants, and references that encode the result of compiler parsing and performing semantic analysis of things like type, scope, and nesting depths of program objects.

The GNU Compiler for Java (GCJ) is a discontinued free compiler for the Java programming language. It was part of the GNU Compiler Collection.

The Simplified Wrapper and Interface Generator (SWIG) is an open-source software tool used to connect computer programs or libraries written in C or C++ with scripting languages such as Lua, Perl, PHP, Python, R, Ruby, Tcl, and other language implementations like C#, Java, JavaScript, Go, D, OCaml, Octave, Scilab and Scheme. Output can also be in the form of XML.

<span class="mw-page-title-main">GNU Guile</span> Extension language

GNU Ubiquitous Intelligent Language for Extensions 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 to large parts of Scheme standards, Guile Scheme includes modularized extensions for many different programming tasks.

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 from Common Lisp code, and runs on most platforms that support a C compiler. The ECL runtime is a dynamically loadable library for use by applications. It is distributed as free software under a GNU Lesser Public License (LGPL) 2.1+.

In programming and software design, a binding is an application programming interface (API) that provides glue code specifically made to allow a programming language to use a foreign library or operating system service.

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 or compiled in another one. An FFI is often used in contexts where calls are made into a binary dynamic-link library.

LispWorks is computer software, a proprietary implementation and integrated development environment (IDE) for the programming language Common Lisp. LispWorks was developed by the UK software company Harlequin Ltd., and first published in 1989. Harlequin ultimately spun off its Lisp division as Xanalys Ltd., which took over management and rights to LispWorks. In January 2005, the Xanalys Lisp team formed LispWorks Ltd. to market, develop, and support the software.

<span class="mw-page-title-main">Scripting language</span> Programming language designed for scripting

In computing, a script is a relatively short and simple set of instructions that typically automate an otherwise manual process. The act of writing a script is called scripting. Scripting language or script language describes a programming language that is used for scripting.

<span class="mw-page-title-main">PicoLisp</span> Programming language, a dialect of the language Lisp

PicoLisp is a programming language, a dialect of the language Lisp. It runs on operating systems including Linux and others that are Portable Operating System Interface (POSIX) compliant. Its most prominent features are simplicity and minimalism. It is built on one internal data type: a cell. On the language level, a programmer can use three different data types being represented by cells and differentiated by bits at the end of the cell. It is free and open-source software released under an MIT License (X11).

Java bytecode is the instruction set of the Java virtual machine (JVM), the language to which Java and other JVM-compatible source code is compiled. Each instruction is represented by a single byte, hence the name bytecode, making it a compact form of data.

References

  1. "Release 3.4.6". 18 February 2024. Retrieved 20 February 2024.
  2. "Status". GitHub . 25 April 2022.
  3. "Archived copy" (PDF). Archived from the original (PDF) on 2009-09-02. Retrieved 2009-08-02.{{cite web}}: CS1 maint: archived copy as title (link)
  4. "Mozilla-central @ 2dc00d4b379a files manifest".
  5. https://developer.mozilla.org/en/js-ctypes Archived 2011-06-29 at the Wayback Machine , js-ctypes reference Archived 2011-06-29 at the Wayback Machine