Пример #1
0
        /// <summary>
        /// load and pcall the trunk
        /// </summary>
        /// <param name="bytes"></param>
        /// <param name="fn"></param>
        /// <param name="ret"></param>
        /// <returns></returns>
        public bool doBuffer(byte[] bytes, string fn, out object ret)
        {
            // ensure no utf-8 bom, LuaJIT can read BOM, but Lua cannot!
            bytes = CleanUTF8Bom(bytes);
            ret   = null;
            int errfunc = LuaObject.pushTry(L);

            if (LuaDLL.luaL_loadbuffer(L, bytes, bytes.Length, fn) == 0)
            {
                if (LuaDLL.lua_pcall(L, 0, LuaDLL.LUA_MULTRET, errfunc) != 0)
                {
                    //first is errorFunc, second is the error object
                    LuaDLL.lua_pop(L, 2);
                    return(false);
                }
                LuaDLL.lua_remove(L, errfunc); // pop error function
                ret = topObjects(errfunc - 1);
                return(true);
            }
            string err = LuaDLL.lua_tostring(L, -1);

            // first is the errorFunc, second is error msg
            LuaDLL.lua_pop(L, 2);
            throw new Exception(err);
        }
Пример #2
0
        public object call()
        {
            int error = LuaObject.pushTry(state.L);

            if (innerCall(0, error))
            {
                return(state.topObjects(error - 1));
            }
            return(null);
        }
Пример #3
0
        public object call(object a1)
        {
            int error = LuaObject.pushTry(state.L);

            LuaObject.pushVar(state.L, a1);
            if (innerCall(1, error))
            {
                return(state.topObjects(error - 1));
            }
            return(null);
        }
Пример #4
0
        internal static void pcall(IntPtr l, LuaCSFunction f)
        {
            int err = LuaObject.pushTry(l);

            LuaDLL.lua_pushcfunction(l, f);
            if (LuaDLL.lua_pcall(l, 0, 0, err) != 0)
            {
                LuaDLL.lua_pop(l, 1);
            }
            LuaDLL.lua_remove(l, err);
        }
Пример #5
0
        // you can add call method with specific type rather than object type to avoid gc alloc, like
        // public object call(int a1,float a2,string a3,object a4)

        // using specific type to avoid type boxing/unboxing

        public object call(float f1)
        {
            int error = LuaObject.pushTry(state.L);

            LuaDLL.lua_pushnumber(L, f1);
            if (innerCall(1, error))
            {
                return(state.topObjects(error - 1));
            }
            return(null);
        }
Пример #6
0
        public object call()
        {
            int error = LuaObject.pushTry(state.L);

            if (innerCall(0, error))
            {
                //return all result from the function result
                return(state.topObjects(error - 1));
            }
            return(null);
        }
Пример #7
0
        public LuaThreadStatus Resume()
        {
            if (!state.isMainThread())
            {
                Logger.LogError("Can't resume lua thread in bg thread");
                return(LuaThreadStatus.LUA_ERRERR);
            }

            yieldResult = null;

            IntPtr L = state.L;

            push(L);
            IntPtr threadL = LuaDLL.lua_tothread(L, -1);

            LuaDLL.lua_pop(L, 1);

            int ret = LuaDLL.lua_resume(threadL, 0);

            if (ret > (int)LuaThreadStatus.LUA_YIELD)
            {
                LuaObject.pushTry(threadL);
                LuaDLL.lua_pushthread(threadL);
                LuaDLL.lua_pushvalue(threadL, -3);                 // error message
                LuaDLL.lua_call(threadL, 2, 0);

                LuaDLL.lua_settop(threadL, 0);
            }
            else
            {
                int top = LuaDLL.lua_gettop(threadL);
                if (top > 0)
                {
                    if (top == 1)
                    {
                        yieldResult = LuaObject.checkVar(threadL, top);
                        LuaDLL.lua_pop(threadL, 1);
                    }
                    else
                    {
                        object[] o = new object[top];
                        for (int n = 1; n <= top; n++)
                        {
                            o [n - 1] = LuaObject.checkVar(threadL, n);
                        }
                        LuaDLL.lua_settop(L, 0);
                        yieldResult = o;
                    }
                }
            }

            return((LuaThreadStatus)ret);
        }
Пример #8
0
        public object call(int a1, GameObject a2)
        {
            int error = LuaObject.pushTry(state.L);

            LuaObject.pushValue(state.L, a1);
            LuaObject.pushVar(state.L, a2);
            if (innerCall(2, error))
            {
                return(state.topObjects(error - 1));
            }
            return(null);
        }
Пример #9
0
        public object call(object a1, float f2)
        {
            int error = LuaObject.pushTry(state.L);

            LuaObject.pushVar(state.L, a1);
            LuaDLL.lua_pushnumber(L, f2);
            if (innerCall(2, error))
            {
                return(state.topObjects(error - 1));
            }
            return(null);
        }
