refactor(loader): simplify Loader.loader_lib

This commit is contained in:
Lewis Russell
2024-10-31 14:25:00 +00:00
parent 53536be62a
commit ab2f2461b5

View File

@@ -205,14 +205,18 @@ function Loader.loader(modname)
return ("\n\tcache_loader: module '%s' not found"):format(modname) return ("\n\tcache_loader: module '%s' not found"):format(modname)
end end
local is_win = vim.fn.has('win32') == 1
--- The `package.loaders` loader for libs --- The `package.loaders` loader for libs
---@param modname string module name ---@param modname string module name
---@return string|function ---@return string|function
---@private ---@private
function Loader.loader_lib(modname) function Loader.loader_lib(modname)
local is_win = vim.fn.has('win32') == 1 local ret = M.find(modname, { patterns = { is_win and '.dll' or '.so' } })[1]
local ret = M.find(modname, { patterns = is_win and { '.dll' } or { '.so' } })[1] if not ret then
if ret then return ("\n\tcache_loader_lib: module '%s' not found"):format(modname)
end
-- Making function name in Lua 5.1 (see src/loadlib.c:mkfuncname) is -- Making function name in Lua 5.1 (see src/loadlib.c:mkfuncname) is
-- a) strip prefix up to and including the first dash, if any -- a) strip prefix up to and including the first dash, if any
-- b) replace all dots by underscores -- b) replace all dots by underscores
@@ -223,8 +227,6 @@ function Loader.loader_lib(modname)
local chunk, err = package.loadlib(ret.modpath, 'luaopen_' .. funcname:gsub('%.', '_')) local chunk, err = package.loadlib(ret.modpath, 'luaopen_' .. funcname:gsub('%.', '_'))
return chunk or error(err) return chunk or error(err)
end end
return ("\n\tcache_loader_lib: module '%s' not found"):format(modname)
end
--- `loadfile` using the cache --- `loadfile` using the cache
--- Note this has the mode and env arguments which is supported by LuaJIT and is 5.1 compatible. --- Note this has the mode and env arguments which is supported by LuaJIT and is 5.1 compatible.