forked from mia/Aegisub
Add an error check for trying to interact with expired subtitles objects
This commit is contained in:
parent
0cc7254210
commit
bd7a71abf4
2 changed files with 17 additions and 14 deletions
|
@ -103,7 +103,7 @@ namespace Automation4 {
|
||||||
// LuaAssFile can only be deleted by the reference count hitting zero
|
// LuaAssFile can only be deleted by the reference count hitting zero
|
||||||
~LuaAssFile();
|
~LuaAssFile();
|
||||||
public:
|
public:
|
||||||
static LuaAssFile *GetObjPointer(lua_State *L, int idx);
|
static LuaAssFile *GetObjPointer(lua_State *L, int idx, bool allow_expired);
|
||||||
|
|
||||||
/// makes a Lua representation of AssEntry and places on the top of the stack
|
/// makes a Lua representation of AssEntry and places on the top of the stack
|
||||||
static void AssEntryToLua(lua_State *L, AssEntry *e);
|
static void AssEntryToLua(lua_State *L, AssEntry *e);
|
||||||
|
|
|
@ -105,13 +105,13 @@ namespace {
|
||||||
template<int (LuaAssFile::*closure)(lua_State *)>
|
template<int (LuaAssFile::*closure)(lua_State *)>
|
||||||
int closure_wrapper(lua_State *L)
|
int closure_wrapper(lua_State *L)
|
||||||
{
|
{
|
||||||
return (LuaAssFile::GetObjPointer(L, lua_upvalueindex(1))->*closure)(L);
|
return (LuaAssFile::GetObjPointer(L, lua_upvalueindex(1), false)->*closure)(L);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<void (LuaAssFile::*closure)(lua_State *)>
|
template<void (LuaAssFile::*closure)(lua_State *), bool allow_expired>
|
||||||
int closure_wrapper_v(lua_State *L)
|
int closure_wrapper_v(lua_State *L)
|
||||||
{
|
{
|
||||||
(LuaAssFile::GetObjPointer(L, lua_upvalueindex(1))->*closure)(L);
|
(LuaAssFile::GetObjPointer(L, lua_upvalueindex(1), allow_expired)->*closure)(L);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -325,13 +325,13 @@ namespace Automation4 {
|
||||||
|
|
||||||
lua_pushvalue(L, 1);
|
lua_pushvalue(L, 1);
|
||||||
if (strcmp(idx, "delete") == 0)
|
if (strcmp(idx, "delete") == 0)
|
||||||
lua_pushcclosure(L, closure_wrapper_v<&LuaAssFile::ObjectDelete>, 1);
|
lua_pushcclosure(L, closure_wrapper_v<&LuaAssFile::ObjectDelete, false>, 1);
|
||||||
else if (strcmp(idx, "deleterange") == 0)
|
else if (strcmp(idx, "deleterange") == 0)
|
||||||
lua_pushcclosure(L, closure_wrapper_v<&LuaAssFile::ObjectDeleteRange>, 1);
|
lua_pushcclosure(L, closure_wrapper_v<&LuaAssFile::ObjectDeleteRange, false>, 1);
|
||||||
else if (strcmp(idx, "insert") == 0)
|
else if (strcmp(idx, "insert") == 0)
|
||||||
lua_pushcclosure(L, closure_wrapper_v<&LuaAssFile::ObjectInsert>, 1);
|
lua_pushcclosure(L, closure_wrapper_v<&LuaAssFile::ObjectInsert, false>, 1);
|
||||||
else if (strcmp(idx, "append") == 0)
|
else if (strcmp(idx, "append") == 0)
|
||||||
lua_pushcclosure(L, closure_wrapper_v<&LuaAssFile::ObjectAppend>, 1);
|
lua_pushcclosure(L, closure_wrapper_v<&LuaAssFile::ObjectAppend, false>, 1);
|
||||||
else {
|
else {
|
||||||
// idiot
|
// idiot
|
||||||
lua_pop(L, 1);
|
lua_pop(L, 1);
|
||||||
|
@ -586,11 +586,14 @@ namespace Automation4 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
LuaAssFile *LuaAssFile::GetObjPointer(lua_State *L, int idx)
|
LuaAssFile *LuaAssFile::GetObjPointer(lua_State *L, int idx, bool allow_expired)
|
||||||
{
|
{
|
||||||
assert(lua_type(L, idx) == LUA_TUSERDATA);
|
assert(lua_type(L, idx) == LUA_TUSERDATA);
|
||||||
void *ud = lua_touserdata(L, idx);
|
auto ud = lua_touserdata(L, idx);
|
||||||
return *((LuaAssFile**)ud);
|
auto laf = *static_cast<LuaAssFile **>(ud);
|
||||||
|
if (!allow_expired && laf->references < 2)
|
||||||
|
luaL_error(L, "Subtitles object is no longer valid");
|
||||||
|
return laf;
|
||||||
}
|
}
|
||||||
|
|
||||||
void LuaAssFile::ProcessingComplete(wxString const& undo_description)
|
void LuaAssFile::ProcessingComplete(wxString const& undo_description)
|
||||||
|
@ -640,9 +643,9 @@ namespace Automation4 {
|
||||||
// make the metatable
|
// make the metatable
|
||||||
lua_newtable(L);
|
lua_newtable(L);
|
||||||
set_field(L, "__index", closure_wrapper<&LuaAssFile::ObjectIndexRead>);
|
set_field(L, "__index", closure_wrapper<&LuaAssFile::ObjectIndexRead>);
|
||||||
set_field(L, "__newindex", closure_wrapper_v<&LuaAssFile::ObjectIndexWrite>);
|
set_field(L, "__newindex", closure_wrapper_v<&LuaAssFile::ObjectIndexWrite, false>);
|
||||||
set_field(L, "__len", closure_wrapper<&LuaAssFile::ObjectGetLen>);
|
set_field(L, "__len", closure_wrapper<&LuaAssFile::ObjectGetLen>);
|
||||||
set_field(L, "__gc", closure_wrapper_v<&LuaAssFile::ObjectGarbageCollect>);
|
set_field(L, "__gc", closure_wrapper_v<&LuaAssFile::ObjectGarbageCollect, true>);
|
||||||
set_field(L, "__ipairs", closure_wrapper<&LuaAssFile::ObjectIPairs>);
|
set_field(L, "__ipairs", closure_wrapper<&LuaAssFile::ObjectIPairs>);
|
||||||
lua_setmetatable(L, -2);
|
lua_setmetatable(L, -2);
|
||||||
|
|
||||||
|
@ -651,7 +654,7 @@ namespace Automation4 {
|
||||||
lua_getglobal(L, "aegisub");
|
lua_getglobal(L, "aegisub");
|
||||||
|
|
||||||
set_field(L, "parse_karaoke_data", closure_wrapper<&LuaAssFile::LuaParseKaraokeData>);
|
set_field(L, "parse_karaoke_data", closure_wrapper<&LuaAssFile::LuaParseKaraokeData>);
|
||||||
set_field(L, "set_undo_point", closure_wrapper_v<&LuaAssFile::LuaSetUndoPoint>);
|
set_field(L, "set_undo_point", closure_wrapper_v<&LuaAssFile::LuaSetUndoPoint, false>);
|
||||||
|
|
||||||
lua_pop(L, 1); // pop "aegisub" table
|
lua_pop(L, 1); // pop "aegisub" table
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue