[Larceny-users] sqlite3 FFI

Felix Klock felixluser at pnkfx.org
Thu May 22 01:57:45 EDT 2008


On May 22, 2008, at 1:25 AM, Peter Keller wrote:

> On Wed, May 21, 2008 at 06:14:29PM -0400, William D Clinger wrote:
>>> If an FFI is
>>> available and reasonably documented, I'd be willing to produce a  
>>> sqlite3
>>> wrapping library for larceny.
>>
>> That'd be great.  Larceny does indeed have an FFI,
>> which is somewhat documented but not well documented.
>> See http://larceny.ccs.neu.edu/doc/ and specifically
>> http://larceny.ccs.neu.edu/doc/user-manual.chunked/ar01s10.html#id2559833
>
> Thanks for the info!
>
> What is the accepted manner in which to access fields in a C  
> structure?
> Do I treat the structure pointer returned by the foreign call into C  
> as
> a byte vector and (after figuring out the padding and whatnot) use
> (%get-unsigned bv i) and friends or maybe the %peek equivalents?

1.) A C structure pointer is generally not something Larceny would  
consider a byte vector.  Or maybe you meant "byte vector" as something  
other than data type provided by (rnrs bytevectors (6))...

2.) I believe peek-bytes and poke-bytes and their ilk could do  
whatever you could want, but its pretty painful, and may introduce  
evil bugs if misused.

3.) For something a little less painful, I recommend that you look at:

lib/Standard/foreign-ctools.sch

Example uses of foreign-ctools appear in

lib/Standard/file-system.sch
lib/Experimental/unix.sch
lib/Experimental/winsock.sch

Other members of the larceny-users list might have better examples of  
code using foreign-ctools.

The main reason to use foreign-ctools is that it allows you to be  
oblivious to the padding/offset issues of your host environment; if  
you have a C compiler available on the host, then foreign-ctools lets  
you delegate such work to that compiler.  (You do still need to go  
through the process of extracting the relevant structure information  
from the relevant header files; sorry about that.)

4.) I apologize that lib/Standard/foreign-ctools.sch is not documented  
apart from sparse comments in its source code.  Despite the lack of  
documentation, I am pretty sure that it or something similar will  
stick around in Larceny for the foreseeable future, so I believe time  
spent learning about foreign-ctools is not going to go to waste if you  
are planning to do things with the FFI.

-Felix Klock, who co-developed foreign-ctools with Jesse Tov.






More information about the Larceny-users mailing list