Esempio n. 1
0
        public void Remove(LuaState luaState)
        {
            if (!translators.ContainsKey (luaState))
                return;

            translators.Remove (luaState);
        }
Esempio n. 2
0
        public ObjectTranslator Find(LuaState luaState)
        {
            if (!translators.ContainsKey(luaState))
                return null;

            return translators [luaState];
        }
Esempio n. 3
0
        public ObjectTranslator Find(LuaState luaState)
        {
            if (translators.ContainsKey (luaState))
                return translators [luaState];

            LuaState main = LuaCore.LuaNetGetMainState (luaState);

            if (translators.ContainsKey (main))
                return translators [main];

            return null;
        }
Esempio n. 4
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 = (_BindingType & BindingFlags.Static) == BindingFlags.Static;
            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 = index - _LastCalledMethod.argTypes.Length;
                                    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 ((_BindingType & BindingFlags.Static) == BindingFlags.Static)
                                _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) {
                        candidateName = member.ReflectedType.Name + "." + member.Name;
                        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;
        }
Esempio n. 5
0
 /*
  * Pushes this table into the Lua stack
  */
 internal void Push(LuaState luaState)
 {
     LuaLib.LuaGetRef (luaState, _Reference);
 }
Esempio n. 6
0
 public static void LuaGetField(LuaState luaState, int stackPos, string meta)
 {
     LuaCore.LuaGetField (luaState, stackPos, meta);
 }
Esempio n. 7
0
 public static void LuaError(LuaState luaState)
 {
     LuaCore.LuaError (luaState);
 }
Esempio n. 8
0
 public static void LuaCreateTable(LuaState luaState, int narr, int nrec)
 {
     LuaCore.LuaCreateTable (luaState, narr, nrec);
 }
Esempio n. 9
0
 public static void LuaCall(LuaState luaState, int nArgs, int nResults)
 {
     LuaCore.LuaCall (luaState, nArgs, nResults);
 }
Esempio n. 10
0
 public static void LuaSetTop(LuaState luaState, int newTop)
 {
     LuaCore.LuaSetTop (luaState, newTop);
 }
Esempio n. 11
0
 public static void LuaSetTable(LuaState luaState, int index)
 {
     LuaCore.LuaSetTable (luaState, index);
 }
Esempio n. 12
0
 public static void LuaSetMetatable(LuaState luaState, int objIndex)
 {
     LuaCore.LuaSetMetatable (luaState, objIndex);
 }
Esempio n. 13
0
 public static void LuaReplace(LuaState luaState, int index)
 {
     LuaCore.LuaReplace (luaState, index);
 }
Esempio n. 14
0
 public static int LuaRef(LuaState luaState, int lockRef)
 {
     return lockRef != 0 ? LuaLRef (luaState, (int)LuaIndexes.Registry) : 0;
 }
Esempio n. 15
0
 public static void LuaRawSetI(LuaState luaState, int tableIndex, int index)
 {
     LuaCore.LuaRawSetI (luaState, tableIndex, index);
 }
Esempio n. 16
0
 public static void LuaUnref(LuaState luaState, int reference)
 {
     LuaCore.LuaLUnref (luaState, (int)LuaIndexes.Registry, reference);
 }
Esempio n. 17
0
 public static void LuaAtPanic(LuaState luaState, LuaNativeFunction panicf)
 {
     LuaCore.LuaAtPanic (luaState, (LuaNativeFunction)panicf);
 }
Esempio n. 18
0
 public static bool LuaToBoolean(LuaState luaState, int index)
 {
     return LuaCore.LuaToBoolean (luaState, index) != 0;
 }
Esempio n. 19
0
 public static bool LuaCheckStack(LuaState luaState, int extra)
 {
     return LuaCore.LuaCheckStack (luaState, extra) != 0;
 }
Esempio n. 20
0
 public static LuaNativeFunction LuaToCFunction(LuaState luaState, int index)
 {
     return LuaCore.LuaToCFunction (luaState, index);
 }
Esempio n. 21
0
 public static int LuaEqual(LuaState luaState, int index1, int index2)
 {
     return LuaCore.LuaNetEqual (luaState, index1, index2);
 }
Esempio n. 22
0
 public static double LuaToNumber(LuaState luaState, int index)
 {
     return LuaCore.LuaNetToNumber (luaState, index);
 }
Esempio n. 23
0
 public static int LuaGC(LuaState luaState, GCOptions what, int data)
 {
     return LuaCore.LuaGC (luaState, (int)what, data);
 }
Esempio n. 24
0
        public static string LuaToString(LuaState luaState, int index)
        {
            // FIXME use the same format string as lua i.e. LUA_NUMBER_FMT
            var t = LuaType (luaState, index);

            if (t == LuaTypes.Number)
                return string.Format ("{0}", LuaToNumber (luaState, index));
            else if (t == LuaTypes.String) {
                uint strlen;
                // Changed 2013-05-18 by Dirk Weltz
                // Changed because binary chunks, which are also transfered as strings
                // get corrupted by conversion to strings because of the encoding.
                // So we use the ToString method with string length, so it could be checked,
                // if string is a binary chunk and if, could transfered to string without
                // encoding.
                return LuaCore.LuaToLString (luaState, index, out strlen).ToString ((int)strlen);
            } else if (t == LuaTypes.Nil)
                return null;			// treat lua nulls to as C# nulls
            else
                return "0";	// Because luaV_tostring does this
        }
Esempio n. 25
0
 public static void LuaGetGlobal(LuaState luaState, string name)
 {
     LuaCore.LuaNetGetGlobal (luaState, name);
 }
Esempio n. 26
0
 public static object LuaToUserData(LuaState luaState, int index)
 {
     return LuaCore.LuaToUserData (luaState, index);
 }
Esempio n. 27
0
 public object ExtractGenerated(LuaState luaState, int stackPos)
 {
     return CodeGeneration.Instance.GetClassInstance (klass, translator.GetTable (luaState, stackPos));
 }
Esempio n. 28
0
 public static LuaTypes LuaType(LuaState luaState, int index)
 {
     return (LuaTypes)LuaCore.LuaType (luaState, index);
 }
Esempio n. 29
0
 /*
  * Pushes the function into the Lua stack
  */
 internal void Push(LuaState luaState)
 {
     if (_Reference != 0)
         LuaLib.LuaGetRef (luaState, _Reference);
     else
         _Interpreter.PushCSFunction (function);
 }
Esempio n. 30
0
 public static string LuaTypeName(LuaState luaState, LuaTypes type)
 {
     return LuaCore.LuaTypeName (luaState, (int)type).ToString ();
 }