Exemple #1
0
        public Dictionary <object, object> ToMap()
        {
            var map    = new Dictionary <object, object>();
            int oldTop = LuaLib.LuaGetTop(luaState);

            try {
                LuaLib.LuaGetRef(this.luaState, this.Reference);
                LuaLib.LuaPushNil(this.luaState);

                while (0 != LuaLib.LuaNext(this.luaState, -2))
                {
                    map[LuaExecuter._ParseLuaValue(this.luaState, -2)] = LuaExecuter._ParseLuaValue(this.luaState, -1);
                    LuaLib.LuaSetTop(this.luaState, -2);
                }
            } finally {
                LuaLib.LuaSetTop(this.luaState, oldTop);
            }

            return(map);
        }
Exemple #2
0
        public object[] ToArray()
        {
            var list   = new List <object>();
            int oldTop = LuaLib.LuaGetTop(luaState);

            try {
                LuaLib.LuaGetRef(this.luaState, this.Reference);
                LuaLib.LuaPushNil(this.luaState);

                while (0 != LuaLib.LuaNext(this.luaState, -2))
                {
                    list.Add(LuaExecuter._ParseLuaValue(this.luaState, -1));
                    LuaLib.LuaSetTop(this.luaState, -2);
                }
            } finally {
                LuaLib.LuaSetTop(this.luaState, oldTop);
            }

            return(list.ToArray());
        }
Exemple #3
0
        private object[] _PopValues(LuaState luaState, int oldTop)
        {
            int newTop = LuaLib.LuaGetTop(luaState);

            if (oldTop == newTop)
            {
                return(null);
            }
            else
            {
                var returnValues = new List <object>();
                for (int i = oldTop + 1; i <= newTop; i++)
                {
                    returnValues.Add(LuaExecuter._ParseLuaValue(luaState, i));
                }

                LuaLib.LuaSetTop(luaState, oldTop);
                return(returnValues.ToArray());
            }
        }
Exemple #4
0
        private static int _PrintCallback(LuaState luaState)
        {
            try {
                var n       = LuaLib.LuaGetTop(luaState);
                var builder = new StringBuilder();
                for (var i = 1; i <= n; ++i)
                {
                    if (LuaLib.LuaIsString(luaState, i))
                    {
                        builder.Append(LuaLib.LuaToString(luaState, i));
                        builder.Append(" ");
                    }
                }
                Logger <ILuaRuntime> .L(builder.ToString());
            } catch (Exception e) {
                Logger <ILuaRuntime> .X(e);
            }

            return(0);
        }
Exemple #5
0
        public object[] Call(object function, params object[] args)
        {
            int oldTop = LuaLib.LuaGetTop(this.luaState);

            if (function is string)
            {
                var fullPath = function as string;
                var path     = fullPath.Split('.');
                LuaLib.LuaGetGlobal(this.luaState, path[0]);
                function = LuaExecuter._ParseLuaValue(luaState, -1);
                if (path.Length > 1)
                {
                    var remainingPath = new string[path.Length - 1];
                    Array.Copy(path, 1, remainingPath, 0, path.Length - 1);
                    function = _GetObject(remainingPath);
                }
            }

            LuaExecuter._PushObject(this.luaState, function);
            if (null != args)
            {
                for (int i = 0; i < args.Length; i++)
                {
                    LuaExecuter._PushObject(this.luaState, args[i]);
                }
            }

            try {
                int error = LuaLib.LuaPCall(this.luaState, args.Length, -1, 0);
                if (error != 0)
                {
                    _ThrowExceptionFromError(this.luaState, oldTop);
                }
            } catch (Exception e) {
                Logger <ILuaRuntime> .X(e);
            }

            return(_PopValues(this.luaState, oldTop));
        }
