libffi

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search
libffi
DeveloperAnthony Green
Initial releaseOctober 7, 1996; 29 years ago (1996-10-07)
Repository
  • {{URL|example.com|optional display text}}Lua error in Module:EditAtWikidata at line 29: attempt to index field 'wikibase' (a nil value).
Written inC, Assembly language
Engine
    Lua error in Module:EditAtWikidata at line 29: attempt to index field 'wikibase' (a nil value).
    Operating systemUnix-like, Microsoft Windows, OS X, iOS, bare metal
    TypeRuntime library
    LicenseMIT License[1]

    Lua error in mw.title.lua at line 392: bad argument #2 to 'title.new' (unrecognized namespace name 'Portal'). 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.

    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,[2] Embeddable Common Lisp and Mozilla.[3]

    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

    [edit | edit source]

    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 (processor–OS 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

    [edit | edit source]

    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

    [edit | edit source]

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

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

    F-Script

    [edit | edit source]
    F-Script
    Dynamically generates Cocoa classes written in F-Script.

    Guile

    [edit | edit source]
    GNU Guile
    libffi is used in Guile 1.9.8 and onwards

    Haskell

    [edit | edit source]
    GHC
    libffi has been used for the majority of the FFI performed by the GHC since late 2009.
    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

    [edit | edit source]
    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).[4]
    node-ffi
    A Node.js addon for loading and calling dynamic libraries from JavaScript.
    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.
    FFI::Raw
    A thin wrapper around libffi.
    FFI::Platypus
    Newer, more convenient wrapper around libffi.

    Python

    [edit | edit source]
    File:UML ffi.svg
    Calling a shared library from within a Python script.
    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

    [edit | edit source]
    Racket
    Call C code from this popular Scheme implementation.
    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.

    References

    [edit | edit source]
    1. ^ Lua error in Module:Citation/CS1/Configuration at line 2172: attempt to index field '?' (a nil value).
    2. ^ Lua error in Module:Citation/CS1/Configuration at line 2172: attempt to index field '?' (a nil value).
    3. ^ Lua error in Module:Citation/CS1/Configuration at line 2172: attempt to index field '?' (a nil value).
    4. ^ 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
    [edit | edit source]
    • Lua error in Module:Official_website at line 94: attempt to index field 'wikibase' (a nil value).
    • Anthony Green's blog