Esempio n. 1
0
        private void FillMethodArguments(LuaState luaState, int numStackToSkip)
        {
            var args = _lastCalledMethod.args;


            for (var i = 0; i < _lastCalledMethod.argTypes.Length; i++)
            {
                var type = _lastCalledMethod.argTypes[i];

                var index = i + 1 + numStackToSkip;


                if (_lastCalledMethod.argTypes[i].IsParamsArray)
                {
                    var count      = _lastCalledMethod.argTypes.Length - i;
                    var paramArray = ObjectTranslator.TableToArray(luaState, type.ExtractValue, type.ParameterType, index, count);
                    args[_lastCalledMethod.argTypes[i].Index] = paramArray;
                }
                else
                {
                    args[type.Index] = type.ExtractValue(luaState, index);
                }

                if (_lastCalledMethod.args[_lastCalledMethod.argTypes[i].Index] == null &&
                    !luaState.IsNil(i + 1 + numStackToSkip))
                {
                    throw new LuaException($"Argument number {(i + 1)} is invalid");
                }
            }
        }
Esempio n. 2
0
        void FillMethodArguments(LuaState luaState, int numStackToSkip)
        {
            object[] args = _lastCalledMethod.args;


            for (int i = 0; i < _lastCalledMethod.argTypes.Length; i++)
            {
                MethodArgs type = _lastCalledMethod.argTypes[i];

                int index = i + 1 + numStackToSkip;


                if (_lastCalledMethod.argTypes[i].IsParamsArray)
                {
                    int   count      = _lastCalledMethod.argTypes.Length - i;
                    Array paramArray = _translator.TableToArray(luaState, type.ExtractValue, type.ParameterType, index, count);
                    args[_lastCalledMethod.argTypes[i].Index] = paramArray;
                }
                else
                {
                    args[type.Index] = type.ExtractValue(luaState, index);
                }

                if (_lastCalledMethod.args[_lastCalledMethod.argTypes[i].Index] == null &&
                    !luaState.IsNil(i + 1 + numStackToSkip))
                {
                    throw new LuaException(string.Format("Argument number {0} is invalid", (i + 1)));
                }
            }
        }
Esempio n. 3
0
 public void Add(LuaState luaState, ObjectTranslator translator)
 {
     if (!translators.TryAdd(luaState, translator))
     {
         throw new ArgumentException("An item with the same key has already been added. ", "luaState");
     }
 }
Esempio n. 4
0
        int CallInvoke(LuaState luaState, MethodBase method, object targetObject)
        {
            if (!luaState.CheckStack(_lastCalledMethod.outList.Length + 6))
            {
                throw new LuaException("Lua stack overflow");
            }

            try
            {
                if (method.IsConstructor)
                {
                    _translator.Push(luaState, ((ConstructorInfo)method).Invoke(_lastCalledMethod.args));
                }
                else
                {
                    _translator.Push(luaState, method.Invoke(targetObject, _lastCalledMethod.args));
                }
            }
            catch (TargetInvocationException e)
            {
                // Failure of method invocation
                if (_translator.interpreter.UseTraceback)
                {
                    e.GetBaseException().Data["Traceback"] = _translator.interpreter.GetDebugTraceback();
                }
                return(SetPendingException(e.GetBaseException()));
            }
            catch (Exception e)
            {
                return(SetPendingException(e));
            }

            return(PushReturnValue(luaState));
        }
Esempio n. 5
0
 public static void Main(string[] args)
 {
     demo.App.Instance.Start();
     KeraLua.Lua lua = new KeraLua.Lua();
     // 需要 Zeze.Services.ToLuaService.cs 文件开头打开编译选项 USE_KERA_LUA
     Zeze.Services.ToLuaService.Kera ilua = new Zeze.Services.ToLuaService.Kera(lua);
     try
     {
         // 网络建立好,handshake 以后的事件会保存下来,等待lua调用ZezeUpdate才会触发。所以可以先连接。
         //demo.App.Instance.Client.Connect("127.0.0.1", 9999, true); // 改到 main.lua 中连接。
         if (lua.DoString("package.path = package.path .. ';../../../LuaSrc/?.lua;../../../LuaGen/?.lua'"))
         {
             throw new Exception("package.path");
         }
         demo.App.Instance.Client.InitializeLua(ilua);
         if (lua.DoString("require 'main'"))
         {
             throw new Exception("run main.lua error");
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex);
         Console.WriteLine(lua.ToString(-1));
     }
     finally
     {
         demo.App.Instance.Stop();
     }
 }
Esempio n. 6
0
        int CallInvokeOnGenericMethod(LuaState luaState, MethodInfo methodToCall, object targetObject)
        {
            //need to make a concrete type of the generic method definition
            var typeArgs = new List <Type>();

            ParameterInfo [] parameters = methodToCall.GetParameters();

            for (int i = 0; i < parameters.Length; i++)
            {
                ParameterInfo parameter = parameters[i];

                if (!parameter.ParameterType.IsGenericParameter)
                {
                    continue;
                }

                typeArgs.Add(_lastCalledMethod.args[i].GetType());
            }

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

            _translator.Push(luaState, concreteMethod.Invoke(targetObject, _lastCalledMethod.args));

            return(PushReturnValue(luaState));
        }
