lua-users home
lua-l archive

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


On Tue, 27 Jul 2010, Ruan Kendall wrote:

> We run an application which supports a number of runtime-loadable code
> modules in the form of .so files. One of these modules is a lua
> interpreter. [...] Some of the modules provide general functionality
> [...] In order for a lua script to access this functionality, we must
> currently:
>  - Define the function in the code module
>  - Pass a pointer to that function back to a global variable in the
> main application when the module is loaded
>  - Defined a wrapper which calls the function in the global variable
>  - Rebuild the world
>
> This means one bit of functionality must appear in three very
> different parts of the code, which isn't really very nice and modular.
> Does anyone have any suggestions on how this might be done better?

Instead of having a global table of functions that might be called by Lua,
just have a global table of dynamic module handles. Change the wrapper
code to use dlsym() to obtain the function pointer using the relevant
module handle. (You will want to cache the result.) Compile the wrapper
code for each module into a shadow module that the Lua module can require.

Then, order for a lua script to access generic functionality, you must:
 - Define the function in the code module
 - Use your concise wrapper definition language to create a shadow module
for use by Lua scripts.
 - Build the two modules.
 - Leave the rest of the world alone.

Lua can't unload modules, so you might have to live with the wrapper
module remaining loaded even if the functional module has been unloaded.

Tony.
-- 
f.anthony.n.finch  <dot@dotat.at>  http://dotat.at/
HUMBER THAMES: WEST, VEERING NORTHWEST, 3 OR 4, OCCASIONALLY 5 LATER. SMOOTH
OR SLIGHT. SHOWERS. GOOD.