Subject: Re: [clisp-list] Re: truly dynamic FFI in CVS

From: "Hoehle, Joerg-Cyril" <[email-masked]>
Date: Wed Sep 3 05:04:08 2003

Hi,

Friday, August 08, 2003, 11:05:55 PM, somebody wrote:

>>> It's a pity that def-call-in hardly can be made dynamic while it
seems
>>> crucial for typical event-driven windows app.
>> well, this is caused by the non-dynamic nature of C/C++, right?
>But you have already done dynamic parameter/result translation! How
>about to made a fixed number of entry points/slots being defined by
>def-call-in ? (and clisp being compiled as a shared library)

I believe somebody is mislead. CLISP FFI has little problems with callbacks AFAIK. DEF-CALL-IN is not the way to go for dynamic callbacks. C/C++ is not the problem.

If you want to install a callback (e.g. for an event driven loop), you need to invoke a def-call-out :library function which takes a C-FUNCTION object as parameter somewhere (or a C-STRUCT which contains such).
A Lisp function passed as parameter thereof will be converted to a callback function pointer that the foreign (C/C++/xyz) world will receive and can use.

Please try it out.

Regards,
 Jorg Hohle.

---

From: Arseny Slobodjuck <[email-masked]>
Date: Fri Sep 5 05:02:04 2003

Hi,

> Please try it out.

Wow.
Woooow!

I've made a dll with the following function

typedef int (*LispFunc)(int parameter);
int CallInFunc4(LispFunc f) {
  return f(5)+11;
}

and called it from Lisp:

declared

(ffi:def-call-out callout4
    (:name "CallInFunc4")
    (:library "lispdll.dll")
    (:arguments (list (ffi:c-function (:arguments (list ffi:int)) (:return-type ffi:int) (:language :stdc))))
    (:return-type ffi:int)
    (:language :stdc))

and used:
    
[2]> (defun f (x) (* x 2))
F
[3]> (callout4 #'f)
21

I never thought it can work this way!

-- 
Best regards,
 Arseny