lua-users home
lua-l archive

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

Jose Marin wrote:
How hard would be write such tool in Lua?
That depends if your goal is to write a tool just for interfacing with Lua, or with multiple languages like SWIG does. SWIG is like a linguistic motherboard: a cross-bar between libraries and languages, and a huge amount of hard work has been put into it.

One significant difference between SWIG and most other language binding tools is that SWIG is multi-language, and has many different language back-ends. It can bind code to a long list of languages including Lua, it doesn't require that your code be written in C++, and it doesn't require you to modify the code or headers in any way (so you can bind pre-existing libraries and source code you don't have control over). It can even handle complex C++ features like templates, operator overloading, references, multiple inheritance, etc.

So the big advantage to using SWIG is that it can bind your code to a long list of scripting languages beyond Lua. This is not so important to Lua-only programmers. But to developers of useful libraries like Ming (the Flash generation library) and rich APIs like OpenCV (the computer vision library), and to developers who use different languages while requiring the same libraries, SWIG is extremely useful. Language developers love SWIG because somebody just has to write one SWIG back-end for their language (ahem: not a trivial task, but hey, they're language developers!), and then all the SWIG-ified libraries are available for their language!

You just write one SWIG interface for your library, and all the SWIG supported scripting languages end up with the same API (modulo differences in the languages' syntax and object systems), so once you know the library, you can use it from Lua, Python, Perl, Java, PHP, etc.

Of course the advantage of using language-specific binding tools is that they generally fit the language very well, and are more efficient (and simpler) than SWIG. However, SWIG does let you tailor the interfaces for specific languages, and write custom typemaps to handle conversion between language specific data types.

There are a lot of "nooks and crannies" in the C++ language and subtle issues about interfacing it with scripting languages, which SWIG deals with pretty well. It has improved a lot over the many years of development that's gone into it. Some languages are supported better than others (its Python back-end is one of the most advanced). So if you're thinking of writing your own language binding tool, then it's a great idea to look at SWIG first, to see what it does and how it works.