Fix crash on script load errors

This commit is contained in:
Thomas Goyne 2014-04-28 13:36:56 -07:00
parent e3fa270345
commit 606e3f4882
2 changed files with 83 additions and 84 deletions

View File

@ -61,6 +61,7 @@
#include <boost/algorithm/string/case_conv.hpp> #include <boost/algorithm/string/case_conv.hpp>
#include <boost/algorithm/string/classification.hpp> #include <boost/algorithm/string/classification.hpp>
#include <boost/format.hpp> #include <boost/format.hpp>
#include <boost/scope_exit.hpp>
#include <cassert> #include <cassert>
#include <cstdint> #include <cstdint>
#include <lauxlib.h> #include <lauxlib.h>
@ -373,9 +374,13 @@ namespace {
{ {
Destroy(); Destroy();
try { name = GetPrettyFilename().string();
// create lua environment // create lua environment
L = lua_open(); L = lua_open();
bool loaded = false;
BOOST_SCOPE_EXIT_ALL(&) { if (!loaded) Destroy(); };
LuaStackcheck stackcheck(L); LuaStackcheck stackcheck(L);
// register standard libs // register standard libs
@ -431,9 +436,9 @@ namespace {
// load user script // load user script
if (!LoadFile(L, GetFilename())) { if (!LoadFile(L, GetFilename())) {
std::string err = get_string_or_default(L, 1); description = get_string_or_default(L, 1);
lua_pop(L, 1); lua_pop(L, 1);
throw ScriptLoadError(err); return;
} }
stackcheck.check_stack(1); stackcheck.check_stack(1);
@ -442,16 +447,17 @@ namespace {
// don't thread this, as there's no point in it and it seems to break on wx 2.8.3, for some reason // don't thread this, as there's no point in it and it seems to break on wx 2.8.3, for some reason
if (lua_pcall(L, 0, 0, 0)) { if (lua_pcall(L, 0, 0, 0)) {
// error occurred, assumed to be on top of Lua stack // error occurred, assumed to be on top of Lua stack
std::string err = str(boost::format("Error initialising Lua script \"%s\":\n\n%s") % GetPrettyFilename().string() % get_string_or_default(L, -1)); description = str(boost::format("Error initialising Lua script \"%s\":\n\n%s") % GetPrettyFilename().string() % get_string_or_default(L, -1));
lua_pop(L, 1); lua_pop(L, 1);
throw ScriptLoadError(err); return;
} }
stackcheck.check_stack(0); stackcheck.check_stack(0);
lua_getglobal(L, "version"); lua_getglobal(L, "version");
if (lua_isnumber(L, -1) && lua_tointeger(L, -1) == 3) { if (lua_isnumber(L, -1) && lua_tointeger(L, -1) == 3) {
lua_pop(L, 1); // just to avoid tripping the stackcheck in debug lua_pop(L, 1); // just to avoid tripping the stackcheck in debug
throw ScriptLoadError("Attempted to load an Automation 3 script as an Automation 4 Lua script. Automation 3 is no longer supported."); description = "Attempted to load an Automation 3 script as an Automation 4 Lua script. Automation 3 is no longer supported.";
return;
} }
name = get_global_string(L, "script_name"); name = get_global_string(L, "script_name");
@ -464,14 +470,7 @@ namespace {
lua_pop(L, 1); lua_pop(L, 1);
// if we got this far, the script should be ready // if we got this far, the script should be ready
stackcheck.check_stack(0); loaded = true;
}
catch (agi::Exception const& e) {
Destroy();
name = GetPrettyFilename().string();
description = e.GetChainedMessage();
}
} }
void LuaScript::Destroy() void LuaScript::Destroy()

View File

@ -173,7 +173,7 @@ namespace Automation4 {
luaL_where(L, 1); luaL_where(L, 1);
lua_insert(L, 1); lua_insert(L, 1);
lua_concat(L, 2); lua_concat(L, 2);
throw error_tag(); lua_error(L);
} }
} }