Permalink
Browse files

Relocatable LUA_ROOT.

  • Loading branch information...
1 parent f3d058b commit 97981fc831edce6a572b4ed08d042d08f1492d0f @brimworks committed Dec 4, 2011
Showing with 33 additions and 4 deletions.
  1. +12 −0 src/lib_package.c
  2. +5 −3 src/luaconf.h
  3. +16 −1 src/luajit.c
View
12 src/lib_package.c
@@ -528,6 +528,18 @@ static void setpath(lua_State *L, const char *fieldname, const char *envname,
luaL_gsub(L, path, AUXMARK, def);
lua_remove(L, -2);
}
+
+ // Replace the lua root maker with the value of LUA_ROOT
+ lua_getglobal(L, "LUA_ROOT");
+ if ( lua_isnil(L, -1) ) {
+ lua_pop(L, 1);
+ lua_pushliteral(L, LUA_ROOT);
+ }
+ luaL_gsub(L, lua_tostring(L, -2), LUA_ROOT_MARK,
+ lua_tostring(L, -1));
+ lua_insert(L, -3);
+ lua_pop(L, 2);
+
setprogdir(L);
lua_setfield(L, -2, fieldname);
}
View
8 src/luaconf.h
@@ -9,6 +9,8 @@
#include <limits.h>
#include <stddef.h>
+#define LUA_ROOT_MARK "..."
+
/* Default path for loading Lua and C modules with require(). */
#if defined(_WIN32)
/*
@@ -23,15 +25,15 @@
".\\?.dll;" LUA_CDIR"?.dll;" LUA_CDIR"loadall.dll"
#else
#define LUA_ROOT "/usr/local/"
-#define LUA_LDIR LUA_ROOT "share/lua/5.1/"
-#define LUA_CDIR LUA_ROOT "lib/lua/5.1/"
+#define LUA_LDIR LUA_ROOT_MARK "/share/lua/5.1/"
+#define LUA_CDIR LUA_ROOT_MARK "/lib/lua/5.1/"
#ifdef LUA_XROOT
#define LUA_JDIR LUA_XROOT "share/luajit-2.0.0-beta8/"
#define LUA_XPATH \
";" LUA_XROOT "share/lua/5.1/?.lua;" LUA_XROOT "share/lua/5.1/?/init.lua"
#define LUA_XCPATH LUA_XROOT "lib/lua/5.1/?.so;"
#else
-#define LUA_JDIR LUA_ROOT "share/luajit-2.0.0-beta8/"
+#define LUA_JDIR LUA_ROOT_MARK "/share/luajit-2.0.0-beta8/"
#define LUA_XPATH
#define LUA_XCPATH
#endif
View
17 src/luajit.c
@@ -500,7 +500,22 @@ static int pmain(lua_State *L)
int script;
int flags = 0;
globalL = L;
- if (argv[0] && argv[0][0]) progname = argv[0];
+ if (argv[0] && argv[0][0]) {
+ const char* end = progname = argv[0];
+ int slashes_found = 0;
+ /* dirname(dirname(progname)) */
+ while ( *end ) end++;
+ for (; 1; end-- ) {
+ if ( end < progname ) goto NO_LUA_ROOT;
+ if ( *end != '/' ) continue;
+ if ( ++slashes_found >= 2 ) break;
+ end--;
+ }
+ lua_pushlstring(L, progname, end-progname);
+ lua_setglobal(L, "LUA_ROOT");
+ }
+ NO_LUA_ROOT:
+
LUAJIT_VERSION_SYM(); /* linker-enforced version check */
lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */
luaL_openlibs(L); /* open libraries */

0 comments on commit 97981fc

Please sign in to comment.