コード例 #1
0
        [TestMethod] public void Native()
        {
            using (var lua = new Lua())
            {
                var       calls  = new List <string>(4);
                LuaThread thread = null;
                lua.RegisterFunction("callback", new Action <string>(s => {
                    calls.Add(s);
                    Assert.AreEqual(LuaCoStatus.Running, thread.Status);
                }));
                using (var body = lua.LoadString(@"
					assert(... == 'start')
					callback('B')
					assert(coroutine.yield('yield') == 'resume')
					callback('D')
					return 'return'
				"                ))
                    thread = lua.NewThread(body);
                using (thread)
                {
                    Assert.AreEqual(LuaCoStatus.Suspended, thread.Status);
                    calls.Add("A");
                    CollectionAssert.AreEqual(new object[] { "yield" }, thread.Resume("start"));
                    Assert.AreEqual(LuaCoStatus.Suspended, thread.Status);
                    calls.Add("C");
                    CollectionAssert.AreEqual(new object[] { "return" }, thread.Resume("resume"));
                    Assert.AreEqual(LuaCoStatus.Dead, thread.Status);
                    calls.Add("E");
                }
                Assert.AreEqual("ABCDE", string.Concat(calls.ToArray()));
            }
        }
コード例 #2
0
 void Update()
 {
     if (thread != null && !thread.IsDead())
     {
         thread.Resume();
     }
 }
コード例 #3
0
        public static void resume(LuaInterop lua)
        {
            LuaThread co = lua.Argument <LuaThread>(0);

            co.BeginResume(lua.ArgumentCount - 1);
            for (int argument = 0; argument < lua.ArgumentCount - 1; ++argument)
            {
                co.ResumeArgument(argument, lua.Argument(argument));
            }

            int resultCount;

            try
            {
                resultCount = co.Resume(LuaThread.VarResult);
            }
            catch (Exception e)
            {
                lua.BeginReturn(2);
                lua.ReturnResult(0, false);
                lua.ReturnResult(1, e.Message);
                lua.EndReturn();
                return;
            }

            lua.BeginReturn(resultCount + 1);
            lua.ReturnResult(0, true);
            for (int result = 0; result < resultCount; ++result)
            {
                lua.ReturnResult(result + 1, co.ResumeResult(result));
            }
            co.EndResume();
            lua.EndReturn();
        }
コード例 #4
0
    private void ResumeThread()
    {
        int ret = -1;

        if (thread != null && thread.Resume(true, out ret) == (int)LuaThreadStatus.LUA_YIELD)
        {
            Debugger.Log("lua yield: " + ret);
        }
    }
コード例 #5
0
 void OnGUI()
 {
     if (GUI.Button(new Rect(10, 10, 120, 40), "Resume Thead"))
     {
         if (thread != null && thread.Resume(true) == (int)LuaThreadStatus.LUA_YIELD)
         {
             object[] objs = thread.GetResult();
             Debugger.Log("lua yield: " + objs[0]);
         }
     }
     else if (GUI.Button(new Rect(10, 60, 120, 40), "Close Thread"))
     {
         if (thread != null)
         {
             thread.Resume(false);
             thread.Dispose();
             thread = null;
         }
     }
 }
コード例 #6
0
ファイル: LuaCoroutines.cs プロジェクト: ChestnutGames/boxing
    // Update is called once per frame
    void Update()
    {
        if (!co.IsDead())
        {
            co.Resume();
        }
        else
        {
            print("Coroutine has exited.");

            // In order to destroy a coroutine (but not the function in lua, just the coroutine stack instance) simply allow C# to clean up the wrapped object
            co = null;
        }
    }
コード例 #7
0
ファイル: TestLuaThread.cs プロジェクト: benbon/tolua
	void Start () 
    {
        state = new LuaState();
        state.Start();
        state.DoString(script);

        LuaFunction func = state.GetFunction("Test");
        func.BeginPCall();
        func.PCall();
        thread = func.CheckLuaThread();
        func.EndPCall();
        func.Dispose();
        func = null;

        thread.Resume(10);
	}
コード例 #8
0
    void Start()
    {
        state = new LuaState();
        state.Start();
        state.DoString(script);

        LuaFunction func = state.GetFunction("Test");

        func.BeginPCall();
        func.PCall();
        thread = func.CheckLuaThread();
        func.EndPCall();
        func.Dispose();
        func = null;

        thread.Resume(10);
    }
コード例 #9
0
    void OnGUI()
    {
        GUI.Label(new Rect(Screen.width / 2 - 300, Screen.height / 2 - 200, 600, 400), tips);

        if (GUI.Button(new Rect(10, 50, 120, 40), "Resume Thead"))
        {
            if (thread != null && thread.Resume(true) == (int)LuaThreadStatus.LUA_YIELD)
            {
                object[] objs = thread.GetResult();
                Debugger.Log("lua yield: " + objs[0]);
            }
        }
        else if (GUI.Button(new Rect(10, 150, 120, 40), "Close Thread"))
        {
            if (thread != null)
            {
                thread.Dispose();
                thread = null;
            }
        }
    }
コード例 #10
0
    void Start()
    {
        Application.logMessageReceived += ShowTips;
        new LuaResLoader();
        state = new LuaState();
        state.Start();
        state.LogGC = true;
        state.DoString(script);

        LuaFunction func = state.GetFunction("Test");

        func.BeginPCall();
        func.PCall();
        thread      = func.CheckLuaThread();
        thread.name = "LuaThread";
        func.EndPCall();
        func.Dispose();
        func = null;

        thread.Resume(10);
    }
コード例 #11
0
    void OnGUI()
    {
        GUI.Label(new Rect(Screen.width / 2 - 300, Screen.height / 2 - 200, 600, 400), tips);

        if (GUI.Button(new Rect(10, 50, 120, 40), "Resume Thread"))
        {
            int ret = -1;
            if (thread != null && thread.Resume(true, out ret) == (int)LuaThreadStatus.LUA_YIELD)
            {
                Debugger.Log("lua yield:" + ret);
            }
        }
        else if (GUI.Button(new Rect(10, 150, 120, 40), "Close thread "))
        {
            if (thread != null)
            {
                thread.Dispose();
                thread = null;
            }
        }
    }
コード例 #12
0
            internal override void Call(LuaThread thread, int frameBase, int argumentCount, int resultCount)
            {
                coroutine.BeginResume(argumentCount);

                // Copy arguments.
                for (int argument = 0; argument < argumentCount; ++argument)
                {
                    coroutine.ResumeArgument(argument, thread.Stack[frameBase + 1 + argument]);
                }

                // Resume.
                int actualResultCount = coroutine.Resume(resultCount);

                // Calculate number of results we want.
                int copyCount;

                if (resultCount == -1)
                {
                    copyCount  = actualResultCount;
                    thread.Top = frameBase + actualResultCount - 1;
                }
                else
                {
                    copyCount = Math.Min(resultCount, actualResultCount);
                }

                // Copy results.
                for (int result = 0; result < copyCount; ++result)
                {
                    thread.Stack[frameBase + result] = coroutine.ResumeResult(result);
                }
                for (int result = copyCount; result < resultCount; ++result)
                {
                    thread.Stack[frameBase + result] = null;
                }

                coroutine.EndResume();
            }
コード例 #13
0
    void Start()
    {
#if UNITY_5 || UNITY_2017 || UNITY_2018
        Application.logMessageReceived += ShowTips;
#else
        Application.RegisterLogCallback(ShowTips);
#endif
        new LuaResLoader();
        state = new LuaState();
        state.Start();
        state.LogGC = true;
        state.DoString(script);

        LuaFunction func = state.GetFunction("Test");
        func.BeginPCall();
        func.PCall();
        thread      = func.CheckLuaThread();
        thread.name = "LuaThread";
        func.EndPCall();
        func.Dispose();
        func = null;
        thread.Resume(10);
    }
コード例 #14
0
    private void Start()
    {
        luaState = new LuaState();
        luaState.Start();
        luaState.LogGC = true;//显示GC回收的LOG日志


        var luaScript = Resources.Load <TextAsset>("TestLuaThread.lua");

        luaState.DoString(luaScript.text);

        LuaFunction func = luaState.GetFunction("Test");

        func.BeginPCall();
        func.PCall();
        thread      = func.CheckLuaThread();
        thread.name = "MyLuaThread";
        func.EndPCall();
        func.Dispose();
        func = null;

        thread.Resume(10);
    }
コード例 #15
0
    public bool MoveNext()
    {
        var status = luaThread.Resume();

        return(status == LuaThreadStatus.LUA_YIELD);
    }