[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: How could I embedded socket in Lua internally, just like oslib, debuglib?
- From: Jerome Vuarand <jerome.vuarand@...>
- Date: Fri, 5 Feb 2010 15:12:17 +0100
2010/2/5 sagasw <firstname.lastname@example.org>:
> 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
> 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_getfield(L, -1, "preload");
if (luaL_loadstring(L, socket_str))
lua_error(L); /* use msg on stack */
lua_setfield(L, -2, "socket");
lua_call(L, 1, 0);