Ejemplo n.º 1
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));
        }
Ejemplo n.º 2
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));
        }