made spawnfile queue an event on a process ending, changed the shell to use it

This commit is contained in:
Izaya 2019-12-16 14:37:29 +11:00
parent 8404946eba
commit dcf0cb75ae
2 changed files with 11 additions and 16 deletions

View File

@ -1,4 +1,5 @@
local serial = require "serialization" local serial = require "serialization"
local event = require "event"
print(pcall(function() print(pcall(function()
local shenv = {} local shenv = {}
function shenv.quit() function shenv.quit()
@ -21,22 +22,15 @@ setmetatable(shenv,{__index=function(_,k)
if _G[k] then if _G[k] then
return _G[k] return _G[k]
elseif fp then elseif fp then
local rqid = string.format("shell-%d",math.random(1,99999))
return function(...) return function(...)
local tA = {...} local tA = {...}
local pid = os.spawn(function() computer.pushSignal(rqid,pcall(loadfile(fp),table.unpack(tA))) end,fp) local pid = os.spawnfile(fp,fp,table.unpack(tA))
local tE = {} local tE = event.pull("process_finished",pid)
repeat
tE = {coroutine.yield()}
if tE[1] == rqid then
table.remove(tE,1)
if tE[1] == true then if tE[1] == true then
table.remove(tE,1) table.remove(tE,1)
end end
return table.unpack(tE) return table.unpack(tE)
end end
until not os.taskInfo(pid)
end
end end
end}) end})
print(_VERSION) print(_VERSION)
@ -51,7 +45,7 @@ while os.getenv("run") do
if tResult[1] == true then table.remove(tResult,1) end if tResult[1] == true then table.remove(tResult,1) end
for k,v in pairs(tResult) do for k,v in pairs(tResult) do
if type(v) == "table" then if type(v) == "table" then
print(serial.serialize(v)) print(serial.serialize(v,true))
else else
print(v) print(v)
end end

View File

@ -7,8 +7,9 @@ end
function runfile(p,...) -- runs file *p* with arbitrary arguments in the current thread function runfile(p,...) -- runs file *p* with arbitrary arguments in the current thread
return loadfile(p)(...) return loadfile(p)(...)
end end
function os.spawnfile(p,n) -- spawns a new process from file *p* with name *n* function os.spawnfile(p,n,...) -- spawns a new process from file *p* with name *n*, with arguments following *n*.
return os.spawn(function() xpcall(loadfile(p),function(e) dprint(e.."\n"..debug.traceback()) end) end,n or p) local tA = {...}
return os.spawn(function() computer.pushSignal("process_finished", os.pid(), pcall(loadfile(p), table.unpack(tA))) end,n or p)
end end
function require(f) -- searches for a library with name *f* and returns what the library returns, if possible 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" local lib = os.getenv("LIB") or "/boot/lib"