bugfixes in coroutines, io.{in,out}put, and checkArgEx

coroutine.resume: the `false` that distinguishes os yields from coroutine.yield() was unintentionally passed back to the caller, and the status of the coroutine wasn't
io.input, io.output: incorrectly testing if the argument is nil caused `io.output()` to set stdout to nil instead of returning it unchanged
checkArgEx: incorrectly bailed early after the first instance of 'integer', 'float', or 'file' that didn't match
main
cinder 6 months ago
parent f0ed0822b8
commit 2fad8db849

@ -8,7 +8,7 @@ local tmpfs = computer.tmpAddress()
--prefix 'k' (for kernel) on any functions userspace will also have a version of to ensure kernelspace --prefix 'k' (for kernel) on any functions userspace will also have a version of to ensure kernelspace
--function usage is intentional (ie prevent accidentally using the kernel's `load` in a userspace `loadfile`) --function usage is intentional (ie prevent accidentally using the kernel's `load` in a userspace `loadfile`)
local envBase = _G --used for creating process environments local envBase = _G --used for creating process environments
local assert, checkArg, error, kload, ipairs, next, pairs, rawequal, rawset, setmetatable, tostring, type = assert, checkArg, error, load, ipairs, next, pairs, rawequal, rawset, setmetatable, tostring, type local assert, checkArg, error, kload, ipairs, next, pairs, rawequal, rawset, select, setmetatable, tostring, type = assert, checkArg, error, load, ipairs, next, pairs, rawequal, rawset, select, setmetatable, tostring, type
local invoke, component_type = component.invoke, component.type local invoke, component_type = component.invoke, component.type
local kpullSignal, shutdown, uptime = computer.pullSignal, computer.shutdown, computer.uptime local kpullSignal, shutdown, uptime = computer.pullSignal, computer.shutdown, computer.uptime
local co_create, co_status, kresume, kyield = coroutine.create, coroutine.status, coroutine.resume, coroutine.yield local co_create, co_status, kresume, kyield = coroutine.create, coroutine.status, coroutine.resume, coroutine.yield
@ -38,7 +38,7 @@ local checkArgEx
do do
-- tail-recursive varargs part of checkArgEx -- tail-recursive varargs part of checkArgEx
local function check(val, t, want, ...) local function check(val, t, want, ...)
if t == want then if want == t then
return want return want
elseif want == 'integer' or want == 'float' then elseif want == 'integer' or want == 'float' then
if math_type(val) == want then if math_type(val) == want then
@ -50,9 +50,8 @@ do
end end
elseif not want then elseif not want then
return nil return nil
else
return check(val, t, ...)
end end
return check(val, t, ...)
end end
function checkArgEx(i, val, ...) function checkArgEx(i, val, ...)

@ -9,7 +9,7 @@ given <https://ocdoc.cil.li/tutorial:custom_oses#what_s_available>:
]] ]]
envBase._OSVERSION = 'kitn 0.2.0-beta.2' --prefer [openloader, openos] "$name $ver" over plan9k "$name/$ver" envBase._OSVERSION = 'kitn 0.2.0-beta.3' --prefer [openloader, openos] "$name $ver" over plan9k "$name/$ver"
envBase.kitn = {} envBase.kitn = {}
envBase._G, envBase.load = nil envBase._G, envBase.load = nil
@ -36,7 +36,7 @@ do
--yield to the scheduler, pass the results (ie signal) back to `co`, then repeat for `co`'s next yield --yield to the scheduler, pass the results (ie signal) back to `co`, then repeat for `co`'s next yield
return forwardYield(co, kresume(co, kyield(...))) return forwardYield(co, kresume(co, kyield(...)))
else --`co` called coroutine.yield(), so return its values to the waiting coroutine.resume() else --`co` called coroutine.yield(), so return its values to the waiting coroutine.resume()
return ... return ok, select(2, ...)
end end
end end
end end
@ -91,7 +91,7 @@ function fillEnv(proc, uenv)
flush = function() return uio.stdout:flush() end, flush = function() return uio.stdout:flush() end,
input = function(file) input = function(file)
local t = checkArgEx(1, file, 'string', 'table', 'nil') local t = checkArgEx(1, file, 'string', 'table', 'nil')
if t ~= 'nil' then if file then
uio.stdin = (t == 'string' and assert(uio.open(file, 'r')) or file) uio.stdin = (t == 'string' and assert(uio.open(file, 'r')) or file)
end end
return uio.stdin return uio.stdin
@ -107,7 +107,7 @@ function fillEnv(proc, uenv)
end, end,
output = function(file) output = function(file)
local t = checkArgEx(1, file, 'string', 'table', 'nil') local t = checkArgEx(1, file, 'string', 'table', 'nil')
if t ~= nil then if file then
uio.stdout = (t == 'string' and assert(uio.open(file, 'w')) or file) uio.stdout = (t == 'string' and assert(uio.open(file, 'w')) or file)
end end
return uio.stdout return uio.stdout

Loading…
Cancel
Save