@@ -251,6 +251,42 @@ namespace RC::LuaType
251251 return " UnknownOperation" ;
252252 }
253253
254+ // https://stackoverflow.com/questions/59091462/from-c-how-can-i-print-the-contents-of-the-lua-stack/59097940#59097940
255+ auto get_stack_dump (const char * message = " " ) const -> std::string
256+ {
257+ auto lua_state = lua.get_lua_state ();
258+ auto out_message = fmt::format (" \n\n LUA Stack dump -> START------------------------------\n {}\n " , message);
259+ int top = lua_gettop (lua_state);
260+ for (int i = 1 ; i <= top; i++)
261+ {
262+ out_message.append (fmt::format (" {}\t {}\t " , i, luaL_typename (lua_state, i)));
263+ switch (lua_type (lua_state, i))
264+ {
265+ case LUA_TNUMBER:
266+ out_message.append (fmt::format (" {}" , lua_tonumber (lua_state, i)));
267+ break ;
268+ case LUA_TSTRING:
269+ out_message.append (fmt::format (" {}" , lua_tostring (lua_state, i)));
270+ break ;
271+ case LUA_TBOOLEAN:
272+ out_message.append (fmt::format (" {}" , (lua_toboolean (lua_state, i) ? " true" : " false" )));
273+ break ;
274+ case LUA_TNIL:
275+ out_message.append (" nil" );
276+ break ;
277+ case LUA_TFUNCTION:
278+ out_message.append (" function" );
279+ break ;
280+ default :
281+ out_message.append (fmt::format (" {}" , lua_topointer (lua_state, i)));
282+ break ;
283+ }
284+ out_message.append (" \n " );
285+ }
286+ out_message.append (" \n LUA Stack dump -> END----------------------------\n\n " );
287+ return out_message;
288+ }
289+
254290 auto throw_error_internal_append_args (std::string&) const -> void
255291 {
256292 }
@@ -288,6 +324,7 @@ namespace RC::LuaType
288324 error_message.append (fmt::format (" StoredAtIndex: {}\n " , stored_at_index));
289325 error_message.append (fmt::format (" CreateNewIfGetNonTrivialLocal: {}\n " , create_new_if_get_non_trivial_local));
290326 throw_error_internal_append_args (error_message, args...);
327+ error_message.append (get_stack_dump ());
291328 lua.throw_error (error_message);
292329 }
293330 };
0 commit comments