lualoader: rewrite try_include using lfs + dofile

Actual modules get require()'d in, rather than try_include(). All instances
of try_include should be provided with proper hooks/API in the rest of
loader to do the work they need to do, since we can't rely on them to exist.
Convert this now to lfs + dofile since we won't really be treating them as
modules.

lfs is required because dofile will properly throw an error if the file
doesn't exist, which is not in the spirit of 'optionally included'.

Getting out of the pcall game allows us to provide a loader.exit() style
call that backs out to the common bits of loader (autoboot sequence unless
disabled with a loader.setenv("autoboot_delay", "NO")). The most ideal way
identified so far to implement loader.exit() is to throw a special
abort-style error that indicates to the caller in interp_lua that we've not
actually errored out, just continue execution. Otherwise, we have to hack in
logic to bubble up and return from loader.lua without continuing further,
which gets kind of ugly depending on the context in which we're aborting.

A compat shim is provided temporarily in case the executing loader doesn't
yet have loader.lua_path, which was just added in r354246.
This commit is contained in:
Kyle Evans 2019-11-02 04:01:39 +00:00
parent 63172bf68b
commit bac5966e2d
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=354247

View File

@ -69,12 +69,28 @@ end
-- try_include will return the loaded module on success, or false and the error
-- message on failure.
function try_include(module)
local status, ret = pcall(require, module)
-- ret is the module if we succeeded.
if status then
return ret
if module:sub(1, 1) ~= "/" then
local lua_path = loader.lua_paths
-- XXX Temporary compat shim; this should be removed once the
-- loader.lua_path export has sufficiently spread.
if lua_path == nil then
lua_path = "/boot/lua"
end
module = lua_path .. "/" .. module
-- We only attempt to append an extension if an absolute path
-- wasn't specified. This assumes that the caller either wants
-- to treat this like it would require() and specify just the
-- base filename, or they know what they're doing as they've
-- specified an absolute path and we shouldn't impede.
if module:match(".lua$") == nil then
module = module .. ".lua"
end
end
return false, ret
if lfs.attributes(module, "mode") ~= "file" then
return
end
return dofile(module)
end
-- Module exports