Foreign function interface
Encyclopedia
A foreign function interface (or FFI) is a mechanism by which a program written in one programming language
Programming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....

 can call routines or make use of services written in another. The term comes from the specification for Common Lisp
Common Lisp
Common Lisp, commonly abbreviated CL, is a dialect of the Lisp programming language, published in ANSI standard document ANSI INCITS 226-1994 , . From the ANSI Common Lisp standard the Common Lisp HyperSpec has been derived for use with web browsers...

, which explicitly refers to the language features for inter-language calls as such; the term is also used officially by the Haskell programming language
Haskell (programming language)
Haskell is a standardized, general-purpose purely functional programming language, with non-strict semantics and strong static typing. It is named after logician Haskell Curry. In Haskell, "a function is a first-class citizen" of the programming language. As a functional programming language, the...

. Other languages use other terminology (the Ada programming language talks about "language bindings", while Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...

 refers to its FFI as the Java Native Interface
Java Native Interface
The Java Native Interface is a programming framework that enables Java code running in a Java Virtual Machine to call and to be called by native applications and libraries written in other languages such as C, C++ and assembly.-Purpose and features:JNI enables one to write native methods to...

, or JNI). Foreign function interface has become generic terminology for mechanisms which provide such services.

Despite the name, FFIs are not necessarily restricted to function calls; many FFIs permit method calls on objects; and some even permit migration of non-trivial datatypes and/or objects across the language boundary.

The term foreign function interface is generally not used to describe multi-lingual runtimes such as the Microsoft Common Language Runtime
Common Language Runtime
The Common Language Runtime is the virtual machine component of Microsoft's .NET framework and is responsible for managing the execution of .NET programs. In a process known as just-in-time compilation, the CLR compiles the intermediate language code known as CIL into the machine instructions...

, where a common "substrate" 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 the Java remote method invocation
Java remote method invocation
The Java Remote Method Invocation Application Programming Interface , or Java RMI, is a Java application programming interface that performs the object-oriented equivalent of remote procedure calls ....

 (RMI), RPC, CORBA
Çorba
Chorba , ciorbă , shurpa , shorpo , or sorpa is one of various kinds of soup or stew found in national cuisines across Middle East...

, SOAP
SOAP
SOAP, originally defined as Simple Object Access Protocol, is a protocol specification for exchanging structured information in the implementation of Web Services in computer networks...

 and D-Bus
D-Bus
In computing, D-Bus is a simple inter-process communication open-source system for software applications to communicate with one another. Heavily influenced by KDE2–3's DCOP system, D-Bus has replaced DCOP in the KDE 4 release. An implementation of D-Bus supports most POSIX operating...

 permit different services to be written in different languages; such architectures are generally not considered FFIs.

In most cases, a FFI is defined by a "higher-level" language, so that it may employ services defined and implemented in a lower level language, typically a systems language like C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

 or C++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...

. This is typically done to either access OS services in the language in which the OS' API is defined, or for performance considerations.

Many FFIs also provide the means for the called language to invoke services in the host language as well.

Operation of an FFI

The primary function of a FFI is to mate the semantics and calling convention
Calling convention
In computer science, a calling convention is a scheme for how subroutines receive parameters from their caller and how they return a result; calling conventions can differ in:...

s of one programming language (the host language, or the language which defines the FFI), with the semantics and conventions of another (the guest language). This process must also take into consideration the runtime environments and/or application binary interface
Application binary interface
In computer software, an application binary interface describes the low-level interface between an application program and the operating system or another application.- Description :...

s of both. This can be done in several ways:
  • Requiring that guest-language functions which are to be host-language callable be specified or implemented in a particular way; often using a compatibility library of some sort.
  • Use of a tool to automatically "wrap" guest-language functions with appropriate glue code
    Glue code
    In programming, glue code is code that does not contribute any functionality towards meeting the program's requirements, but instead serves solely to "glue together" different parts of code that would not otherwise be compatible...

    , which performs any necessary translation.
  • Use of wrapper libraries
    Wrapper library
    In computer programming, a library is a collection of subroutines or classes used to develop software. Libraries expose interfaces which clients of the library use to execute library routines. Wrapper libraries consist of a thin layer of code which translates a library's existing interface into a...

  • Restricting the set of host language capabilities which can be used cross-language. For example, C++ functions called from C may not (in general) include reference parameters or throw exceptions.


