Update ProgramDirectory to prepend Lua path instead of appending (#862).

This commit is contained in:
Paul Kulchenko 2023-07-23 17:15:53 -07:00
parent 18536950b3
commit 3359af9fc9

View file

@ -1084,15 +1084,29 @@ static bool HasString(struct Strings *l, const char *s, size_t n) {
return false; return false;
} }
static const char* DEFAULTLUAPATH = "/zip/.lua/?.lua;/zip/.lua/?/init.lua";
static void UpdateLuaPath(const char *s) { static void UpdateLuaPath(const char *s) {
#ifndef STATIC #ifndef STATIC
lua_State *L = GL; lua_State *L = GL;
char *curpath = "";
char *respath = 0;
char *t;
int n = lua_gettop(L); int n = lua_gettop(L);
lua_getglobal(L, "package"); lua_getglobal(L, "package");
if (lua_istable(L, -1)) { if (lua_istable(L, -1)) {
lua_getfield(L, -1, "path"); lua_getfield(L, -1, "path");
lua_pushstring(L, _gc(xasprintf("%s;%s/.lua/?.lua;%s/.lua/?/init.lua", curpath = luaL_optstring(L, -1, "");
luaL_optstring(L, -1, ""), s, s))); if (t = strstr(curpath, DEFAULTLUAPATH)) {
// if the DEFAULT path is found, prepend the path in front of it
respath = xasprintf("%.*s%s/.lua/?.lua;%s/.lua/?/init.lua;%s",
t-curpath, curpath, s, s, t);
} else {
// if the DEFAULT path is not found, append to the end
respath = xasprintf("%s;%s/.lua/?.lua;%s/.lua/?/init.lua",
curpath, s, s);
}
lua_pushstring(L, _gc(respath));
lua_setfield(L, -3, "path"); lua_setfield(L, -3, "path");
} }
lua_settop(L, n); lua_settop(L, n);
@ -5342,7 +5356,7 @@ static char *GetDefaultLuaPath(void) {
appendf(&s, "%s/.lua/?.lua;%s/.lua/?/init.lua;", stagedirs.p[i].s, appendf(&s, "%s/.lua/?.lua;%s/.lua/?/init.lua;", stagedirs.p[i].s,
stagedirs.p[i].s); stagedirs.p[i].s);
} }
appends(&s, "/zip/.lua/?.lua;/zip/.lua/?/init.lua"); appends(&s, DEFAULTLUAPATH);
return s; return s;
} }