From c96987909e4435bc354967a799758efa865e8c92 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Mon, 9 Jan 2017 01:03:16 +0100 Subject: [PATCH] Modify package.cpath (#1371) * luaa.c: Remove useless stack operation We get package.loaded and immediately throw away the result. That's pointless, so remove this. Signed-off-by: Uli Schlachter * Refactor modification of package.path Awesome adds various entries to package.path during startup. This commit moves that into a helper function. No functional changes intended. The only change I did to the code was changing a call to lua_type(L, 2) into lua_type(L, -1);. Signed-off-by: Uli Schlachter * Modify package.cpath just like package.path This adds, for example, paths specified via the --search argument also to package.cpath. Fixes: https://github.com/awesomeWM/awesome/issues/1248 Signed-off-by: Uli Schlachter --- luaa.c | 82 +++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 53 insertions(+), 29 deletions(-) diff --git a/luaa.c b/luaa.c index 332105d1..3b699334 100644 --- a/luaa.c +++ b/luaa.c @@ -630,6 +630,54 @@ setup_awesome_signals(lua_State *L) lua_pop(L, 1); } +/* Add things to the string on top of the stack */ +static void +add_to_search_path(lua_State *L, string_array_t *searchpath, bool for_lua) +{ + if (LUA_TSTRING != lua_type(L, -1)) + { + warn("package.path is not a string"); + return; + } + + foreach(entry, *searchpath) + { + int components; + size_t len = a_strlen(*entry); + lua_pushliteral(L, ";"); + lua_pushlstring(L, *entry, len); + if (for_lua) + lua_pushliteral(L, "/?.lua"); + else + lua_pushliteral(L, "/?.so"); + lua_concat(L, 3); + + if (for_lua) + { + lua_pushliteral(L, ";"); + lua_pushlstring(L, *entry, len); + lua_pushliteral(L, "/?/init.lua"); + lua_concat(L, 3); + + components = 2; + } else { + components = 1; + } + lua_concat(L, components + 1); /* concatenate with string on top of the stack */ + } + + /* add Lua lib path (/usr/share/awesome/lib by default) */ + if (for_lua) + { + lua_pushliteral(L, ";" AWESOME_LUA_LIB_PATH "/?.lua"); + lua_pushliteral(L, ";" AWESOME_LUA_LIB_PATH "/?/init.lua"); + lua_concat(L, 3); /* concatenate with thing on top of the stack when we were called */ + } else { + lua_pushliteral(L, ";" AWESOME_LUA_LIB_PATH "/?.so"); + lua_concat(L, 2); /* concatenate with thing on top of the stack when we were called */ + } +} + /** Initialize the Lua VM * \param xdg An xdg handle to use to get XDG basedir. */ @@ -747,38 +795,14 @@ luaA_init(xdgHandle* xdg, string_array_t *searchpath) return; } lua_getfield(L, 1, "path"); - if (LUA_TSTRING != lua_type(L, 2)) - { - warn("package.path is not a string"); - lua_pop(L, 1); - return; - } - - foreach(entry, *searchpath) - { - size_t len = a_strlen(*entry); - lua_pushliteral(L, ";"); - lua_pushlstring(L, *entry, len); - lua_pushliteral(L, "/?.lua"); - lua_concat(L, 3); - - lua_pushliteral(L, ";"); - lua_pushlstring(L, *entry, len); - lua_pushliteral(L, "/?/init.lua"); - lua_concat(L, 3); - - lua_concat(L, 3); /* concatenate with package.path */ - } - - /* add Lua lib path (/usr/share/awesome/lib by default) */ - lua_pushliteral(L, ";" AWESOME_LUA_LIB_PATH "/?.lua"); - lua_pushliteral(L, ";" AWESOME_LUA_LIB_PATH "/?/init.lua"); - lua_concat(L, 3); /* concatenate with package.path */ + add_to_search_path(L, searchpath, true); lua_setfield(L, 1, "path"); /* package.path = "concatenated string" */ - lua_getfield(L, 1, "loaded"); + lua_getfield(L, 1, "cpath"); + add_to_search_path(L, searchpath, false); + lua_setfield(L, 1, "cpath"); /* package.cpath = "concatenated string" */ - lua_pop(L, 2); /* pop "package" and "package.loaded" */ + lua_pop(L, 1); /* pop "package" */ } static void