lua_checkstack() public static method

public static lua_checkstack ( IntPtr luaState, int extra ) : bool
luaState System.IntPtr
extra int
return bool
示例#1
0
文件: Lua.cs 项目: weimingtom/pap2
        /*
         * Calls the object as a function with the provided arguments,
         * returning the function's returned values inside an array
         */
        internal object[] callFunction(object function, object[] args)
        {
            int nArgs  = 0;
            int oldTop = LuaDLL.lua_gettop(luaState);

            if (args != null && !LuaDLL.lua_checkstack(luaState, args.Length + 6))
            {
                throw new LuaException("Lua stack overflow");
            }
            translator.push(luaState, function);
            if (args != null)
            {
                nArgs = args.Length;
                for (int i = 0; i < args.Length; i++)
                {
                    translator.push(luaState, args[i]);
                }
            }
            int error = LuaDLL.lua_pcall(luaState, nArgs, -1, 0);

            if (error != 0)
            {
                ThrowExceptionFromError(oldTop);
            }

//             object[] results = translator.popValues(luaState, oldTop);
//             if (results != null && results.Length < 1)
//             {
//                 return null;
//             }
//             return results;
            return(translator.popValues(luaState, oldTop));
        }
示例#2
0
        /*
         * Calls the object as a function with the provided arguments and
         * casting returned values to the types in returnTypes before returning
         * them in an array
         */
        internal object[] callFunction(object function, object[] args, Type[] returnTypes)
        {
            int nArgs  = 0;
            int oldTop = LuaDLL.lua_gettop(luaState);

            if (!LuaDLL.lua_checkstack(luaState, args.Length + 6))
            {
                throw new LuaException("Lua stack overflow");
            }
            translator.push(luaState, function);
            if (args != null)
            {
                nArgs = args.Length;
                for (int i = 0; i < args.Length; i++)
                {
                    translator.push(luaState, args[i]);
                }
            }
            int error = LuaDLL.lua_pcall(luaState, nArgs, -1, 0);

            if (error != 0)
            {
                ThrowExceptionFromError(oldTop);
            }

            if (returnTypes != null)
            {
                return(translator.popValues(luaState, oldTop, returnTypes));
            }
            else
            {
                return(translator.popValues(luaState, oldTop));
            }
        }
示例#3
0
 public static void luaL_checkstack(IntPtr L, int space, string mes)
 {
     if (LuaDLL.lua_checkstack(L, space) == 0)
     {
         throw new LuaException(string.Format("stack overflow (%s)", mes), null, 1);
     }
 }
示例#4
0
        /*
         * Calls the function casting return values to the types
         * in returnTypes
         */
        internal object[] call(object[] args, Type[] returnTypes)
        {
            int nArgs = 0;

            LuaScriptMgr.PushTraceBack(L);
            int oldTop = LuaDLL.lua_gettop(L);

            if (!LuaDLL.lua_checkstack(L, args.Length + 6))
            {
                LuaDLL.lua_pop(L, 1);
                throw new LuaException("Lua stack overflow");
            }

            push(L);

            if (args != null)
            {
                nArgs = args.Length;

                for (int i = 0; i < args.Length; i++)
                {
                    PushArgs(L, args[i]);
                }
            }
            int error = 0;

            try
            {
                error = LuaDLL.lua_pcall(L, nArgs, -1, -nArgs - 2);
            }
            catch (Exception e)
            {
                //Debug.LogError(e.Message + "\n" + e.StackTrace);
                object[] trackBack = LuaScriptMgr.Instance.CallLuaFunction("debug.traceback");
                if (trackBack != null && trackBack.Length >= 1)
                {
                    //Debug.LogError(trackBack[0].ToString());
                }
            }
            if (error != 0)
            {
                string err = LuaDLL.lua_tostring(L, -1);
                LuaDLL.lua_settop(L, oldTop - 1);
                if (err == null)
                {
                    err = "Unknown Lua Error";
                }
                throw new LuaScriptException(err, "");
            }

            object[] ret = returnTypes != null?translator.popValues(L, oldTop, returnTypes) : translator.popValues(L, oldTop);

            LuaDLL.lua_settop(L, oldTop - 1);
            return(ret);
        }
 internal int returnValues(IntPtr luaState, object[] returnValues)
 {
     if (LuaDLL.lua_checkstack(luaState, returnValues.Length + 5))
     {
         for (int i = 0; i < returnValues.Length; i++)
         {
             this.push(luaState, returnValues[i]);
         }
         return(returnValues.Length);
     }
     return(0);
 }
