How could I embed a socket in Lua internally, like oslib, debuglib?

I want to implement a function such as embedding a socket function in my Lua assembly. Therefore, I no longer need to copy the socket.core.dll file (just for fun).

I am looking for a miner and see some guys discussing the topic, http://lua-users.org/lists/lua-l/2005-10/msg00269.html

But I have a question for detailed steps that can give me detailed steps for modifying the lua and luasocket code so that they work together (and not with the dll method).

I tried these steps in Windows XP using VC2008:

1) copy the luasocket code into the Lua project.

2) add 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 a project and run it.

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

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

But when I try to use other functions, for example bind, it cannot work.

Who could explain the reason to me?

+3
source share
3 answers

you need to put the luasocket stuff in the package.preload table as follows:

 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... 
+3
source

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

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

So, for your application to work, you need to collect all the .dll and .lua files into your application and manually download them (or configure them to load correctly using custom package loaders).

The email you provided includes the package.preload table (so that it now looks a bit strange, but might work anyway) to load the loaded C code correctly when calling require.

+1
source

Try to run

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

and maybe we can help.

0
source

All Articles