Exemple #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="chunk"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public LuaFunction LoadString(string chunk, string name, LuaTable env)
        {
            int oldTop = LuaDLL.lua_gettop(L);

            byte[] bt = Encoding.UTF8.GetBytes(chunk);

            if (LuaDLL.luaL_loadbuffer(L, bt, bt.Length, name) != 0)
            {
                ThrowExceptionFromError(oldTop);
            }

            if (env != null)
            {
                env.push(L);
                LuaDLL.lua_setfenv(L, -2);
            }

            LuaFunction result = translator.getFunction(L, -1);

            translator.popValues(L, oldTop);

            return(result);
        }
Exemple #2
0
        public object[] DoString(string chunk, string chunkName, LuaTable env)
        {
            int oldTop = LuaDLL.lua_gettop(this.L);

            byte[] bytes = Encoding.UTF8.GetBytes(chunk);
            if (LuaDLL.luaL_loadbuffer(this.L, bytes, bytes.Length, chunkName) == 0)
            {
                if (env != null)
                {
                    env.push(this.L);
                    LuaDLL.lua_setfenv(this.L, -2);
                }
                if (LuaDLL.lua_pcall(this.L, 0, -1, 0) == 0)
                {
                    return(this.translator.popValues(this.L, oldTop));
                }
                this.ThrowExceptionFromError(oldTop);
            }
            else
            {
                this.ThrowExceptionFromError(oldTop);
            }
            return(null);
        }
Exemple #3
0
        public int Resume(object[] args, LuaTable env)
        {
            int result = 0;
			int oldTop = LuaDLL.lua_gettop(L);

            // If thread isn't started, it needs to be restarted
            if( start )
            {
				start = false;
                func.push( L );
				
				if (env != null)
	            {
	                env.push(L);
	                LuaDLL.lua_setfenv(L, -2);
	            }
				
                result = resume(args, oldTop);

            }
            // If thread is suspended, resume it
            else if( IsSuspended() )
            {
                result = resume(args, oldTop);
            }

            return result;
        }
Exemple #4
0
        /*
         * Excutes a Lua file and returns all the chunk's return
         * values in an array
         */
        public object[] DoFile(string fileName, LuaTable env)
        {            
            LuaDLL.lua_pushstdcallcfunction(L,tracebackFunction);
            int oldTop=LuaDLL.lua_gettop(L);

            // Load with Unity3D resources            
            byte[] text = LuaStatic.Load(fileName);

            if (text == null)
            {
                ThrowExceptionFromError(oldTop);
            }

            //Encoding.UTF8.GetByteCount(text)
            if (LuaDLL.luaL_loadbuffer(L, text, text.Length, fileName) == 0)
            {
                if (env != null)
                {
                    env.push(L);
                    //LuaDLL.lua_setfenv(L, -1);
                    LuaDLL.lua_setfenv(L, -2);
                }                

                if (LuaDLL.lua_pcall(L, 0, -1, -2) == 0)
                {
                    object[] results = translator.popValues(L, oldTop);
                    LuaDLL.lua_pop(L, 1);
                    return results;
                }
                else
                {
                    ThrowExceptionFromError(oldTop);
                }
            }
            else
            {
                ThrowExceptionFromError(oldTop);
            }

            return null;            // Never reached - keeps compiler happy
        }
Exemple #5
0
        /// <summary>
        /// Executes a Lua chnk and returns all the chunk's return values in an array.
        /// </summary>
        /// <param name="chunk">Chunk to execute</param>
        /// <param name="chunkName">Name to associate with the chunk</param>
        /// <returns></returns>
        public object[] DoString(string chunk, string chunkName, LuaTable env)
        {
            int oldTop = LuaDLL.lua_gettop(L);
            byte[] bt = Encoding.UTF8.GetBytes(chunk);

            if (LuaDLL.luaL_loadbuffer(L, bt, bt.Length, chunkName) == 0)
            {
                if (env != null)
                {
                    env.push(L);
                    //LuaDLL.lua_setfenv(L, -1);
                    LuaDLL.lua_setfenv(L, -2);
                }

                if (LuaDLL.lua_pcall(L, 0, -1, 0) == 0)
                    return translator.popValues(L, oldTop);
                else
                    ThrowExceptionFromError(oldTop);
            }
            else
                ThrowExceptionFromError(oldTop);

            return null;            // Never reached - keeps compiler happy
        }
Exemple #6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="chunk"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public LuaFunction LoadString(string chunk, string name, LuaTable env)
        {
            int oldTop = LuaDLL.lua_gettop(L);
            byte[] bt = Encoding.UTF8.GetBytes(chunk);

            if (LuaDLL.luaL_loadbuffer(L, bt, bt.Length, name) != 0)
                ThrowExceptionFromError(oldTop);

            if (env != null)
            {
                env.push(L);
                LuaDLL.lua_setfenv(L, -2);
            }

            LuaFunction result = translator.getFunction(L, -1);
            translator.popValues(L, oldTop);

            return result;            
        }
Exemple #7
0
 public void SetMetaTable(LuaTable metaTable)
 {
     push(_Interpreter.L);
     metaTable.push(_Interpreter.L);
     LuaDLL.lua_setmetatable(_Interpreter.L, -2);
     LuaDLL.lua_pop(_Interpreter.L, 1);
 }
Exemple #8
0
        /// <summary>
        /// Executes a Lua chnk and returns all the chunk's return values in an array.
        /// </summary>
        /// <param name="chunk">Chunk to execute</param>
        /// <param name="chunkName">Name to associate with the chunk</param>
        /// <returns></returns>
        public object[] DoString(string chunk, string chunkName, LuaTable env)
        {
			//Returns the index of the top element in the stack. Because indices start at 1, 
			//this result is equal to the number of elements in the stack;
			//in particular, 0 means an empty stack.
            int oldTop = LuaDLL.lua_gettop(L);   

            byte[] bt = Encoding.UTF8.GetBytes(chunk);


			//Loads a Lua chunk without running it. 
			//If there are no errors, lua_load pushes the compiled chunk as a Lua function on top of the stack. 
			//Otherwise, it pushes an error message.
            if (LuaDLL.luaL_loadbuffer(L, bt, bt.Length, chunkName) == 0)
            {
                if (env != null)
                {
                    env.push(L);
                    //LuaDLL.lua_setfenv(L, -1);
                    LuaDLL.lua_setfenv(L, -2);
                }

				//However, if there is any error, lua_pcall catches it, pushes a single value on the stack (the error message),and returns an error code. 
				//Like lua_call, lua_pcall always removes the function and its arguments from the stack.
                if (LuaDLL.lua_pcall(L, 0, -1, 0) == 0)
					//Revert to the state before call trunk
                    return translator.popValues(L, oldTop);
                else
                    ThrowExceptionFromError(oldTop);
            }
            else
                ThrowExceptionFromError(oldTop);

            return null;            // Never reached - keeps compiler happy
        }