title: Understand foreign function interface (FFI) and language binding
body: <p>Mixing different programming languages has long been something I don't quite understand. According to <a href="http://en.wikipedia.org/wiki/Foreign_function_interface#Operation_of_an_FFI" rel="noreferrer">this Wikipedia article</a>, a foreign function interface (or FFI) can be done in several ways:</p>
  <blockquote>
  <ol>
  <li>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.</li>
  <li>Use of a tool to automatically &quot;wrap&quot;    guest-language functions with    appropriate glue code, which performs    any necessary translation.</li>
  <li>Use of wrapper libraries</li>
  <li>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.</li>
  </ol>
  </blockquote>
  <p><strong>My questions:</strong></p>
  <ol>
  <li><p>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.</p>
  </li>
  <li><p><a href="http://www.c2.com/cgi/wiki?ForeignFunctionInterface" rel="noreferrer">One source it links</a> says,
  implementing an FFI can be done in
  several ways:</p>
  <blockquote>
  <ul>
  <li>Requiring the called functions in the target language implement a
  specific    protocol.</li>
  <li>Implementing a wrapper library that  takes a given low-language
  function, and &quot;wraps&quot; it with code to do data  conversion to/from the
  high-level language conventions.</li>
  <li>Requiring functions declared native    to use a subset of high-level  functionality (which is compatible with the low-level  language).</li>
  </ul>
  </blockquote>
  <p>I was wondering if the first way in
  the linked source is the same as the
  first way in Wikipedia?</p>
  <p>What does the third way in this
  source mean? Does it corresponds to the 4th way in Wikipedia?</p>
  </li>
  <li><p>In <a href="http://www.c2.com/cgi/wiki?ForeignFunctionInterface" rel="noreferrer"> the same source</a>, 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?</p>
  </li>
  <li><p>Are <a href="http://en.wikipedia.org/wiki/Language_binding" rel="noreferrer">Language binding</a> and FFI
  equivalent concepts? How are they
  related and differ?</p>
  <blockquote>
  <p>a binding from a programming language
  to a library or OS service is an API
  providing that service in the
  language.</p>
  </blockquote>
  </li>
  <li><p>I was wondering which way in the quotation from Wikipedia or from the source each of  the following  examples belongs to?</p>
  <ul>
  <li><a href="http://en.wikipedia.org/wiki/CORBA" rel="noreferrer">Common Object Request Broker Architecture (CORBA)</a></li>
  <li>Calling C in C++, by the extern &quot;C&quot;    declaration in C++ to
  disable name    mangling.</li>
  <li>Calling C in Matlab, by <a href="http://www.mathworks.com/help/techdoc/matlab_external/f43202.html" rel="noreferrer">MATLAB Interface to Shared Libraries</a>,     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().</li>
  <li>Calling C in Matlab, by <a href="http://www.mathworks.com/help/techdoc/matlab_external/f7667.html" rel="noreferrer">Creating C/C++ Language MEX-Files</a></li>
  <li>Calling Matlab in C, by <a href="http://www.mathworks.com/help/toolbox/compiler/mcc.html" rel="noreferrer">mcc compiler</a></li>
  <li>Calling C++ in Java, by <a href="http://en.wikipedia.org/wiki/Java_Native_Interface" rel="noreferrer">JNI</a>, and Calling Java in C++, also by <a href="http://en.wikipedia.org/wiki/Java_Native_Interface" rel="noreferrer">JNI</a></li>
  <li>Calling C/C++ in other languages, Using <a href="http://www.swig.org/" rel="noreferrer">SWIG</a></li>
  <li>Calling C in Python, by  <a href="http://www.google.com/url?sa=t&amp;source=web&amp;cd=1&amp;ved=0CBQQFjAA&amp;url=http://docs.python.org/library/ctypes.html&amp;rct=j&amp;q=%20python%20Ctypes%20module&amp;ei=CJ6NTa_JO6HD0QGMm5GzCw&amp;usg=AFQjCNGFU4h9pH7gqGNesVBGbT-a_rhhTg&amp;sig2=FhphdTx19dXmEVZAOAMhPA&amp;cad=rja" rel="noreferrer">Ctypes    module</a>.</li>
  <li><a href="http://cython.org/" rel="noreferrer">Cython</a></li>
  <li>Calling R in Python, by <a href="http://rpy.sourceforge.net/" rel="noreferrer">RPy</a></li>
  <li><a href="http://www.opengl.org/resources/bindings/" rel="noreferrer">Programming Language Bindings to OpenGL from various languages</a>, such as Python, Fortran and Java</li>
  <li>Bindings for a C library, such as    <a href="http://cairographics.org/bindings/" rel="noreferrer">Cairo</a>, from various languages,
  such as C++, Python, Java, Common Lisp</li>
  </ul>
  </li>
  </ol>