Beispiel #1
0
        /*
         * Calls the object as a function with the provided arguments and
         * casting returned values to the types in returnTypes before returning
         * them in an array
         */
        internal object[] CallFunction(object function, object[] args, Type[] returnTypes)
        {
            int nArgs  = 0;
            int oldTop = luaState.GetTop();

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

            translator.Push(luaState, function);

            if (args.Length > 0)
            {
                nArgs = args.Length;

                for (int i = 0; i < args.Length; i++)
                {
                    translator.Push(luaState, args[i]);
                }
            }

            executing = true;

            try
            {
                int errfunction = 0;
                if (UseTraceback)
                {
                    errfunction = PushDebugTraceback(luaState, nArgs);
                    oldTop++;
                }

                LuaStatus error = luaState.PCall(nArgs, -1, errfunction);
                if (error != LuaStatus.OK)
                {
                    ThrowExceptionFromError(oldTop);
                }
            }
            finally
            {
                executing = false;
            }

            if (returnTypes != null)
            {
                return(translator.PopValues(luaState, oldTop, returnTypes));
            }

            return(translator.PopValues(luaState, oldTop));
        }