4.1 The Foreign Function Interface
4.1.7 Passing foreign and Lisp data types The figures in this section list the Lisp data types that can be passed to foreign functions and the foreign data types that can be passed to Lisp. You define foreign functions that can be called from Lisp with the macro
def-foreign-function; the macro
def-foreign-callable allows you to call a Lisp function from a function defined in another language.
You should consider the following information when passing data types to foreign functions from Lisp and vice versa:
- The call discipline indicates the way information passes between a function argument and its associated parameter. The call discipline
:value represents call-by-value parameter passing, and
:reference represents call-by-reference parameter passing. When the call discipline is
nil, the calling standard depends on the Lisp argument type.
- The foreign type
:lisp passes a pointer to a Lisp object directly to a foreign function with no further coercion.
- The Lisp type
foreign-pointer is a pointer to an object whose structure is defined by
- C functions expect strings to end with a null byte. All Common Lisp simple strings are stored with a terminating null byte so that they can be passed to C functions without difficulty. The terminating null byte is not included in the length of the string returned by the Common Lisp function
- To pass any other type of string to a C function, you must ensure that the string ends with a null byte and declare it to be of type
- The type
fortran-string is the same as the following type for byte-addressable machines where the size of a character is one byte:
(subtypep (array-element-type string) 'string-char)
- The length of a string is always passed to FORTRAN functions.
- When passing arrays, the Foreign Function Interface attempts to convert the array to a simple vector of the same type as the array. If the conversion is not possible, an error is signaled.
- The foreign type
coercible-to-foreign-array specifies a Lisp array that can be converted to a foreign array. A coercible Lisp array must be either a simple array, a simple vector, or an array with an offset that is aligned at an offset. You cannot pass, for example, a displaced bit vector to a foreign function because the foreign function is expecting an address, not a pointer.
- All modifiable Lisp data that are passed to a foreign function must be stationary data; that is, they do not move during a garbage collection. Examples of stationary data include data that have been allocated within a call to the macro
with-static-area or symbols that have been freshly created in a package by the Common Lisp function
intern; note that the function
intern. If dynamic data are passed to foreign functions, the results are unpredictable. See the reference page for
stationary-object-p for more information.
- 126.96.36.199 - Passing C data to Lisp functions
- 188.8.131.52 - Passing FORTRAN data to Lisp functions
- 184.108.40.206 - Passing Pascal data to Lisp functions
- 220.127.116.11 - Passing Lisp data to C functions
- 18.104.22.168 - Passing Lisp data to FORTRAN functions
- 22.214.171.124 - Passing Lisp data to Pascal functions
The Advanced User's Guide - 9 SEP 1996
Generated with Harlequin WebMaker