@@ -764,7 +764,7 @@ int lua_metaipair(lua_State* L)
764
764
765
765
} // anonymous namespace
766
766
767
- bool LuaState::push_debug_traceback ()
767
+ int LuaState::push_debug_traceback ()
768
768
{
769
769
// Push debug.traceback() onto the stack as lua_pcall()'s error
770
770
// handler function. On error, lua_pcall() calls the specified error
@@ -778,18 +778,18 @@ bool LuaState::push_debug_traceback()
778
778
{
779
779
lua_pop (mState , 1 );
780
780
LL_WARNS (" Lua" ) << " 'debug' table not found" << LL_ENDL;
781
- return false ;
781
+ return 0 ;
782
782
}
783
783
lua_getfield (mState , -1 , " traceback" );
784
784
if (!lua_isfunction (mState , -1 ))
785
785
{
786
786
lua_pop (mState , 2 );
787
787
LL_WARNS (" Lua" ) << " 'traceback' func not found" << LL_ENDL;
788
- return false ;
788
+ return 0 ;
789
789
}
790
790
// ditch "debug"
791
791
lua_remove (mState , -2 );
792
- return true ;
792
+ return lua_gettop ( mState ) ;
793
793
}
794
794
795
795
LuaState::~LuaState ()
@@ -812,6 +812,8 @@ LuaState::~LuaState()
812
812
// stack contains Registry.atexit
813
813
if (lua_istable (mState , -1 ))
814
814
{
815
+ int atexit = lua_gettop (mState );
816
+
815
817
// We happen to know that Registry.atexit is built by appending array
816
818
// entries using table.insert(). That's important because it means
817
819
// there are no holes, and therefore lua_objlen() should be correct.
@@ -822,21 +824,21 @@ LuaState::~LuaState()
822
824
<< len << " entries" << LL_ENDL;
823
825
824
826
// TODO: 'debug' global shouldn't be overwritten and should be accessible at this stage
825
- bool debug_traceback = push_debug_traceback ();
827
+ S32 debug_traceback_idx = push_debug_traceback ();
826
828
// if debug_traceback is true, stack now contains atexit, /debug.traceback()/
827
829
// otherwise just atexit
828
830
for (int i (len); i >= 1 ; --i)
829
831
{
830
832
lua_pushinteger (mState , i);
831
833
// stack contains Registry.atexit, /debug.traceback()/, i
832
- lua_gettable (mState , debug_traceback ? - 3 : - 2 );
834
+ lua_gettable (mState , atexit );
833
835
// stack contains Registry.atexit, /debug.traceback()/, atexit[i]
834
836
// Call atexit[i](), no args, no return values.
835
837
// Use lua_pcall() because errors in any one atexit() function
836
838
// shouldn't cancel the rest of them. Pass debug.traceback() as
837
839
// the error handler function.
838
840
LL_DEBUGS (" Lua" ) << LLCoros::getName () << " : calling atexit(" << i << " )" << LL_ENDL;
839
- if (lua_pcall (mState , 0 , 0 , debug_traceback ? - 2 : 0 ) != LUA_OK)
841
+ if (lua_pcall (mState , 0 , 0 , debug_traceback_idx ) != LUA_OK)
840
842
{
841
843
auto error{ lua_tostdstring (mState , -1 ) };
842
844
LL_WARNS (" Lua" ) << LLCoros::getName () << " : atexit(" << i << " ) error: " << error << LL_ENDL;
@@ -848,8 +850,8 @@ LuaState::~LuaState()
848
850
// stack contains atexit, debug.traceback()
849
851
}
850
852
// pop debug.traceback()
851
- if (debug_traceback )
852
- lua_pop (mState , 1 );
853
+ if (debug_traceback_idx )
854
+ lua_remove (mState , debug_traceback_idx );
853
855
}
854
856
// pop Registry.atexit (either table or nil)
855
857
lua_pop (mState , 1 );
0 commit comments