示例#6
0
文件: LuaState.cs 项目: sakyaer/emoji
        public object[] CallGlobalFunction(string functionName, object[] args, int returnNum)
        {
            if (!LuaDLL.lua_checkstack(L, args.Length + 6))
            {
                LuaStatic.print("Lua stack overflow");
                return(null);
            }
            int oldTop = LuaDLL.lua_gettop(L);

            LuaDLL.lua_getglobal(L, functionName);

            object[] ret = CallLuaFunction(args, returnNum, false);
            LuaDLL.lua_settop(L, oldTop);
            return(ret);
        }
 /*
  * Pushes the entire array into the Lua stack and returns the number
  * of elements pushed.
  */
 internal int returnValues(KopiLua.Lua.lua_State luaState, object[] returnValues)
 {
     if (LuaDLL.lua_checkstack(luaState, returnValues.Length + 5))
     {
         for (int i = 0; i < returnValues.Length; i++)
         {
             push(luaState, returnValues[i]);
         }
         return(returnValues.Length);
     }
     else
     {
         return(0);
     }
 }
示例#8
0
        public static String GetTraceBackInfo(IntPtr L, string err = "Lua traceback:")
        {
            int oldTop = LuaDLL.lua_gettop(L);

            LuaDLL.lua_checkstack(L, 3);
            LuaDLL.lua_getglobal(L, "debug");
            LuaDLL.lua_getfield(L, -1, "traceback");
            LuaDLL.lua_pushstring(L, err);
            LuaDLL.lua_pushnumber(L, 1);
            LuaDLL.lua_call(L, 2, 1);
            string trace = LuaDLL.lua_tostring(L, -1);

            LuaDLL.lua_settop(L, oldTop);
            return(trace);
        }
示例#9
0
        /*
         * Calls the function casting return values to the types
         * in returnTypes
         */
        internal object[] call(object[] args, Type[] returnTypes)
        {
            int nArgs = 0;

            LuaScriptMgr.PushTraceBack(L);
            int oldTop = LuaDLL.lua_gettop(L);

            if (!LuaDLL.lua_checkstack(L, args.Length + 6))
            {
                LuaDLL.lua_pop(L, 1);
                throw new LuaException("Lua stack overflow");
            }

            push(L);

            if (args != null)
            {
                nArgs = args.Length;

                for (int i = 0; i < args.Length; i++)
                {
                    PushArgs(L, args[i]);
                }
            }

            int error = LuaDLL.lua_pcall(L, nArgs, -1, -nArgs - 2);

            if (error != 0)
            {
                string err = LuaDLL.lua_tostring(L, -1);
                LuaDLL.lua_settop(L, oldTop - 1);
                if (err == null)
                {
                    err = "Unknown Lua Error";
                }
                Debugger.LogError(err);
                LuaDLL.lua_settop(L, oldTop - 1);
                return(null);
            }

            object[] ret = returnTypes != null?translator.popValues(L, oldTop, returnTypes) : translator.popValues(L, oldTop);

            LuaDLL.lua_settop(L, oldTop - 1);
            return(ret);
        }
示例#10
0
文件: LuaState.cs 项目: sakyaer/emoji
        public object[] CallTableFunction(string tableName, string functionName, object[] args, int returnNum)
        {
            if (!LuaDLL.lua_checkstack(L, args.Length + 6))
            {
                LuaStatic.print("Lua stack overflow");
                return(null);
            }
            int oldTop = LuaDLL.lua_gettop(L);

            LuaDLL.lua_getglobal(L, tableName);
            if (LuaDLL.lua_type(L, -1) != LuaTypes.LUA_TTABLE)
            {
                LuaDLL.lua_pop(L, 1);
                LuaStatic.print("Not a Lua table");
                return(null);
            }
            LuaDLL.lua_getfield(L, -1, functionName);
            object[] ret = CallLuaFunction(args, returnNum, true);
            LuaDLL.lua_settop(L, oldTop);
            return(ret);
        }
示例#11
0
        internal object[] call(object[] args, Type[] returnTypes)
        {
            int num = 0;

            LuaScriptMgr.PushTraceBack(this.L);
            int num2 = LuaDLL.lua_gettop(this.L);

            if (!LuaDLL.lua_checkstack(this.L, args.Length + 6))
            {
                LuaDLL.lua_pop(this.L, 1);
                throw new LuaException("Lua stack overflow");
            }
            this.push(this.L);
            if (args != null)
            {
                num = args.Length;
                for (int i = 0; i < args.Length; i++)
                {
                    base.PushArgs(this.L, args[i]);
                }
            }
            int num3 = LuaDLL.lua_pcall(this.L, num, -1, -num - 2);

            if (num3 != 0)
            {
                string text = LuaDLL.lua_tostring(this.L, -1);
                LuaDLL.lua_settop(this.L, num2 - 1);
                if (text == null)
                {
                    text = "Unknown Lua Error";
                }
                throw new LuaScriptException(text, string.Empty);
            }
            object[] result = (returnTypes == null) ? this.translator.popValues(this.L, num2) : this.translator.popValues(this.L, num2, returnTypes);
            LuaDLL.lua_settop(this.L, num2 - 1);
            return(result);
        }
示例#12
0
        /*
         * Calls the method. Receives the arguments from the Lua stack
         * and returns values in it.
         */
        public int call(KopiLua.Lua.lua_State luaState)
        {
            MethodBase methodToCall  = _Method;
            object     targetObject  = _Target;
            bool       failedCall    = true;
            int        nReturnValues = 0;

            if (!LuaDLL.lua_checkstack(luaState, 5))
            {
                throw new LuaException("Lua stack overflow");
            }

            bool isStatic = (_BindingType & BindingFlags.Static) == BindingFlags.Static;

            SetPendingException(null);

            if (methodToCall == null) // Method from name
            {
                if (isStatic)
                {
                    targetObject = null;
                }
                else
                {
                    targetObject = _ExtractTarget(luaState, 1);
                }

                //LuaDLL.lua_remove(luaState,1); // Pops the receiver
                if (_LastCalledMethod.cachedMethod != null)       // Cached?
                {
                    int        numStackToSkip = isStatic ? 0 : 1; // If this is an instance invoe we will have an extra arg on the stack for the targetObject
                    int        numArgsPassed  = LuaDLL.lua_gettop(luaState) - numStackToSkip;
                    MethodBase method         = _LastCalledMethod.cachedMethod;

                    if (numArgsPassed == _LastCalledMethod.argTypes.Length)                     // No. of args match?
                    {
                        if (!LuaDLL.lua_checkstack(luaState, _LastCalledMethod.outList.Length + 6))
                        {
                            throw new LuaException("Lua stack overflow");
                        }

                        object[] args = _LastCalledMethod.args;

                        try
                        {
                            for (int i = 0; i < _LastCalledMethod.argTypes.Length; i++)
                            {
                                MethodArgs type          = _LastCalledMethod.argTypes[i];
                                object     luaParamValue = type.extractValue(luaState, i + 1 + numStackToSkip);
                                if (_LastCalledMethod.argTypes[i].isParamsArray)
                                {
                                    args[type.index] = _Translator.tableToArray(luaParamValue, type.paramsArrayType);
                                }
                                else
                                {
                                    args[type.index] = luaParamValue;
                                }

                                if (args[type.index] == null &&
                                    !LuaDLL.lua_isnil(luaState, i + 1 + numStackToSkip))
                                {
                                    throw new LuaException("argument number " + (i + 1) + " is invalid");
                                }
                            }
                            if ((_BindingType & BindingFlags.Static) == BindingFlags.Static)
                            {
                                _Translator.push(luaState, method.Invoke(null, args));
                            }
                            else
                            {
                                if (_LastCalledMethod.cachedMethod.IsConstructor)
                                {
                                    _Translator.push(luaState, ((ConstructorInfo)method).Invoke(args));
                                }
                                else
                                {
                                    _Translator.push(luaState, method.Invoke(targetObject, args));
                                }
                            }
                            failedCall = false;
                        }
                        catch (TargetInvocationException e)
                        {
                            // Failure of method invocation
                            return(SetPendingException(e.GetBaseException()));
                        }
                        catch (Exception e)
                        {
                            if (_Members.Length == 1) // Is the method overloaded?
                            // No, throw error
                            {
                                return(SetPendingException(e));
                            }
                        }
                    }
                }

                // Cache miss
                if (failedCall)
                {
                    // System.Diagnostics.Debug.WriteLine("cache miss on " + methodName);

                    // If we are running an instance variable, we can now pop the targetObject from the stack
                    if (!isStatic)
                    {
                        if (targetObject == null)
                        {
                            _Translator.throwError(luaState, String.Format("instance method '{0}' requires a non null target object", _MethodName));
                            LuaDLL.lua_pushnil(luaState);
                            return(1);
                        }

                        LuaDLL.lua_remove(luaState, 1); // Pops the receiver
                    }

                    bool   hasMatch      = false;
                    string candidateName = null;

                    foreach (MemberInfo member in _Members)
                    {
                        candidateName = member.ReflectedType.Name + "." + member.Name;

                        MethodBase m = (MethodInfo)member;

                        bool isMethod = _Translator.matchParameters(luaState, m, ref _LastCalledMethod);
                        if (isMethod)
                        {
                            hasMatch = true;
                            break;
                        }
                    }
                    if (!hasMatch)
                    {
                        string msg = (candidateName == null)
                            ? "invalid arguments to method call"
                            : ("invalid arguments to method: " + candidateName);

                        _Translator.throwError(luaState, msg);
                        LuaDLL.lua_pushnil(luaState);
                        return(1);
                    }
                }
            }
            else // Method from MethodBase instance
            {
                if (methodToCall.ContainsGenericParameters)
                {
                    // bool isMethod = //* not used
                    _Translator.matchParameters(luaState, methodToCall, ref _LastCalledMethod);

                    if (methodToCall.IsGenericMethodDefinition)
                    {
                        //need to make a concrete type of the generic method definition
                        List <Type> typeArgs = new List <Type>();

                        foreach (object arg in _LastCalledMethod.args)
                        {
                            typeArgs.Add(arg.GetType());
                        }

                        MethodInfo concreteMethod = (methodToCall as MethodInfo).MakeGenericMethod(typeArgs.ToArray());

                        _Translator.push(luaState, concreteMethod.Invoke(targetObject, _LastCalledMethod.args));
                        failedCall = false;
                    }
                    else if (methodToCall.ContainsGenericParameters)
                    {
                        _Translator.throwError(luaState, "unable to invoke method on generic class as the current method is an open generic method");
                        LuaDLL.lua_pushnil(luaState);
                        return(1);
                    }
                }
                else
                {
                    if (!methodToCall.IsStatic && !methodToCall.IsConstructor && targetObject == null)
                    {
                        targetObject = _ExtractTarget(luaState, 1);
                        LuaDLL.lua_remove(luaState, 1); // Pops the receiver
                    }

                    if (!_Translator.matchParameters(luaState, methodToCall, ref _LastCalledMethod))
                    {
                        _Translator.throwError(luaState, "invalid arguments to method call");
                        LuaDLL.lua_pushnil(luaState);
                        return(1);
                    }
                }
            }

            if (failedCall)
            {
                if (!LuaDLL.lua_checkstack(luaState, _LastCalledMethod.outList.Length + 6))
                {
                    throw new LuaException("Lua stack overflow");
                }
                try
                {
                    if (isStatic)
                    {
                        _Translator.push(luaState, _LastCalledMethod.cachedMethod.Invoke(null, _LastCalledMethod.args));
                    }
                    else
                    {
                        if (_LastCalledMethod.cachedMethod.IsConstructor)
                        {
                            _Translator.push(luaState, ((ConstructorInfo)_LastCalledMethod.cachedMethod).Invoke(_LastCalledMethod.args));
                        }
                        else
                        {
                            _Translator.push(luaState, _LastCalledMethod.cachedMethod.Invoke(targetObject, _LastCalledMethod.args));
                        }
                    }
                }
                catch (TargetInvocationException e)
                {
                    return(SetPendingException(e.GetBaseException()));
                }
                catch (Exception e)
                {
                    return(SetPendingException(e));
                }
            }

            // Pushes out and ref return values
            for (int index = 0; index < _LastCalledMethod.outList.Length; index++)
            {
                nReturnValues++;
                _Translator.push(luaState, _LastCalledMethod.args[_LastCalledMethod.outList[index]]);
            }

            //by isSingle 2010-09-10 11:26:31
            //Desc:
            //  if not return void,we need add 1,
            //  or we will lost the function's return value
            //  when call dotnet function like "int foo(arg1,out arg2,out arg3)" in lua code
            if (!_LastCalledMethod.IsReturnVoid && nReturnValues > 0)
            {
                nReturnValues++;
            }

            return(nReturnValues < 1 ? 1 : nReturnValues);
        }
