Example #1
0
        /*
         * 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);
        }