aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim/loader.lua
diff options
context:
space:
mode:
authorLewis Russell <lewis6991@gmail.com>2024-10-31 14:30:14 +0000
committerLewis Russell <lewis6991@gmail.com>2024-10-31 15:20:29 +0000
commit7ccdd9235a362f75b6d242af8ca0db9c21f968f7 (patch)
tree14d5896c87eb1c6b10f87ea0a40ce4af020b6dc6 /runtime/lua/vim/loader.lua
parentab2f2461b5ec2fd3a9c0924cd7dcfaa6028b8d21 (diff)
downloadrneovim-7ccdd9235a362f75b6d242af8ca0db9c21f968f7.tar.gz
rneovim-7ccdd9235a362f75b6d242af8ca0db9c21f968f7.tar.bz2
rneovim-7ccdd9235a362f75b6d242af8ca0db9c21f968f7.zip
refactor(loader): inline Loader.load into Loader.loadfile
Diffstat (limited to 'runtime/lua/vim/loader.lua')
-rw-r--r--runtime/lua/vim/loader.lua60
1 files changed, 21 insertions, 39 deletions
diff --git a/runtime/lua/vim/loader.lua b/runtime/lua/vim/loader.lua
index 21662668e3..c839707e1d 100644
--- a/runtime/lua/vim/loader.lua
+++ b/runtime/lua/vim/loader.lua
@@ -231,13 +231,30 @@ end
--- `loadfile` using the cache
--- Note this has the mode and env arguments which is supported by LuaJIT and is 5.1 compatible.
---@param filename? string
----@param _mode? "b"|"t"|"bt"
+---@param mode? "b"|"t"|"bt"
---@param env? table
---@return function?, string? error_message
---@private
-function Loader.loadfile(filename, _mode, env)
- -- ignore mode, since we byte-compile the Lua source files
- return Loader.load(normalize(filename), { env = env })
+function Loader.loadfile(filename, mode, env)
+ local modpath = normalize(filename)
+ local hash = Loader.get_hash(modpath)
+ local cname = Loader.cache_file(modpath)
+ if hash then
+ local e_hash, e_chunk = Loader.read(cname)
+ if Loader.eq(e_hash, hash) and e_chunk then
+ -- found in cache and up to date
+ local chunk, err = load(e_chunk, '@' .. modpath, mode, env)
+ if not (err and err:find('cannot load incompatible bytecode', 1, true)) then
+ return chunk, err
+ end
+ end
+ end
+
+ local chunk, err = Loader._loadfile(modpath, mode, env)
+ if chunk then
+ Loader.write(cname, hash, chunk)
+ end
+ return chunk, err
end
--- Checks whether two cache hashes are the same based on:
@@ -255,40 +272,6 @@ function Loader.eq(a, b)
and a.mtime.nsec == b.mtime.nsec
end
---- Loads the given module path using the cache
----@param modpath string
----@param opts? {mode?: "b"|"t"|"bt", env?:table} (table|nil) Options for loading the module:
---- - mode: (string) the mode to load the module with. "b"|"t"|"bt" (defaults to `nil`)
---- - env: (table) the environment to load the module in. (defaults to `nil`)
----@see |luaL_loadfile()|
----@return function?, string? error_message
----@private
-function Loader.load(modpath, opts)
- opts = opts or {}
- local hash = Loader.get_hash(modpath)
- if not hash then
- -- trigger correct error
- return Loader._loadfile(modpath, opts.mode, opts.env)
- end
-
- local cname = Loader.cache_file(modpath)
-
- local e_hash, e_chunk = Loader.read(cname)
- if Loader.eq(e_hash, hash) and e_chunk then
- -- found in cache and up to date
- local chunk, err = load(e_chunk, '@' .. modpath, opts.mode, opts.env)
- if not (err and err:find('cannot load incompatible bytecode', 1, true)) then
- return chunk, err
- end
- end
-
- local chunk, err = Loader._loadfile(modpath, opts.mode, opts.env)
- if chunk then
- Loader.write(cname, hash, chunk)
- end
- return chunk, err
-end
-
--- Finds Lua modules for the given module name.
---
--- @since 0
@@ -501,7 +484,6 @@ function M._profile(opts)
Loader.loader = Loader.track('loader', Loader.loader)
Loader.loader_lib = Loader.track('loader_lib', Loader.loader_lib)
Loader.loadfile = Loader.track('loadfile', Loader.loadfile)
- Loader.load = Loader.track('load', Loader.load)
M.find = Loader.track('find', M.find)
Loader.lsmod = Loader.track('lsmod', Loader.lsmod)