示例#13
0
 public bool LuaCheckStack(int args)
 {
     return(LuaDLL.lua_checkstack(L, args) != 0);
 }
示例#14
0
        /*
         * Calls the object as a function with the provided arguments and
         * casting returned values to the types in returnTypes before returning
         * them in an array
         */
        internal object[] callFunction(object function, object[] args, Type[] returnTypes)
        {
            //int nArgs=0;
            //int oldTop=LuaDLL.lua_gettop(L);
            //if(!LuaDLL.lua_checkstack(L,args.Length+6))
            //    throw new LuaException("Lua stack overflow");
            //translator.push(L,function);
            //if(args!=null)
            //{
            //    nArgs=args.Length;
            //    for(int i=0;i<args.Length;i++)
            //    {
            //        translator.push(L,args[i]);
            //    }
            //}
            //int error = LuaDLL.lua_pcall(L, nArgs, -1, 0);
            //if (error != 0)
            //    ThrowExceptionFromError(oldTop);

            //if(returnTypes != null)
            //    return translator.popValues(L,oldTop,returnTypes);
            //else
            //    return translator.popValues(L, oldTop);

            int nArgs = 0;

            LuaDLL.lua_getglobal(L, "traceback");
            int oldTop = LuaDLL.lua_gettop(L);

            if (!LuaDLL.lua_checkstack(L, args.Length + 6))
            {
                LuaDLL.lua_pop(L, 1);
                throw new LuaException("Lua stack overflow");
            }

            translator.push(L, function);

            if (args != null)
            {
                nArgs = args.Length;
                for (int i = 0; i < args.Length; i++)
                {
                    translator.push(L, args[i]);
                }
            }

            int error = LuaDLL.lua_pcall(L, nArgs, -1, -nArgs - 2);

            if (error != 0)
            {
                string err = LuaDLL.lua_tostring(L, -1);
                LuaDLL.lua_settop(L, oldTop);
                //LuaTypes luatype = LuaDLL.lua_type(L, -1);
                LuaDLL.lua_pop(L, 1);
                if (err == null)
                {
                    err = "Unknown Lua Error";
                }
                throw new LuaScriptException(err.ToString(), "");
            }

            object[] ret = returnTypes != null?translator.popValues(L, oldTop, returnTypes) : translator.popValues(L, oldTop);

            LuaDLL.lua_pop(L, 1);
            return(ret);
        }
