Foreign function interface

Last updated

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 binary dynamic-link library.

Contents

Naming

The term comes from the specification for Common Lisp, which explicitly refers to the programming language feature enabling for inter-language calls as such;[ citation needed ] the term is also often used officially by the Haskell, [1] Rust, [2] PHP, [3] Python, and LuaJIT (Lua) [4] [5] :35 interpreter and compiler documentations. [6] Other languages use other terminology: the Ada programming language talks about "language bindings", while Java refers to its FFI as the JNI (Java Native Interface) or JNA (Java Native Access). Foreign function interface has become generic terminology for mechanisms which provide such services.

Operation

The primary function of a foreign function interface is to mate the semantics and calling conventions 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 application binary interfaces of both. This can be done in several ways:

FFIs may be complicated by the following considerations:

UML ffi.svg

Examples of FFIs include:

In addition, many FFIs can be generated automatically: for example, SWIG. 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 [24] for GIMP. [25]

Some FFIs are restricted to free standing functions, while others also allow calls of functions embedded in an object or class (often called method calls); some even permit migration of complex datatypes or objects across the language boundary.

In most cases, an 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 or C++. 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.

The term foreign function interface is generally not used to describe multi-lingual runtimes such as the Microsoft Common Language Runtime, 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 (RMI), RPC, CORBA, SOAP and D-Bus permit different services to be written in different languages; such architectures are generally not considered FFIs.

Special cases

There are some special cases, in which the languages compile into the same bytecode VM, like Clojure and Java, as well as Elixir and Erlang. Since there is no interface, it is not an FFI, strictly speaking, while it offers the same functionality to the user.

See also

Related Research Articles

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.

This is a "genealogy" of programming languages. Languages are categorized under the ancestor language with the strongest influence. Those ancestor languages are listed in alphabetic order. Any such categorization has a large arbitrary element, since programming languages often incorporate major ideas from multiple sources.

In software design, the Java Native Interface (JNI) is a foreign function interface programming framework that enables Java code running in a Java virtual machine (JVM) to call and be called by native applications and libraries written in other languages such as C, C++ and assembly.

In computer science, a dynamic programming language is a class of high-level programming languages, which at runtime execute many common programming behaviours that static programming languages perform during compilation. These behaviors could include an extension of the program, by adding new code, by extending objects and definitions, or by modifying the type system. Although similar behaviors can be emulated in nearly any language, with varying degrees of difficulty, complexity and performance costs, dynamic languages provide direct tools to make use of them. Many of these features were first implemented as native features in the Lisp programming language.

In computer science, primitive data types are a set of basic data types from which all other data types are constructed. Specifically it often refers to the limited set of data representations in use by a particular processor, which all compiled programs must use. Most processors support a similar set of primitive data types, although the specific representations vary. More generally, "primitive data types" may refer to the standard data types built into a programming language. Data types which are not primitive are referred to as derived or composite.

Metaprogramming is a programming technique in which computer programs have the ability to treat other programs as their data. It means that a program can be designed to read, generate, analyze or transform other programs, and even modify itself while running. In some cases, this allows programmers to minimize the number of lines of code to express a solution, in turn reducing development time. It also allows programs a greater flexibility to efficiently handle new situations without recompilation.

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.

In some programming languages, eval, short for the English evaluate, is a function which evaluates a string as though it were an expression in the language, and returns a result; in others, it executes multiple lines of code as though they had been included instead of the line including the eval. The input to eval is not necessarily a string; it may be structured representation of code, such as an abstract syntax tree, or of special type such as code. The analog for a statement is exec, which executes a string as if it were a statement; in some languages, such as Python, both are present, while in other languages only one of either eval or exec is.

In computer programming, a callback or callback function is any reference to executable code that is passed as an argument to another piece of code; that code is expected to call back (execute) the callback function as part of its job. This execution may be immediate as in a synchronous callback, or it might happen at a later point in time as in an asynchronous callback. They are also called blocking and non-blocking.

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.

