Skip to content

Commit

Permalink
OpenOS: implement package.searchers, make package.preload implementat…
Browse files Browse the repository at this point in the history
…ion more conformant
  • Loading branch information
asiekierka committed Apr 7, 2024
1 parent dde8337 commit 31dff6e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 28 deletions.
2 changes: 1 addition & 1 deletion build.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
minecraft.version=1.7.10
forge.version=10.13.4.1614-1.7.10

oc.version=1.8.3-snapshot
oc.version=1.8.4-snapshot

ae2.version=rv2-stable-10
bc.version=7.1.24
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
-- called from /init.lua
local raw_loadfile = ...

_G._OSVERSION = "OpenOS 1.8.3"
_G._OSVERSION = "OpenOS 1.8.4"

-- luacheck: globals component computer unicode _OSVERSION
local component = component
Expand Down
71 changes: 45 additions & 26 deletions src/main/resources/assets/opencomputers/loot/openos/lib/package.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ package.config = "/\n;\n?\n!\n-\n"
package.path = "/lib/?.lua;/usr/lib/?.lua;/home/lib/?.lua;./?.lua;/lib/?/init.lua;/usr/lib/?/init.lua;/home/lib/?/init.lua;./?/init.lua"

local loading = {}

local preload = {}
local searchers = {}

local loaded = {
["_G"] = _G,
Expand All @@ -20,6 +20,7 @@ local loaded = {
}
package.loaded = loaded
package.preload = preload
package.searchers = searchers

function package.searchpath(name, path, sep, rep)
checkArg(1, name, "string")
Expand All @@ -42,43 +43,61 @@ function package.searchpath(name, path, sep, rep)
return subPath
end
end
table.insert(errorFiles, "\tno file '" .. subPath .. "'")
table.insert(errorFiles, "no file '" .. subPath .. "'")
end
return nil, table.concat(errorFiles, "\n")
return nil, table.concat(errorFiles, "\n\t")
end

table.insert(searchers, function(module)
if package.preload[module] then
return package.preload[module]
end

return "no field package.preload['" .. module .. "']"
end)
table.insert(searchers, function(module)
local library, path, status

path, status = package.searchpath(module, package.path)
if not path then
return status
end

library, status = loadfile(path)
if not library then
error("error loading module '%s' from file '%s':\n\t%s", module, path, status)
end

return library, module
end)

function require(module)
checkArg(1, module, "string")
if loaded[module] ~= nil then
return loaded[module]
elseif package.preload[module] then
assert(type(package.preload[module]) == "function", string.format("package.preload for '%s' is not a function", module))
loading[module] = true
local library, status = pcall(package.preload[module], module)
loading[module] = false
assert(library, string.format("module '%s' load failed:\n%s", module, status))
loaded[module] = status
return status
elseif not loading[module] then
local library, status, step

step, library, status = "not found", package.searchpath(module, package.path)

if library then
step, library, status = "loadfile failed", loadfile(library)
end
elseif loading[module] then
error("already loading: " .. module .. "\n" .. debug.traceback(), 2)
else
local library, status, arg
local errors = ""

if library then
loading[module] = true
step, library, status = "load failed", pcall(library, module)
loading[module] = false
if type(searchers) ~= "table" then error("'package.searchers' must be a table") end
for _, searcher in pairs(searchers) do
library, arg = searcher(module)
if type(library) == "function" then break end
if type(library) ~= nil then
errors = errors .. "\n\t" .. tostring(library)
library = nil
end
end
if not library then error(string.format("module '%s' not found:%s", module, errors)) end

assert(library, string.format("module '%s' %s:\n%s", module, step, status))
loading[module] = true
library, status = pcall(library, arg or module)
loading[module] = false
assert(library, string.format("module '%s' load failed:\n%s", module, status))
loaded[module] = status
return status
else
error("already loading: " .. module .. "\n" .. debug.traceback(), 2)
end
end

Expand Down

0 comments on commit 31dff6e

Please sign in to comment.