示例#15
0
        /*
         * Calls the method. Receives the arguments from the Lua stack
         * and returns values in it.
         */
        public int call(IntPtr luaState)
        {
            MethodBase methodToCall  = method;
            object     targetObject  = target;
            bool       failedCall    = true;
            int        nReturnValues = 0;

            if (!LuaDLL.lua_checkstack(luaState, 5))
            {
                throw new LuaException("Lua stack overflow");
            }

            bool isStatic = (bindingType & BindingFlags.Static) == BindingFlags.Static;

            SetPendingException(null);

            if (methodToCall == null)          // Method from name
            {
                if (isStatic)
                {
                    targetObject = null;
                }
                else
                {
                    targetObject = extractTarget(luaState, 1);
                }
                //LuaDLL.lua_remove(luaState,1); // Pops the receiver
                if (lastCalledMethod.cachedMethod != null) // Cached?
                {
                    int numStackToSkip = isStatic ? 0 : 1; // If this is an instance invoe we will have an extra arg on the stack for the targetObject
                    int numArgsPassed  = LuaDLL.lua_gettop(luaState) - numStackToSkip;

                    if (numArgsPassed == lastCalledMethod.argTypes.Length)                    // No. of args match?
                    {
                        if (!LuaDLL.lua_checkstack(luaState, lastCalledMethod.outList.Length + 6))
                        {
                            throw new LuaException("Lua stack overflow");
                        }
                        try
                        {
                            for (int i = 0; i < lastCalledMethod.argTypes.Length; i++)
                            {
                                lastCalledMethod.args[lastCalledMethod.argTypes[i].index] =
                                    lastCalledMethod.argTypes[i].extractValue(luaState, i + 1 + numStackToSkip);

                                if (lastCalledMethod.args[lastCalledMethod.argTypes[i].index] == null &&
                                    !LuaDLL.lua_isnil(luaState, i + 1 + numStackToSkip))
                                {
                                    throw new LuaException("argument number " + (i + 1) + " is invalid");
                                }
                            }
                            if ((bindingType & BindingFlags.Static) == BindingFlags.Static)
                            {
                                translator.push(luaState, lastCalledMethod.cachedMethod.Invoke(null, lastCalledMethod.args));
                            }
                            else
                            {
                                if (lastCalledMethod.cachedMethod.IsConstructor)
                                {
                                    translator.push(luaState, ((ConstructorInfo)lastCalledMethod.cachedMethod).Invoke(lastCalledMethod.args));
                                }
                                else
                                {
                                    translator.push(luaState, lastCalledMethod.cachedMethod.Invoke(targetObject, lastCalledMethod.args));
                                }
                            }
                            failedCall = false;
                        }
                        catch (TargetInvocationException e)
                        {
                            // Failure of method invocation
                            return(SetPendingException(e.GetBaseException()));
                        }
                        catch (Exception e)
                        {
                            if (members.Length == 1)                          // Is the method overloaded?
                            // No, throw error
                            {
                                return(SetPendingException(e));
                            }
                        }
                    }
                }

                // Cache miss
                if (failedCall)
                {
                    // System.Diagnostics.Debug.WriteLine("cache miss on " + methodName);

                    // If we are running an instance variable, we can now pop the targetObject from the stack
                    if (!isStatic)
                    {
                        if (targetObject == null)
                        {
                            translator.throwError(luaState, String.Format("instance method '{0}' requires a non null target object", methodName));
                            LuaDLL.lua_pushnil(luaState);
                            return(1);
                        }

                        LuaDLL.lua_remove(luaState, 1); // Pops the receiver
                    }

                    bool   hasMatch      = false;
                    string candidateName = null;

                    foreach (MemberInfo member in members)
                    {
                        candidateName = member.ReflectedType.Name + "." + member.Name;

                        MethodBase m = (MethodInfo)member;

                        bool isMethod = translator.matchParameters(luaState, m, ref lastCalledMethod);
                        if (isMethod)
                        {
                            hasMatch = true;
                            break;
                        }
                    }
                    if (!hasMatch)
                    {
                        string msg = (candidateName == null)
                            ? "invalid arguments to method call"
                            : ("invalid arguments to method: " + candidateName);

                        translator.throwError(luaState, msg);
                        LuaDLL.lua_pushnil(luaState);
                        return(1);
                    }
                }
            }
            else             // Method from MethodBase instance
            {
                if (!methodToCall.IsStatic && !methodToCall.IsConstructor && targetObject == null)
                {
                    targetObject = extractTarget(luaState, 1);
                    LuaDLL.lua_remove(luaState, 1);                    // Pops the receiver
                }
                if (!translator.matchParameters(luaState, methodToCall, ref lastCalledMethod))
                {
                    translator.throwError(luaState, "invalid arguments to method call");
                    LuaDLL.lua_pushnil(luaState);
                    return(1);
                }
            }

            if (failedCall)
            {
                if (!LuaDLL.lua_checkstack(luaState, lastCalledMethod.outList.Length + 6))
                {
                    throw new LuaException("Lua stack overflow");
                }
                try
                {
                    if (isStatic)
                    {
                        translator.push(luaState, lastCalledMethod.cachedMethod.Invoke(null, lastCalledMethod.args));
                    }
                    else
                    {
                        if (lastCalledMethod.cachedMethod.IsConstructor)
                        {
                            translator.push(luaState, ((ConstructorInfo)lastCalledMethod.cachedMethod).Invoke(lastCalledMethod.args));
                        }
                        else
                        {
                            translator.push(luaState, lastCalledMethod.cachedMethod.Invoke(targetObject, lastCalledMethod.args));
                        }
                    }
                }
                catch (TargetInvocationException e)
                {
                    return(SetPendingException(e.GetBaseException()));
                }
                catch (Exception e)
                {
                    return(SetPendingException(e));
                }
            }

            // Pushes out and ref return values
            for (int index = 0; index < lastCalledMethod.outList.Length; index++)
            {
                nReturnValues++;
                //for(int i=0;i<lastCalledMethod.outList.Length;i++)
                translator.push(luaState, lastCalledMethod.args[lastCalledMethod.outList[index]]);
            }
            return(nReturnValues < 1 ? 1 : nReturnValues);
        }
        public int call(IntPtr luaState)
        {
            MethodBase method = this._Method;
            object     obj    = this._Target;
            bool       flag   = true;
            int        num    = 0;

            if (!LuaDLL.lua_checkstack(luaState, 5))
            {
                throw new LuaException("Lua stack overflow");
            }
            bool flag2 = (this._BindingType & BindingFlags.Static) == BindingFlags.Static;

            this.SetPendingException(null);
            if (method == null)
            {
                if (flag2)
                {
                    obj = null;
                }
                else
                {
                    obj = this._ExtractTarget(luaState, 1);
                }
                if (this._LastCalledMethod.cachedMethod != null)
                {
                    int        num2         = (!flag2) ? 1 : 0;
                    int        num3         = LuaDLL.lua_gettop(luaState) - num2;
                    MethodBase cachedMethod = this._LastCalledMethod.cachedMethod;
                    if (num3 == this._LastCalledMethod.argTypes.Length)
                    {
                        if (!LuaDLL.lua_checkstack(luaState, this._LastCalledMethod.outList.Length + 6))
                        {
                            throw new LuaException("Lua stack overflow");
                        }
                        object[] args = this._LastCalledMethod.args;
                        try
                        {
                            for (int i = 0; i < this._LastCalledMethod.argTypes.Length; i++)
                            {
                                MethodArgs methodArgs = this._LastCalledMethod.argTypes[i];
                                object     obj2       = methodArgs.extractValue(luaState, i + 1 + num2);
                                if (this._LastCalledMethod.argTypes[i].isParamsArray)
                                {
                                    args[methodArgs.index] = this._Translator.tableToArray(obj2, methodArgs.paramsArrayType);
                                }
                                else
                                {
                                    args[methodArgs.index] = obj2;
                                }
                                if (args[methodArgs.index] == null && !LuaDLL.lua_isnil(luaState, i + 1 + num2))
                                {
                                    throw new LuaException("argument number " + (i + 1) + " is invalid");
                                }
                            }
                            if ((this._BindingType & BindingFlags.Static) == BindingFlags.Static)
                            {
                                this._Translator.push(luaState, cachedMethod.Invoke(null, args));
                            }
                            else if (this._LastCalledMethod.cachedMethod.IsConstructor)
                            {
                                this._Translator.push(luaState, ((ConstructorInfo)cachedMethod).Invoke(args));
                            }
                            else
                            {
                                this._Translator.push(luaState, cachedMethod.Invoke(obj, args));
                            }
                            flag = false;
                        }
                        catch (TargetInvocationException ex)
                        {
                            int result = this.SetPendingException(ex.GetBaseException());
                            return(result);
                        }
                        catch (Exception pendingException)
                        {
                            if (this._Members.Length == 1)
                            {
                                int result = this.SetPendingException(pendingException);
                                return(result);
                            }
                        }
                    }
                }
                if (flag)
                {
                    if (!flag2)
                    {
                        if (obj == null)
                        {
                            this._Translator.throwError(luaState, string.Format("instance method '{0}' requires a non null target object", this._MethodName));
                            LuaDLL.lua_pushnil(luaState);
                            return(1);
                        }
                        LuaDLL.lua_remove(luaState, 1);
                    }
                    bool         flag3   = false;
                    string       text    = null;
                    MemberInfo[] members = this._Members;
                    for (int j = 0; j < members.Length; j++)
                    {
                        MemberInfo memberInfo = members[j];
                        text = memberInfo.ReflectedType.Name + "." + memberInfo.Name;
                        MethodBase method2 = (MethodInfo)memberInfo;
                        bool       flag4   = this._Translator.matchParameters(luaState, method2, ref this._LastCalledMethod);
                        if (flag4)
                        {
                            flag3 = true;
                            break;
                        }
                    }
                    if (!flag3)
                    {
                        string message = (text != null) ? ("invalid arguments to method: " + text) : "invalid arguments to method call";
                        LuaDLL.luaL_error(luaState, message);
                        LuaDLL.lua_pushnil(luaState);
                        this.ClearCachedArgs();
                        return(1);
                    }
                }
            }
            else if (method.ContainsGenericParameters)
            {
                this._Translator.matchParameters(luaState, method, ref this._LastCalledMethod);
                if (method.IsGenericMethodDefinition)
                {
                    List <Type> list  = new List <Type>();
                    object[]    args2 = this._LastCalledMethod.args;
                    for (int k = 0; k < args2.Length; k++)
                    {
                        object obj3 = args2[k];
                        list.Add(obj3.GetType());
                    }
                    MethodInfo methodInfo = (method as MethodInfo).MakeGenericMethod(list.ToArray());
                    this._Translator.push(luaState, methodInfo.Invoke(obj, this._LastCalledMethod.args));
                    flag = false;
                }
                else if (method.ContainsGenericParameters)
                {
                    LuaDLL.luaL_error(luaState, "unable to invoke method on generic class as the current method is an open generic method");
                    LuaDLL.lua_pushnil(luaState);
                    this.ClearCachedArgs();
                    return(1);
                }
            }
            else
            {
                if (!method.IsStatic && !method.IsConstructor && obj == null)
                {
                    obj = this._ExtractTarget(luaState, 1);
                    LuaDLL.lua_remove(luaState, 1);
                }
                if (!this._Translator.matchParameters(luaState, method, ref this._LastCalledMethod))
                {
                    LuaDLL.luaL_error(luaState, "invalid arguments to method call");
                    LuaDLL.lua_pushnil(luaState);
                    this.ClearCachedArgs();
                    return(1);
                }
            }
            if (flag)
            {
                if (!LuaDLL.lua_checkstack(luaState, this._LastCalledMethod.outList.Length + 6))
                {
                    this.ClearCachedArgs();
                    throw new LuaException("Lua stack overflow");
                }
                try
                {
                    if (flag2)
                    {
                        this._Translator.push(luaState, this._LastCalledMethod.cachedMethod.Invoke(null, this._LastCalledMethod.args));
                    }
                    else if (this._LastCalledMethod.cachedMethod.IsConstructor)
                    {
                        this._Translator.push(luaState, ((ConstructorInfo)this._LastCalledMethod.cachedMethod).Invoke(this._LastCalledMethod.args));
                    }
                    else
                    {
                        this._Translator.push(luaState, this._LastCalledMethod.cachedMethod.Invoke(obj, this._LastCalledMethod.args));
                    }
                }
                catch (TargetInvocationException ex2)
                {
                    this.ClearCachedArgs();
                    int result = this.SetPendingException(ex2.GetBaseException());
                    return(result);
                }
                catch (Exception pendingException2)
                {
                    this.ClearCachedArgs();
                    int result = this.SetPendingException(pendingException2);
                    return(result);
                }
            }
            for (int l = 0; l < this._LastCalledMethod.outList.Length; l++)
            {
                num++;
                this._Translator.push(luaState, this._LastCalledMethod.args[this._LastCalledMethod.outList[l]]);
            }
            if (!this._LastCalledMethod.IsReturnVoid && num > 0)
            {
                num++;
            }
            this.ClearCachedArgs();
            return((num >= 1) ? num : 1);
        }