[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

FFI... (any C hackers around?)




This is a bit too long, but the result of the story (marked with a
"***") looks like something that can be useful...

So, I just discovered IBM's ViaVoice Outloud, and since I was always
fascinated by speech synthesizers I began playing with it, and it
wasn't long before the standard sample executables were too limited
for what I wanted - connecting it with my MzScheme's mail script and
have the computer tell me when I have new mail in a better way than my
current "Whoop! ping ping ping boom boom" etc...

Anyway, the first step is to open a process and keep feeding it input,
but I wanted better API access, so the next step was writing a small C
program - since I wanted it to handle simple text, I started making it
understand my own "commands" to change parameters etc, and soon enough
I realized that the whole thing will look much better if it gets
embedded in Scheme.

So, the next step is to have a small eci.so file written in C, which
can be loaded into MzScheme.  Now, when this is made, you continuously
have to decide what functionality you want to implement in the C level
and what do you leave for Scheme.  As I love Scheme enough to be
registered on this list, I decided to have the thinnest layer -
basically just getting Scheme_Object*'s, checking types, calling the
API function and convert the result back to some Scheme_Object*.

There is definitely an enjoyable side effect for doing this, as you
have more and more functionality available from Scheme, but around the
5th function it get unavoidable to question the intelligence involved,
since the whole thing is pretty mechanical.

***

So, I started writing something that will do things automatically, and
it started getting closer and closer to Allegro's FFI - you can open a
shared library and pull out a function as easily as:

  (define eci:add-text
    (dyn-function "libibmeci50.so" "AddText" '(eci string) 'boolean))

...and this sounds much more attractive than writing that thin
extension.

So, right now it works enough to provide some cute functionality,
except there are two things I don't know how to do:

1. Passing a variable number of arguments to a function, with varying
   sizes - now I have a big & ugly case statement with only 32bit
   types.

2. Missing any way of making a C closure that is callable from C -
   something that can be passed on to some C function that will call
   the Scheme function.  A crude solution will be to rely on such
   callbacks to have a data pointer and play with that...

It should probably be very simple to make it work on other OS's as
well, since only the shared library openning and getting a symbol's
address are the OS-dependent parts, and that is pretty similar to the
MzScheme source.

So, any interest?

-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
         http://www.cs.cornell.edu/eli/meaning.html        Maze is Life!