lua-users home
lua-l archive

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


Hi,

These days I frequently find myself wanting to create a Lua module
that is part C code and part Lua code.  The 'module' and 'require'
functions seem to be set up to support one or the other, but not
both in combination.

Here is what I do with LuaSocket.  I put the C functionality
in socket/core.so. The Lua functionality is put in
socket.lua. When the user runs require"socket", the Lua file
is invoked. This, in turn, runs require"socket.core", which
loads the C functionality. After that, the Lua module runs
module"socket". The interesting part is in the C module. It
exports luaopen_socket_core, but calls

    luaL_openlib(L, "socket", funcs, 0)

instead of using "socket.core". This means the the functions
get exported to the same namespace as the Lua module. It
works very well for my purposes.

Regards,
Diego.