lua-users home
lua-l archive

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


On Tue, Jul 21, 2015 at 12:22 PM, Alexander Kluev <akluev@inbox.ru> wrote:
> Another quick and backward compatible solution to avoid explicit linkage in
> C libs is passing path to lua*.dll as a second arg to luaopen_*
>
> So it will be posible avoid static linkage and use dynamic linking to
> current lua*dll instead.
> Old method will also work.
>
> for example
>
> void luaopen2_mylib(lua_State *L, const wchar_t *lua_dll) // new luaopen2_
> signature for two args function
> {
>     HMODULE h = LoadLibraryW(lua_dll); // load actual lua.dll
>
> // obtain api
>     dll_pushnumber = GetProcAddress(h, "lua_pushnumber");
>     dll_pushcclosure = GetProcAddress(h, "lua_pushcclosure");
>     // ...
>
> // use api
>     dll_pushnumber(L, 3.14);
> }
>
> for statick linkage just use old luaopen_ signature
>
>

This approach requires using platform specific functions
(LoadLibraryW) to load DLL. By the way any loading is not needed,
because Lua functions must be already available when luaopen_* is
called (because it must be Lua who calls luaopen_*, so Lua functions
must be available for that Lua). So the better solution would be to
provide Lua API functions from that Lua to the luaopen_*, function.
Remember we can't use any functions to do it. (Chicken or the egg
problem!)

int luaopen_foo(lua_State *L) {
    typedef void* (*lua_MethodExtractor) (lua_State *L, const char*);
    lua_MethodExtractor extractor = L->extractor;
    typedef void (Tlua_pushnumber) (lua_State *L, lua_Number n);
    Tlua_pushnumber mylua_pushnumber = extractor(L, "lua_pushnumber");
    mylua_pushnumber(L, 42);
    return 1;
}

This code it backward compatible and platform-independent, doesn't
change signature of luaopen_* and requires only one new member in
lua_State - a pointer to function "extractor", This extractor function
takes a Lua state and a Lua API function name as a string and returns
a pointer to that function as void*. Caller (luaopen_*) must cast
void* to right signature of the Lua API function.


-- 


Best regards,
Boris Nagaev