lua-users home
lua-l archive

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


Hi everyone.

I'm writing a C library which has submodules written in Lua. So, I have Foo (a C library), Bar (a Lua module) and Baz (another Lua module).

require "Foo"
require "Foo.Bar"
require "Foo.Baz"

So far, so good. The problem I have is that I need to add a C function to Foo.Bar and I can't find a good way to do that.

I tried the following:
1) in luaopen_Foo, I do:
   luaL_Reg functions[] = {
   { "func", a_c_function },
       { 0 }
   };
luaL_register(L, "Foo.Bar", function);
   lua_dofile(L, "/my/path/to/Bar.lua");
While that works, I'm forcing the load of "Foo.Bar" even when it won't be used.

2) using package.preload, placing a function to handle "Foo.Bar". Here I have two issues. One, since /my/path/to/Bar.lua could change (for instance, now I'm deploying with LuaRocks 1.0 but that could change in the future) I should issue another 'require' (but I'll call myself again).

I've considered adding Bar.lua as a resource to the library but I'd like to avoid that (I don't want to recompile Foo.dll for each change needed to Bar.lua)

Basically, something like "package.postload" would be what I need. Some hook to do stuff *after* a module has been required.
Is this possible or am I going the wrong way?

Regards,
Ignacio Burgueño