#434 #454 PPM improvements and undefined function overhaul

This commit is contained in:
Mikayla Fischler
2024-03-23 00:26:58 -04:00
parent 1c0f61b3e0
commit 19869416af
12 changed files with 186 additions and 265 deletions

View File

@@ -51,11 +51,13 @@ local function peri_init(iface)
self.device = peripheral.wrap(iface)
end
-- initialization process (re-map)
for key, func in pairs(self.device) do
self.fault_counts[key] = 0
self.device[key] = function (...)
-- create a protected version of a peripheral function call
---@nodiscard
---@param key string function name
---@param func function function
---@return function method protected version of the function
local function protect_peri_function(key, func)
return function (...)
local return_table = table.pack(pcall(func, ...))
local status = return_table[1]
@@ -85,20 +87,24 @@ local function peri_init(iface)
count_str = " [" .. self.fault_counts[key] .. " total faults]"
end
log.error(util.c("PPM: protected ", key, "() -> ", result, count_str))
log.error(util.c("PPM: [@", iface, "] protected ", key, "() -> ", result, count_str))
end
self.fault_counts[key] = self.fault_counts[key] + 1
if result == "Terminated" then
ppm_sys.terminate = true
end
if result == "Terminated" then ppm_sys.terminate = true end
return ACCESS_FAULT
return ACCESS_FAULT, result
end
end
end
-- initialization process (re-map)
for key, func in pairs(self.device) do
self.fault_counts[key] = 0
self.device[key] = protect_peri_function(key, func)
end
-- fault management & monitoring functions
local function clear_fault() self.faulted = false end
@@ -131,12 +137,21 @@ local function peri_init(iface)
local mt = {
__index = function (_, key)
-- try to find the function in case it was added (multiblock formed)
local funcs = peripheral.wrap(iface)
if (type(funcs) == "table") and (type(funcs[key]) == "function") then
-- add this function then return it
self.device[key] = protect_peri_function(key, funcs[key])
log.info(util.c("PPM: [@", iface, "] initialized previously undefined field ", key, "()"))
return self.device[key]
end
-- function still missing, return an undefined function handler
-- note: code should avoid storing functions for multiblocks and instead try to index them again
return (function ()
-- this will continuously be counting calls here as faults
-- unlike other functions, faults here can't be cleared as it is just not defined
if self.fault_counts[key] == nil then
self.fault_counts[key] = 0
end
if self.fault_counts[key] == nil then self.fault_counts[key] = 0 end
-- function failed
self.faulted = true
@@ -151,12 +166,12 @@ local function peri_init(iface)
count_str = " [" .. self.fault_counts[key] .. " total calls]"
end
log.error(util.c("PPM: caught undefined function ", key, "()", count_str))
log.error(util.c("PPM: [@", iface, "] caught undefined function ", key, "()", count_str))
end
self.fault_counts[key] = self.fault_counts[key] + 1
return UNDEFINED_FIELD
return ACCESS_FAULT, UNDEFINED_FIELD
end)
end
}