lua-users home
lua-l archive

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

If you've already hashed the function names (and I assume from that this
also means the functions all have identical signatures or you already have
parameter/return value marshalling type code worked out), then probably the
simplest solution for you is to hook the __index method on the global table
(or some sub-table) and do your look-up in response to this (just return the
appropriate Lua function for the target function to be called).

If you feel that having a 1 to 1 mapping between Lua C functions (i.e. the C
functions that Lua will call) is too much and want to run them all through 1
function then you can use an upvalue to tell your single C function which
target function to call.

If you would prefer to leave the functions "registered" after they have been
called once, then I suggest that you modify the above suggestion to actually
set the requested function in the containing table in addition to returning
it from the __index metamethod, that way the __index metamethod won't be
used to retrieve the function the next time around (i.e. you've accomplished
"lazy" function registration).

-----Original Message-----
[]On Behalf Of Miston Drirr C.
Sent: Thursday, October 23, 2003 1:35 PM
Subject: Undefined function


I'm rather new to Lua, but it looks like a good language for what I'm

In my program I have a large API I want to export to LUA, however I will
be creating and destroying the Lua context often, and registering every
function is not practical, both because of the time it takes to do so
and because the program will have the function names in a hash table
within the program already.

Is there a way of telling Lua to call a C function whenever it finds an
unknown function? That way the main program would do the lookup and
handle the function call. That, or telling Lua to ask the main program
for the symbol and a pointer to a C function whenever it doesn't find
the symbol within it's own table?

...or perhaps there is an easier way of doing this that I overlooked.