lua-users home
lua-l archive

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


Hi,

luaopen_lsocket is a C function which needs a wrapper to be called from
lua, as far as I am aware...

Luaopen_lsocket shouldn't be called directly from Lua. It should be
called by require, when require"lsocket" is invoked by socket.lua, which
in turn is executed when require"socket" is called by the user.

In a traditional instalation, require"lsocket" will search for
lsocket.dll and use loadlib to get luaopen_lsocket. Placing it in
package.preload["lsocket"] simply avoids the search. Everything else
works the same.

This is not "just" placing luaopen_lsocket in package.preload["lsocket"].
I feel that my method is more transparent in that no changes have to be
made in lua code for it to work (after my patch is applied) and if someone
does a require"lsocket" with the current socket then a load of "socket" will
fail, i.e. any order of loading modules works and loading the C module first
requires no special handling.

Why isn't this "just" placing it in package.preload? You don't need to
use lua_register. You can use lua_pushcfunction and then lua_settable from C. No need to create the global "open_lsocket" or change Lua code.

Regards,
Diego.