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,105 +374,103 @@ namespace {
{ {
Destroy(); Destroy();
try { name = GetPrettyFilename().string();
// create lua environment
L = lua_open();
LuaStackcheck stackcheck(L);
// register standard libs // create lua environment
preload_modules(L); L = lua_open();
stackcheck.check_stack(0);
// dofile and loadfile are replaced with include bool loaded = false;
lua_pushnil(L); BOOST_SCOPE_EXIT_ALL(&) { if (!loaded) Destroy(); };
lua_setglobal(L, "dofile"); LuaStackcheck stackcheck(L);
lua_pushnil(L);
lua_setglobal(L, "loadfile");
push_value(L, exception_wrapper<LuaInclude>);
lua_setglobal(L, "include");
// Replace the default lua module loader with our unicode compatible // register standard libs
// one and set the module search path preload_modules(L);
Install(L, include_path); stackcheck.check_stack(0);
stackcheck.check_stack(0);
// prepare stuff in the registry // dofile and loadfile are replaced with include
lua_pushnil(L);
lua_setglobal(L, "dofile");
lua_pushnil(L);
lua_setglobal(L, "loadfile");
push_value(L, exception_wrapper<LuaInclude>);
lua_setglobal(L, "include");
// store the script's filename // Replace the default lua module loader with our unicode compatible
push_value(L, GetFilename().stem()); // one and set the module search path
lua_setfield(L, LUA_REGISTRYINDEX, "filename"); Install(L, include_path);
stackcheck.check_stack(0); stackcheck.check_stack(0);
// reference to the script object // prepare stuff in the registry
push_value(L, this);
lua_setfield(L, LUA_REGISTRYINDEX, "aegisub");
stackcheck.check_stack(0);
// make "aegisub" table // store the script's filename
lua_pushstring(L, "aegisub"); push_value(L, GetFilename().stem());
lua_createtable(L, 0, 12); lua_setfield(L, LUA_REGISTRYINDEX, "filename");
stackcheck.check_stack(0);
set_field<LuaCommand::LuaRegister>(L, "register_macro"); // reference to the script object
set_field<LuaExportFilter::LuaRegister>(L, "register_filter"); push_value(L, this);
set_field<lua_text_textents>(L, "text_extents"); lua_setfield(L, LUA_REGISTRYINDEX, "aegisub");
set_field<frame_from_ms>(L, "frame_from_ms"); stackcheck.check_stack(0);
set_field<ms_from_frame>(L, "ms_from_frame");
set_field<video_size>(L, "video_size");
set_field<get_keyframes>(L, "keyframes");
set_field<decode_path>(L, "decode_path");
set_field<cancel_script>(L, "cancel");
set_field(L, "lua_automation_version", 4);
set_field<clipboard_init>(L, "__init_clipboard");
set_field<get_file_name>(L, "file_name");
set_field<get_translation>(L, "gettext");
// store aegisub table to globals // make "aegisub" table
lua_settable(L, LUA_GLOBALSINDEX); lua_pushstring(L, "aegisub");
stackcheck.check_stack(0); lua_createtable(L, 0, 12);
// load user script set_field<LuaCommand::LuaRegister>(L, "register_macro");
if (!LoadFile(L, GetFilename())) { set_field<LuaExportFilter::LuaRegister>(L, "register_filter");
std::string err = get_string_or_default(L, 1); set_field<lua_text_textents>(L, "text_extents");
lua_pop(L, 1); set_field<frame_from_ms>(L, "frame_from_ms");
throw ScriptLoadError(err); set_field<ms_from_frame>(L, "ms_from_frame");
} set_field<video_size>(L, "video_size");
stackcheck.check_stack(1); set_field<get_keyframes>(L, "keyframes");
set_field<decode_path>(L, "decode_path");
set_field<cancel_script>(L, "cancel");
set_field(L, "lua_automation_version", 4);
set_field<clipboard_init>(L, "__init_clipboard");
set_field<get_file_name>(L, "file_name");
set_field<get_translation>(L, "gettext");
// and execute it // store aegisub table to globals
// this is where features are registered lua_settable(L, LUA_GLOBALSINDEX);
// don't thread this, as there's no point in it and it seems to break on wx 2.8.3, for some reason stackcheck.check_stack(0);
if (lua_pcall(L, 0, 0, 0)) {
// 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));
lua_pop(L, 1);
throw ScriptLoadError(err);
}
stackcheck.check_stack(0);
lua_getglobal(L, "version");
if (lua_isnumber(L, -1) && lua_tointeger(L, -1) == 3) {
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.");
}
name = get_global_string(L, "script_name");
description = get_global_string(L, "script_description");
author = get_global_string(L, "script_author");
version = get_global_string(L, "script_version");
if (name.empty())
name = GetPrettyFilename().string();
// load user script
if (!LoadFile(L, GetFilename())) {
description = get_string_or_default(L, 1);
lua_pop(L, 1); lua_pop(L, 1);
// if we got this far, the script should be ready return;
stackcheck.check_stack(0); }
stackcheck.check_stack(1);
// and execute it
// this is where features are registered
// 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)) {
// error occurred, assumed to be on top of Lua stack
description = str(boost::format("Error initialising Lua script \"%s\":\n\n%s") % GetPrettyFilename().string() % get_string_or_default(L, -1));
lua_pop(L, 1);
return;
} }
catch (agi::Exception const& e) { stackcheck.check_stack(0);
Destroy();
lua_getglobal(L, "version");
if (lua_isnumber(L, -1) && lua_tointeger(L, -1) == 3) {
lua_pop(L, 1); // just to avoid tripping the stackcheck in debug
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");
description = get_global_string(L, "script_description");
author = get_global_string(L, "script_author");
version = get_global_string(L, "script_version");
if (name.empty())
name = GetPrettyFilename().string(); name = GetPrettyFilename().string();
description = e.GetChainedMessage();
} lua_pop(L, 1);
// if we got this far, the script should be ready
loaded = true;
} }
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);
} }
} }