How could I embedded socket in Lua internally, just like oslib, debuglib?
Asked Answered
B

3

4

I want to implement the function like embedding the socket function in my Lua build. So I don't need to copy socket.core.dll any more (just for fun).

I search the maillist, and see some guys discuss the topic, http://lua-users.org/lists/lua-l/2005-10/msg00269.html

But I have question for the details steps, who could give me a detailed steps for changing the lua and luasocket code to make them work together (not with dll method).

I tried these steps in windows xp with VC2008:

1) copy luasocket code to Lua project.

2) add some code

static const luaL_Reg lualibs[] = {
  {"", luaopen_base},
  {LUA_LOADLIBNAME, luaopen_package},
  {LUA_TABLIBNAME, luaopen_table},
  {LUA_IOLIBNAME, luaopen_io},
  {LUA_OSLIBNAME, luaopen_os},
  {LUA_STRLIBNAME, luaopen_string},
  {LUA_MATHLIBNAME, luaopen_math},
  {LUA_DBLIBNAME, luaopen_debug},
  {LUA_SOCKETLIBNAME, luaopen_socket_core}, // add this line
  {LUA_MIMELIBNAME, luaopen_socket_core}, // add this line
  {NULL, NULL}
};

3) build the project, and run it.

When I type print(socket._VERSION), it shows luasocket 2.0.2, it is correct.

When I type print(socket.dns.toip("localhost")), it shows 127.0.0.1 table: 00480AD0, it is correct too.

But when I try to use other features, for example bind, it can't work.

Who could tell me the reason?

Benniebenning answered 4/2, 2010 at 3:31 Comment(0)
W
6

you need put luasocket stuff into the package.preload table, in this way:

lua_getfield(L, LUA_GLOBALSINDEX, "package");
lua_getfield(L, -1, "preload");
lua_pushcfunction(L, luaopen_socket_core);
lua_setfield(L, -2, "socket.core");

// add mime.core yourself...
Withoutdoors answered 7/11, 2012 at 7:8 Comment(1)
For people like me who weren't sure where to put this code, I put it after: luaL_openlibs(L); in my main().Wessling
C
1

luasocket is a mixed C/lua module, you need to bundle both versions into your application if you want it to work without any extra files.

socket.lua loads socket.core (from socket/core.dll)
mime.lua loads mime.core (from mime/core.dll)

So in order for your application to work you will need to build all the .dll files and the .lua files into your application and manually load them (or set them up to be loaded correctly via custom package loaders).

The email you quoted is tweaking the package.preload table (in a way that appears a tad odd now but might work anyway) to get the built-in C code to be loaded correctly when require is called.

Calumniate answered 20/2, 2010 at 3:54 Comment(0)
T
-1

Try running

for k, v in pairs(socket) do print(k, v) end

and maybe we'll be able to help.

Turnpike answered 5/2, 2010 at 1:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.