/// <summary> /// Assuming we have a Lua error string sitting on the stack, throw a C# exception out to the user's app /// </summary> /// <exception cref="LuaScriptException">Thrown if the script caused an exception</exception> void ThrowExceptionFromError(int oldTop) { object err = translator.getObject(luaState, -1); LuaDLL.lua_settop(luaState, oldTop); // A pre-wrapped exception - just rethrow it (stack trace of InnerException will be preserved) LuaScriptException luaEx = err as LuaScriptException; if (luaEx != null) { throw luaEx; } // A non-wrapped Lua error (best interpreted as a string) - wrap it and throw it if (err == null) { err = "Unknown Lua Error"; } throw new LuaScriptException(err.ToString(), ""); }
/* * Passes errors (argument e) to the Lua interpreter */ internal void throwError(ILuaState luaState, object e) { // We use this to remove anything pushed by luaL_where int oldTop = LuaDLL.lua_gettop(luaState); // Stack frame #1 is our C# wrapper, so not very interesting to the user // Stack frame #2 must be the lua code that called us, so that's what we want to use LuaDLL.luaL_where(luaState, 1); object[] curlev = popValues(luaState, oldTop); // Determine the position in the script where the exception was triggered string errLocation = ""; if (curlev.Length > 0) { errLocation = curlev[0].ToString(); } string message = e as string; if (message != null) { // Wrap Lua error (just a string) and store the error location e = new LuaScriptException(message, errLocation); } else { Exception ex = e as Exception; if (ex != null) { // Wrap generic .NET exception as an InnerException and store the error location e = new LuaScriptException(ex, errLocation); } } push(luaState, e); LuaDLL.lua_error(luaState); }