lua-users home
lua-l archive

[PATCH] LuaInterface - Add traceback support to pcall

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hello,
With the attached patch, any errors thrown from within a pcall will
use _TRACEBACK to display the stack call instead of just the error
message. If _TRACEBACK is not defined (usually meaning that the debug
library wasn't opened) the behavior will be the same as before.
Eventually the same functionality should probably be added to DoString
and DoFile.
-- 
Zachary P. Landau <kapheine@gmail.com>
diff -urN luainterface-1.3.0.orig/src/LuaInterface/Lua.cs luainterface-1.3.0/src/LuaInterface/Lua.cs
--- luainterface-1.3.0.orig/src/LuaInterface/Lua.cs	2005年06月21日 14:25:42.000000000 -0400
+++ luainterface-1.3.0/src/LuaInterface/Lua.cs	2005年08月03日 18:08:23.000000000 -0400
@@ -302,6 +302,14 @@
 		 */
 		internal object[] callFunction(object function,object[] args) 
 		{
+			LuaDLL.lua_pushstring(luaState, "_TRACEBACK");
+			LuaDLL.lua_gettable(luaState, LuaIndexes.LUA_GLOBALSINDEX);
+			int traceback = LuaDLL.lua_gettop(luaState);
+			if (LuaDLL.lua_isnil(luaState, -1)) 
+			{
+				LuaDLL.lua_settop(luaState, -2);
+				traceback = 0;
+			}
 			int nArgs=0;
 			int oldTop=LuaDLL.lua_gettop(luaState);
 			if(args!=null && !LuaDLL.lua_checkstack(luaState,args.Length+6))
@@ -315,12 +323,15 @@
 					translator.push(luaState,args[i]);
 				}
 			}
-			int error=LuaDLL.lua_pcall(luaState,nArgs,-1,0);
+			int error=LuaDLL.lua_pcall(luaState,nArgs,-1,traceback);
 			if(error!=0) 
 			{
 				throw new Exception(LuaDLL.lua_tostring(luaState,-1));
 			}
-			return translator.popValues(luaState,oldTop);
+			object[] ret = translator.popValues(luaState,oldTop);
+			if (traceback != 0)
+				LuaDLL.lua_settop(luaState,-2);
+			return ret;
 		}
 		/*
 		 * Calls the object as a function with the provided arguments and
@@ -767,4 +778,4 @@
 		}
 	}
 
-}
\ No newline at end of file
+}

AltStyle によって変換されたページ (->オリジナル) /