2022-11-30 13:19:50 +00:00
|
|
|
local ffi = require("ffi")
|
|
|
|
---@diagnostic disable-next-line: no-unknown
|
|
|
|
local uv = vim.loop
|
2022-11-22 20:12:33 +00:00
|
|
|
|
|
|
|
local M = {}
|
2022-11-30 13:19:50 +00:00
|
|
|
M.dirty = false
|
2022-11-22 20:12:33 +00:00
|
|
|
|
2022-11-30 13:19:50 +00:00
|
|
|
local cache_path = vim.fn.stdpath("state") .. "/lazy.state"
|
|
|
|
---@type CacheHash
|
|
|
|
local cache_hash
|
|
|
|
|
|
|
|
---@alias CacheHash {mtime: {sec:number, nsec:number}, size:number}
|
|
|
|
---@alias CacheEntry {hash:CacheHash, chunk:string, used:boolean}
|
|
|
|
---@type table<string,CacheEntry?>
|
|
|
|
M.cache = {}
|
2022-11-22 20:12:33 +00:00
|
|
|
|
|
|
|
---@param modname string
|
2022-11-24 21:04:23 +00:00
|
|
|
---@param modpath string
|
2022-11-29 23:18:59 +00:00
|
|
|
---@return any
|
2022-11-24 21:04:23 +00:00
|
|
|
function M.load(modname, modpath)
|
2022-11-30 13:19:50 +00:00
|
|
|
local entry = M.cache[modname]
|
|
|
|
local hash = assert(M.hash(modpath))
|
2022-11-24 21:04:23 +00:00
|
|
|
|
2022-11-30 13:19:50 +00:00
|
|
|
if entry and not M.eq(entry.hash, hash) then
|
|
|
|
entry = nil
|
2022-11-22 20:12:33 +00:00
|
|
|
end
|
|
|
|
|
2022-11-30 13:19:50 +00:00
|
|
|
local chunk, err
|
|
|
|
if entry then
|
|
|
|
entry.used = true
|
|
|
|
chunk, err = load(entry.chunk --[[@as string]], "@" .. modpath, "b")
|
2022-11-24 21:04:23 +00:00
|
|
|
else
|
|
|
|
vim.schedule(function()
|
|
|
|
vim.notify("loadfile(" .. modname .. ")")
|
|
|
|
end)
|
|
|
|
chunk, err = loadfile(modpath)
|
2022-11-30 13:19:50 +00:00
|
|
|
if chunk then
|
|
|
|
M.dirty = true
|
|
|
|
M.cache[modname] = { hash = hash, chunk = string.dump(chunk), used = true }
|
2022-11-22 20:12:33 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-30 13:19:50 +00:00
|
|
|
return chunk and chunk() or error(err)
|
2022-11-22 20:12:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function M.setup()
|
2022-11-30 13:19:50 +00:00
|
|
|
M.load_cache()
|
2022-11-22 20:12:33 +00:00
|
|
|
-- preload core modules
|
|
|
|
local root = vim.fn.fnamemodify(debug.getinfo(1, "S").source:sub(2), ":p:h:h")
|
2022-11-26 20:29:40 +00:00
|
|
|
for _, name in ipairs({ "util", "config", "loader", "plugin", "handler" }) do
|
2022-11-22 20:28:08 +00:00
|
|
|
local modname = "lazy.core." .. name
|
2022-11-24 21:04:23 +00:00
|
|
|
---@diagnostic disable-next-line: no-unknown
|
|
|
|
package.preload[modname] = function()
|
|
|
|
return M.load(modname, root .. "/core/" .. name:gsub("%.", "/") .. ".lua")
|
2022-11-22 20:12:33 +00:00
|
|
|
end
|
2022-11-24 21:04:23 +00:00
|
|
|
end
|
2022-11-22 20:12:33 +00:00
|
|
|
return M
|
|
|
|
end
|
|
|
|
|
2022-11-30 13:19:50 +00:00
|
|
|
---@return CacheHash?
|
|
|
|
function M.hash(file)
|
|
|
|
return uv.fs_stat(file)
|
|
|
|
end
|
|
|
|
|
|
|
|
---@param h1 CacheHash
|
|
|
|
---@param h2 CacheHash
|
|
|
|
function M.eq(h1, h2)
|
|
|
|
return h1 and h2 and h1.size == h2.size and h1.mtime.sec == h2.mtime.sec and h1.mtime.nsec == h2.mtime.nsec
|
|
|
|
end
|
|
|
|
|
|
|
|
function M.save_cache()
|
|
|
|
local f = assert(uv.fs_open(cache_path, "w", 438))
|
|
|
|
vim.loop.fs_ftruncate(f, 0)
|
|
|
|
for modname, entry in pairs(M.cache) do
|
|
|
|
if entry.used then
|
|
|
|
entry.modname = modname
|
|
|
|
local header = { entry.hash.size, entry.hash.mtime.sec, entry.hash.mtime.nsec, #modname, #entry.chunk }
|
|
|
|
uv.fs_write(f, ffi.string(ffi.new("const uint32_t[5]", header), 20))
|
|
|
|
uv.fs_write(f, modname)
|
|
|
|
uv.fs_write(f, entry.chunk)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
uv.fs_close(f)
|
|
|
|
end
|
|
|
|
|
|
|
|
function M.load_cache()
|
|
|
|
M.cache = {}
|
|
|
|
local f = uv.fs_open(cache_path, "r", 438)
|
|
|
|
if f then
|
|
|
|
cache_hash = uv.fs_fstat(f) --[[@as CacheHash]]
|
|
|
|
local data = uv.fs_read(f, cache_hash.size, 0) --[[@as string]]
|
|
|
|
uv.fs_close(f)
|
|
|
|
|
|
|
|
local offset = 1
|
|
|
|
while offset + 1 < #data do
|
|
|
|
local header = ffi.cast("uint32_t*", ffi.new("const char[20]", data:sub(offset, offset + 19)))
|
|
|
|
offset = offset + 20
|
|
|
|
local modname = data:sub(offset, offset + header[3] - 1)
|
|
|
|
offset = offset + header[3]
|
|
|
|
local chunk = data:sub(offset, offset + header[4] - 1)
|
|
|
|
offset = offset + header[4]
|
|
|
|
M.cache[modname] = { hash = { size = header[0], mtime = { sec = header[1], nsec = header[2] } }, chunk = chunk }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function M.autosave()
|
|
|
|
vim.api.nvim_create_autocmd("VimLeavePre", {
|
|
|
|
callback = function()
|
|
|
|
if M.dirty then
|
|
|
|
local hash = M.hash(cache_path)
|
|
|
|
-- abort when the file was changed in the meantime
|
|
|
|
if hash == nil or M.eq(cache_hash, hash) then
|
|
|
|
vim.fn.system("echo start >> foo.txt")
|
|
|
|
M.save_cache()
|
|
|
|
vim.fn.system("echo stop >> foo.txt")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
})
|
2022-11-22 20:12:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return M
|