像oslib,debuglib一样,我如何在内部将套接字嵌入Lua? [英] How could I embedded socket in Lua internally, just like oslib, debuglib?
问题描述
我想实现类似将套接字功能嵌入我的Lua构建中的功能. 因此,我不再需要复制socket.core.dll(只是为了好玩).
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).
我搜索了邮件列表,看到一些人在讨论这个话题, http://lua-users.org/lists/lua- l/2005-10/msg00269.html
I search the maillist, and see some guys discuss the topic, http://lua-users.org/lists/lua-l/2005-10/msg00269.html
但是我对详细步骤有疑问,谁能给我详细的步骤来更改lua和luasocket代码以使它们协同工作(而不是使用dll方法).
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).
我使用VC2008在Windows xp中尝试了以下步骤:
I tried these steps in windows xp with VC2008:
1)将luasocket代码复制到Lua项目中.
1) copy luasocket code to Lua project.
2)添加一些代码
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)构建项目,然后运行它.
3) build the project, and run it.
当我键入print(socket._VERSION)
时,它显示luasocket 2.0.2
,这是正确的.
When I type print(socket._VERSION)
, it shows luasocket 2.0.2
, it is correct.
当我键入print(socket.dns.toip("localhost"))
时,它显示127.0.0.1 table: 00480AD0
,这也是正确的.
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.
谁能告诉我原因?
推荐答案
您需要通过以下方式将luasocket内容放入package.preload表中:
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...
这篇关于像oslib,debuglib一样,我如何在内部将套接字嵌入Lua?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!