commit ab2f2461b5ec2fd3a9c0924cd7dcfaa6028b8d21
parent 53536be62a9efe8e641214a3950b904e108bfe28
Author: Lewis Russell <lewis6991@gmail.com>
Date: Thu, 31 Oct 2024 14:25:00 +0000
refactor(loader): simplify Loader.loader_lib
Diffstat:
1 file changed, 15 insertions(+), 13 deletions(-)
diff --git a/runtime/lua/vim/loader.lua b/runtime/lua/vim/loader.lua
@@ -205,25 +205,27 @@ function Loader.loader(modname)
return ("\n\tcache_loader: module '%s' not found"):format(modname)
end
+local is_win = vim.fn.has('win32') == 1
+
--- The `package.loaders` loader for libs
---@param modname string module name
---@return string|function
---@private
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]
- if ret then
- -- 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
- -- b) replace all dots by underscores
- -- c) prepend "luaopen_"
- -- So "foo-bar.baz" should result in "luaopen_bar_baz"
- local dash = modname:find('-', 1, true)
- local funcname = dash and modname:sub(dash + 1) or modname
- local chunk, err = package.loadlib(ret.modpath, 'luaopen_' .. funcname:gsub('%.', '_'))
- return chunk or error(err)
+ local ret = M.find(modname, { patterns = { is_win and '.dll' or '.so' } })[1]
+ if not ret then
+ return ("\n\tcache_loader_lib: module '%s' not found"):format(modname)
end
- return ("\n\tcache_loader_lib: module '%s' not found"):format(modname)
+
+ -- 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
+ -- b) replace all dots by underscores
+ -- c) prepend "luaopen_"
+ -- So "foo-bar.baz" should result in "luaopen_bar_baz"
+ local dash = modname:find('-', 1, true)
+ local funcname = dash and modname:sub(dash + 1) or modname
+ local chunk, err = package.loadlib(ret.modpath, 'luaopen_' .. funcname:gsub('%.', '_'))
+ return chunk or error(err)
end
--- `loadfile` using the cache