lua-users home
lua-l archive

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

2010/2/5 sagasw <>:
> Thanks your reply Jerome.
> I mentioned this topic is just for fun. But now, I think it seems a
> difficult issue to Lua, isn't it?
> In tutorial and book, the official extension method is write dynamic library
> with C/C++,
> and seldom mentioned how could we link some useful function like xml/socket
> internally.
> I think somebody must use Luasocket with static link method too, so if you
> tried this and have detailed information,
> could you provide me?

First you have to decide whether you want to limit the number of files
or not. If you don't mind having multiple files, just make sure your
package.path variable points to the place where you put socket.lua.

On the other hand if you want to limit the number of files, you can
embed the socket.lua file in your executable. The method you use to
put it in your executable will dictate the way you have to load it.
Assuming that you put the content of socket.lua inside a C string,
named socket_str, here is the code to load it, using the "preload"

const char* socket_str = "<content of socket.lua, properly escaped>"

void custom_init(lua_State* L)
    lua_getglobal(L, "package");
    lua_getfield(L, -1, "preload");
    if (luaL_loadstring(L, socket_str))
        lua_error(L); /* use msg on stack */
    lua_setfield(L, -2, "socket");
    lua_pop(L, 2);
    lua_getglobal(L, "require");
    lua_pushstring(L, "socket");
    lua_call(L, 1, 0);