Ejemplo n.º 1
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 = LuaAPI.lua_gettop(L);

            if (!LuaAPI.lua_checkstack(L, args.Length + 6))
            {
                LuaAPI.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 = LuaAPI.lua_pcall(L, nArgs, -1, -nArgs - 2);

            if (error != 0)
            {
                string err = LuaAPI.lua_tostring(L, -1);
                LuaAPI.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);

            LuaAPI.lua_settop(L, oldTop - 1);
            return(ret);
        }
Ejemplo n.º 2
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);
        }