From dcf0cb75ae288f95d6654eeefe30d4a6cf8dba20 Mon Sep 17 00:00:00 2001 From: XeonSquared Date: Mon, 16 Dec 2019 14:37:29 +1100 Subject: [PATCH] made spawnfile queue an event on a process ending, changed the shell to use it --- exec/shell.lua | 22 ++++++++-------------- module/loadfile.lua | 5 +++-- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/exec/shell.lua b/exec/shell.lua index 325d318..802455e 100644 --- a/exec/shell.lua +++ b/exec/shell.lua @@ -1,4 +1,5 @@ local serial = require "serialization" +local event = require "event" print(pcall(function() local shenv = {} function shenv.quit() @@ -21,21 +22,14 @@ setmetatable(shenv,{__index=function(_,k) if _G[k] then return _G[k] elseif fp then - local rqid = string.format("shell-%d",math.random(1,99999)) return function(...) local tA = {...} - local pid = os.spawn(function() computer.pushSignal(rqid,pcall(loadfile(fp),table.unpack(tA))) end,fp) - local tE = {} - repeat - tE = {coroutine.yield()} - if tE[1] == rqid then - table.remove(tE,1) - if tE[1] == true then - table.remove(tE,1) - end - return table.unpack(tE) - end - until not os.taskInfo(pid) + local pid = os.spawnfile(fp,fp,table.unpack(tA)) + local tE = event.pull("process_finished",pid) + if tE[1] == true then + table.remove(tE,1) + end + return table.unpack(tE) end end end}) @@ -51,7 +45,7 @@ while os.getenv("run") do if tResult[1] == true then table.remove(tResult,1) end for k,v in pairs(tResult) do if type(v) == "table" then - print(serial.serialize(v)) + print(serial.serialize(v,true)) else print(v) end diff --git a/module/loadfile.lua b/module/loadfile.lua index b26aeb4..b572cb2 100644 --- a/module/loadfile.lua +++ b/module/loadfile.lua @@ -7,8 +7,9 @@ end function runfile(p,...) -- runs file *p* with arbitrary arguments in the current thread return loadfile(p)(...) end -function os.spawnfile(p,n) -- spawns a new process from file *p* with name *n* - return os.spawn(function() xpcall(loadfile(p),function(e) dprint(e.."\n"..debug.traceback()) end) end,n or p) +function os.spawnfile(p,n,...) -- spawns a new process from file *p* with name *n*, with arguments following *n*. + local tA = {...} + return os.spawn(function() computer.pushSignal("process_finished", os.pid(), pcall(loadfile(p), table.unpack(tA))) end,n or p) end function require(f) -- searches for a library with name *f* and returns what the library returns, if possible local lib = os.getenv("LIB") or "/boot/lib"