FFIs may be complicated by the following considerations:
  • If one language supports garbage collection
    Garbage collection (computer science)
    In computer science, garbage collection is a form of automatic memory management. The garbage collector, or just collector, attempts to reclaim garbage, or memory occupied by objects that are no longer in use by the program...

     (GC) and the other does not; care must be taken that the non-GC language code doesn't do something to cause the GC to fail. In JNI, for example, C code which "holds on to" object references passed from Java must "register" this fact with the Java runtime; otherwise the referred-to objects may be garbage-collected if no more valid references to the object(s) exist within the Java environment. (The C code must likewise release such references manually when the corresponding object is no longer needed).
  • Complicated or non-trivial objects or datatype may be difficult to map from one environment to another.
  • It may not be possible for both languages to maintain references to the same instance of a mutable object, due to the mapping issue above.
  • One or both languages may be running on a virtual machine
    Virtual machine
    A virtual machine is a "completely isolated guest operating system installation within a normal host operating system". Modern virtual machines are implemented with either software emulation or hardware virtualization or both together.-VM Definitions:A virtual machine is a software...

     (including different VMs).
  • Cross language inheritance
    Inheritance (computer science)
    In object-oriented programming , inheritance is a way to reuse code of existing objects, establish a subtype from an existing object, or both, depending upon programming language support...

     or other forms of type or object composition may be especially difficult.

Examples

Examples of FFIs include:
  • Ada
    Ada (programming language)
    Ada is a structured, statically typed, imperative, wide-spectrum, and object-oriented high-level computer programming language, extended from Pascal and other languages...

     language bindings, allowing not only to call foreign functions but also to export its functions and methods to be called from non-Ada code.http://www.adaic.org/standards/05aarm/html/AA-B.html
  • C++
    C++
    C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...

     has a trivial FFI with C
    C (programming language)
    C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

    , as the languages share a significant common subset. The primary effect of the extern "C" declaration in C++ is to disable name mangling
    Name mangling
    In compiler construction, name mangling is a technique used to solve various problems caused by the need to resolve unique names for programming entities in many modern programming languages....

    .
  • JNI
    Java Native Interface
    The Java Native Interface is a programming framework that enables Java code running in a Java Virtual Machine to call and to be called by native applications and libraries written in other languages such as C, C++ and assembly.-Purpose and features:JNI enables one to write native methods to...

    , which provides an interface between Java
    Java (programming language)
    Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...

     and C/C++, the preferred systems language on most systems where Java is deployed. JNA
    Java Native Access
    Java Native Access provides Java programs easy access to native shared libraries without using the Java Native Interface. JNA's design aims to provide native access in a natural way with a minimum of effort...

     provide an interface with native libraries without having to write Glue code
    Glue code
    In programming, glue code is code that does not contribute any functionality towards meeting the program's requirements, but instead serves solely to "glue together" different parts of code that would not otherwise be compatible...

    .
  • CNI, alternative to JNI used in the GNU compiler environment.
  • The of Common Lisp
    Common Lisp
    Common Lisp, commonly abbreviated CL, is a dialect of the Lisp programming language, published in ANSI standard document ANSI INCITS 226-1994 , . From the ANSI Common Lisp standard the Common Lisp HyperSpec has been derived for use with web browsers...

     and Haskell
    Haskell (programming language)
    Haskell is a standardized, general-purpose purely functional programming language, with non-strict semantics and strong static typing. It is named after logician Haskell Curry. In Haskell, "a function is a first-class citizen" of the programming language. As a functional programming language, the...

  • The major dynamic languages, such as Python
    Python (programming language)
    Python is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Python claims to "[combine] remarkable power with very clear syntax", and its standard library is large and comprehensive...

    , Perl
    Perl
    Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular...

    , Tcl
    Tcl
    Tcl is a scripting language created by John Ousterhout. Originally "born out of frustration", according to the author, with programmers devising their own languages intended to be embedded into applications, Tcl gained acceptance on its own...

    , and Ruby, all provide easy access to native code written in C/C++ (or any other language obeying C/C++ calling conventions).
    • Python additionally provides the Ctypes module http://www.python.org/doc/2.5/lib/module-ctypes.html, which can load C functions from shared libraries/DLL
      Dynamic-link library
      Dynamic-link library , or DLL, is Microsoft's implementation of the shared library concept in the Microsoft Windows and OS/2 operating systems...

      s on-the-fly and translate simple data types automatically between Python and C semantics. For example:


