lua-users home
lua-l archive

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

2007/9/7, Daniel Collins <>:
> I was just wondering how most people here bind there native code to lua.
> [...]
> So what are the thoughts and experiences of others here on this issue?

So far I've done all my bindings by hand. All third-party APIs I've
bound to Lua (for example win32, libpng, freetype, opengl, ode, fuse,
etc.) were so different from each other that each time I had to take a
different binding style. At the beginning I thought that I lacked
experience, but when I come back to mu old bindings I rarely see how I
could rewrite them better.

Among techniques that are adapted to some libs but not to others :
- everything written by hand vs. a little Lua script generates the binding
- C++ templates vs. C macros vs. nothing generic
- a C file per function
- all C vs. a C core and a Lua wrapper
- metatable per object vs. metatable per class
- environment per object vs. environment per class vs. no environment
All these choices are valid and each appeared to be the best for some
lib I bound in the past. For my personnal libs, I try to be more
consistent, but I still bind them by hand.

Advantages I see in doing hand-written bindings:
- using the lib in Lua code is easier, more natural and adapted to the
language, eventually reusing some aspect of the standard lib API that
everyone knows ('file' object methods for example)
- you can keep the paradigm and syntax style preferred by the lib, it
makes porting examples and tutorials straightforward, and let you
redirect to the original documentation

Also an important point is that since it requires much more work, I
tend to make only partial bindings (ie. I don't expose all functions).
This is an advantage in the sense that I only bind methods that I use
(and so I test them all), but it could prevent other people from using
the lib as is (but I have enough spare time to address all eventual
request I have to complete my bindings).