From: Sudhakar Yerramareddy
Subject: passing Lisp functions to C
Date: 
Message-ID: <sudha.672658705@milton>
I am calling a C function c_function from LISP.  The c_function has the
following declarations:

void c_function(a,b,func)
float *a,*b;
float (*func)();

I use:

(def-foreign-function (lisp-function (:return-type :null)
				     (:name "c_function")
				     (:language :c))
  (a (:pointer :single-float))
  (b (:pointer :single-float))
  (func *what-should-be-here?* ))


I create a function #'run-time-function at run-time.  I want this function
to be passed as the third argument to the c_function (via lisp-function).

My call looks like this:

(lisp-function pointer-a pointer-b *what-should-be-here?*)

I am using Lucid Common Lisp.  How do I do this?  Any suggestions
appreciated.

Thanks.
-- sudhakar
From: Barry Margolin
Subject: Re: passing Lisp functions to C
Date: 
Message-ID: <1991Apr26.154656.13177@Think.COM>
In article <···············@milton> ·····@herodotus.cs.uiuc.edu (Sudhakar Yerramareddy) writes:
>I am calling a C function c_function from LISP.  The c_function has the
>following declarations:
>
>void c_function(a,b,func)
>float *a,*b;
>float (*func)();

>I create a function #'run-time-function at run-time.  I want this function
>to be passed as the third argument to the c_function (via lisp-function).
>
>My call looks like this:
>
>(lisp-function pointer-a pointer-b *what-should-be-here?*)
>
>I am using Lucid Common Lisp.  How do I do this?  Any suggestions
>appreciated.

We have some code that needs to do this, and we use a kludge.  Instead of
passing the function to C, we bind a special variable to the function.
Then we define a foreign callable function whose only purpose is to FUNCALL
the function in the special variable.  It looks something like this

(defvar *callback-function*)

(def-foreign-callable (call-function
			(:language :c)
			(:name "_lisp_callback")
			(:return-type :single-float))
  (...) ; argument descriptions go here
  (funcall *callback-function* ...))

Then the call to lisp-function is done with

(let ((*callback-function* #'run-time-function))
  (lisp-function pointer-a pointer-b))

The other thing you have to do is write the C code that uses lisp_callback
instead of a function passed as an argument.  You can do this in the C code
by having an auxiliary function:

float lisp_callback();

float
lisp_interface_to_c_function (a, b)
float *a, *b;
{
	return c_function (a, b, lisp_callback);
}
--
Barry Margolin, Thinking Machines Corp.

······@think.com
{uunet,harvard}!think!barmar