import ctypes
libc = ctypes.CDLL( '/lib/libc.so.6' ) # under Linux/Unix
t = libc.time(None) # equivalent C code: t = time(NULL)
print t
  • P/Invoke, which provides an interface between the Microsoft Common Language Runtime
    Common Language Runtime
    The Common Language Runtime is the virtual machine component of Microsoft's .NET framework and is responsible for managing the execution of .NET programs. In a process known as just-in-time compilation, the CLR compiles the intermediate language code known as CIL into the machine instructions...

     and native code.
  • Racket has a native FFI based heavily on macros that enables importing arbitrary shared libraries dynamically.http://docs.racket-lang.org/foreign/
  • Factor
    Factor (programming language)
    Factor is a stack-oriented programming language created by Slava Pestov. Factor is dynamically typed and has automatic memory management, as well as powerful metaprogramming features. The language has a single implementation featuring a self-hosted optimizing compiler and an interactive development...

     has FFI for Chttp://fun-factor.blogspot.com/2007/10/getting-started-with-factor-easy-ffi.html, Fortranhttp://article.gmane.org/gmane.comp.lang.factor.general/2790, Objective-Chttp://docs.factorcode.org/content/vocab-cocoa.html, and Windows COMhttp://docs.factorcode.org/content/vocab-windows.com.html; all of these enable importing and calling arbitrary shared libraries dynamically.
  • Visual Basic
    Visual Basic
    Visual Basic is the third-generation event-driven programming language and integrated development environment from Microsoft for its COM programming model...

     has a declarative syntax that allows it to call non-unicode C functions.
  • One of the bases of the Component Object Model
    Component Object Model
    Component Object Model is a binary-interface standard for software componentry introduced by Microsoft in 1993. It is used to enable interprocess communication and dynamic object creation in a large range of programming languages...

     is a common interface format, which natively uses the same types as Visual Basic for strings and arrays.
  • GWT
    Google Web Toolkit
    Google Web Toolkit is an open source set of tools that allows web developers to create and maintain complex JavaScript front-end applications in Java. Other than a few native libraries, everything is Java source that can be built on any supported platform with the included GWT Ant build files...

    , in which Java is compiled to JavaScript, has a FFI called JSNI which allows Java source to call arbitrary JavaScript functions, and for JavaScript to call back into Java.
  • LuaJIT, a just-in-time
    Just-in-time compilation
    In computing, just-in-time compilation , also known as dynamic translation, is a method to improve the runtime performance of computer programs. Historically, computer programs had two modes of runtime operation, either interpreted or static compilation...

     implementation of Lua, does have a FFI that allows "calling external C functions and using C data structures from pure Lua code"http://luajit.org/ext_ffi.html.


In addition, many FFIs can be generated automatically: for example, SWIG
SWIG
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 languages like C#, Java, Modula-3, Objective Caml, Octave, and Scheme...

. However, in the case of an Extension 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 http://www.gimp.org/docs/scheme_plugin/scheme-sample.html for GIMP http://www.gimp.org/docs/scheme_plugin/.

See also

  • Language binding
    Language binding
    In computing, a binding from a programming language to a library or OS service is an API providing that service in the language.Many software libraries are written in systems programming languages such as C or C++...

  • Calling convention
    Calling convention
    In computer science, a calling convention is a scheme for how subroutines receive parameters from their caller and how they return a result; calling conventions can differ in:...

  • Name mangling
    Name mangling
    In compiler construction, name mangling is a technique used to solve various problems caused by the need to resolve unique names for programming entities in many modern programming languages....

  • Application programming interface
    Application programming interface
    An application programming interface is a source code based specification intended to be used as an interface by software components to communicate with each other...

     - API
  • Application Binary Interface
    Application binary interface
    In computer software, an application binary interface describes the low-level interface between an application program and the operating system or another application.- Description :...

     - ABI
  • Comparison of application virtual machines
    Comparison of Application Virtual Machines
    This article lists some software virtual machines that are typically used for allowing application bytecode to be portably run on many different computer architectures and operating systems. The application is usually run on the computer using an interpreter or just-in-time compilation...

  • SWIG
    SWIG
    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 languages like C#, Java, Modula-3, Objective Caml, Octave, and Scheme...

    - opensource interfaces bindings generator from many languages to many languages

External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK