[C2hs] Binding to Libdb, FunPtrs

Manuel M T Chakravarty chak at cse.unsw.edu.au
Fri Apr 28 21:48:55 EDT 2006

Udo Stenzel:
> Duncan Coutts wrote:
> > Manuel may well have some comments on the issues you've brought up but
> > he's often quite busy so you may like to start discussing/hacking the
> > least controversial bits.
> And so I did.  Attached is a path that defers the error about variadic
> functions a bit.  This was surprisingly simple, it basically worked on
> the first try.  Now I can write get hooks for the "methods" in libdb,
> and if I manually write lots of foreign import dynamic declarations, I
> call call them.

Good work!  I just added the patch to the public repo.

> The next step is to generate them.  I think, the easiest way is to
> extend the call and fun hooks to accept cids of the form struct->method,
> which would then create a dynamic import and act like a combination of
> get and call hooks.  Libdb always passes the struct itself as the first
> argument to the method.  This seems to be a fairly common convention.
> Do you think it deserves special support?

I actually looked at Sleepy Cat's LibDB myself at some point, but then
never got around to doing anything about it.  I remember their OOish set
up.  Your plan sounds reasonable.

As for the struct as the first argument, I think it is a common pattern,
but I it might be a bit awkward to implement.  If it is, maybe leave it
for a second round.

Fun hooks share the `callImport' function with call hooks, which is what
generates the import declaration (this is in c2hs/gen/GenBind.hs).  The
struct-method variant should also be able to share code like this (but I
guess, you figured that out already anyway).

In a previous message, you asked,

> - defines.  Am I right in assuming that c2hs does not recognize
>   #define'd constant and so cannot reflect them in Haskell?

Correct.  There is a plan to add some support for this to enum hooks,
but I never got around to implement that.  I currently suggest to use
inline C in .chs file to bind #define'd enums.  See c2hs/tests/Enum.chs
for an example (the ThisThat definition).

Thanks for the code!

More information about the C2hs mailing list