Esempio n. 7
0
        /*
         * Calls the method. Receives the arguments from the Lua stack
         * and returns values in it.
         */
        int Call(IntPtr state)
        {
            var luaState = LuaState.FromIntPtr(state);

            MethodBase methodToCall = _method;
            object     targetObject = _target;

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

            SetPendingException(null);

            // Method from name
            if (methodToCall == null)
            {
                return(CallMethodFromName(luaState));
            }

            // Method from MethodBase instance
            if (!methodToCall.ContainsGenericParameters)
            {
                if (!methodToCall.IsStatic && !methodToCall.IsConstructor && targetObject == null)
                {
                    targetObject = _extractTarget(luaState, 1);
                    luaState.Remove(1); // Pops the receiver
                }

                if (!_translator.MatchParameters(luaState, methodToCall, _lastCalledMethod, 0))
                {
                    _translator.ThrowError(luaState, "Invalid arguments to method call");
                    luaState.PushNil();
                    return(1);
                }
            }
            else
            {
                if (!methodToCall.IsGenericMethodDefinition)
                {
                    _translator.ThrowError(luaState,
                                           "Unable to invoke method on generic class as the current method is an open generic method");
                    luaState.PushNil();
                    return(1);
                }

                _translator.MatchParameters(luaState, methodToCall, _lastCalledMethod, 0);

                return(CallInvokeOnGenericMethod(luaState, (MethodInfo)methodToCall, targetObject));
            }

            if (_isStatic)
            {
                targetObject = null;
            }

            return(CallInvoke(luaState, _lastCalledMethod.cachedMethod, targetObject));
        }
Esempio n. 8
0
        public void XMove(LuaState to, object val, int index = 1)
        {
            int oldTop = _luaState.GetTop();

            _translator.Push(_luaState, val);
            _luaState.XMove(to, index);

            _luaState.SetTop(oldTop);
        }
        public ObjectTranslator Find(LuaState luaState)
        {
            if (!_translators.TryGetValue(luaState, out var translator))
            {
                var main = luaState.MainThread;

                if (!_translators.TryGetValue(main, out translator))
                {
                    return(null);
                }
            }
            return(translator);
        }
Esempio n. 10
0
        public ObjectTranslator Find(LuaState luaState)
        {
            ObjectTranslator translator;

            if (!translators.TryGetValue(luaState, out translator))
            {
                LuaState main = luaState.MainThread;

                if (!translators.TryGetValue(main, out translator))
                {
                    throw new Exception("Invalid luaState, couldn't find ObjectTranslator");
                }
            }
            return(translator);
        }
Esempio n. 11
0
        int CallInvokeOnGenericMethod(LuaState luaState, MethodInfo methodToCall, object targetObject)
        {
            _translator.MatchParameters(luaState, methodToCall, _lastCalledMethod, 0);

            //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.MakeGenericMethod(typeArgs.ToArray());

            _translator.Push(luaState, concreteMethod.Invoke(targetObject, _lastCalledMethod.args));

            return(PushReturnValue(luaState));
        }
Esempio n. 12
0
        bool IsMethodCached(LuaState luaState, int numArgsPassed, int skipParams)
        {
            if (_lastCalledMethod.cachedMethod == null)
            {
                return(false);
            }

            if (numArgsPassed != _lastCalledMethod.argTypes.Length)
            {
                return(false);
            }

            // If there is no method overloads, is ok to use the cached method
            if (_members.Length == 1)
            {
                return(true);
            }

            return(_translator.MatchParameters(luaState, _lastCalledMethod.cachedMethod, _lastCalledMethod, skipParams));
        }
Esempio n. 13
0
        private int PushReturnValue(LuaState luaState)
        {
            var nReturnValues = 0;

            // Pushes out and ref return values
            foreach (var t in _lastCalledMethod.outList)
            {
                nReturnValues++;
                _translator.Push(luaState, _lastCalledMethod.args[t]);
            }

            //  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);
        }
Esempio n. 14
0
        int PushReturnValue(LuaState luaState)
        {
            int nReturnValues = 0;

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

            //  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);
        }
Esempio n. 15
0
 public object ExtractGenerated(LuaState luaState, int stackPos)
 {
     return(CodeGeneration.Instance.GetDelegate(_delegateType, _translator.GetFunction(luaState, stackPos)));
 }
 public void Remove(LuaState luaState)
 {
     _translators.TryRemove(luaState, out _);
 }