Пример #10
0
        public static int require_bytes(IntPtr l)
        {
            try
            {
                object a1;
                checkType(l, 1, out a1);
                byte[] bytes = null;
                //Debug.LogFormat(" a1={0}  is array = {1}", a1.GetType(), a1 is System.Array);
                if (a1 is TextAsset)
                {
                    bytes = ((TextAsset)a1).bytes;
                }
                else if (a1 is System.Array)
                {
                    bytes = (byte[])a1;
                }
                else if (a1 is string)
                {
                    bytes = System.Text.Encoding.UTF8.GetBytes((string)a1);
                }

                if (bytes != null)
                {
                    int errfunc = LuaObject.pushTry(l);
                    LuaDLL.lua_pushboolean(l, true);

                    if (LuaDLL.luaL_loadbuffer(l, bytes, bytes.Length, "tmp require") == 0)
                    {
                        if (LuaDLL.lua_pcall(l, 0, LuaDLL.LUA_MULTRET, errfunc) != 0)
                        {
                            LuaDLL.lua_pop(l, 2);
                            return(1);
                        }
                        //int top1 = LuaDLL.lua_gettop(l);
                        LuaDLL.lua_remove(l, errfunc); // pop error function
                        int top   = LuaDLL.lua_gettop(l);
                        int nArgs = top - (errfunc - 1);
                        return(nArgs);
                    }
                    string err = LuaDLL.lua_tostring(l, -1);
                    LuaDLL.lua_pop(l, 2);
                    throw new Exception(err);
                }
                pushValue(l, true);
                LuaDLL.lua_pushnil(l);
                return(2);
            }
            catch (Exception e)
            {
                return(error(l, e));
            }
        }
Пример #11
0
        public object[] Call(object a1, object a2, object a3)
        {
            int error = LuaObject.pushTry(state.L);

            LuaObject.pushVar(state.L, a1);
            LuaObject.pushVar(state.L, a2);
            LuaObject.pushVar(state.L, a3);
            if (innerCall(3, error))
            {
                return(state.topObjects(error - 1));
            }
            return(null);
        }
Пример #12
0
        public bool loadBuffer(byte[] bytes, string fn, out object ret)
        {
            ret = null;
            int errfunc = LuaObject.pushTry(L);

            if (LuaDLL.luaL_loadbuffer(L, bytes, bytes.Length, fn) == 0)
            {
                LuaDLL.lua_remove(L, errfunc); // pop error function
                ret = topObjects(errfunc - 1);
                return(true);
            }
            string err = LuaDLL.lua_tostring(L, -1);

            LuaDLL.lua_pop(L, 2);
            throw new Exception(err);
        }
Пример #13
0
        public object call(params object[] args)
        {
            int error = LuaObject.pushTry(state.L);

            for (int n = 0; args != null && n < args.Length; n++)
            {
                LuaObject.pushVar(L, args[n]);
            }

            if (innerCall(args != null ? args.Length : 0, error))
            {
                return(state.topObjects(error - 1));
            }

            return(null);
        }
Пример #14
0
        public object call(object _a1, params object[] _args)
        {
            int error = LuaObject.pushTry(state.L);

            LuaObject.pushVar(state.L, _a1);
            if (_args != null)
            {
                for (int n = 0; n < _args.Length; ++n)
                {
                    LuaObject.pushVar(state.L, _args[n]);
                }
            }

            if (innerCall(1 + (_args != null ? _args.Length : 0), error))
            {
                return(state.topObjects(error - 1));
            }

            return(null);
        }
Пример #15
0
        public int require(string ns)
        {
            IntPtr l = L;

            int top     = LuaDLL.lua_gettop(l);
            int errFunc = LuaObject.pushTry(l);                            // 1

            LuaDLL.lua_getglobal(l, "require");                            // 2
            LuaDLL.lua_pushstring(l, ns);                                  // 3
            int ret = LuaDLL.lua_pcall(l, 1, LuaDLL.LUA_MULTRET, errFunc); // 1

            if (ret == 0)
            {
                LuaDLL.lua_remove(l, errFunc);                  // 0
            }
            else
            {
                LuaDLL.lua_settop(l, top);                  // 0
            }

            return(ret);
        }
Пример #16
0
        public bool doBuffer(byte[] bytes, string fn, out object ret)
        {
            ret = null;
            int errfunc = LuaObject.pushTry(L);

            if (LuaDLL.luaL_loadbuffer(L, bytes, bytes.Length, fn) == 0)
            {
                if (LuaDLL.lua_pcall(L, 0, LuaDLL.LUA_MULTRET, errfunc) != 0)
                {
                    LuaDLL.lua_pop(L, 2);
                    return(false);
                }
                LuaDLL.lua_remove(L, errfunc);                 // pop error function
                ret = topObjects(errfunc - 1);
                return(true);
            }
            string err = LuaDLL.lua_tostring(L, -1);

            Debug.LogError(err);
            LuaDLL.lua_pop(L, 2);
            return(false);
        }
