feat(cache): drop dependency on ffi

This commit is contained in:
Folke Lemaitre 2023-03-14 18:33:16 +01:00
parent da295017e4
commit 810acc1e86
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
1 changed files with 9 additions and 8 deletions

View File

@ -7,7 +7,6 @@ if type(package.loaded["vim.cache"]) == "table" then
return package.loaded["vim.cache"] return package.loaded["vim.cache"]
end end
local ffi = require("ffi")
local uv = vim.loop local uv = vim.loop
local M = {} local M = {}
@ -20,7 +19,7 @@ local M = {}
---@field patterns? string[] Paterns to use (defaults to `{"/init.lua", ".lua"}`) ---@field patterns? string[] Paterns to use (defaults to `{"/init.lua", ".lua"}`)
---@field paths? string[] Extra paths to search for modname ---@field paths? string[] Extra paths to search for modname
M.VERSION = 2 M.VERSION = 3
M.path = vim.fn.stdpath("cache") .. "/luac" M.path = vim.fn.stdpath("cache") .. "/luac"
M.enabled = false M.enabled = false
---@type table<string, {total:number, time:number, [string]:number?}?> ---@type table<string, {total:number, time:number, [string]:number?}?>
@ -114,7 +113,7 @@ function Cache.write(name, entry)
entry.hash.mtime.sec, entry.hash.mtime.sec,
entry.hash.mtime.nsec, entry.hash.mtime.nsec,
} }
uv.fs_write(f, ffi.string(ffi.new("const uint32_t[4]", header), 16)) uv.fs_write(f, table.concat(header, ",") .. "\0")
uv.fs_write(f, entry.chunk) uv.fs_write(f, entry.chunk)
uv.fs_close(f) uv.fs_close(f)
end end
@ -132,15 +131,17 @@ function Cache.read(name)
local data = uv.fs_read(f, hash.size, 0) --[[@as string]] local data = uv.fs_read(f, hash.size, 0) --[[@as string]]
uv.fs_close(f) uv.fs_close(f)
local zero = data:find("\0", 1, true)
---@type integer[]|{[0]:integer} ---@type integer[]|{[0]:integer}
local header = ffi.cast("uint32_t*", ffi.new("const char[16]", data:sub(1, 16))) local header = vim.split(data:sub(1, zero - 1), ",")
if header[0] ~= M.VERSION then if tonumber(header[1]) ~= M.VERSION then
return return
end end
M.track("read", start) M._track("read", start)
return { return {
hash = { size = header[1], mtime = { sec = header[2], nsec = header[3] } }, hash = { size = tonumber(header[2]), mtime = { sec = tonumber(header[3]), nsec = tonumber(header[4]) } },
chunk = data:sub(16 + 1), chunk = data:sub(zero + 1),
} }
end end
M._track("read", start) M._track("read", start)