Esempio n. 17
0
        int CallMethodFromName(LuaState luaState)
        {
            object targetObject = null;

            if (!_isStatic)
            {
                targetObject = _extractTarget(luaState, 1);
            }

            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 = luaState.GetTop() - numStackToSkip;

            // Cached?
            if (IsMethodCached(luaState, numArgsPassed, numStackToSkip))
            {
                MethodBase method = _lastCalledMethod.cachedMethod;

                if (!luaState.CheckStack(_lastCalledMethod.outList.Length + 6))
                {
                    throw new LuaException("Lua stack overflow");
                }

                FillMethodArguments(luaState, numStackToSkip);

                return(CallInvoke(luaState, method, targetObject));
            }

            // 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));
                    return(1);
                }

                luaState.Remove(1); // Pops the receiver
            }

            bool   hasMatch      = false;
            string candidateName = null;

            foreach (var member in _members)
            {
                if (member.ReflectedType == null)
                {
                    continue;
                }

                candidateName = member.ReflectedType.Name + "." + member.Name;
                bool isMethod = _translator.MatchParameters(luaState, member, _lastCalledMethod, 0);

                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);
                return(1);
            }

            if (_lastCalledMethod.cachedMethod.ContainsGenericParameters)
            {
                return(CallInvokeOnGenericMethod(luaState, (MethodInfo)_lastCalledMethod.cachedMethod, targetObject));
            }

            return(CallInvoke(luaState, _lastCalledMethod.cachedMethod, targetObject));
        }
Esempio n. 18
0
 public bool Equals(LuaState other) => this.State == other;
Esempio n. 19
0
 public LuaThread(int reference, Lua interpreter) : base(reference, interpreter)
 {
     _luaState   = interpreter.GetThreadState(reference);
     _translator = interpreter.Translator;
 }
Esempio n. 20
0
 public object ExtractGenerated(LuaState luaState, int stackPos)
 {
     return(CodeGeneration.Instance.GetClassInstance(_klass, _translator.GetTable(luaState, stackPos)));
 }
Esempio n. 21
0
        public void Remove(LuaState luaState)
        {
            ObjectTranslator translator;

            translators.TryRemove(luaState, out translator);
        }
        /*
         * Calls the method. Receives the arguments from the Lua stack
         * and returns values in it.
         */
        int Call(IntPtr state)
        {
            var luaState = LuaState.FromIntPtr(state);

            MethodBase methodToCall = _method;
            object     targetObject = _target;

            bool failedCall    = true;
            int  nReturnValues = 0;

            if (!luaState.CheckStack(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  = luaState.GetTop() - numStackToSkip;
                    MethodBase method         = _lastCalledMethod.cachedMethod;

                    if (numArgsPassed == _lastCalledMethod.argTypes.Length)
                    { // No. of args match?
                        if (!luaState.CheckStack(_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;


                                if (_lastCalledMethod.argTypes[i].IsParamsArray)
                                {
                                    int   count      = _lastCalledMethod.argTypes.Length - i;
                                    Array paramArray = _translator.TableToArray(luaState, type.ExtractValue, type.ParamsArrayType, index, count);
                                    args[_lastCalledMethod.argTypes[i].Index] = paramArray;
                                }
                                else
                                {
                                    args[type.Index] = type.ExtractValue(luaState, index);
                                }

                                if (_lastCalledMethod.args[_lastCalledMethod.argTypes[i].Index] == null &&
                                    !luaState.IsNil(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
                            if (_translator.interpreter.UseTraceback)
                            {
                                e.GetBaseException().Data["Traceback"] = _translator.interpreter.GetDebugTraceback();
                            }
                            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));
                            luaState.PushNil();
                            return(1);
                        }

                        luaState.Remove(1); // Pops the receiver
                    }

                    bool   hasMatch      = false;
                    string candidateName = null;

                    foreach (var member in _members)
                    {
                        candidateName = member.ReflectedType.Name + "." + member.Name;
                        bool isMethod = _translator.MatchParameters(luaState, member, 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);
                        luaState.PushNil();
                        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 = ((MethodInfo)methodToCall).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");
                        luaState.PushNil();
                        return(1);
                    }
                }
                else
                {
                    if (!methodToCall.IsStatic && !methodToCall.IsConstructor && targetObject == null)
                    {
                        targetObject = _extractTarget(luaState, 1);
                        luaState.Remove(1); // Pops the receiver
                    }

                    if (!_translator.MatchParameters(luaState, methodToCall, ref _lastCalledMethod))
                    {
                        _translator.ThrowError(luaState, "Invalid arguments to method call");
                        luaState.PushNil();
                        return(1);
                    }
                }
            }

            if (failedCall)
            {
                if (!luaState.CheckStack(_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)
                {
                    if (_translator.interpreter.UseTraceback)
                    {
                        e.GetBaseException().Data["Traceback"] = _translator.interpreter.GetDebugTraceback();
                    }
                    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]]);
            }


            //  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);
        }
Esempio n. 23
0
 public LuaSandbox(KeraLua.Lua state)
 {
     State = new Lua(state);
 }
Esempio n. 24
0
 /*
  * Pushes this table into the Lua stack
  */
 internal void Push(LuaState luaState)
 {
     luaState.GetRef(_Reference);
 }