mirror of
https://github.com/neovim/neovim.git
synced 2025-12-20 05:15:31 +00:00
refactor(loader): use the term stat instead of hash
This commit is contained in:
@@ -2519,8 +2519,8 @@ vim.loader.find({modname}, {opts}) *vim.loader.find()*
|
|||||||
(`table[]`) A list of objects with the following fields:
|
(`table[]`) A list of objects with the following fields:
|
||||||
• {modpath} (`string`) Path of the module
|
• {modpath} (`string`) Path of the module
|
||||||
• {modname} (`string`) Name of the module
|
• {modname} (`string`) Name of the module
|
||||||
• {stat}? (`uv.uv_fs_t`) The fs_stat of the module path. Won't be
|
• {stat}? (`uv.fs_stat.result`) The fs_stat of the module path. Won't
|
||||||
returned for `modname="*"`
|
be returned for `modname="*"`
|
||||||
|
|
||||||
vim.loader.reset({path}) *vim.loader.reset()*
|
vim.loader.reset({path}) *vim.loader.reset()*
|
||||||
WARNING: This feature is experimental/unstable.
|
WARNING: This feature is experimental/unstable.
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ local M = {}
|
|||||||
--- @field modname string
|
--- @field modname string
|
||||||
---
|
---
|
||||||
--- The fs_stat of the module path. Won't be returned for `modname="*"`
|
--- The fs_stat of the module path. Won't be returned for `modname="*"`
|
||||||
--- @field stat? uv.uv_fs_t
|
--- @field stat? uv.fs_stat.result
|
||||||
|
|
||||||
---@alias vim.loader.Stats table<string, {total:number, time:number, [string]:number?}?>
|
---@alias vim.loader.Stats table<string, {total:number, time:number, [string]:number?}?>
|
||||||
|
|
||||||
@@ -56,25 +56,25 @@ M.enabled = false
|
|||||||
---@type vim.loader.Stats
|
---@type vim.loader.Stats
|
||||||
local stats = { find = { total = 0, time = 0, not_found = 0 } }
|
local stats = { find = { total = 0, time = 0, not_found = 0 } }
|
||||||
|
|
||||||
--- @type table<string, vim.loader.CacheHash>?
|
--- @type table<string, uv.fs_stat.result>?
|
||||||
local hashes
|
local fs_stat_cache
|
||||||
|
|
||||||
---@type table<string, table<string,vim.loader.ModuleInfo>>
|
---@type table<string, table<string,vim.loader.ModuleInfo>>
|
||||||
local indexed = {}
|
local indexed = {}
|
||||||
|
|
||||||
--- @param path string
|
--- @param path string
|
||||||
--- @return vim.loader.CacheHash
|
--- @return uv.fs_stat.result?
|
||||||
local function get_hash(path)
|
local function fs_stat_cached(path)
|
||||||
if not hashes then
|
if not fs_stat_cache then
|
||||||
return uv.fs_stat(path) --[[@as vim.loader.CacheHash]]
|
return uv.fs_stat(path)
|
||||||
end
|
end
|
||||||
|
|
||||||
if not hashes[path] then
|
if not fs_stat_cache[path] then
|
||||||
-- Note we must never save a stat for a non-existent path.
|
-- Note we must never save a stat for a non-existent path.
|
||||||
-- For non-existent paths fs_stat() will return nil.
|
-- For non-existent paths fs_stat() will return nil.
|
||||||
hashes[path] = uv.fs_stat(path)
|
fs_stat_cache[path] = uv.fs_stat(path)
|
||||||
end
|
end
|
||||||
return hashes[path]
|
return fs_stat_cache[path]
|
||||||
end
|
end
|
||||||
|
|
||||||
local function normalize(path)
|
local function normalize(path)
|
||||||
@@ -143,8 +143,8 @@ end
|
|||||||
local function readfile(path, mode)
|
local function readfile(path, mode)
|
||||||
local f = uv.fs_open(path, 'r', mode)
|
local f = uv.fs_open(path, 'r', mode)
|
||||||
if f then
|
if f then
|
||||||
local hash = assert(uv.fs_fstat(f))
|
local size = assert(uv.fs_fstat(f)).size
|
||||||
local data = uv.fs_read(f, hash.size, 0) --[[@as string?]]
|
local data = uv.fs_read(f, size, 0) --[[@as string?]]
|
||||||
uv.fs_close(f)
|
uv.fs_close(f)
|
||||||
return data
|
return data
|
||||||
end
|
end
|
||||||
@@ -185,16 +185,16 @@ end
|
|||||||
---@param modname string module name
|
---@param modname string module name
|
||||||
---@return string|function
|
---@return string|function
|
||||||
local function loader_cached(modname)
|
local function loader_cached(modname)
|
||||||
hashes = {}
|
fs_stat_cache = {}
|
||||||
local ret = M.find(modname)[1]
|
local ret = M.find(modname)[1]
|
||||||
if ret then
|
if ret then
|
||||||
-- Make sure to call the global loadfile so we respect any augmentations done elsewhere.
|
-- Make sure to call the global loadfile so we respect any augmentations done elsewhere.
|
||||||
-- E.g. profiling
|
-- E.g. profiling
|
||||||
local chunk, err = loadfile(ret.modpath)
|
local chunk, err = loadfile(ret.modpath)
|
||||||
hashes = nil
|
fs_stat_cache = nil
|
||||||
return chunk or error(err)
|
return chunk or error(err)
|
||||||
end
|
end
|
||||||
hashes = nil
|
fs_stat_cache = nil
|
||||||
return ("\n\tcache_loader: module '%s' not found"):format(modname)
|
return ("\n\tcache_loader: module '%s' not found"):format(modname)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -242,11 +242,11 @@ end
|
|||||||
---@return function?, string? error_message
|
---@return function?, string? error_message
|
||||||
local function loadfile_cached(filename, mode, env)
|
local function loadfile_cached(filename, mode, env)
|
||||||
local modpath = normalize(filename)
|
local modpath = normalize(filename)
|
||||||
local hash = get_hash(modpath)
|
local stat = fs_stat_cached(modpath)
|
||||||
local cname = cache_filename(modpath)
|
local cname = cache_filename(modpath)
|
||||||
if hash then
|
if stat then
|
||||||
local e_hash, e_chunk = read_cachefile(cname)
|
local e_hash, e_chunk = read_cachefile(cname)
|
||||||
if hash_eq(e_hash, hash) and e_chunk then
|
if hash_eq(e_hash, stat) and e_chunk then
|
||||||
-- found in cache and up to date
|
-- found in cache and up to date
|
||||||
local chunk, err = load(e_chunk, '@' .. modpath, mode, env)
|
local chunk, err = load(e_chunk, '@' .. modpath, mode, env)
|
||||||
if not (err and err:find('cannot load incompatible bytecode', 1, true)) then
|
if not (err and err:find('cannot load incompatible bytecode', 1, true)) then
|
||||||
@@ -256,8 +256,8 @@ local function loadfile_cached(filename, mode, env)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local chunk, err = _loadfile(modpath, mode, env)
|
local chunk, err = _loadfile(modpath, mode, env)
|
||||||
if chunk then
|
if chunk and stat then
|
||||||
write_cachefile(cname, hash, chunk)
|
write_cachefile(cname, stat, chunk)
|
||||||
end
|
end
|
||||||
return chunk, err
|
return chunk, err
|
||||||
end
|
end
|
||||||
@@ -270,7 +270,7 @@ local function lsmod(path)
|
|||||||
for name, t in fs.dir(path .. '/lua') do
|
for name, t in fs.dir(path .. '/lua') do
|
||||||
local modpath = path .. '/lua/' .. name
|
local modpath = path .. '/lua/' .. name
|
||||||
-- HACK: type is not always returned due to a bug in luv
|
-- HACK: type is not always returned due to a bug in luv
|
||||||
t = t or get_hash(modpath).type
|
t = t or fs_stat_cached(modpath).type
|
||||||
---@type string
|
---@type string
|
||||||
local topname
|
local topname
|
||||||
local ext = name:sub(-4)
|
local ext = name:sub(-4)
|
||||||
@@ -345,9 +345,9 @@ function M.find(modname, opts)
|
|||||||
for _, pattern in ipairs(patterns) do
|
for _, pattern in ipairs(patterns) do
|
||||||
local modpath = path .. pattern
|
local modpath = path .. pattern
|
||||||
stats.find.stat = (stats.find.stat or 0) + 1
|
stats.find.stat = (stats.find.stat or 0) + 1
|
||||||
local hash = get_hash(modpath)
|
local stat = fs_stat_cached(modpath)
|
||||||
if hash then
|
if stat then
|
||||||
results[#results + 1] = { modpath = modpath, stat = hash, modname = modname }
|
results[#results + 1] = { modpath = modpath, stat = stat, modname = modname }
|
||||||
if not continue() then
|
if not continue() then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
@@ -394,8 +394,8 @@ function M.reset(path)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- Path could be a directory so just clear all the hashes.
|
-- Path could be a directory so just clear all the hashes.
|
||||||
if hashes then
|
if fs_stat_cache then
|
||||||
hashes = {}
|
fs_stat_cache = {}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user