Exemple #6
0
        public object[] PopParameters()
        {
            var n = LuaLib.LuaGetTop(this.luaState);

            if (n < this.input)
            {
                Logger <ILuaRuntime> .W("Method input parameter count is incorrect since definition is " + this.input + ", but only" + n + " is input!");
            }

            if (n > 0)
            {
                var parameters = new object[n];
                for (var i = 0; i < n; ++i)
                {
                    parameters[i] = _ParseLuaValue(this.luaState, i + 1);
                }

                return(parameters);
            }
            else
            {
                return(null);
            }
        }
Exemple #7
0
        /*
         * Calls the method. Receives the arguments from the Lua stack
         * and returns values in it.
         */
        int Call(LuaState luaState)
        {
            var    methodToCall  = _Method;
            object targetObject  = _Target;
            bool   failedCall    = true;
            int    nReturnValues = 0;

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

            bool isStatic = _IsStatic;

            SetPendingException(null);

            if (methodToCall == null)               // Method from name
            {
                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  = LuaLib.LuaGetTop(luaState) - numStackToSkip;
                    MethodBase method         = _LastCalledMethod.cachedMethod;

                    if (numArgsPassed == _LastCalledMethod.argTypes.Length)                       // No. of args match?
                    {
                        if (!LuaLib.LuaCheckStack(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];

                                int index = i + 1 + numStackToSkip;

                                Func <int, object> valueExtractor = (currentParam) => {
                                    return(type.extractValue(luaState, currentParam));
                                };

                                if (_LastCalledMethod.argTypes [i].isParamsArray)
                                {
                                    int   count      = _LastCalledMethod.argTypes.Length - i;
                                    Array paramArray = _Translator.TableToArray(valueExtractor, type.paramsArrayType, index, count);
                                    args [_LastCalledMethod.argTypes [i].index] = paramArray;
                                }
                                else
                                {
                                    args [type.index] = valueExtractor(index);
                                }

                                if (_LastCalledMethod.args [_LastCalledMethod.argTypes [i].index] == null &&
                                    !LuaLib.LuaIsNil(luaState, i + 1 + numStackToSkip))
                                {
                                    throw new LuaException(string.Format("argument number {0} is invalid", (i + 1)));
                                }
                            }

                            if (_IsStatic)
                            {
                                _Translator.Push(luaState, method.Invoke(null, _LastCalledMethod.args));
                            }
                            else
                            {
                                if (method.IsConstructor)
                                {
                                    _Translator.Push(luaState, ((ConstructorInfo)method).Invoke(_LastCalledMethod.args));
                                }
                                else
                                {
                                    _Translator.Push(luaState, method.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));
                            LuaLib.LuaPushNil(luaState);
                            return(1);
                        }

                        LuaLib.LuaRemove(luaState, 1);                          // Pops the receiver
                    }

                    bool   hasMatch      = false;
                    string candidateName = null;

                    foreach (var member in _Members)
                    {
#if NETFX_CORE
                        candidateName = member.DeclaringType.Name + "." + member.Name;
#else
                        candidateName = member.ReflectedType.Name + "." + member.Name;
#endif
                        var  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);
                        LuaLib.LuaPushNil(luaState);
                        return(1);
                    }
                }
            }
            else                 // Method from MethodBase instance
            {
                if (methodToCall.ContainsGenericParameters)
                {
                    _Translator.MatchParameters(luaState, methodToCall, ref _LastCalledMethod);

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

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

                        var 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");
                        LuaLib.LuaPushNil(luaState);
                        return(1);
                    }
                }
                else
                {
                    if (!methodToCall.IsStatic && !methodToCall.IsConstructor && targetObject == null)
                    {
                        targetObject = _ExtractTarget(luaState, 1);
                        LuaLib.LuaRemove(luaState, 1);                          // Pops the receiver
                    }

                    if (!_Translator.MatchParameters(luaState, methodToCall, ref _LastCalledMethod))
                    {
                        _Translator.ThrowError(luaState, "invalid arguments to method call");
                        LuaLib.LuaPushNil(luaState);
                        return(1);
                    }
                }
            }

            if (failedCall)
            {
                if (!LuaLib.LuaCheckStack(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);
        }