Debugging Lua

BlitzMax Forums/BlitzMax Programming/Debugging Lua

JoshK(Posted 2010) [#1]
I am trying to add some debugging functionality to my Lua implementation. After a crash occurs, I need to call some code to make Lua print out the line number and file name the error occurred during the execution of. This is what I am trying:
		Local whatever:String
		
		Try
			result=lua.dofile(path)'crash occurs somewhere in a command lua calls
		Catch whatever:String
			lua_getglobal(luastate.L,"debug")
			If lua_istable(luastate.L,-1)
				lua_getfield(luastate.L,-1,"Traceback")
				If lua_isfunction(luastate.L,-1)
					lua_pcall(luastate.L,0,0,0)
				EndIf
			EndIf
		EndTry

Does BlitzMax have the ability to catch an error this way and run code after a crash occurs? I can't add Throw() to every single function in the engine for every single error that might occur.


JoshK(Posted 2010) [#2]
Well, it works. Debugging Lua is actually incredibly simple!

Local dummy:Object

Try
	luaL_dofile(L,path)
Catch
	lua_getglobal(L,"debug")
	If lua_istable(L,-1)
		lua_getfield(L,-1,"traceback")
		If lua_isfunction(L,-1)
			If lua_pcall(L,0,1,0)=0
				If lua_isstring(L,-1)
					Print(lua_tostring(L,-1))
				EndIf
			EndIf
		EndIf
	EndIf
	End
EndMethod


This will print out all the information you need to determine what file and line a crash occured on.