Ejemplo n.º 1
0
        /*
         * Resets this thread, cleaning its call stack and closing all pending to-be-closed variables.
         */
        public int Reset()
        {
            int oldTop = _luaState.GetTop();

            int statusCode = _luaState.ResetThread();  /* close its tbc variables */

            _luaState.SetTop(oldTop);
            return(statusCode);
        }
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));
        }