mirror of
https://github.com/MightyPirates/OpenComputers.git
synced 2025-09-18 03:36:47 -04:00
properly handling errors in module loaders in package library (avoid lingering entry in "loading" table); throwing an error in robot library if its not a robot; automatically trying to load modules in interpreter (somewhat experimental)
This commit is contained in:
parent
6df7c8aed1
commit
3c1eaf165b
@ -3,9 +3,16 @@ local package = require("package")
|
|||||||
local term = require("term")
|
local term = require("term")
|
||||||
local text = require("text")
|
local text = require("text")
|
||||||
|
|
||||||
|
local function optrequire(...)
|
||||||
|
local success, module = pcall(require, ...)
|
||||||
|
if success then
|
||||||
|
return module
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local history = {}
|
local history = {}
|
||||||
local env = setmetatable({}, {__index = function(t, k)
|
local env = setmetatable({}, {__index = function(t, k)
|
||||||
return _ENV[k] or package.loaded[k]
|
return _ENV[k] or optrequire(k)
|
||||||
end})
|
end})
|
||||||
|
|
||||||
component.gpu.setForeground(0xFFFFFF)
|
component.gpu.setForeground(0xFFFFFF)
|
||||||
|
@ -74,12 +74,12 @@ table.insert(package.searchers, pathSearcher)
|
|||||||
|
|
||||||
function require(module)
|
function require(module)
|
||||||
checkArg(1, module, "string")
|
checkArg(1, module, "string")
|
||||||
if loaded[module] then
|
if loaded[module] ~= nil then
|
||||||
return loaded[module]
|
return loaded[module]
|
||||||
elseif not loading[module] then
|
elseif not loading[module] then
|
||||||
loading[module] = true
|
loading[module] = true
|
||||||
local loader, value, errorMsg = nil, nil, {"module '" .. module .. "' not found:"}
|
local loader, value, errorMsg = nil, nil, {"module '" .. module .. "' not found:"}
|
||||||
for i=1, #package.searchers do
|
for i = 1, #package.searchers do
|
||||||
local f, extra = package.searchers[i](module)
|
local f, extra = package.searchers[i](module)
|
||||||
if f and type(f) ~= "string" then
|
if f and type(f) ~= "string" then
|
||||||
loader = f
|
loader = f
|
||||||
@ -90,20 +90,23 @@ function require(module)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
if loader then
|
if loader then
|
||||||
local result = loader(module, value)
|
local success, result = pcall(loader, module, value)
|
||||||
|
loading[module] = false
|
||||||
|
if not success then
|
||||||
|
error(result, 2)
|
||||||
|
end
|
||||||
if result then
|
if result then
|
||||||
loaded[module] = result
|
loaded[module] = result
|
||||||
elseif not loaded[module] then
|
elseif not loaded[module] then
|
||||||
loaded[module] = true
|
loaded[module] = true
|
||||||
end
|
end
|
||||||
loading[module] = false
|
|
||||||
return loaded[module]
|
return loaded[module]
|
||||||
else
|
else
|
||||||
loading[module] = false
|
loading[module] = false
|
||||||
error(table.concat(errorMsg, "\n"))
|
error(table.concat(errorMsg, "\n"), 2)
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
error("already loading: " .. module)
|
error("already loading: " .. module, 2)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@ local computer = require("computer")
|
|||||||
local sides = require("sides")
|
local sides = require("sides")
|
||||||
|
|
||||||
if not computer.isRobot() then
|
if not computer.isRobot() then
|
||||||
return nil, "not a robot"
|
error("not a robot")
|
||||||
end
|
end
|
||||||
|
|
||||||
local robot = {}
|
local robot = {}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user