Setting the global LUA_PATH variable from C++/C?
Solution 1
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
Solution 2
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]); ...
Solution 3
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.
Solution 4
You can also change package.path
in Lua before calling require
.
Related videos on Youtube
Goles
Software Craftsman. Extremely interested in Web Technologies and Mobile Game Development. Using the sweet Obj-C, the evil C++ and the elegant Lua. Have been using lot's of Ruby spices in the mix lately.
Updated on March 07, 2020Comments
-
Goles over 3 years
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 ?
-
Puppy about 13 yearsI believe that they used to be forbidden but aren't any longer.
-
Kai about 13 years@DeadMG2 Maybe i missed something. I think no one ever reads in detail the frequent changes and additions to the contract
-
lhf about 13 yearsOr
env LUA_PATH="foo" /path/to/executable
. -
SoapiestWaffles almost 11 yearsThis works flawlessly, thank you. Exactly what i was looking for.
-
Watusimoto about 10 yearsI don't think LUA_PATH is an environment variable.
-
Shayne over 5 yearsThis isn't an answer to the question.
-
OLL almost 3 yearsI tried this, but
luaL_dostring
returns and error. The message I get isattempt to index a nil value (global 'package')
-
OLL almost 3 yearsActually turns that I was getting that error because I wasn't calling
luaL_openlibs(L)
. This solution works if the package lib is loaded properly