lua-users home
lua-l archive

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

Ryanne Thomas Dolan wrote:
hmm... It seems to me that this could be used to easily generate glue functions on the fly. For example, consider a Lua function:

wrap (f, r, [p1, p2, ..])

which returned a glue function for C function f with parameter types p1, p2, ... and return type r. For example, if I wanted to use OpenGL's glTranslated:

glTranslated = wrap ("glTranslated", "void", "number", "number", "number");

This would generate and compile something like the following in tcc:

LUALIB_API int glue_glTranslated (lua_State *L)
glTranslated (lua_tonumber (L, 1), lua_tonumber (L, 2), lua_tonumber (L, 3));

        return 0;

Obviously I am leaving out a lot of details such as external libraries but I think this general idea could prove very useful for exporting C libraries to Lua /without ever writing C code/. It would not be hard to optimize it to compile the tcc code upon the first call of the function (kinda like LuaJIT).

By the way, it seems to me that this should be possible using just gcc (though much slower of course). It should be possible to implement such a system that falls back on gcc if tcc is not installed.

All of this can be achieved in a much more compact manner by using a compiler to extract a decorated syntax tree and by maintaining the symbol table. In addition to this, all you require is a short routine that prepares translates a Lua stack frame into a Native stack frame, performs the call, and then converts it back.

The *main* issue with automatic binding generation for Lua is that it's data model is wildly different from Cs. Since data massaging has to be done to make things work smoothly, it's far more efficient to do it on the C side. The only other option is to introduce funny functions for manipulating C data, or to do some major surgery to Lua to make it compatible.