Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
0
        public static int Loader(IntPtr ptr)
        {
            string fileName = LuaNativeMethods.lua_tostring(ptr, 1);

            byte[] bytes = LoadFile(fileName);
            if (bytes != null)
            {
                if (LuaNativeMethods.luaL_loadbuffer(ptr, bytes, bytes.Length, "@" + fileName) == 0)
                {
                    LuaObject.PushValue(ptr, true);
                    LuaNativeMethods.lua_insert(ptr, -2);
                    return(2);
                }
                else
                {
                    string errstr = LuaNativeMethods.lua_tostring(ptr, -1);
                    return(LuaObject.Error(ptr, errstr));
                }
            }

            LuaObject.PushValue(ptr, true);
            LuaNativeMethods.lua_pushnil(ptr);
            return(2);
        }