Extended process lib to allow storing data per process, using it for io lib to store processes' stdin and stdout instead of the local hackaround.

This commit is contained in:
Florian Nücke 2014-12-03 14:05:02 +01:00
parent d326e98714
commit e285c5875e
6 changed files with 22 additions and 57 deletions

View File

@ -0,0 +1,2 @@
-- Initialize process module.
require("process").install("/init.lua", "init")

View File

@ -143,9 +143,6 @@ do
dofile(scripts[i])
end
-- Initialize process module.
require("process").install("/init.lua", "init")
status("Initializing components...")
local primaries = {}

View File

@ -1,45 +1,4 @@
local io, file = {}, {}
local input, output
local programs = setmetatable({}, {__mode="k"}) -- maps program envs to i/o
local function findOverride(filter)
local override
pcall(function()
for level = 1, math.huge do
local path, env = require("process").running(level)
if not path or override then
return
end
if programs[env] then
override = filter(programs[env])
end
end
end)
return override
end
local function setInput(value)
if not pcall(function()
local path, env = require("process").running()
programs[env] = programs[env] or {}
programs[env].input = value
end)
then
input = value
end
end
local function setOutput(value)
if not pcall(function()
local path, env = require("process").running()
programs[env] = programs[env] or {}
programs[env].output = value
end)
then
output = value
end
end
local io = {}
-------------------------------------------------------------------------------
@ -58,14 +17,13 @@ function io.input(file)
if not result then
error(reason, 2)
end
setInput(result)
elseif io.type(file) then
setInput(file)
else
file = result
elseif not io.type(file) then
error("bad argument #1 (string or file expected, got " .. type(file) .. ")", 2)
end
require("process").info().data.io_input = file
end
return findOverride(function(env) return env.input end) or input
return require("process").info().data.io_input
end
function io.lines(filename, ...)
@ -109,14 +67,13 @@ function io.output(file)
if not result then
error(reason, 2)
end
setOutput(result)
elseif io.type(file) then
setOutput(file)
else
file = result
elseif not io.type(file) then
error("bad argument #1 (string or file expected, got " .. type(file) .. ")", 2)
end
require("process").info().data.io_output = file
end
return findOverride(function(env) return env.output end) or output
return require("process").info().data.io_output
end
-- TODO io.popen = function(prog, mode) end

View File

@ -69,13 +69,21 @@ function process.load(path, env, init, name)
path = path,
command = name,
env = env,
data = setmetatable({}, {__index=process and process.data or nil}),
parent = process,
instances = setmetatable({}, {__mode="v"})
}
return thread
end
function process.running(level)
function process.running(level) -- kept for backwards compat, prefer process.info
local info = process.info(level)
if info then
return process.path, process.env, process.command
end
end
function process.info(level)
level = level or 1
local process = findProcess()
while level > 1 and process do
@ -83,7 +91,7 @@ function process.running(level)
level = level - 1
end
if process then
return process.path, process.env, process.command
return {path=process.path, env=process.env, command=process.command, data=process.data}
end
end
@ -103,6 +111,7 @@ function process.install(path, name)
path = path,
command = name,
env = _ENV,
data = {},
instances = setmetatable({}, {__mode="v"})
}
end