From 99577e806a7d649e5e4ab193d92ba454d04cf1d7 Mon Sep 17 00:00:00 2001 From: Ilari Liusvaara Date: Fri, 6 Jun 2014 15:53:38 +0300 Subject: [PATCH] Actually don't start if sysrc.lua is bad --- include/lua/lua.hpp | 4 ++-- src/lua/lua.cpp | 25 +++++++++++++++++-------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/include/lua/lua.hpp b/include/lua/lua.hpp index 674dbf94..d0fcf4ea 100644 --- a/include/lua/lua.hpp +++ b/include/lua/lua.hpp @@ -85,7 +85,7 @@ struct lua_state void do_eval_lua(const std::string& c) throw(std::bad_alloc); void do_run_lua(const std::string& c) throw(std::bad_alloc); - void run_sysrc_lua(); + void run_sysrc_lua(bool rerun); bool requests_repaint; bool requests_subframe_paint; @@ -103,7 +103,7 @@ struct lua_state std::list startup_scripts; std::map watch_vars; private: - void run_lua_fragment() throw(std::bad_alloc); + bool run_lua_fragment() throw(std::bad_alloc); template bool run_callback(lua::state::callback_list& list, T... args); void run_synchronous_paint(struct lua::render_context* ctx); lua::state& L; diff --git a/src/lua/lua.cpp b/src/lua/lua.cpp index e1844fb7..9be830ed 100644 --- a/src/lua/lua.cpp +++ b/src/lua/lua.cpp @@ -371,7 +371,7 @@ namespace core.lua->reset(); luaL_openlibs(core.lua->handle()); - core.lua2->run_sysrc_lua(); + core.lua2->run_sysrc_lua(true); copy_system_tables(*core.lua); messages << "Lua VM reset" << std::endl; }); @@ -412,7 +412,7 @@ void init_lua() throw() fatal_error(); } luaL_openlibs(core.lua->handle()); - core.lua2->run_sysrc_lua(); + core.lua2->run_sysrc_lua(false); copy_system_tables(*core.lua); } @@ -501,10 +501,11 @@ const std::map& lua_state::get_watch_vars() return watch_vars; } -void lua_state::run_lua_fragment() throw(std::bad_alloc) +bool lua_state::run_lua_fragment() throw(std::bad_alloc) { + bool result = true; if(recursive_flag) - return; + return false; #if LUA_VERSION_NUM == 501 int t = L.load(read_lua_fragment, &luareader_fragment, "run_lua_fragment"); #endif @@ -515,12 +516,12 @@ void lua_state::run_lua_fragment() throw(std::bad_alloc) messages << "Can't run Lua: Internal syntax error: " << L.tostring(-1) << std::endl; L.pop(1); - return; + return false; } if(t == LUA_ERRMEM) { messages << "Can't run Lua: Out of memory" << std::endl; L.pop(1); - return; + return false; } recursive_flag = true; int r = L.pcall(0, 0, 0); @@ -528,20 +529,24 @@ void lua_state::run_lua_fragment() throw(std::bad_alloc) if(r == LUA_ERRRUN) { messages << "Error running Lua hunk: " << L.tostring(-1) << std::endl; L.pop(1); + result = false; } if(r == LUA_ERRMEM) { messages << "Error running Lua hunk: Out of memory" << std::endl; L.pop(1); + result = false; } if(r == LUA_ERRERR) { messages << "Error running Lua hunk: Double Fault???" << std::endl; L.pop(1); + result = false; } render_ctx = NULL; if(requests_repaint) { requests_repaint = false; command.invoke("repaint"); } + return result; } void lua_state::do_eval_lua(const std::string& c) throw(std::bad_alloc) @@ -582,12 +587,16 @@ template bool lua_state::run_callback(lua::state::callback_list& return true; } -void lua_state::run_sysrc_lua() +void lua_state::run_sysrc_lua(bool rerun) { L.pushstring(lua_sysrc_script); L.setglobal(TEMPORARY); luareader_fragment = eval_sysrc_lua; - run_lua_fragment(); + if(!run_lua_fragment() && !rerun) { + //run_lua_fragment shows error. + //messages << "Failed to run sysrc lua script" << std::endl; + fatal_error(); + } } void lua_state::run_synchronous_paint(struct lua::render_context* ctx)