[英]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 ). 我正在尝试直接从C / C ++设置我的全局LUA_PATH变量,我在我的iPhone应用程序中使用Lua,因此我的路径在应用程序之间会发生变化(每个iPhone应用程序在设备中都有一个单独的文件夹)。
I know I could set the LUA_PATH by recompiling lua with a "fixed" path, but that's quite far from ideal. 我知道我可以通过使用“固定”路径重新编译lua来设置LUA_PATH,但这远非理想。
( I'm trying to do this in order to be able to use require
, from my .lua
scripts. (我试图这样做是为了能够从我的
.lua
脚本中使用require
。
Could anyone help me out here ? 谁能帮到我这里?
In C++: 在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
我用过: http : //lua.org/pil和http://lua.org/manual/5.1
ObjC: Following from the other answer, here's what works for me. ObjC:从另一个答案来看,这对我有用。 The appending of "/?.lua" is needed.
需要附加“/?.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]); ...
您也可以在调用require
之前更改Lua中的package.path
。
You can set the LUA_PATH and LUA_CPATH within c++ very easily by executing a couple lual_dostring functions. 您可以通过执行一对lual_dostring函数,非常轻松地在c ++中设置LUA_PATH和LUA_CPATH。
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. 在你拥有lua_State(L here)之后,在你的lual_loadfile()和lau_pcall()函数调用之前调用这两行将添加到当前设置的路径和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. 我花了几个小时才找到这个解决方案..我希望它有所帮助。
I guess this will not be possibles since, as you metioned, for security reasons each iPhone app lives in it own sandbox. 我想这是不可能的,因为正如你所提到的,出于安全原因,每个iPhone应用程序都存在于自己的沙盒中。
I think using setenv
will set the environment variable only for the current process and it's children. 我认为使用
setenv
只会为当前进程设置环境变量,而它是子进程。
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. 顺便说一句:如果计划将您的应用程序提交到AppStore,(据我所知)脚本语言/口译员会受到您签署的合同的影响。
#include <stdlib.h>
... ...
setenv ( "LUA_PATH", (char *)my_path, 1 );
...or something like that... ...或类似的东西...
I'm not too familiar with iPhone development, but can you set the LUA_PATH env variable before executing your application? 我对iPhone开发不是很熟悉,但你可以在执行应用程序之前设置LUA_PATH env变量吗?
For example, in Linux, I could write a script that executes your binary like so: 例如,在Linux中,我可以编写一个执行二进制文件的脚本,如下所示:
export LUA_PATH="foo"
/path/to/executable
Windows has similar functionality with batch files. Windows具有与批处理文件类似的功能。
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. 如果你真的需要在代码中更改它,我不知道如何使用luaL_loadbuffer和lua_pcall来执行“package.path = blah”命令。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.