mirror of
https://github.com/Dadido3/noita-mapcap.git
synced 2024-11-22 21:17:33 +00:00
Add cyclic dependency detection
This commit is contained in:
parent
931c4df18a
commit
afaedf9159
@ -44,6 +44,9 @@ package.loaded = package.loaded or {
|
|||||||
|
|
||||||
local oldRequire = require
|
local oldRequire = require
|
||||||
|
|
||||||
|
local recursionSet = {}
|
||||||
|
local recursionLast
|
||||||
|
|
||||||
---Emulated require function in case the Lua API is restricted.
|
---Emulated require function in case the Lua API is restricted.
|
||||||
---It's probably good enough for most usecases.
|
---It's probably good enough for most usecases.
|
||||||
---
|
---
|
||||||
@ -54,6 +57,11 @@ function require(modName)
|
|||||||
-- Check if package was already loaded, return previous result.
|
-- Check if package was already loaded, return previous result.
|
||||||
if package.loaded[modName] ~= nil then return package.loaded[modName] end
|
if package.loaded[modName] ~= nil then return package.loaded[modName] end
|
||||||
|
|
||||||
|
if recursionSet[modName] then
|
||||||
|
error(string.format("Cyclic dependency with module %q called by %q", modName, recursionLast))
|
||||||
|
end
|
||||||
|
recursionSet[modName], recursionLast = true, modName
|
||||||
|
|
||||||
local notFoundStr = ""
|
local notFoundStr = ""
|
||||||
|
|
||||||
-- Check if there is an entry in the preload table.
|
-- Check if there is an entry in the preload table.
|
||||||
@ -63,6 +71,7 @@ function require(modName)
|
|||||||
|
|
||||||
if res == nil then res = true end
|
if res == nil then res = true end
|
||||||
package.loaded[modName] = res
|
package.loaded[modName] = res
|
||||||
|
recursionSet[modName], recursionLast = nil, nil
|
||||||
return res
|
return res
|
||||||
else
|
else
|
||||||
notFoundStr = notFoundStr .. string.format("\tno field package.preload['%s']\n", modName)
|
notFoundStr = notFoundStr .. string.format("\tno field package.preload['%s']\n", modName)
|
||||||
@ -81,6 +90,7 @@ function require(modName)
|
|||||||
else
|
else
|
||||||
if res == nil then res = true end
|
if res == nil then res = true end
|
||||||
package.loaded[modName] = res
|
package.loaded[modName] = res
|
||||||
|
recursionSet[modName], recursionLast = nil, nil
|
||||||
return res
|
return res
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
@ -92,12 +102,14 @@ function require(modName)
|
|||||||
if oldRequire then
|
if oldRequire then
|
||||||
local ok, res = pcall(oldRequire, modName)
|
local ok, res = pcall(oldRequire, modName)
|
||||||
if ok then
|
if ok then
|
||||||
|
recursionSet[modName], recursionLast = nil, nil
|
||||||
return res
|
return res
|
||||||
else
|
else
|
||||||
notFoundStr = notFoundStr .. string.format("\toriginal require:%s", res)
|
notFoundStr = notFoundStr .. string.format("\toriginal require:%s", res)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
recursionSet[modName], recursionLast = nil, nil
|
||||||
error(string.format("module %q not found:\n%s", modName, notFoundStr))
|
error(string.format("module %q not found:\n%s", modName, notFoundStr))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user