From 9bcfd3f83098dcd4246b95fc37d523e06129ed12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Tue, 24 May 2016 21:37:33 +0200 Subject: 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. --- vis-lua.c | 28 +++++++++++++++++++++++++--- 1 file 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) { -- cgit v1.2.3