[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: dynamically associating functions with lua
- From: Luiz Henrique de Figueiredo <lhf@...>
- Date: Tue, 21 Oct 2003 09:41:13 -0200
>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 router=gimp
gimp={router=router}
setmetatable(gimp,{
__index=function (t,k)
local f=function (...) router(k,unpack(arg)) end
rawset(t,k,f)
return f
end
})
gimp.gimp_message("Hello from GIMP and Lua!")
gimp.extension_db_browser(0)
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.
--lhf