Example #1
0
        public bool InnerCall(int args, int errfunc)
        {
            bool ret = ProtectedCall(args, errfunc);

            LuaNativeMethods.lua_remove(VariablePointer, errfunc);
            return(ret);
        }
Example #2
0
        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(statePointer);

            if (LuaNativeMethods.luaL_loadbuffer(statePointer, bytes, bytes.Length, fn) == 0)
            {
                if (LuaNativeMethods.lua_pcall(statePointer, 0, LuaNativeMethods.LUAMultRet, errfunc) != 0)
                {
                    LuaNativeMethods.lua_pop(statePointer, 2);
                    return(false);
                }

                LuaNativeMethods.lua_remove(statePointer, errfunc); // pop error function
                ret = TopObjects(errfunc - 1);
                return(true);
            }

            string err = LuaNativeMethods.lua_tostring(statePointer, -1);

            LuaNativeMethods.lua_pop(statePointer, 2);
            throw new Exception("File " + fn + ": " + err);
        }
Example #3
0
        public static int GetOpFunction(IntPtr ptr, string f, string tip)
        {
            int err = PushTry(ptr);

            CheckLuaObject(ptr, 1);

            while (!LuaNativeMethods.lua_isnil(ptr, -1))
            {
                LuaNativeMethods.lua_getfield(ptr, -1, f);
                if (!LuaNativeMethods.lua_isnil(ptr, -1))
                {
                    LuaNativeMethods.lua_remove(ptr, -2);
                    break;
                }

                LuaNativeMethods.lua_pop(ptr, 1);     // pop nil
                LuaNativeMethods.lua_getfield(ptr, -1, "__parent");
                LuaNativeMethods.lua_remove(ptr, -2); // pop base
            }

            if (LuaNativeMethods.lua_isnil(ptr, -1))
            {
                LuaNativeMethods.lua_pop(ptr, 1);
                throw new Exception(string.Format("No {0} operator", tip));
            }

            return(err);
        }
Example #4
0
            public void Dispose()
            {
                if (iterPhase == 1)
                {
                    LuaNativeMethods.lua_pop(table.VariablePointer, 2);
                }

                LuaNativeMethods.lua_remove(table.VariablePointer, tableIndex);
            }
Example #5
0
        public static void ProtectedCall(IntPtr ptr, LuaCSFunction f)
        {
            int err = LuaObject.PushTry(ptr);

            LuaNativeMethods.lua_pushcfunction(ptr, f);
            if (LuaNativeMethods.lua_pcall(ptr, 0, 0, err) != 0)
            {
                LuaNativeMethods.lua_pop(ptr, 1);
            }

            LuaNativeMethods.lua_remove(ptr, err);
        }
Example #6
0
        public static int LuaUnaryOp(IntPtr ptr, string f, string tip)
        {
            int err = GetOpFunction(ptr, f, tip);

            LuaNativeMethods.lua_pushvalue(ptr, 1);
            if (LuaNativeMethods.lua_pcall(ptr, 1, 1, err) != 0)
            {
                LuaNativeMethods.lua_pop(ptr, 1);
            }

            LuaNativeMethods.lua_remove(ptr, err);
            PushValue(ptr, true);
            LuaNativeMethods.lua_insert(ptr, -2);
            return(2);
        }
Example #7
0
        public static int Import(IntPtr ptr)
        {
            try
            {
                LuaNativeMethods.luaL_checktype(ptr, 1, LuaTypes.TYPE_STRING);
                string str = LuaNativeMethods.lua_tostring(ptr, 1);

                string[] ns = str.Split('.');

                LuaNativeMethods.lua_pushglobaltable(ptr);

                for (int n = 0; n < ns.Length; n++)
                {
                    LuaNativeMethods.lua_getfield(ptr, -1, ns[n]);
                    if (!LuaNativeMethods.lua_istable(ptr, -1))
                    {
                        return(LuaObject.Error(ptr, "expect {0} is type table", ns));
                    }

                    LuaNativeMethods.lua_remove(ptr, -2);
                }

                LuaNativeMethods.lua_pushnil(ptr);
                while (LuaNativeMethods.lua_next(ptr, -2) != 0)
                {
                    string key = LuaNativeMethods.lua_tostring(ptr, -2);
                    LuaNativeMethods.lua_getglobal(ptr, key);
                    if (!LuaNativeMethods.lua_isnil(ptr, -1))
                    {
                        LuaNativeMethods.lua_pop(ptr, 1);
                        return(LuaObject.Error(ptr, "{0} had existed, import can't overload it.", key));
                    }

                    LuaNativeMethods.lua_pop(ptr, 1);
                    LuaNativeMethods.lua_setglobal(ptr, key);
                }

                LuaNativeMethods.lua_pop(ptr, 1);

                LuaObject.PushValue(ptr, true);
                return(1);
            }
            catch (Exception e)
            {
                return(LuaObject.Error(ptr, e));
            }
        }
Example #8
0
        public object GetObject(string[] remainingPath)
        {
            object returnValue = null;

            for (int i = 0; i < remainingPath.Length; i++)
            {
                LuaNativeMethods.lua_pushstring(statePointer, remainingPath[i]);
                LuaNativeMethods.lua_gettable(statePointer, -2);
                returnValue = this.GetObject(statePointer, -1);
                LuaNativeMethods.lua_remove(statePointer, -2);
                if (returnValue == null)
                {
                    break;
                }
            }

            return(returnValue);
        }
Example #9
0
        public static void NewTypeTable(IntPtr ptr, string name)
        {
            string[] subt = name.Split('.');

            LuaNativeMethods.lua_pushglobaltable(ptr);

            foreach (string t in subt)
            {
                LuaNativeMethods.lua_pushstring(ptr, t);
                LuaNativeMethods.lua_rawget(ptr, -2);
                if (LuaNativeMethods.lua_isnil(ptr, -1))
                {
                    LuaNativeMethods.lua_pop(ptr, 1);
                    LuaNativeMethods.lua_createtable(ptr, 0, 0);
                    LuaNativeMethods.lua_pushstring(ptr, t);
                    LuaNativeMethods.lua_pushvalue(ptr, -2);
                    LuaNativeMethods.lua_rawset(ptr, -4);
                }

                LuaNativeMethods.lua_remove(ptr, -2);
            }
        }
Example #10
0
        public static int ErrorReport(IntPtr ptr)
        {
            LuaNativeMethods.lua_getglobal(ptr, "debug");
            LuaNativeMethods.lua_getfield(ptr, -1, "traceback");
            LuaNativeMethods.lua_pushvalue(ptr, 1);
            LuaNativeMethods.lua_pushnumber(ptr, 2);
            LuaNativeMethods.lua_call(ptr, 2, 1);
            LuaNativeMethods.lua_remove(ptr, -2);
            string error = LuaNativeMethods.lua_tostring(ptr, -1);

            LuaNativeMethods.lua_pop(ptr, 1);

            Logger.LogError(error, true);
            if (ErrorEvent != null)
            {
                ErrorEvent(error);
            }

            LuaNativeMethods.lua_getglobal(ptr, "dumpstack");
            LuaNativeMethods.lua_call(ptr, 0, 0);

            return(0);
        }