mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-02-25 07:19:02 +00:00
* Update ProgramDirectory to prepend Lua path instead of appending (#862). * Update redbean ProgramDirectory to raise Lua error for easier handling * Update redbean default Lua path handling to simplify
This commit is contained in:
parent
d0d027810a
commit
d967a94c9a
1 changed files with 23 additions and 15 deletions
|
@ -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);
|
||||||
|
@ -4574,6 +4588,12 @@ static int LuaProgramBrand(lua_State *L) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static int LuaProgramDirectory(lua_State *L) {
|
static int LuaProgramDirectory(lua_State *L) {
|
||||||
|
struct stat st;
|
||||||
|
char *path = luaL_checkstring(L, 1);
|
||||||
|
// check to raise a Lua error, to allow it to be handled
|
||||||
|
if (stat(path, &st) == -1 || !S_ISDIR(st.st_mode)) {
|
||||||
|
return luaL_argerror(L, 1, "not a directory");
|
||||||
|
}
|
||||||
return LuaProgramString(L, ProgramDirectory);
|
return LuaProgramString(L, ProgramDirectory);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5339,22 +5359,11 @@ static void LuaSetConstant(lua_State *L, const char *s, long x) {
|
||||||
lua_setglobal(L, s);
|
lua_setglobal(L, s);
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *GetDefaultLuaPath(void) {
|
|
||||||
char *s;
|
|
||||||
size_t i;
|
|
||||||
for (s = 0, i = 0; i < stagedirs.n; ++i) {
|
|
||||||
appendf(&s, "%s/.lua/?.lua;%s/.lua/?/init.lua;", stagedirs.p[i].s,
|
|
||||||
stagedirs.p[i].s);
|
|
||||||
}
|
|
||||||
appends(&s, "/zip/.lua/?.lua;/zip/.lua/?/init.lua");
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void LuaStart(void) {
|
static void LuaStart(void) {
|
||||||
#ifndef STATIC
|
#ifndef STATIC
|
||||||
size_t i;
|
size_t i;
|
||||||
lua_State *L = GL = luaL_newstate();
|
lua_State *L = GL = luaL_newstate();
|
||||||
g_lua_path_default = GetDefaultLuaPath();
|
g_lua_path_default = DEFAULTLUAPATH;
|
||||||
luaL_openlibs(L);
|
luaL_openlibs(L);
|
||||||
for (i = 0; i < ARRAYLEN(kLuaLibs); ++i) {
|
for (i = 0; i < ARRAYLEN(kLuaLibs); ++i) {
|
||||||
luaL_requiref(L, kLuaLibs[i].name, kLuaLibs[i].func, 1);
|
luaL_requiref(L, kLuaLibs[i].name, kLuaLibs[i].func, 1);
|
||||||
|
@ -5504,7 +5513,6 @@ static void LuaDestroy(void) {
|
||||||
#ifndef STATIC
|
#ifndef STATIC
|
||||||
lua_State *L = GL;
|
lua_State *L = GL;
|
||||||
lua_close(L);
|
lua_close(L);
|
||||||
free(g_lua_path_default);
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue