/*
         * 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);
        }
Exemple #2
0
        /*
         * Matches a method against its arguments in the Lua stack. Returns
         * if the match was succesful. It it was also returns the information
         * necessary to invoke the method.
         */
        internal bool matchParameters(KopiLua.Lua.lua_State luaState, MethodBase method, ref MethodCache methodCache)
        {
            ExtractValue extractValue;
            bool         isMethod = true;

            ParameterInfo[]   paramInfo       = method.GetParameters();
            int               currentLuaParam = 1;
            int               nLuaParams      = LuaDLL.lua_gettop(luaState);
            ArrayList         paramList       = new ArrayList();
            List <int>        outList         = new List <int>();
            List <MethodArgs> argTypes        = new List <MethodArgs>();

            foreach (ParameterInfo currentNetParam in paramInfo)
            {
                if (!currentNetParam.IsIn && currentNetParam.IsOut)  // Skips out params
                {
                    outList.Add(paramList.Add(null));
                }
                else if (currentLuaParam > nLuaParams) // Adds optional parameters
                {
                    if (currentNetParam.IsOptional)
                    {
                        paramList.Add(currentNetParam.DefaultValue);
                    }
                    else
                    {
                        isMethod = false;
                        break;
                    }
                }
                else if ((extractValue = translator.typeChecker.checkType(luaState, currentLuaParam, currentNetParam.ParameterType)) != null)  // Type checking
                {
                    int        index     = paramList.Add(extractValue(luaState, currentLuaParam));
                    MethodArgs methodArg = new MethodArgs();
                    methodArg.index        = index;
                    methodArg.extractValue = extractValue;
                    argTypes.Add(methodArg);
                    if (currentNetParam.ParameterType.IsByRef)
                    {
                        outList.Add(index);
                    }
                    currentLuaParam++;
                }  // Type does not match, ignore if the parameter is optional
                else if (currentNetParam.IsOptional)
                {
                    paramList.Add(currentNetParam.DefaultValue);
                }
                else  // No match
                {
                    isMethod = false;
                    break;
                }
            }
            if (currentLuaParam != nLuaParams + 1) // Number of parameters does not match
            {
                isMethod = false;
            }
            if (isMethod)
            {
                methodCache.args         = paramList.ToArray();
                methodCache.cachedMethod = method;
                methodCache.outList      = outList.ToArray();
                methodCache.argTypes     = argTypes.ToArray();
            }
            return(isMethod);
        }
Exemple #3
0
 /*
  * Matches a method against its arguments in the Lua stack. Returns
  * if the match was succesful. It it was also returns the information
  * necessary to invoke the method.
  */
 internal bool matchParameters(IntPtr luaState, MethodBase method, ref MethodCache methodCache)
 {
     ExtractValue extractValue;
     bool isMethod = true;
     ParameterInfo[] paramInfo = method.GetParameters();
     int currentLuaParam = 1;
     int nLuaParams = LuaDLL.lua_gettop(luaState);
     ArrayList paramList = new ArrayList();
     ArrayList outList = new ArrayList();
     ArrayList argTypes = new ArrayList();
     foreach (ParameterInfo currentNetParam in paramInfo)
     {
         if (!currentNetParam.IsIn && currentNetParam.IsOut)  // Skips out params
         {
             outList.Add(paramList.Add(null));
         }
         else if (currentLuaParam > nLuaParams) // Adds optional parameters
         {
             if (currentNetParam.IsOptional)
             {
                 paramList.Add(currentNetParam.DefaultValue);
             }
             else
             {
                 isMethod = false;
                 break;
             }
         }
         else if ((extractValue = translator.typeChecker.checkType(luaState, currentLuaParam, currentNetParam.ParameterType)) != null)  // Type checking
         {
             int index = paramList.Add(extractValue(luaState, currentLuaParam));
             MethodArgs methodArg = new MethodArgs();
             methodArg.index = index;
             methodArg.extractValue = extractValue;
             argTypes.Add(methodArg);
             if (currentNetParam.ParameterType.IsByRef)
                 outList.Add(index);
             currentLuaParam++;
         }  // Type does not match, ignore if the parameter is optional
         else if (currentNetParam.IsOptional)
         {
             paramList.Add(currentNetParam.DefaultValue);
         }
         else  // No match
         {
             isMethod = false;
             break;
         }
     }
     if (currentLuaParam != nLuaParams + 1) // Number of parameters does not match
         isMethod = false;
     if (isMethod)
     {
         methodCache.args = paramList.ToArray();
         methodCache.cachedMethod = method;
         methodCache.outList = (int[])outList.ToArray(typeof(int));
         methodCache.argTypes = (MethodArgs[])argTypes.ToArray(typeof(MethodArgs));
     }
     return isMethod;
 }
Exemple #4
0
        /*
         * Matches a method against its arguments in the Lua stack. Returns
         * if the match was succesful. It it was also returns the information
         * necessary to invoke the method.
         */
        internal bool matchParameters(IntPtr luaState, MethodBase method, ref MethodCache methodCache)
        {
            ExtractValue extractValue;
            bool isMethod = true;
            var paramInfo = method.GetParameters ();
            int currentLuaParam = 1;
            int nLuaParams = LuaDLL.lua_gettop(luaState);
            var paramList = new List<object> ();
            var outList = new List<int> ();
            var argTypes = new List<MethodArgs> ();

            foreach (var currentNetParam in paramInfo) {
                if (!currentNetParam.IsIn && currentNetParam.IsOut)  // Skips out params 
                {
                    paramList.Add (null);
                    outList.Add (paramList.LastIndexOf (null));
                }  else if (currentLuaParam <= nLuaParams && _IsTypeCorrect (luaState, currentLuaParam, currentNetParam, out extractValue)) {  // Type checking
                    var value = extractValue (luaState, currentLuaParam);
                    paramList.Add (value);
                    int index = paramList.LastIndexOf (value);
                    var methodArg = new MethodArgs ();
                    methodArg.index = index;
                    methodArg.extractValue = extractValue;
                    argTypes.Add (methodArg);

                    if (currentNetParam.ParameterType.IsByRef)
                        outList.Add (index);

                    currentLuaParam++;
                }  // Type does not match, ignore if the parameter is optional
                else if (_IsParamsArray (luaState, currentLuaParam, currentNetParam, out extractValue)) {

                    var paramArrayType = currentNetParam.ParameterType.GetElementType ();

                    Func<int, object> extractDelegate = (currentParam) => {
                        currentLuaParam ++;
                        return extractValue (luaState, currentParam);
                    };
                    int count = (nLuaParams - currentLuaParam) + 1;
                    Array paramArray = TableToArray (extractDelegate, paramArrayType, currentLuaParam, count);

                    paramList.Add (paramArray);
                    int index = paramList.LastIndexOf (paramArray);
                    var methodArg = new MethodArgs ();
                    methodArg.index = index;
                    methodArg.extractValue = extractValue;
                    methodArg.isParamsArray = true;
                    methodArg.paramsArrayType = paramArrayType;
                    argTypes.Add (methodArg);

                } else if (currentLuaParam > nLuaParams) { // Adds optional parameters
                    if (currentNetParam.IsOptional)
                        paramList.Add (currentNetParam.DefaultValue);
                    else {
                        isMethod = false;
                        break;
                    }
                } else if (currentNetParam.IsOptional)
                    paramList.Add (currentNetParam.DefaultValue);
                else {  // No match
                    isMethod = false;
                    break;
                }
            }

            if (currentLuaParam != nLuaParams + 1) // Number of parameters does not match
                isMethod = false;
            if (isMethod) {
                methodCache.args = paramList.ToArray ();
                methodCache.cachedMethod = method;
                methodCache.outList = outList.ToArray ();
                methodCache.argTypes = argTypes.ToArray ();
            }

            return isMethod;
        }
        /*
         * Matches a method against its arguments in the Lua stack. Returns
         * if the match was succesful. It it was also returns the information
         * necessary to invoke the method.
         */
        internal bool matchParameters(IntPtr luaState, MethodBase method, ref MethodCache methodCache)
        {
            ExtractValue extractValue;
            bool         isMethod = true;

            ParameterInfo[]   paramInfo       = method.GetParameters();
            int               currentLuaParam = 1;
            int               nLuaParams      = LuaDLL.lua_gettop(luaState);
            ArrayList         paramList       = new ArrayList();
            List <int>        outList         = new List <int>();
            List <MethodArgs> argTypes        = new List <MethodArgs>();

            foreach (ParameterInfo currentNetParam in paramInfo)
            {
                if (!currentNetParam.IsIn && currentNetParam.IsOut)  // Skips out params
                {
                    outList.Add(paramList.Add(null));
                }
                else if (currentLuaParam > nLuaParams) // Adds optional parameters
                {
                    if (currentNetParam.IsOptional)
                    {
                        paramList.Add(currentNetParam.DefaultValue);
                    }
                    else
                    {
                        isMethod = false;
                        break;
                    }
                }
                else if (_IsTypeCorrect(luaState, currentLuaParam, currentNetParam, out extractValue))  // Type checking
                {
                    int index = paramList.Add(extractValue(luaState, currentLuaParam));

                    MethodArgs methodArg = new MethodArgs();
                    methodArg.index        = index;
                    methodArg.extractValue = extractValue;
                    argTypes.Add(methodArg);

                    if (currentNetParam.ParameterType.IsByRef)
                    {
                        outList.Add(index);
                    }
                    currentLuaParam++;
                }  // Type does not match, ignore if the parameter is optional
                else if (_IsParamsArray(luaState, currentLuaParam, currentNetParam, out extractValue))
                {
                    object luaParamValue = extractValue(luaState, currentLuaParam);

                    Type paramArrayType = currentNetParam.ParameterType.GetElementType();

                    Array paramArray;

                    if (luaParamValue is LuaTable)
                    {
                        LuaTable table = (LuaTable)luaParamValue;
                        IDictionaryEnumerator tableEnumerator = table.GetEnumerator();

                        paramArray = Array.CreateInstance(paramArrayType, table.Values.Count);

                        tableEnumerator.Reset();

                        int paramArrayIndex = 0;

                        while (tableEnumerator.MoveNext())
                        {
                            paramArray.SetValue(Convert.ChangeType(tableEnumerator.Value, currentNetParam.ParameterType.GetElementType()), paramArrayIndex);
                            paramArrayIndex++;
                        }
                    }
                    else
                    {
                        paramArray = Array.CreateInstance(paramArrayType, 1);
                        paramArray.SetValue(luaParamValue, 0);
                    }

                    int index = paramList.Add(paramArray);

                    MethodArgs methodArg = new MethodArgs();
                    methodArg.index           = index;
                    methodArg.extractValue    = extractValue;
                    methodArg.isParamsArray   = true;
                    methodArg.paramsArrayType = paramArrayType;
                    argTypes.Add(methodArg);

                    currentLuaParam++;
                }
                else if (currentNetParam.IsOptional)
                {
                    paramList.Add(currentNetParam.DefaultValue);
                }
                else  // No match
                {
                    isMethod = false;
                    break;
                }
            }
            if (currentLuaParam != nLuaParams + 1) // Number of parameters does not match
            {
                isMethod = false;
            }
            if (isMethod)
            {
                methodCache.args         = paramList.ToArray();
                methodCache.cachedMethod = method;
                methodCache.outList      = outList.ToArray();
                methodCache.argTypes     = argTypes.ToArray();
            }
            return(isMethod);
        }
