Setting the global LUA_PATH variable from C++/C?

前端 未结 7 485
说谎
说谎 2020-12-28 09:58

I\'m trying to set my global LUA_PATH variable directly from C/C++, I\'m using Lua from my iPhone applications, so my path tends does change between applications ( each iPho

相关标签:
7条回答
  • 2020-12-28 10:05

    You can also change package.path in Lua before calling require.

    0 讨论(0)
  • 2020-12-28 10:06

    I guess this will not be possibles since, as you metioned, for security reasons each iPhone app lives in it own sandbox.

    I think using setenv will set the environment variable only for the current process and it's children.

    And by the way: If plan to submit your app to the AppStore, (as far as i know) script languages/interpreters are fobidden by the contract you signed.

    0 讨论(0)
  • 2020-12-28 10:07

    ObjC: Following from the other answer, here's what works for me. The appending of "/?.lua" is needed.

    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]);
       ...
    
    0 讨论(0)
  • 2020-12-28 10:11
    #include <stdlib.h>
    

    ...

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

    ...or something like that...

    0 讨论(0)
  • 2020-12-28 10:21

    You can set the LUA_PATH and LUA_CPATH within c++ very easily by executing a couple lual_dostring functions.

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

    These two line called after you have your lua_State (L here) and before your lual_loadfile() and lau_pcall() function calls will add to the currently set path and cpath. In this case I add to both an instruction to look local to the execution.

    It took me hours to find this solution.. I hope it helps.

    0 讨论(0)
  • 2020-12-28 10:24

    I'm not too familiar with iPhone development, but can you set the LUA_PATH env variable before executing your application?

    For example, in Linux, I could write a script that executes your binary like so:

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

    Windows has similar functionality with batch files.

    If you really need to change it in code, I don't know how to do that short of using luaL_loadbuffer and lua_pcall to execute a "package.path = blah" command.

    0 讨论(0)
提交回复
热议问题