From d137c46ed9752751f997a6360468683d0de58eef Mon Sep 17 00:00:00 2001 From: Alessandro Proto Date: Tue, 7 Mar 2023 11:09:10 +0100 Subject: [PATCH] Use Lua threads to conserve data from tasks --- Capy64/Runtime/Libraries/Machine.cs | 10 ++++++---- Capy64/Runtime/Objects/Task.cs | 22 +++++++++++++++------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/Capy64/Runtime/Libraries/Machine.cs b/Capy64/Runtime/Libraries/Machine.cs index 210368c..cc4d54f 100644 --- a/Capy64/Runtime/Libraries/Machine.cs +++ b/Capy64/Runtime/Libraries/Machine.cs @@ -96,10 +96,12 @@ public class Machine : IComponent { var L = Lua.FromIntPtr(state); - var task = new Objects.TaskMeta.RuntimeTask("test"); - - ObjectManager.PushObject(L, task); - L.SetMetaTable(Objects.TaskMeta.ObjectType); + var task = Objects.TaskMeta.Push(L, "test"); + task.Fulfill(lk => { + lk.NewTable(); + lk.PushString("value"); + lk.SetField(-2, "key"); + }); return 1; } diff --git a/Capy64/Runtime/Objects/Task.cs b/Capy64/Runtime/Objects/Task.cs index ad87885..4f8110a 100644 --- a/Capy64/Runtime/Objects/Task.cs +++ b/Capy64/Runtime/Objects/Task.cs @@ -77,13 +77,20 @@ public class TaskMeta : IComponent { Status = TaskStatus.Succeeded; - Result = lk; - _game.LuaRuntime.QueueEvent("task_finish", LK => { LK.PushString(Guid.ToString()); - lk(LK); + // Create Lua thread to store Lua native result data + Result = LK.NewThread(); + var thread = (Lua)Result; + LK.Pop(1); + + lk(thread); + + // Push copy of value on top and move it to LK + thread.PushCopy(-1); + thread.XMove(LK, 1); LK.PushNil(); @@ -139,7 +146,7 @@ public class TaskMeta : IComponent new() { name = "getError", - function = L_GetResult, + function = L_GetError, }, new(), }; @@ -282,10 +289,11 @@ public class TaskMeta : IComponent if (task.Status == TaskStatus.Succeeded) { - if (task.Result is Action lk) + if (task.Result is Lua thread) { - // todo: make use of first-generated data - lk(L); + // Push copy of value on top and move it to LK + thread.PushCopy(-1); + thread.XMove(L, 1); } else {