Exemple #6
0
        /*
         * Matches a method against its arguments in the Lua stack. Returns
         * if the match was succesful. It it was also returns the information
         * necessary to invoke the method.
         */
        internal bool matchParameters(IntPtr luaState, MethodBase method, ref MethodCache methodCache)
        {
            ExtractValue extractValue;
            bool         isMethod        = true;
            var          paramInfo       = method.GetParameters();
            int          currentLuaParam = 1;
            int          nLuaParams      = LuaDLL.lua_gettop(luaState);
            var          paramList       = new List <object> ();
            var          outList         = new List <int> ();
            var          argTypes        = new List <MethodArgs> ();

            foreach (var currentNetParam in paramInfo)
            {
                if (!currentNetParam.IsIn && currentNetParam.IsOut)  // Skips out params
                {
                    paramList.Add(null);
                    outList.Add(paramList.LastIndexOf(null));
                }
                else if (currentLuaParam <= nLuaParams && _IsTypeCorrect(luaState, currentLuaParam, currentNetParam, out extractValue))        // Type checking
                {
                    var value = extractValue(luaState, currentLuaParam);
                    paramList.Add(value);
                    int index     = paramList.LastIndexOf(value);
                    var methodArg = new MethodArgs();
                    methodArg.index        = index;
                    methodArg.extractValue = extractValue;
                    argTypes.Add(methodArg);

                    if (currentNetParam.ParameterType.IsByRef)
                    {
                        outList.Add(index);
                    }

                    currentLuaParam++;
                }  // Type does not match, ignore if the parameter is optional
                else if (_IsParamsArray(luaState, currentLuaParam, currentNetParam, out extractValue))
                {
                    var paramArrayType = currentNetParam.ParameterType.GetElementType();

                    Func <int, object> extractDelegate = (currentParam) => {
                        currentLuaParam++;
                        return(extractValue(luaState, currentParam));
                    };
                    int   count      = (nLuaParams - currentLuaParam) + 1;
                    Array paramArray = TableToArray(extractDelegate, paramArrayType, currentLuaParam, count);

                    paramList.Add(paramArray);
                    int index     = paramList.LastIndexOf(paramArray);
                    var methodArg = new MethodArgs();
                    methodArg.index           = index;
                    methodArg.extractValue    = extractValue;
                    methodArg.isParamsArray   = true;
                    methodArg.paramsArrayType = paramArrayType;
                    argTypes.Add(methodArg);
                }
                else if (currentLuaParam > nLuaParams)     // Adds optional parameters
                {
                    if (currentNetParam.IsOptional)
                    {
                        paramList.Add(currentNetParam.DefaultValue);
                    }
                    else
                    {
                        isMethod = false;
                        break;
                    }
                }
                else if (currentNetParam.IsOptional)
                {
                    paramList.Add(currentNetParam.DefaultValue);
                }
                else    // No match
                {
                    isMethod = false;
                    break;
                }
            }

            if (currentLuaParam != nLuaParams + 1) // Number of parameters does not match
            {
                isMethod = false;
            }
            if (isMethod)
            {
                methodCache.args         = paramList.ToArray();
                methodCache.cachedMethod = method;
                methodCache.outList      = outList.ToArray();
                methodCache.argTypes     = argTypes.ToArray();
            }

            return(isMethod);
        }
Exemple #7
0
        /// <summary>Calls the method. Receives the arguments from the Lua stack and returns values in it.</summary>
        public int call(lua.State L)
        {
            using (luanet.entercfunction(L, _Translator.interpreter))
            {
                MethodBase methodToCall  = _Method;
                object     targetObject  = _Target;
                bool       failedCall    = true;
                int        nReturnValues = 0;

                luaL.checkstack(L, 5, "MethodWrapper.call");

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

                if (methodToCall == null)                 // Method from name
                {
                    targetObject = isStatic ? null : _ExtractTarget(L, 1);

                    //lua.remove(L,1); // Pops the receiver
                    if (_LastCalledMethod.cachedMethod != null && _Translator.memberIsAllowed(_LastCalledMethod.cachedMethod)) // 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  = lua.gettop(L) - numStackToSkip;
                        MethodBase method         = _LastCalledMethod.cachedMethod;

                        if (numArgsPassed == _LastCalledMethod.argTypes.Length)                         // No. of args match?
                        {
                            luaL.checkstack(L, _LastCalledMethod.outList.Length + 6, "MethodWrapper.call");
                            object[] args = _LastCalledMethod.args;

                            try
                            {
                                for (int i = 0; i < _LastCalledMethod.argTypes.Length; i++)
                                {
                                    MethodArgs type          = _LastCalledMethod.argTypes[i];
                                    object     luaParamValue = type.extractValue(L, i + 1 + numStackToSkip);

                                    args[type.index] = _LastCalledMethod.argTypes[i].isParamsArray
                                                                                ? ObjectTranslator.TableToArray(luaParamValue, type.paramsArrayType)
                                                                                : luaParamValue;

                                    if (args[type.index] == null && !lua.isnil(L, i + 1 + numStackToSkip))
                                    {
                                        throw new LuaException("argument number " + (i + 1) + " is invalid");
                                    }
                                }
                                _Translator.pushReturnValue(L, method.IsConstructor
                                                                        ? ((ConstructorInfo)method).Invoke(args)
                                                                        : method.Invoke(targetObject, args));

                                failedCall = false;
                            }
                            catch (TargetInvocationException ex) { return(_Translator.throwError(L, luaclr.verifyex(ex.InnerException))); }
                            catch (LuaInternalException) { throw; }
                            catch (Exception ex)
                            {
                                if (_Members.Length == 1)                                          // Is the method overloaded?
                                {
                                    return(luaL.error(L, "method call failed ({0})", ex.Message)); // No, throw error
                                }
                            }
                        }
                    }

                    // 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)
                            {
                                return(luaL.error(L, String.Format("instance method '{0}' requires a non null target object", _MethodName)));
                            }

                            lua.remove(L, 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(L, m, ref _LastCalledMethod);
                            if (isMethod)
                            {
                                if (_Translator.memberIsAllowed(member))
                                {
                                    hasMatch = true;
                                    break;
                                }
                                if (_Members.Length == 1)
                                {
                                    return(luaL.error(L, "method call failed (access denied)"));
                                }
                            }
                        }
                        if (!hasMatch)
                        {
                            return(luaL.error(L, (candidateName == null)
                                                                ? "invalid arguments to method call"
                                                                : "invalid arguments to method: " + candidateName));
                        }
                    }
                }
                else                 // Method from MethodBase instance
                {
                    if (!_Translator.memberIsAllowed(methodToCall))
                    {
                        return(luaL.error(L, "method call failed (access denied)"));
                    }

                    if (methodToCall.ContainsGenericParameters)
                    {
                        // bool isMethod = //* not used
                        _Translator.matchParameters(L, methodToCall, ref _LastCalledMethod);

                        if (methodToCall.IsGenericMethodDefinition)
                        {
                            //need to make a concrete type of the generic method definition
                            var args     = _LastCalledMethod.args;
                            var typeArgs = new Type[args.Length];

                            for (int i = 0; i < args.Length; ++i)
                            {
                                typeArgs[i] = args[i].GetType();
                            }

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

                            _Translator.pushReturnValue(L, concreteMethod.Invoke(targetObject, args));

                            failedCall = false;
                        }
                        else if (methodToCall.ContainsGenericParameters)
                        {
                            return(luaL.error(L, "unable to invoke method on generic class as the current method is an open generic method"));
                        }
                    }
                    else
                    {
                        if (!methodToCall.IsStatic && !methodToCall.IsConstructor && targetObject == null)
                        {
                            targetObject = _ExtractTarget(L, 1);
                            lua.remove(L, 1);                             // Pops the receiver
                        }

                        if (!_Translator.matchParameters(L, methodToCall, ref _LastCalledMethod))
                        {
                            return(luaL.error(L, "invalid arguments to method call"));
                        }
                    }
                }

                if (failedCall)
                {
                    if (!_Translator.memberIsAllowed(_LastCalledMethod.cachedMethod))
                    {
                        return(luaL.error(L, "method call failed (access denied)"));
                    }
                    luaL.checkstack(L, _LastCalledMethod.outList.Length + 6, "MethodWrapper.call");
                    try
                    {
                        _Translator.pushReturnValue(L, _LastCalledMethod.cachedMethod.IsConstructor
                                                        ? ((ConstructorInfo)_LastCalledMethod.cachedMethod).Invoke(_LastCalledMethod.args)
                                                        : _LastCalledMethod.cachedMethod.Invoke(isStatic ? null : targetObject, _LastCalledMethod.args));
                    }
                    catch (TargetInvocationException ex) { return(_Translator.throwError(L, luaclr.verifyex(ex.InnerException))); }
                    catch (LuaInternalException) { throw; }
                    catch (Exception ex) { return(luaL.error(L, "method call failed ({0})", ex.Message)); }
                }

                // Pushes out and ref return values
                foreach (int arg in _LastCalledMethod.outList)
                {
                    nReturnValues++;
                    _Translator.pushReturnValue(L, _LastCalledMethod.args[arg]);
                }

                //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);
            }
        }
Exemple #8
0
        /*
         * Matches a method against its arguments in the Lua stack. Returns
         * if the match was succesful. It it was also returns the information
         * necessary to invoke the method.
         */
        internal bool matchParameters(IntPtr luaState, MethodBase method, ref MethodCache methodCache)
        {
            ExtractValue extractValue;
            bool isMethod = true;
            ParameterInfo[] paramInfo = method.GetParameters();
            int currentLuaParam = 1;
            int nLuaParams = LuaDLL.lua_gettop(luaState);
            ArrayList paramList = new ArrayList();
            List<int> outList = new List<int>();
            List<MethodArgs> argTypes = new List<MethodArgs>();
            foreach (ParameterInfo currentNetParam in paramInfo)
            {
                if (!currentNetParam.IsIn && currentNetParam.IsOut)  // Skips out params
                {
                    outList.Add(paramList.Add(null));
                }
                else if (currentLuaParam > nLuaParams) // Adds optional parameters
                {
                    if (currentNetParam.IsOptional)
                    {
                        paramList.Add(currentNetParam.DefaultValue);
                    }
                    else
                    {
                        isMethod = false;
                        break;
                    }
                }
                else if (_IsTypeCorrect(luaState, currentLuaParam, currentNetParam, out extractValue))  // Type checking
                {
                    int index = paramList.Add(extractValue(luaState, currentLuaParam));

                    MethodArgs methodArg = new MethodArgs();
                    methodArg.index = index;
                    methodArg.extractValue = extractValue;
                    argTypes.Add(methodArg);

                    if (currentNetParam.ParameterType.IsByRef)
                        outList.Add(index);
                    currentLuaParam++;
                }  // Type does not match, ignore if the parameter is optional
                else if (_IsParamsArray(luaState, currentLuaParam, currentNetParam, out extractValue))
                {
                    object luaParamValue = extractValue(luaState, currentLuaParam);

                    Type paramArrayType = currentNetParam.ParameterType.GetElementType();

                    Array paramArray;

                    if (luaParamValue is LuaTable)
                    {
                        LuaTable table = (LuaTable)luaParamValue;
                        IDictionaryEnumerator tableEnumerator = table.GetEnumerator();

                        paramArray = Array.CreateInstance(paramArrayType, table.Values.Count);

                        tableEnumerator.Reset();

                        int paramArrayIndex = 0;

                        while(tableEnumerator.MoveNext())
                        {
                            paramArray.SetValue(Convert.ChangeType(tableEnumerator.Value, currentNetParam.ParameterType.GetElementType()), paramArrayIndex);
                            paramArrayIndex++;
                        }
                    }
                    else
                    {
                        paramArray = Array.CreateInstance(paramArrayType, 1);
                        paramArray.SetValue(luaParamValue, 0);
                    }

                    int index = paramList.Add(paramArray);

                    MethodArgs methodArg = new MethodArgs();
                    methodArg.index = index;
                    methodArg.extractValue = extractValue;
                    methodArg.isParamsArray = true;
                    methodArg.paramsArrayType = paramArrayType;
                    argTypes.Add(methodArg);

                    currentLuaParam++;
                }
                else if (currentNetParam.IsOptional)
                {
                    paramList.Add(currentNetParam.DefaultValue);
                }
                else  // No match
                {
                    isMethod = false;
                    break;
                }
            }
            if (currentLuaParam != nLuaParams + 1) // Number of parameters does not match
                isMethod = false;
            if (isMethod)
            {
                methodCache.args = paramList.ToArray();
                methodCache.cachedMethod = method;
                methodCache.outList = outList.ToArray();
                methodCache.argTypes = argTypes.ToArray();
            }
            return isMethod;
        }
 internal bool matchParameters(IntPtr luaState, MethodBase method, ref MethodCache methodCache)
 {
     bool flag = true;
     ParameterInfo[] parameters = method.GetParameters();
     int currentLuaParam = 1;
     int num2 = LuaDLL.lua_gettop(luaState);
     ArrayList list = new ArrayList();
     List<int> list2 = new List<int>();
     List<MethodArgs> list3 = new List<MethodArgs>();
     foreach (ParameterInfo info in parameters)
     {
         if (!info.IsIn && info.IsOut)
         {
             list2.Add(list.Add(null));
         }
         else
         {
             ExtractValue value2;
             if (currentLuaParam > num2)
             {
                 if (info.IsOptional)
                 {
                     list.Add(info.DefaultValue);
                     goto Label_0105;
                 }
                 flag = false;
                 break;
             }
             if (this._IsTypeCorrect(luaState, currentLuaParam, info, out value2))
             {
                 int item = list.Add(value2(luaState, currentLuaParam));
                 MethodArgs args = new MethodArgs();
                 args.index = item;
                 args.extractValue = value2;
                 list3.Add(args);
                 if (info.ParameterType.IsByRef)
                 {
                     list2.Add(item);
                 }
                 currentLuaParam++;
             }
             else if (info.IsOptional)
             {
                 list.Add(info.DefaultValue);
             }
             else
             {
                 flag = false;
                 break;
             }
         Label_0105:;
         }
     }
     if (currentLuaParam != (num2 + 1))
     {
         flag = false;
     }
     if (flag)
     {
         methodCache.args = list.ToArray();
         methodCache.cachedMethod = method;
         methodCache.outList = list2.ToArray();
         methodCache.argTypes = list3.ToArray();
     }
     return flag;
 }
        /*
         * Matches a method against its arguments in the Lua stack. Returns
         * if the match was succesful. It it was also returns the information
         * necessary to invoke the method.
         */
        internal bool matchParameters(IntPtr luaState, MethodBase method, ref MethodCache methodCache)
        {
            ExtractValue extractValue;
            bool isMethod = true;
            ParameterInfo[] paramInfo = method.GetParameters();
            int currentLuaParam = 1;
            int nLuaParams = LuaDLL.lua_gettop(luaState);
            ArrayList paramList = new ArrayList();
            List<int> outList = new List<int>();
            List<MethodArgs> argTypes = new List<MethodArgs>();
            foreach (ParameterInfo currentNetParam in paramInfo)
            {
                if (!currentNetParam.IsIn && currentNetParam.IsOut)  // Skips out params
                {
                    outList.Add(paramList.Add(null));
                }
                else if (currentLuaParam > nLuaParams) // Adds optional parameters
                {
                    if (currentNetParam.IsOptional)
                    {
                        paramList.Add(currentNetParam.DefaultValue);
                    }
                    else
                    {
                        isMethod = false;
                        break;
                    }
                }
                else if (_IsTypeCorrect(luaState, currentLuaParam, currentNetParam, out extractValue))  // Type checking
                {
                    int index = paramList.Add(extractValue(luaState, currentLuaParam));

                    MethodArgs methodArg = new MethodArgs();
                    methodArg.index = index;
                    methodArg.extractValue = extractValue;
                    argTypes.Add(methodArg);

                    if (currentNetParam.ParameterType.IsByRef)
                        outList.Add(index);
                    currentLuaParam++;
                }  // Type does not match, ignore if the parameter is optional
                else if (_IsParamsArray(luaState, currentLuaParam, currentNetParam, out extractValue))
                {
                    object luaParamValue = extractValue(luaState, currentLuaParam);
                    Type paramArrayType = currentNetParam.ParameterType.GetElementType();

                    Array paramArray = TableToArray(luaParamValue, paramArrayType);
                    int index = paramList.Add(paramArray);

                    MethodArgs methodArg = new MethodArgs();
                    methodArg.index = index;
                    methodArg.extractValue = extractValue;
                    methodArg.isParamsArray = true;
                    methodArg.paramsArrayType = paramArrayType;
                    argTypes.Add(methodArg);

                    currentLuaParam++;
                }
                else if (currentNetParam.IsOptional)
                {
                    paramList.Add(currentNetParam.DefaultValue);
                }
                else  // No match
                {
                    isMethod = false;
                    break;
                }
            }
            if (currentLuaParam != nLuaParams + 1) // Number of parameters does not match
                isMethod = false;
            if (isMethod)
            {
                methodCache.args = paramList.ToArray();
                methodCache.cachedMethod = method;
                methodCache.outList = outList.ToArray();
                methodCache.argTypes = argTypes.ToArray();
            }
            return isMethod;
        }
Exemple #11
0
        public int call(IntPtr luaState)
        {
            object targetObject;
            bool   failedCall    = true;
            int    nReturnValues = 0;

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

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

            SetPendingException(null);

            if (isStatic)
            {
                targetObject = null;
            }
            else
            {
                targetObject = _ExtractTarget(luaState, 1);
            }

            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  = LuaAPI.lua_gettop(luaState) - numStackToSkip;
                MethodBase method         = _LastCalledMethod.cachedMethod;

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

                    //这里 args 只是将 _LastCalledMethod.args 拿来做缓冲区用,避免内存再分配, 里面的值是可以干掉的
                    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 &&
                                !LuaAPI.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)
                    {
                        return(SetPendingException(e.GetBaseException()));
                    }
                    catch (Exception e)
                    {
                        if (_Members.Length == 1)
                        {
                            return(SetPendingException(e));
                        }
                    }
                }
            }

            // Cache miss
            if (failedCall)
            {
                if (!isStatic)
                {
                    if (targetObject == null)
                    {
                        _Translator.throwError(luaState, String.Format("instance method '{0}' requires a non null target object", _MethodName));
                        LuaAPI.lua_pushnil(luaState);
                        return(1);
                    }
                    LuaAPI.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);

                    LuaAPI.luaL_error(luaState, msg);
                    LuaAPI.lua_pushnil(luaState);
                    ClearCachedArgs();
                    return(1);
                }
            }

            if (failedCall)
            {
                if (!LuaAPI.lua_checkstack(luaState, _LastCalledMethod.outList.Length + 6))
                {
                    ClearCachedArgs();
                    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)
                {
                    ClearCachedArgs();
                    return(SetPendingException(e.GetBaseException()));
                }
                catch (Exception e)
                {
                    ClearCachedArgs();
                    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]]);
            }

            //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++;
            }
            ClearCachedArgs();
            return(nReturnValues < 1 ? 1 : nReturnValues);
        }
Exemple #12
0
        /*
         * Matches a method against its arguments in the Lua stack. Returns
         * if the match was succesful. It it was also returns the information
         * necessary to invoke the method.
         */
        internal bool matchParameters(IntPtr luaState, MethodBase method, ref MethodCache methodCache)
        {
            ExtractValue extractValue;
            bool isMethod = true;
            ParameterInfo[] paramInfo = method.GetParameters();
            int currentLuaParam = 1;
            int nLuaParams = LuaDLL.lua_gettop(luaState);
            ArrayList paramList = new ArrayList();
            List<int> outList = new List<int>();
            List<MethodArgs> argTypes = new List<MethodArgs>();
            foreach (ParameterInfo currentNetParam in paramInfo)
            {
                if (!currentNetParam.IsIn && currentNetParam.IsOut)  // Skips out params
                {
                    outList.Add(paramList.Add(null));
                }
                else if (currentLuaParam > nLuaParams) // Adds optional parameters
                {
                    if (currentNetParam.IsOptional)
                    {
                        paramList.Add(currentNetParam.DefaultValue);
                    }
                    else
                    {
                        isMethod = false;
                        break;
                    }
                }
                else if (_IsTypeCorrect(luaState, currentLuaParam, currentNetParam, out extractValue))  // Type checking
                {
                    int index = paramList.Add(extractValue(luaState, currentLuaParam));

                    MethodArgs methodArg = new MethodArgs();
                    methodArg.index = index;
                    methodArg.extractValue = extractValue;
                    argTypes.Add(methodArg);

                    if (currentNetParam.ParameterType.IsByRef)
                        outList.Add(index);
                    currentLuaParam++;
                }  // Type does not match, ignore if the parameter is optional
                else if (_IsParamsArray(luaState, currentLuaParam, currentNetParam, out extractValue))
                {
                    object luaParamValue = extractValue(luaState, currentLuaParam);

                    Type paramArrayType = currentNetParam.ParameterType.GetElementType();

                    Array paramArray;

                    if (luaParamValue is LuaTable)
                    {
                        LuaTable table = (LuaTable)luaParamValue;
                        IDictionaryEnumerator tableEnumerator = table.GetEnumerator();

                        paramArray = Array.CreateInstance(paramArrayType, table.Values.Count);

                        tableEnumerator.Reset();

                        int paramArrayIndex = 0;

                        while(tableEnumerator.MoveNext())
                        {
                            paramArray.SetValue(Convert.ChangeType(tableEnumerator.Value, currentNetParam.ParameterType.GetElementType()), paramArrayIndex);
                            paramArrayIndex++;
                        }
                    }
                    else
                    {
                        paramArray = Array.CreateInstance(paramArrayType, 1);
                        paramArray.SetValue(luaParamValue, 0);
                    }

                    int index = paramList.Add(paramArray);

                    MethodArgs methodArg = new MethodArgs();
                    methodArg.index = index;
                    methodArg.extractValue = extractValue;
                    methodArg.isParamsArray = true;
                    methodArg.paramsArrayType = paramArrayType;
                    argTypes.Add(methodArg);

                    currentLuaParam++;
                }
                else if (currentNetParam.IsOptional)
                {
                    paramList.Add(currentNetParam.DefaultValue);
                }
                else  // No match
                {
                    isMethod = false;
                    break;
                }
            }
            // HACK: If a CSFunction is used as the generator in a for loop
            // Then the for loop calls the CSFunction with two null parameters
            // I assume some Lua functions use these parameters to speed up the loop
            //
            // As a side effect of this hack you can pass any number of null/nil parameters
            // to a CSFunction that normaly takes no parameters
             if (currentLuaParam != nLuaParams + 1) // Number of parameters does not match
            {
                isMethod = false;
                if(currentLuaParam == 1){
                    while(LuaDLL.lua_gettop(luaState) != 0){
                        //FIXME: LuaDLL.lua_isnil doesn't seem to detect LuaTypes.LUA_TNIL as nil
                        if(LuaDLL.lua_isnil(luaState, LuaDLL.lua_gettop(luaState)) || LuaDLL.lua_type(luaState, 1) == LuaTypes.LUA_TNIL)
                        {
                            LuaDLL.lua_pop(luaState, 1);
                            isMethod = true;
                        }else{
                            isMethod = false;
                            break;
                        }
                    }
                }
            }
            if (isMethod)
            {
                methodCache.args = paramList.ToArray();
                methodCache.cachedMethod = method;
                methodCache.outList = outList.ToArray();
                methodCache.argTypes = argTypes.ToArray();
            }
            return isMethod;
        }
        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);
        }
Exemple #14
0
        /*
         * Matches a method against its arguments in the Lua stack. Returns
         * if the match was succesful. It it was also returns the information
         * necessary to invoke the method.
         */
        internal bool matchParameters(IntPtr luaState, MethodBase method, ref MethodCache methodCache)
        {
            ExtractValue extractValue;
            bool         isMethod = true;

            ParameterInfo[]   paramInfo       = method.GetParameters();
            int               currentLuaParam = 1;
            int               nLuaParams      = LuaAPI.lua_gettop(luaState);
            ArrayList         paramList       = new ArrayList();
            List <int>        outList         = new List <int>();
            List <MethodArgs> argTypes        = new List <MethodArgs>();

            foreach (ParameterInfo currentNetParam in paramInfo)
            {
                if (!currentNetParam.IsIn && currentNetParam.IsOut)  // Skips out params
                {
                    outList.Add(paramList.Add(null));
                }
                else if (currentLuaParam > nLuaParams) // Adds optional parameters
                {
                    if (currentNetParam.IsOptional)
                    {
                        paramList.Add(currentNetParam.DefaultValue);
                    }
                    else
                    {
                        isMethod = false;
                        break;
                    }
                }
                else if (_IsTypeCorrect(luaState, currentLuaParam, currentNetParam, out extractValue))  // Type checking
                {
                    int index = paramList.Add(extractValue(luaState, currentLuaParam));

                    MethodArgs methodArg = new MethodArgs();
                    methodArg.index        = index;
                    methodArg.extractValue = extractValue;
                    argTypes.Add(methodArg);

                    if (currentNetParam.ParameterType.IsByRef)
                    {
                        outList.Add(index);
                    }
                    currentLuaParam++;
                }  // Type does not match, ignore if the parameter is optional
                else if (_IsParamsArray(luaState, currentLuaParam, currentNetParam, out extractValue))
                {
                    object luaParamValue  = extractValue(luaState, currentLuaParam);
                    Type   paramArrayType = currentNetParam.ParameterType.GetElementType();

                    Array paramArray = TableToArray(luaParamValue, paramArrayType);
                    int   index      = paramList.Add(paramArray);

                    MethodArgs methodArg = new MethodArgs();
                    methodArg.index           = index;
                    methodArg.extractValue    = extractValue;
                    methodArg.isParamsArray   = true;
                    methodArg.paramsArrayType = paramArrayType;
                    argTypes.Add(methodArg);

                    currentLuaParam++;
                }
                else if (currentNetParam.IsOptional)
                {
                    paramList.Add(currentNetParam.DefaultValue);
                }
                else  // No match
                {
                    isMethod = false;
                    break;
                }
            }
            if (currentLuaParam != nLuaParams + 1) // Number of parameters does not match
            {
                isMethod = false;
            }
            if (isMethod)
            {
                methodCache.args         = paramList.ToArray();
                methodCache.cachedMethod = method;
                methodCache.outList      = outList.ToArray();
                methodCache.argTypes     = argTypes.ToArray();
            }
            return(isMethod);
        }