lua-users home
lua-l archive

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




Lisa Parratt wrote:

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.

Yep but...All depends the usage you want to have for this. If its just generating stub for lua, tools already exist. I think the use for such a compiler in the Lua context is to generate simple function with "classic" args (you're right for data model.) for fast prototyping issues (Sometimes it's a little bit "mean" to wrapp by hand one or two functions in C ;-). If we need more complicated wrapped functions I think an offline tools is better.

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.

Right.

dh.
begin:vcard
fn:David HERVIOU
n:HERVIOU;David
org;quoted-printable:Ecole Nationale d'Ing=C3=A9nieurs de Brest
adr;quoted-printable;dom:BP 38;;25, rue Claude Chappe;Plouzan=C3=A9;France;29280
email;internet:herviou@enib.fr
title;quoted-printable:Centre Europ=C3=A9en de R=C3=A9alit=C3=A9 Virtuelle
tel;work:+33 (0)2 98 05 89 45
tel;fax:+33 (0)2 98 05 89 79
note;quoted-printable:Doctorant En Informatique au laboratoire LISyC (Laboratoire d'Informatiqu=
	e des Syst=C3=A8mes Complexes) dans le cadre du projet AR=C3=A9Vi.
url:http://www.cerv.fr/~herviou
version:2.1
end:vcard