Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c

scripting

lua

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 ?

like image 392
Goles Avatar asked Nov 08 '10 16:11

Goles


People also ask

What is LUA_PATH?

Lua modules and packages - what you need to know Lua module names are directly derived from the filename and relative path to the Lua search path. As with most search paths, the LUA_PATH is actually a semicolon-separated collection of filesystem paths. Lua scans them in the order they are listed to find a module.

How require works in lua?

Lua offers a higher-level function to load and run libraries, called require . Roughly, require does the same job as dofile , but with two important differences. First, require searches for the file in a path; second, require controls whether a file has already been run to avoid duplicating the work.

Where does Lua look for modules?

And Lua will now search for modules in the lib directory (in addition to where it usually does). You can also use the LUA_PATH environment variable to do this before even starting Lua.


2 Answers

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

like image 101
Jonathan Swinney Avatar answered Oct 03 '22 21:10

Jonathan Swinney


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]);
   ...
like image 37
Graham Perks Avatar answered Oct 03 '22 19:10

Graham Perks