aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2016-05-24 21:37:33 +0200
committerMarc André Tanner <mat@brain-dump.org>2016-05-24 21:37:33 +0200
commit9bcfd3f83098dcd4246b95fc37d523e06129ed12 (patch)
tree377d9e6dd924aa126a55565c53df8ebdbdc735f8
parentf4174f325cfe4b2db1f6e07183b1f5df654ca3a8 (diff)
downloadvis-9bcfd3f83098dcd4246b95fc37d523e06129ed12.tar.gz
vis-9bcfd3f83098dcd4246b95fc37d523e06129ed12.tar.xz
vis-lua: improve error handling when loading visrc.lua
If loading fails because visrc.lua is not found, then simply display an information message. However if there is a syntax error, display a complete stack trace. This fixes commit 352155889aad57f8cb6d20317ffef81073fb6533.
-rw-r--r--vis-lua.c28
1 files changed, 25 insertions, 3 deletions
diff --git a/vis-lua.c b/vis-lua.c
index 3f4a21e..6bd2917 100644
--- a/vis-lua.c
+++ b/vis-lua.c
@@ -1284,6 +1284,25 @@ const char *vis_lua_paths_get(Vis *vis) {
return lua_tostring(L, -1);
}
+static bool package_exist(Vis *vis, lua_State *L, const char *name) {
+ const char lua[] =
+ "local name = ...\n"
+ "for _, searcher in ipairs(package.searchers or package.loaders) do\n"
+ "local loader = searcher(name)\n"
+ "if type(loader) == 'function' then\n"
+ "return true\n"
+ "end\n"
+ "end\n"
+ "return false\n";
+ if (luaL_loadstring(L, lua) != LUA_OK)
+ return false;
+ lua_pushstring(L, name);
+ /* an error indicates package exists */
+ bool ret = lua_pcall(L, 1, 1, 0) != LUA_OK || lua_toboolean(L, -1);
+ lua_pop(L, 1);
+ return ret;
+}
+
void vis_lua_init(Vis *vis) {
lua_State *L = luaL_newstate();
if (!L)
@@ -1395,10 +1414,13 @@ void vis_lua_init(Vis *vis) {
obj_ref_new(L, vis, "vis");
lua_setglobal(L, "vis");
- lua_getglobal(L, "require");
- lua_pushstring(L, "visrc");
- if (lua_pcall(L, 1, 0, 0))
+ if (!package_exist(vis, L, "visrc")) {
vis_info_show(vis, "WARNING: failed to load visrc.lua");
+ } else {
+ lua_getglobal(L, "require");
+ lua_pushstring(L, "visrc");
+ pcall(vis, L, 1, 0);
+ }
}
void vis_lua_start(Vis *vis) {