代码之家  ›  专栏  ›  技术社区  ›  Goles

从C++/C设置全局LuaYApple变量?

  •  11
  • Goles  · 技术社区  · 15 年前

    我试图直接从C/C++中设置我的全局LuaYPATH变量,我使用的是来自我的iPhone应用程序的Lua,所以我的路径倾向于在应用程序之间发生变化(每个iPhone应用程序在设备中都有一个单独的文件夹)。

    我知道我可以通过用一个“固定”路径重新编译LUA来设置LUA廑u路径,但这还远远不够理想。

    (我这样做是为了能够使用 require .lua 剧本。

    有人能帮我吗?

    7 回复  |  直到 15 年前
        1
  •  20
  •   Mike Caron    11 年前

    在C++中:

    int setLuaPath( lua_State* L, const char* path )
    {
        lua_getglobal( L, "package" );
        lua_getfield( L, -1, "path" ); // get field "path" from table at top of stack (-1)
        std::string cur_path = lua_tostring( L, -1 ); // grab path string from top of stack
        cur_path.append( ";" ); // do your path magic here
        cur_path.append( path );
        lua_pop( L, 1 ); // get rid of the string on the stack we just pushed on line 5
        lua_pushstring( L, cur_path.c_str() ); // push the new one
        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
        return 0; // all done!
    }
    

    我还没有测试或编译它。我用过: http://lua.org/pil http://lua.org/manual/5.1

        2
  •  4
  •   Graham Perks    10 年前

    ObjC:从另一个答案来看,这是对我有效的。加上“/?.lua“是必需的。

    int setLuaPath( NSString* path )  
    {
        lua_getglobal( L, "package" );
        lua_getfield( L, -1, "path" ); // get field "path" from table at top of stack (-1)
        NSString * cur_path = [NSString stringWithUTF8String:lua_tostring( L, -1 )]; // grab path string from top of stack
        cur_path = [cur_path stringByAppendingString:@";"]; // do your path magic here
        cur_path = [cur_path stringByAppendingString:path];
        cur_path = [cur_path stringByAppendingString:@"/?.lua"];
        lua_pop( L, 1 ); // get rid of the string on the stack we just pushed on line 5
        lua_pushstring( L, [cur_path UTF8String]); // push the new one
        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
        return 0; // all done!
    }
    
       ... add this code somewhere, near where you lua_open() for example
    
       // Set Lua's Package.path to where our Lua files can be found
       NSString *luaPath = [[NSBundle mainBundle] pathForResource:@"name of any one of my lua files" ofType:@"lua"];
       setLuaPath([luaPath stringByDeletingLastPathComponent]);
       ...
    
        3
  •  3
  •   lhf    15 年前

    package.path 打电话之前在卢阿 require .

        4
  •  3
  •   DusX    9 年前

    通过执行两个lual_dostring函数,可以很容易地在c++中设置LUA_PATH和LUA_CPATH。

    luaL_dostring(L, "package.path = package.path .. ';?.lua'");
    luaL_dostring(L, "package.cpath = package.cpath .. ';?.dll'");
    

    我花了几个小时才找到这个解决办法。。我希望有帮助。

        5
  •  0
  •   onemasse    15 年前
    #include <stdlib.h>
    

    ...

    setenv ( "LUA_PATH", (char *)my_path, 1 );
    

        6
  •  0
  •   Kai    15 年前

    我想这是不可能的,因为,正如你所见,出于安全原因,每个iPhone应用程序都生活在自己的沙盒中。

    我想用 setenv 将只为当前进程及其子进程设置环境变量。

    顺便说一句:如果计划将你的应用程序提交给AppStore,(据我所知),脚本语言/解释器将根据你签署的合同进行修改。

        7
  •  -1
  •   lhumongous    15 年前

    我不太熟悉iPhone的开发,但是在执行应用程序之前可以设置LUA_PATH env变量吗?

    export LUA_PATH="foo"
    /path/to/executable
    

    Windows具有与批处理文件类似的功能。