Пример #17
0
        // Token: 0x06024488 RID: 148616 RVA: 0x00CC1F90 File Offset: 0x00CC0190
        private static bool checkType(IntPtr l, int p, out UnityAction <bool> ua)
        {
            LuaDLL.luaL_checktype(l, p, LuaTypes.LUA_TFUNCTION);
            LuaDelegate ld;

            LuaObject.checkType(l, p, out ld);
            if (ld.d != null)
            {
                ua = (UnityAction <bool>)ld.d;
                return(true);
            }
            l  = LuaState.get(l).L;
            ua = delegate(bool v0)
            {
                int num = LuaObject.pushTry(l);
                LuaObject.pushValue(l, v0);
                ld.pcall(1, num);
                LuaDLL.lua_settop(l, num - 1);
            };
            ld.d = ua;
            return(true);
        }
Пример #18
0
        static public int Yield(IntPtr l)
        {
            try
            {
                if (LuaDLL.lua_pushthread(l) == 1)
                {
                    LuaDLL.luaL_error(l, "should put Yield call into lua coroutine.");
                    return(0);
                }
                object y = checkObj(l, 1);

                Action act = () =>
                {
#if LUA_5_3
                    if (LuaDLL.lua_resume(l, IntPtr.Zero, 0) > (int)LuaThreadStatus.LUA_YIELD)
#else
                    if (LuaDLL.lua_resume(l, 0) > (int)LuaThreadStatus.LUA_YIELD)
#endif
                    {
                        LuaObject.pushTry(l);
                        LuaDLL.lua_pushvalue(l, -2);
                        LuaDLL.lua_call(l, 1, 0);
                        LuaDLL.lua_pop(l, 1);
                    }
                };

                mb.StartCoroutine(yieldReturn(y, act));
#if LUA_5_3
                return(LuaDLL.luaS_yield(l, 0));
#else
                return(LuaDLL.lua_yield(l, 0));
#endif
            }
            catch (Exception e)
            {
                LuaDLL.luaL_error(l, e.ToString());
                return(0);
            }
        }
Пример #19
0
        public bool doBuffer(byte[] bytes, string fn, out object ret)
        {
#if GROOT_SLUA_EXTENSION
            // ensure no utf-8 bom, LuaJIT can read BOM, but Lua cannot!
            Boolean detected = CheckAndCleanUTF8BOM(bytes);
            ret = null;
            m_temp_allow_bg_thread = true;
            int errfunc = LuaObject.pushTry(L);
            if (LuaDLL.luaL_loadbuffer(L, bytes, detected ? bytes.Length - 3 : bytes.Length, fn) == 0)
#else
            bytes = CleanUTF8Bom(bytes);
            ret   = null;
            int errfunc = LuaObject.pushTry(L);
            if (LuaDLL.luaL_loadbuffer(L, bytes, bytes.Length, fn) == 0)
#endif
            {
                if (LuaDLL.lua_pcall(L, 0, LuaDLL.LUA_MULTRET, errfunc) != 0)
                {
                    LuaDLL.lua_pop(L, 2);
#if GROOT_SLUA_EXTENSION
                    m_temp_allow_bg_thread = false;
#endif
                    return(false);
                }
                LuaDLL.lua_remove(L, errfunc);                   // pop error function
                ret = topObjects(errfunc - 1);
#if GROOT_SLUA_EXTENSION
                m_temp_allow_bg_thread = false;
#endif
                return(true);
            }
            string err = LuaDLL.lua_tostring(L, -1);
            LuaDLL.lua_pop(L, 2);
#if GROOT_SLUA_EXTENSION
            m_temp_allow_bg_thread = false;
#endif
            throw new Exception(err);
        }
Пример #20
0
        public bool doBuffer(byte[] bytes, string fn, out object ret)
        {
            ret = null;
            int errfunc = LuaObject.pushTry(L);

            if (LuaDLL.luaL_loadbuffer(L, bytes, bytes.Length, fn) == 0)
            {
                if (LuaDLL.lua_pcall(L, 0, LuaDLL.LUA_MULTRET, errfunc) != 0)
                {
                    LogUtil.Debug("doBuffer: " + fn + " failed!");
                    LuaDLL.lua_pop(L, 2);
                    return(false);
                }
                LogUtil.Debug("doBuffer: " + fn + " success!");
                LuaDLL.lua_remove(L, errfunc);                 // pop error function
                ret = topObjects(errfunc - 1);
                return(true);
            }
            string err = LuaDLL.lua_tostring(L, -1);

            LuaDLL.lua_pop(L, 2);
            throw new Exception(err);
        }