示例#1
0
        /// <summary>
        /// Creates a function.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="status"></param>
        /// <param name="factory"></param>
        /// <returns>The new function. If an error is thrown while creating the function, null is returned
        /// and status is set accordingly (if applicable).</returns>
        public static LuaFunction CreateFunction(string source, ref LuaStatus status, Lua factory)
        {
            var state            = factory.State;
            var global           = (LuaTable)factory.GetObjectFromPath("_G");
            var globalKeysBefore = (ICollection <object>)global.Keys;

            // compiles and pushes to the stack as a function
            // if theres an error then it pushes that error to the stack
            // we need to clean that up if that happens
            status = state.LoadString(source);
            if (status != LuaStatus.OK)
            {
                state.Pop(1);
                return(null);
            }
            status = state.PCall(0, 0, 0);             // run the function on the stack, ie the chunk
            if (status != LuaStatus.OK)
            {
                return(null);
            }

            var globalKeysAfter = (ICollection <object>)global.Keys;
            var difference      = Enumerable.Except(globalKeysAfter, globalKeysBefore);

            Console.WriteLine(difference.Count());
            var first = difference.First();

            Console.WriteLine(first);
            return((LuaFunction)global[first]);
        }
示例#2
0
        public void CompileAndRunString()
        {
            state = new Lua();

            LuaStatus result = state.LoadString("x=1");

            stackDump(state);

            state.Call(0, 0);
            stackDump(state);

            result = state.LoadString("1");
            stackDump(state);

            state.PCall(0, 0, 0);
            stackDump(state);

            result = state.LoadString("print(x)"); // stdout??
            stackDump(state);

            state.Call(0, 0);
            stackDump(state);

            state.Close();
        }
示例#3
0
 static LuaStatus dochunk(ILuaState L, LuaStatus status)
 {
     if (status == LuaStatus.OK)
     {
         status = docall(L, 0, 0);
     }
     return(report(L, status));
 }
示例#4
0
 /*
 ** Check whether 'status' is not OK and, if so, prints the error
 ** message on the top of the stack. It assumes that the error object
 ** is a string, as it was either generated by Lua or by 'msghandler'.
 */
 static LuaStatus report(ILuaState L, LuaStatus status)
 {
     if (status != LuaStatus.OK)
     {
         String msg = L.ToString(-1);
         l_message(L, progname, msg);
         L.Pop(1);  /* remove message */
     }
     return(status);
 }
示例#5
0
        public void ResumeAcceptsNull()
        {
            using (var lua = new Lua())
            {
                lua.LoadString("hello = 1");
                LuaStatus result = lua.Resume(null, 0);

                Assert.AreEqual(LuaStatus.OK, result);
            }
        }
示例#6
0
        public LuaState(bool loadLibs = true)
        {
            lua_State = CApi.luaL_newstate();

            if (loadLibs)
            {
                CApi.luaL_openlibs(lua_State);
            }

            Status = LuaStatus.Ok;
        }
示例#7
0
        public void LuaScript_smoketest()
        {
            state = new Lua();
            LuaStatus result = state.LoadFile(GetScriptPath("smoketest"));

            Assert.AreEqual(LuaStatus.OK, result);

            result = state.PCall(0, -1, 0);
            Assert.AreEqual(LuaStatus.OK, result);

            state.Close();
        }
示例#8
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));
        }
示例#9
0
 /*
 ** Check whether 'status' signals a syntax error and the error
 ** message at the top of the stack ends with the above mark for
 ** incomplete statements.
 */
 static int incomplete(ILuaState L, LuaStatus status)
 {
     if (status == LuaStatus.ErrSyntax)
     {
         int    lmsg;
         String msg = L.ToLString(-1, out lmsg);
         //if (lmsg >= marklen && String.Compare(msg + lmsg - marklen, EOFMARK) == 0)
         if (lmsg >= marklen && msg.EndsWith(EOFMARK))
         {
             L.Pop(1);
             return(1);
         }
     }
     return(0);  /* else... */
 }
示例#10
0
        /*
        ** Interface to 'lua_pcall', which sets appropriate message function
        ** and C-signal handler. Used to run all chunks.
        */
        static LuaStatus docall(ILuaState L, int narg, int nres)
        {
            LuaStatus status = 0;
            int       base_  = L.GetTop() - narg; /* function index */

            L.PushFunction(msghandler);           /* push message handler */
            L.Insert(base_);                      /* put it under function and args */
            globalL = L;                          /* to be available to 'laction' */
            Console.CancelKeyPress += Console_CancelKeyPress;
            //signal(SIGINT, laction);  /* set C-signal handler */
            status = L.PCall(narg, nres, base_);
            //signal(SIGINT, SIG_DFL); /* reset C-signal handler */
            Console.CancelKeyPress -= Console_CancelKeyPress;
            L.Remove(base_);  /* remove message handler from the stack */
            return(status);
        }
