Mixing different programming languages has long been something I don't quite understand. According to this Wikipedia article, a foreign function interface (or FFI) 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, which performs any necessary translation.
- Use of wrapper libraries
- 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.
My questions:
What are the differences between the 1st, 2nd and 3rd ways? It seems to me they are all to compile the code of the called language into some library with object files and header files, which are then called by the calling language.
One source it links says, implementing an FFI can be done in several ways:
- Requiring the called functions in the target language implement a specific protocol.
- Implementing a wrapper library that takes a given low-language function, and "wraps" it with code to do data conversion to/from the high-level language conventions.
- Requiring functions declared native to use a subset of high-level functionality (which is compatible with the low-level language).
I was wondering if the first way in the linked source is the same as the first way in Wikipedia?
What does the third way in this source mean? Does it corresponds to the 4th way in Wikipedia?
In the same source, when comparing the three ways it lists, it seems to say the job of filling the gap between the two languages is gradually shifted from the called language to the calling language. I was wondering how to understand that? Is this shifting also true for the four ways in Wikipedia?
Are Language binding and FFI equivalent concepts? How are they related and differ?
a binding from a programming language to a library or OS service is an API providing that service in the language.
I was wondering which way in the quotation from Wikipedia or from the source each of the following examples belongs to?
- Common Object Request Broker Architecture (CORBA)
- Calling C in C++, by the extern "C" declaration in C++ to disable name mangling.
- Calling C in Matlab, by MATLAB Interface to Shared Libraries, i.e., first compiling C code to shared library via general C compiler such as gcc, and then loading, calling a function from and unloading the shared library via Matlab functions loadlibrary(), calllib() and unloadlibrary().
- Calling C in Matlab, by Creating C/C++ Language MEX-Files
- Calling Matlab in C, by mcc compiler
- Calling C++ in Java, by JNI, and Calling Java in C++, also by JNI
- Calling C/C++ in other languages, Using SWIG
- Calling C in Python, by Ctypes module.
- Cython
- Calling R in Python, by RPy
- Programming Language Bindings to OpenGL from various languages, such as Python, Fortran and Java
- Bindings for a C library, such as Cairo, from various languages, such as C++, Python, Java, Common Lisp