@@ -822,34 +822,34 @@ LuaState::~LuaState()
822
822
<< len << " entries" << LL_ENDL;
823
823
824
824
// TODO: 'debug' global shouldn't be overwritten and should be accessible at this stage
825
- if (push_debug_traceback ())
825
+ bool debug_traceback = push_debug_traceback ();
826
+ // if debug_traceback is true, stack now contains atexit, /debug.traceback()/
827
+ // otherwise just atexit
828
+ for (int i (len); i >= 1 ; --i)
826
829
{
827
- // stack now contains atexit, debug.traceback()
828
- for (int i (len); i >= 1 ; --i)
830
+ lua_pushinteger (mState , i);
831
+ // stack contains Registry.atexit, /debug.traceback()/, i
832
+ lua_gettable (mState , debug_traceback ? -3 : -2 );
833
+ // stack contains Registry.atexit, /debug.traceback()/, atexit[i]
834
+ // Call atexit[i](), no args, no return values.
835
+ // Use lua_pcall() because errors in any one atexit() function
836
+ // shouldn't cancel the rest of them. Pass debug.traceback() as
837
+ // the error handler function.
838
+ LL_DEBUGS (" Lua" ) << LLCoros::getName () << " : calling atexit(" << i << " )" << LL_ENDL;
839
+ if (lua_pcall (mState , 0 , 0 , debug_traceback ? -2 : 0 ) != LUA_OK)
829
840
{
830
- lua_pushinteger (mState , i);
831
- // stack contains Registry.atexit, debug.traceback(), i
832
- lua_gettable (mState , -3 );
833
- // stack contains Registry.atexit, debug.traceback(), atexit[i]
834
- // Call atexit[i](), no args, no return values.
835
- // Use lua_pcall() because errors in any one atexit() function
836
- // shouldn't cancel the rest of them. Pass debug.traceback() as
837
- // the error handler function.
838
- LL_DEBUGS (" Lua" ) << LLCoros::getName () << " : calling atexit(" << i << " )" << LL_ENDL;
839
- if (lua_pcall (mState , 0 , 0 , -2 ) != LUA_OK)
840
- {
841
- auto error{ lua_tostdstring (mState , -1 ) };
842
- LL_WARNS (" Lua" ) << LLCoros::getName () << " : atexit(" << i << " ) error: " << error << LL_ENDL;
843
- // pop error message
844
- lua_pop (mState , 1 );
845
- }
846
- LL_DEBUGS (" Lua" ) << LLCoros::getName () << " : atexit(" << i << " ) done" << LL_ENDL;
847
- // lua_pcall() has already popped atexit[i]:
848
- // stack contains atexit, debug.traceback()
841
+ auto error{ lua_tostdstring (mState , -1 ) };
842
+ LL_WARNS (" Lua" ) << LLCoros::getName () << " : atexit(" << i << " ) error: " << error << LL_ENDL;
843
+ // pop error message
844
+ lua_pop (mState , 1 );
849
845
}
850
- // pop debug.traceback()
851
- lua_pop (mState , 1 );
846
+ LL_DEBUGS (" Lua" ) << LLCoros::getName () << " : atexit(" << i << " ) done" << LL_ENDL;
847
+ // lua_pcall() has already popped atexit[i]:
848
+ // stack contains atexit, debug.traceback()
852
849
}
850
+ // pop debug.traceback()
851
+ if (debug_traceback)
852
+ lua_pop (mState , 1 );
853
853
}
854
854
// pop Registry.atexit (either table or nil)
855
855
lua_pop (mState , 1 );
0 commit comments