示例#11
0
 private void UpdateLua(string source)
 {
     Status = LuaState.State.LoadString(source);
     if (Status == LuaStatus.OK)
     {
         Status = LuaState.State.PCall(0, -1, 0);
     }
     if (Status != LuaStatus.OK)
     {
         OnLuaError(this, LuaState);
     }
     else
     {
         OnLuaChanged(this, LuaState);
     }
 }
示例#12
0
 /*
 ** Read multiple lines until a complete Lua statement
 */
 static LuaStatus multiline(ILuaState L)
 {
     for (; ;)
     {                                                         /* repeat until gets a complete statement */
         int       len;
         String    line   = L.ToLString(1, out len);           /* get what it has */
         LuaStatus status = L.LoadBuffer(line, len, "=stdin"); /* try it */
         if (0 == incomplete(L, status) || 0 == pushline(L, false))
         {
             lua_saveline(L, line); /* keep history */
             return(status);        /* cannot or should not try to add continuation line */
         }
         L.PushLiteral("\n");       /* add newline... */
         L.Insert(-2);              /* ...between the two lines */
         L.Concat(3);               /* join them */
     }
 }
示例#13
0
        void AssertString(string chunk, Lua state)
        {
            string error = string.Empty;

            LuaStatus result = state.LoadString(chunk);

            if (result != LuaStatus.OK)
            {
                error = state.ToString(1, false);
            }

            Assert.True(result == LuaStatus.OK, "Fail loading string: " + chunk + "ERROR:" + error);

            result = state.PCall(0, -1, 0);

            if (result != 0)
            {
                error = state.ToString(1, false);
            }

            Assert.True(result == 0, "Fail calling chunk: " + chunk + " ERROR: " + error);
        }
示例#14
0
        void AssertFile(string path)
        {
            string error = string.Empty;

            LuaStatus result = state.LoadFile(path);

            if (result != LuaStatus.OK)
            {
                error = state.ToString(1);
            }

            Assert.True(result == LuaStatus.OK, "Fail loading file: " + path + "ERROR:" + error);

            result = state.PCall(0, -1, 0);

            if (result != LuaStatus.OK)
            {
                error = state.ToString(1);
            }

            Assert.True(result == LuaStatus.OK, "Fail calling file: " + path + " ERROR: " + error);
        }
示例#15
0
        public void PanicError() // PANIC: unprotected error in call to Lua API (attempt to call a string value)
        {
            state = new Lua();

            LuaFunction oldPanic = state.AtPanic(MyPanicFunc);

            stackDump(state);

            LuaStatus result = state.LoadString("1");

            stackDump(state);

            try
            {
                state.Call(0, 0);
                Assert.Fail("Shoudln't go so far");
            }
            catch (MyPanicException ex)
            { }
            stackDump(state);

            state.Close();
        }
示例#16
0
文件: Program.cs 项目: ygrenier/LuaN
 static LuaStatus dochunk(ILuaState L, LuaStatus status)
 {
     if (status == LuaStatus.OK) status = docall(L, 0, 0);
     return report(L, status);
 }
示例#17
0
文件: Program.cs 项目: ygrenier/LuaN
 /*
 ** Check whether 'status' is not OK and, if so, prints the error
 ** message on the top of the stack. It assumes that the error object
 ** is a string, as it was either generated by Lua or by 'msghandler'.
 */
 static LuaStatus report(ILuaState L, LuaStatus status)
 {
     if (status != LuaStatus.OK)
     {
         String msg = L.ToString(-1);
         l_message(L, progname, msg);
         L.Pop(1);  /* remove message */
     }
     return status;
 }
示例#18
0
 /// <summary>
 /// Creates a function given source and sets status ref.
 /// </summary>
 public static LuaFunction CreateFunction(string source, ref LuaStatus status) =>
 CreateFunction(source, ref status, defaultLuaFactory);
示例#19
0
 /// <summary>
 /// Creates a function from whatever is in the GuiElementTextBase and sets status ref.
 /// </summary>
 public static LuaFunction CreateFunctionFromTextField(
     GuiElementTextBase field, ref LuaStatus status
     ) => CreateFunction(field.GetText(), ref status);
示例#20
0
文件: Program.cs 项目: ygrenier/LuaN
 /*
 ** Check whether 'status' signals a syntax error and the error
 ** message at the top of the stack ends with the above mark for
 ** incomplete statements.
 */
 static int incomplete(ILuaState L, LuaStatus status)
 {
     if (status == LuaStatus.ErrSyntax)
     {
         int lmsg;
         String msg = L.ToLString(-1, out lmsg);
         //if (lmsg >= marklen && String.Compare(msg + lmsg - marklen, EOFMARK) == 0)
         if (lmsg >= marklen && msg.EndsWith(EOFMARK))
         {
             L.Pop(1);
             return 1;
         }
     }
     return 0;  /* else... */
 }