From 2fad8db8496e8fb5d9d35db8ef79da4470da8edb Mon Sep 17 00:00:00 2001 From: cinder <> Date: Thu, 9 May 2024 03:12:31 -0700 Subject: [PATCH] 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 --- src/kernel/00_preamble.lua | 7 +++---- src/kernel/04_environment.lua | 8 ++++---- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/kernel/00_preamble.lua b/src/kernel/00_preamble.lua index 7fc5e01..21bcfde 100644 --- a/src/kernel/00_preamble.lua +++ b/src/kernel/00_preamble.lua @@ -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 --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 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 kpullSignal, shutdown, uptime = computer.pullSignal, computer.shutdown, computer.uptime local co_create, co_status, kresume, kyield = coroutine.create, coroutine.status, coroutine.resume, coroutine.yield @@ -38,7 +38,7 @@ local checkArgEx do -- tail-recursive varargs part of checkArgEx local function check(val, t, want, ...) - if t == want then + if want == t then return want elseif want == 'integer' or want == 'float' then if math_type(val) == want then @@ -50,9 +50,8 @@ do end elseif not want then return nil - else - return check(val, t, ...) end + return check(val, t, ...) end function checkArgEx(i, val, ...) diff --git a/src/kernel/04_environment.lua b/src/kernel/04_environment.lua index 744637c..75f472d 100644 --- a/src/kernel/04_environment.lua +++ b/src/kernel/04_environment.lua @@ -9,7 +9,7 @@ given : ]] -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._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 return forwardYield(co, kresume(co, kyield(...))) else --`co` called coroutine.yield(), so return its values to the waiting coroutine.resume() - return ... + return ok, select(2, ...) end end end @@ -91,7 +91,7 @@ function fillEnv(proc, uenv) flush = function() return uio.stdout:flush() end, input = function(file) 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) end return uio.stdin @@ -107,7 +107,7 @@ function fillEnv(proc, uenv) end, output = function(file) 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) end return uio.stdout