Next: , Previous: , Up: Top   [Contents][Index]


7 Foreign Function Calling

Macro: %foreign-funcall name {arg-type arg}* &optional result-type ⇒ object
Macro: %foreign-funcall-pointer ptr {arg-type arg}* &optional result-type ⇒ object

Invoke a foreign function called name in the foreign source code.

Each arg-type is a foreign type specifier, followed by arg, Lisp data to be converted to foreign data of type arg-type. result-type is the foreign type of the function’s return value, and is assumed to be :void if not supplied.

%foreign-funcall-pointer takes a pointer ptr to the function, as returned by foreign-symbol-pointer, rather than a string name.

Macro: %foreign-funcall-varargs name ({fixed-type arg}*) {vararg-type arg}* &optional result-type ⇒ object
Macro: %foreign-funcall-varargs-pointer ptr ({fixed-type arg}*) {vararg-type arg}* &optional result-type ⇒ object

Invoke a foreign variadic function called name in the foreign source code.

Each fixed-type and vararg-type is a foreign type specifier, followed by arg, Lisp data to be converted to foreign data of type arg-type. result-type is the foreign type of the function’s return value, and is assumed to be :void if not supplied.

%foreign-funcall-pointer-varargs takes a pointer ptr to the variadic function, as returned by foreign-symbol-pointer, rather than a string name.

Both functions have default implementation which call %foreign-funcall and %foreign-funcall-pointer approprietly.

Examples

  ;; Calling a standard C library function:
  (%foreign-funcall "sqrtf" :float 16.0 :float) ⇒ 4.0
  ;; Dynamic allocation of a buffer and passing to a function:
  (with-foreign-ptr (buf 255 buf-size)
    (%foreign-funcall "gethostname" :pointer buf :size buf-size :int)
    ;; Convert buf to a Lisp string using MAKE-STRING and %MEM-REF or
    ;; a portable CFFI function such as CFFI:FOREIGN-STRING-TO-LISP.
    )

Next: , Previous: , Up: Top   [Contents][Index]