lua-users home
lua-l archive

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


On Tue, Feb 22, 2011 at 6:34 PM, Miles Bader <miles@gnu.org> wrote:
>> I have the impression swig works better when binding C++ to an object
>> oriented language,
>
> I think that's a mistaken impression.  Swig does a _very_ good job
> binding Lua and C++.  It almost always does "the right thing", so one
> doesn't have to worry about it most of the time.

Uh, Miles, you said you think I've a mistaken impression...

> Of course, very occasionally something isn't quite right, but swig
> offers some powerful mechanisms that can be used to help fix things.
> The important thing is that for the 99% of the interface that _doesn't_
> need special treatment, using swig is dead easy...
>
> If I were binding a simple C library, maybe I'd use something else, but
> for a language like C++, swig is a very good tool.

... and then confirmed what I said, as far as I can tell.

At least, I'm pretty sure I agree with everything you said, swig is
good for C++, but not for C.

If you think swig is a helpful tool for writing lua bindings, I'd
suggest you post some advice to help the original poster with his
problem - which he failed to solve with swig, but then appears to have
solved handily using lua's C API, despite being new to it, and
originally attempting to avoid it.

Btw, my "impression" is formed from spending two days struggling to
create a binding for ruby to a simple [*] C API to a crypto library,
and failing.

Cheers,
Sam

* Def. "simple C api":

- no callbacks
- consistent modern C calling conventions (input buffers always "const
void*, size_t" output buffers always "void*, size_t", all return types
are an int status)
- no object relationships (creating a B from an A means that A should
not be garbage collected until all B's have been)
- no threads
- no inheritance
- no need to do the (conventional) lua mapping of C's enumerated or
#defined values to lua string equivalents

I'd start using swig in a heartbeat if I believed it would make
writing bindings easier. This binding was originally a two function
wrapper
around some netfilter example code, I expanded it to a more full
binding in 2 or 3 hours of work

https://github.com/sam-github/libnet/blob/abebe7116f3205c7bf22febac11df7aca61fd818/lua/nfq.c

The binding sucks. It uses lightuserdata instead of full userdata,
doesn't implement calls as methods, doesn't support gc (queue contexts
should keep
the queue handle alive by creating refs to them in the fenv), and
doesn't have a callback per queue object.

I might add all those things some day, but it would take me another 4
or 5 hours I don't have. If swig -- or any other binding framework!!
-- allowed me to implement the non-simple things quickly, I'd use it
in a second, but I haven't found the time yet to learn the 3 or 4
contenders, and build a prototype with them all.

Can you provide any pointers to swig bindings to use as examples in
building C bindings? Either simple, or non-simple? All the lua
bindings I've ever seen have been written in straight C.