mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-11-20 23:34:48 +00:00
Expose expect as a module (#267)
This moves expect from the bios into a new craftos.expect module, removing the internal _G["~expect"] definition. Apparently people were using this irrespective of the "don't use this" comment, so we need to find another solution. While this does introduce some ugliness (having to load the module in weird ways for programs, duplicating the expect function in memory), it does allow people to use the function in a supported way, and removes the global ugliness.
This commit is contained in:
@@ -1,48 +1,19 @@
|
||||
local native_select, native_type = select, type
|
||||
|
||||
--- Expect an argument to have a specific type.
|
||||
-- Load in expect from the module path.
|
||||
--
|
||||
-- @tparam int index The 1-based argument index.
|
||||
-- @param value The argument's value.
|
||||
-- @tparam string ... The allowed types of the argument.
|
||||
-- @throws If the value is not one of the allowed types.
|
||||
local function expect(index, value, ...)
|
||||
local t = native_type(value)
|
||||
for i = 1, native_select("#", ...) do
|
||||
if t == native_select(i, ...) then return true end
|
||||
end
|
||||
-- Ideally we'd use require, but that is part of the shell, and so is not
|
||||
-- available to the BIOS or any APIs. All APIs load this using dofile, but that
|
||||
-- has not been defined at this point.
|
||||
local expect
|
||||
|
||||
local types = table.pack(...)
|
||||
for i = types.n, 1, -1 do
|
||||
if types[i] == "nil" then table.remove(types, i) end
|
||||
end
|
||||
do
|
||||
local h = fs.open("rom/modules/main/craftos/expect.lua", "r")
|
||||
local f, err = loadstring(h.readAll(), "@expect.lua")
|
||||
h.close()
|
||||
|
||||
local type_names
|
||||
if #types <= 1 then
|
||||
type_names = tostring(...)
|
||||
else
|
||||
type_names = table.concat(types, ", ", 1, #types - 1) .. " or " .. types[#types]
|
||||
end
|
||||
|
||||
-- If we can determine the function name with a high level of confidence, try to include it.
|
||||
local name
|
||||
if native_type(debug) == "table" and native_type(debug.getinfo) == "function" then
|
||||
local ok, info = pcall(debug.getinfo, 3, "nS")
|
||||
if ok and info.name and #info.name ~= "" and info.what ~= "C" then name = info.name end
|
||||
end
|
||||
|
||||
if name then
|
||||
error( ("bad argument #%d to '%s' (expected %s, got %s)"):format(index, name, type_names, t), 3 )
|
||||
else
|
||||
error( ("bad argument #%d (expected %s, got %s)"):format(index, type_names, t), 3 )
|
||||
end
|
||||
if not f then error(err) end
|
||||
expect = f().expect
|
||||
end
|
||||
|
||||
-- We expose expect in the global table as APIs need to access it, but give it
|
||||
-- a non-identifier name - meaning it does not show up in auto-completion.
|
||||
-- expect is an internal function, and should not be used by users.
|
||||
_G["~expect"] = expect
|
||||
|
||||
if _VERSION == "Lua 5.1" then
|
||||
-- If we're on Lua 5.1, install parts of the Lua 5.2/5.3 API so that programs can be written against it
|
||||
local type = type
|
||||
|
||||
Reference in New Issue
Block a user