lua-users home
lua-l archive

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


Hi,

Please take the time to read and understand
http://loop.luaforge.net/release/preload.html.

Generate a .c/.h file for each .lua file in the list

    ftp.lua
    http.lua
    smtp.lua
    tp.lua
    ltn12.lua
    mime.lua
    socket.lua
    url.lua

You should now have one C function that is equivalent to
each of these Lua modules. Make sure they have the
following names:

    ltn12.lua -> luaopen_ltn12
    mime.lua -> luaopen_mime
    socket.lua -> luaopen_socket
    url.lua -> luaopen_socket_url
    ftp.lua -> luaopen_socket_ftp
    http.lua -> luaopen_socket_http
    smtp.lua -> luaopen_socket_smtp
    tp.lua -> luaopen_socket_tp

Include the .h files and create the following table in your
lua.c:

    static luaL_Reg pre = {
        {"socket.core", luaopen_socket_core},
        {"mime.core", luaopen_mime_core},
        {"socket", luaopen_socket},
        {"socket.smtp", luaopen_socket_smtp},
        {"socket.http", luaopen_socket_http},
        {"socket.ftp", luaopen_socket_ftp},
        {"socket.tp", luaopen_socket_tp},
        {"socket.url", luaopen_socket_url},
        {"mime", luaopen_mime},
        {"ltn12", luaopen_mime}
        {NULL, NULL}
    };

Call this function right after the call to luaL_openlibs in
lua.c:

    void initpre(lua_State *L) {
        const luaL_Reg *lib = pre;
        luaL_findtable(L, LUA_GLOBALSINDEX, "package.preload",
            sizeof(pre)/sizeof(pre[0])-1);
        for (; pre->func; pre++) {
            lua_pushstring(L, pre->name);
            lua_pushcfunction(L, pre->func);
            lua_rawset(L, -3);
        }
        lua_pop(L, 1);
    }

Compile everything together in a big mush. Voila.

Regards,
Diego

Disclamer: It's late and I didn't test this code, but the
idea should work.