In programming and software design, 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.

Programming languages are used for controlling the behavior of a machine. Like natural languages, programming languages follow rules for syntax and semantics.

This comparison of programming languages compares the features of language syntax (format) for over 50 computer programming languages.

The following tables provide a comparison of numerical analysis software.

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.

<span class="mw-page-title-main">Scripting language</span> Programming language for run-time events

A scripting language or script language is a programming language that is used to manipulate, customize, and automate the facilities of an existing system. Scripting languages are usually interpreted at runtime rather than compiled.

Wrapper libraries consist of a thin layer of code which translates a library's existing interface into a compatible interface. This is done for several reasons:

Language interoperability is the capability of two different programming languages to natively interact as part of the same system and operate on the same kind of data structures.

In computer science, bridging describes systems that map the runtime behaviour of different programming languages so they can share common resources. They are often used to allow "foreign" languages to operate a host platform's native object libraries, translating data and state across the two sides of the bridge. Bridging contrasts with "embedding" systems that allow limited interaction through a black box mechanism, where state sharing is limited or non-existent.

References

  1. "FFI Introduction". HaskellWiki. Retrieved 19 June 2015. Haskell's FFI is used to call functions from other languages (basically C at this point), and for C to call Haskell functions.
  2. "std::ffi - Rust" . Retrieved 1 April 2021. This module provides utilities to handle data across non-Rust interfaces, like other programming languages and the underlying operating system. It is mainly of use for FFI (Foreign Function Interface) bindings and code that needs to exchange C-like strings with other languages.
  3. "PHP FFI Manual". PHP Manual. Retrieved 31 August 2023. Defined C variables are made available as properties of the FFI instance.
  4. 1 2 Mike Pall. "FFI Library". Luajit.org. Retrieved 2013-09-29.
  5. 1 2 Heintz, Joachim (2013). Ways Ahead : Proceedings of the First International Csound Conference. Alex Hofmann, Iain McCurdy. Newcastle upon Tyne: Cambridge Scholars Publishing. ISBN   978-1-4438-5122-0. OCLC   855505215.
  6. "CFFI documentation" . Retrieved 19 June 2015. C Foreign Function Interface for Python. The goal is to provide a convenient and reliable way to call compiled C code from Python using interface declarations written in C.
  7. "Interface to Other Languages". Adaic.org. Retrieved 2013-09-29.
  8. "Foreign Export" . Retrieved 2020-05-25.
  9. "Calling C From Clean" . Retrieved 2018-04-25.
  10. "dart:ffi library" . Retrieved 2020-01-01.
  11. "'fortran-iso-c-binding' tag wiki". Stack Overflow.
  12. "cgo — The Go Programming Language" . Retrieved 2015-08-23.
  13. "Foreign Function Interface | Manual". Deno. Retrieved 2023-02-08.
  14. "FFI API". Bun Docs.
  15. "Calling C and Fortran Code · The Julia Language". docs.julialang.org. Retrieved 2018-02-11.
  16. PyCall.jl: Package to call Python functions from the Julia language, JuliaPy, 2018-02-08, retrieved 2018-02-11
  17. "PHP: FFI - Manual". The PHP Group. Retrieved 13 June 2019.
  18. Eli Barzilay. "The Racket Foreign Interface". Docs.racket-lang.org. Retrieved 2013-09-29.
  19. "TR600.pdf" (PDF). Archived from the original (PDF) on 2009-09-02. Retrieved 2013-09-29.
  20. "Inline implementations" . Retrieved 2017-08-15.
  21. "Native Call" . Retrieved 2017-08-15.
  22. "Using extern Functions to Call External Code" . Retrieved 2019-06-01.
  23. "Import from C Header File". Zig Software Foundation. Retrieved 2021-03-11.
  24. "4. A sample script". Gimp.org. 2001-02-04. Retrieved 2013-09-29.
  25. "Script-Fu and plug-ins for The GIMP". Gimp.org. Retrieved 2013-09-29.