Setting the global LUA_PATH variable from C++/C?
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 iPhone app has a separate folder in the device ).
I know I could set the LUA_PATH by recompiling lua with a "fixed" path, but that's quite far from ideal.
( I'm trying to do this in order to be able to use require
, from my .lua
scripts.
Could anyone help me out开发者_运维问答 here ?
In 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!
}
I haven't tested or compiled it. I used: http://lua.org/pil and http://lua.org/manual/5.1
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]); ...
You can also change package.path
in Lua before calling require
.
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.
#include <stdlib.h>
...
setenv ( "LUA_PATH", (char *)my_path, 1 );
...or something like that...
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.
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.
精彩评论