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?

4

3 回答 3

6

你需要把 luasocket 的东西放到 package.preload 表中,这样:

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...
于 2012-11-07T07:08:29.750 回答
1

luasocket 是一个混合的 C/lua 模块,如果您希望它在没有任何额外文件的情况下工作,您需要将两个版本捆绑到您的应用程序中。

socket.lua 加载 socket.core(来自 socket/core.dll)
mime.lua 加载 mime.core(来自 mime/core.dll)

因此,为了让您的应用程序正常工作,您需要将所有 .dll 文件和 .lua 文件构建到您的应用程序中并手动加载它们(或将它们设置为通过自定义包加载器正确加载)。

您引用的电子邮件正在调整 package.preload 表(以一种现在看起来有点奇怪但可能仍然有效的方式)以在调用 require 时正确加载内置 C 代码。

于 2010-02-20T03:54:26.967 回答
0

尝试运行

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

也许我们可以提供帮助。

于 2010-02-05T01:51:18.293 回答