Use Lua threads to conserve data from tasks

This commit is contained in:
Alessandro Proto 2023-03-07 11:09:10 +01:00
parent 0e4d3c135a
commit d137c46ed9
2 changed files with 21 additions and 11 deletions

View file

@ -96,10 +96,12 @@ public class Machine : IComponent
{ {
var L = Lua.FromIntPtr(state); var L = Lua.FromIntPtr(state);
var task = new Objects.TaskMeta.RuntimeTask("test"); var task = Objects.TaskMeta.Push(L, "test");
task.Fulfill(lk => {
ObjectManager.PushObject(L, task); lk.NewTable();
L.SetMetaTable(Objects.TaskMeta.ObjectType); lk.PushString("value");
lk.SetField(-2, "key");
});
return 1; return 1;
} }

View file

@ -77,13 +77,20 @@ public class TaskMeta : IComponent
{ {
Status = TaskStatus.Succeeded; Status = TaskStatus.Succeeded;
Result = lk;
_game.LuaRuntime.QueueEvent("task_finish", LK => _game.LuaRuntime.QueueEvent("task_finish", LK =>
{ {
LK.PushString(Guid.ToString()); 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(); LK.PushNil();
@ -139,7 +146,7 @@ public class TaskMeta : IComponent
new() new()
{ {
name = "getError", name = "getError",
function = L_GetResult, function = L_GetError,
}, },
new(), new(),
}; };
@ -282,10 +289,11 @@ public class TaskMeta : IComponent
if (task.Status == TaskStatus.Succeeded) if (task.Status == TaskStatus.Succeeded)
{ {
if (task.Result is Action<Lua> lk) if (task.Result is Lua thread)
{ {
// todo: make use of first-generated data // Push copy of value on top and move it to LK
lk(L); thread.PushCopy(-1);
thread.XMove(L, 1);
} }
else else
{ {