[plt-scheme] C++ Bindings using FFI

From: Eli Barzilay (eli at barzilay.org)
Date: Sun May 7 15:31:11 EDT 2006

On May  7, Hans Oesterholt-Dijkema wrote:
> 
> > 1. The SWIG code that you sent doesn't do anything with the Scheme
> >    foreign interface.
> 
> Indeed, you are right. What I sent was what I need to feed SWIG *at
> this moment*.  SWIG will parse these things, and generate C glue
> code from it.

IIUC, it does not generate the code from scratch -- for example, when
you write:

  %typemap(in) T * {
    mzgtk2_catch(SCHEME_LISTP($input),TYPENAME);
    $1=NULL;
    {
      int i,N=scheme_list_length($input);
      Scheme_Object *obj,*list=$input;
      for(i=0;i<N;i++) {
        obj=scheme_car(list);
        list=scheme_cdr(list);
        mzgtk2_catch(PREP(obj),TYPENAME);
        $1=GTKCALL(g_list_append,($1,MZ_C(obj)));
      }
    }
  }

it looks like a bunch of code that it will send to GCC.


> I'm personally not using C++ header files. But swig can parse C++
> classes, and the generating backend will generate C glue code.
> 
> ''If you want to use SWIG with the FFI, you will need to write a
> generator that generates FFI scheme code''.
> > 2. It won't help you deal with the difficulties of interfacing C++
> >    libraries.
> >   
> Indeed. It won't solve the name mangling stuff for the FFI.
> 
> ==> There's years of development time in SWIG. It parses stuff very
> well.  I would try to integrate an FFI backend into SWIG. Only the
> developers need SWIG after that. Users can just rely on the FFI
> code.

(Using SWIG to generate Scheme glue code means that you only use its
parsing ability.)


> Again. SWIG is only needed for the parsing and generation part. You
> don't need to distribute SWIG with your code.

Yes, I'm aware of that.  The point I made is different: say that you
write an interface for `libfoo', which provides a `foo' int->int
function -- SWIG knows about this from the header file and will
generate the Scheme glue code.  Later on I download your code, but I
have an older version of libfoo, where `foo' is char->char.  (More
than that -- I might not have the libfoo header files.)

-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!


Posted on the users mailing list.