1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-01-26 00:46:54 +00:00

Make require a little more consistent with Lua 5.1

- Error messages are indented correctly
 - The module's name is passed as the first argument to modules
 - Make error messages match Lua's
This commit is contained in:
SquidDev 2019-01-23 18:50:17 +00:00
parent e41377f862
commit c9f05a2939

View File

@ -63,7 +63,7 @@ local function createShellEnv( sDir )
end end
else else
if #sError > 0 then if #sError > 0 then
sError = sError .. "\n" sError = sError .. "\n "
end end
sError = sError .. "no file '" .. sPath .. "'" sError = sError .. "no file '" .. sPath .. "'"
end end
@ -78,27 +78,24 @@ local function createShellEnv( sDir )
error( "bad argument #1 (expected string, got " .. type( name ) .. ")", 2 ) error( "bad argument #1 (expected string, got " .. type( name ) .. ")", 2 )
end end
if package.loaded[name] == sentinel then if package.loaded[name] == sentinel then
error("Loop detected requiring '" .. name .. "'", 0) error("loop or previous error loading module '" .. name .. "'", 0)
end end
if package.loaded[name] then if package.loaded[name] then
return package.loaded[name] return package.loaded[name]
end end
local sError = "Error loading module '" .. name .. "':" local sError = "module '" .. name .. "' not found:"
for n,searcher in ipairs(package.loaders) do for _, searcher in ipairs(package.loaders) do
local loader, err = searcher(name) local loader = table.pack(searcher(name))
if loader then if loader[1] then
package.loaded[name] = sentinel package.loaded[name] = sentinel
local result = loader( err ) local result = loader[1](name, table.unpack(loader, 2, loader.n))
if result ~= nil then if result == nil then result = true end
package.loaded[name] = result
return result package.loaded[name] = result
else return result
package.loaded[name] = true
return true
end
else else
sError = sError .. "\n" .. err sError = sError .. "\n " .. loader[2]
end end
end end
error(sError, 2) error(sError, 2)