Ejemplo n.º 1
0
        /*
         * Passes errors (argument e) to the Lua interpreter
         */
        internal void ThrowError(LuaState luaState, object e)
        {
            // We use this to remove anything pushed by luaL_where
            int oldTop = luaState.GetTop();

            // 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
            luaState.Where(1);
            var curlev = PopValues(luaState, oldTop);

            // Determine the position in the script where the exception was triggered
            string errLocation = string.Empty;

            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
                if (interpreter.UseTraceback)
                {
                    message += Environment.NewLine + interpreter.GetDebugTraceback();
                }
                e = new LuaScriptException(message, errLocation);
            }
            else
            {
                var ex = e as Exception;

                if (ex != null)
                {
                    // Wrap generic .NET exception as an InnerException and store the error location
                    if (interpreter.UseTraceback)
                    {
                        ex.Data["Traceback"] = interpreter.GetDebugTraceback();
                    }
                    e = new LuaScriptException(ex, errLocation);
                }
            }

            Push(luaState, e);
            luaState.Error();
        }