From 810acc1e86180403308e1cf650ed9fb0c5d27a44 Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Tue, 14 Mar 2023 18:33:16 +0100 Subject: [PATCH] feat(cache): drop dependency on ffi --- lua/lazy/core/cache.lua | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/lua/lazy/core/cache.lua b/lua/lazy/core/cache.lua index ff7785f..d164f9a 100644 --- a/lua/lazy/core/cache.lua +++ b/lua/lazy/core/cache.lua @@ -7,7 +7,6 @@ if type(package.loaded["vim.cache"]) == "table" then return package.loaded["vim.cache"] end -local ffi = require("ffi") local uv = vim.loop local M = {} @@ -20,7 +19,7 @@ local M = {} ---@field patterns? string[] Paterns to use (defaults to `{"/init.lua", ".lua"}`) ---@field paths? string[] Extra paths to search for modname -M.VERSION = 2 +M.VERSION = 3 M.path = vim.fn.stdpath("cache") .. "/luac" M.enabled = false ---@type table @@ -114,7 +113,7 @@ function Cache.write(name, entry) entry.hash.mtime.sec, 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_close(f) end @@ -132,15 +131,17 @@ function Cache.read(name) local data = uv.fs_read(f, hash.size, 0) --[[@as string]] uv.fs_close(f) + local zero = data:find("\0", 1, true) + ---@type integer[]|{[0]:integer} - local header = ffi.cast("uint32_t*", ffi.new("const char[16]", data:sub(1, 16))) - if header[0] ~= M.VERSION then + local header = vim.split(data:sub(1, zero - 1), ",") + if tonumber(header[1]) ~= M.VERSION then return end - M.track("read", start) + M._track("read", start) return { - hash = { size = header[1], mtime = { sec = header[2], nsec = header[3] } }, - chunk = data:sub(16 + 1), + hash = { size = tonumber(header[2]), mtime = { sec = tonumber(header[3]), nsec = tonumber(header[4]) } }, + chunk = data:sub(zero + 1), } end M._track("read", start)