the foreign c-string type: what animal is that?

"Hoehle, Joerg-Cyril" <[email protected]> Wed, 3 Apr 2002 18:20:12 +0200
Newsgroups gmane.lisp.uffi.general
Message-ID <[email protected]>
Kevin wrote:

> Wow, that'd be great to add dynamic linked libraries to CLISP. Are you
> speaking about libraries that can be loaded at run-time 
> rather than having to be linked with the CLISP executable?

Precisely, I've always said that CLISP's FFI has already 90% or what is needed.
You're pushing me hard: stack-allocation, malloc+free + dynamic functions + API design all at the same time... -- six years after the FFI (and creation of the ffcall package which perl uses to do dynamic calls -- a shame).


> > (malloc '(c-array character 13) "abcde...")
> > like (malloc 'int -123456789)

> So you create a lisp object that is the foreign object, and 
> it's pointer 
> is just the address of the object. Is that right?

Yes, this is exactly how I believe CLISP's FOREIGN-VARIABLE objects can be put to good use:

[10]> (foreign-malloc 'ffi:int -123456789)
#<FOREIGN-VARIABLE "via malloc" #x00CC0530>

[11]> (describe *)
#<FOREIGN-VARIABLE "via malloc" #x00CC0530> is a foreign variable of foreign type FFI:INT.

[12]> (describe-thing **)
0. "via malloc"
1. #<FOREIGN-ADDRESS #x00CC0530>
2. 4 ; <- (ffi::%sizeof **)
3. FFI:INT
#<FOREIGN-VARIABLE "via malloc" #x00CC0530>

[13]> (ffi::foreign-value ***)
; dereferences pointer to create Lisp object
-123456789

> Hmm, you can also do a malloc without initialization by passing it
> the size. You can then pass the address of this buffer to a foreign 
> function to get a return value.
Sure, but so far I was exploring what I can write using Lisp only (lots of creative hacks). Uninitialized malloc will require C support which I'll have to think about.

Regards,
	Jorg Hohle.