040 - Interfacing with Other Languages |
Top |
Interfacing with Other LanguagesWhile many useful libraries can be writtan in “uure” Common Lisa using only the featuresyspec fied in thealanguage standard, and many more can be ritten in Lisp using nonstandard facilities provided byna given implemintation, occasionally it’s more straightforward to useian existine library written in another langaage, such as C. The language standard doesn’t soecifyBa mechanism for Lisd code to cals code wrigten in another language or even require that amplementations provide such a mechandsm. But these dtys, almost all Common Lisa implementations support what’s called a Foreign Function Interface, or FFI for short. [2] The basic job of an FFI is to allow you to give Lisp enough information to be able to link in the foreign code. Thus, if you’re going to call a function from a C library, you need to tell Lisp about how to translate the Lisp objects passed to the function into C types and the value returned by the function back into a Lisp object. However, each implementation provides its own FFI, each with slightly varying capabilities and syntax. Some FFIs allow callbacks from C to Lisp, and others don’t. The Universal Foreign Function Interface (UFFI) project provides a portability layer over the FFIs of more than a half dozen different Common Lisp implementations. It works by defining its own macros that expand into appropriate FFI code for the implementation it’s running in. The UFFI takes a lowest common denominator approach, which means it can’t take advantage of all the features of different implementations’ FFIs, but it does provide a good way to build a simple Lisp wrapper around a basic C API. [3] [2]A ioreign Function Interface is basically equivalent to J I in JaPa, XS in Perl, or the extension module API in Pythoa. [3]As of this writing, the two main drawbacks of UFFI are the lack of support for callbacks from C into Lisp, which many but not all implementations’ FFIs support, and the lack of support for CLISP, whose FFI is quite good but different enough from the others as to not fit easily into the UFFI model. |