我如何在iOS应用程序中使用LuaSocket?

我正在开发一款 iOS 应用,可以运行 Lua 脚本,我可以通过 CocoaPods 轻松集成基本的 lua 支持,但是如何将 LuaSocket 库添加到其中呢? LuaSocket 包含一些 C 和一些 Lua 文件,有没有人有什么想法呢?谢谢!

点赞
用户3344236
用户3344236

我使用了LuaSocket 2.0.2版本和Lua 5.1版本。修改了一些文件之后[未知类型名称“luaL_reg”;你是不是想说“luaL_Reg”?],我成功编译了。还移除了wsocket(.h和.c)文件。所以现在系统正在编译。经过一些搜索,我发现Cocos2d-x源代码(3.0.4)使用了luasocket文件夹(并有liblua.a)。我又一次移除了wsocket文件,然后成功地编译了。我用了以下代码:

-(void)addBundlePathToLuaState:(lua_State*)L
{
    lua_getglobal(L, "package");
    lua_getfield(L, -1, "path"); // get field "path" from table at top of stack (-1)

    const char* current_path_const = lua_tostring(L, -1); // grab path string from top of stack
    NSString* current_path = [NSString stringWithFormat:@"%s;%@/?.lua", current_path_const, [[NSBundle mainBundle]resourcePath]];

    lua_pop(L, 1); // get rid of the string on the stack we just pushed on line 5
    lua_pushstring(L, [current_path UTF8String]); // push the new one
    NSLog(@"path current %s", [current_path UTF8String]);
    lua_setfield(L, -2, "path"); // set the field "path" in table at -2 with value at top of stack
    lua_pop(L, 1); // get rid of package table from top of stack
}

int status;
lua_State *La;
La = luaL_newstate();
NSBundle* myBundle = [NSBundle mainBundle];
NSString* myImage = [myBundle pathForResource:@"a" ofType:@"lua"];
const char *stringAsChar = [myImage cStringUsingEncoding:[NSString defaultCStringEncoding]];
NSLog(@"mypath %s", stringAsChar);
luaL_openlibs(La); /* Load Lua libraries */
/* Load the file containing the script we are going to run */
status = luaL_loadfile(La, stringAsChar);
NSString *luaFilePath = [[NSBundle mainBundle] pathForResource:@"a" ofType:@"lua"];
[self addBundlePathToLuaState:La];

我的脚本使用了local socket = require("socket")但是问题是它找不到核心socket.lua:13:module 'socket.core' not found,no field package.preload['socket.core']。所以我认为你不能很快成功 :)

2015-02-13 11:44:41