[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: dynamically associating functions with lua
- From: Martin Slater <mslater@...>
- Date: Wed, 22 Oct 2003 00:06:01 +1000
Many thanks for that, a much much nicer way of dealing with it.
Luiz Henrique de Figueiredo wrote:
The only thought i have is to auto generate a lua script that implements
a function stub of the same name as the the dll function, push the
function name and all its parameters into a table and call a 'c' side
do_function_call that pulls this info and makes the call.
A simpler way is to expose a table with an __index metamethod that routes
the call to a single C function.
Here is what I've doing in my binding for GIMP:
local f=function (...) router(k,unpack(arg)) end
gimp.gimp_message("Hello from GIMP and Lua!")
Note that "gimp" is the single C function that is exported by C. The code
above immediately redefines "gimp" to be a table with an __index metamethod
that does the routing. Note that the routing is by demand and that once
a function is called it becomes part of the table, that is, the __index
metamethod is called at most once for each function.