Skip to content

Commit 675295d

Browse files
committed
fix: pass opts.name to serpent to correct reference deserialize
Before, obj/func ref is sometimes deserialized as nil since: 1. serpent don't know who fn1 before the full table is returned since the serialized data is a compact version to ensure func/obj ref is consistant 2. lua table is random, so it's also possible to deserialize fn2 correctly, but fn1=nil ``` return { fn1 = my_func, fn2 = fn1 } ``` Apply opts.name="_" for serpent, it would be like: ``` local _ = { fn1 = my_func } _.fn2 = _.fn1 return _ ``` ci: pass exec_lua function ci: refactor test
1 parent bd9ead9 commit 675295d

6 files changed

Lines changed: 26 additions & 15 deletions

File tree

lua/fzf-lua/libuv.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ M.async_spawn = coroutinify(M.spawn)
376376
---@param b64? boolean
377377
---@return string, boolean -- boolean used for ./scripts/headless_fd.sh
378378
M.serialize = function(obj, b64)
379-
local str = serpent.line(obj, { comment = false, sortkeys = false })
379+
local str = serpent.line(obj, { name = "_", comment = false, sortkeys = false })
380380
str = b64 ~= false and base64.encode(str) or str
381381
return "return [==[" .. str .. "]==]", (b64 ~= false and true or false)
382382
end

lua/fzf-lua/previewer/builtin.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,13 +1565,12 @@ end
15651565

15661566
---@diagnostic disable-next-line: unused
15671567
function Previewer.highlights:parse_entry(entry_str)
1568-
local serpent = require "fzf-lua.lib.serpent"
15691568
local hl = entry_str:match("^[^%s]+")
15701569
local hlgroup = hl
15711570
local lines = {}
15721571
repeat
15731572
local hl_def = api.nvim_get_hl(0, { name = hl, link = true })
1574-
local block = utils.strsplit(serpent.block(hl_def, { comment = false, sortkeys = false }), "\n")
1573+
local block = utils.strsplit(vim.inspect(hl_def, { indent = (" "):rep(fn.shiftwidth()) }), "\n")
15751574
block[1] = string.format("%s = %s", hl, block[1])
15761575
vim.tbl_map(function(l) table.insert(lines, l) end, block)
15771576
hl = hl_def.link

lua/fzf-lua/test/exec_lua.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,12 @@ function M.handler(bytecode, upvalues, ...)
100100
return ret, new_upvalues, messages
101101
end
102102

103-
--- @param child MiniTest.child
103+
--- @param exec_lua function
104104
--- @param lvl integer
105105
--- @param code function
106106
--- @param arg table
107-
function M.run(child, lvl, code, arg)
108-
local rv = child.lua(
107+
function M.run(exec_lua, lvl, code, arg)
108+
local rv = exec_lua(
109109
[[return { require('fzf-lua.test.exec_lua').handler(...) }]],
110110
{ string.dump(code), get_upvalues(code), unpack(arg or {}) })
111111

@@ -197,7 +197,7 @@ M.serialize = function(...)
197197
for _, v in ipairs(args) do
198198
save_upvalues(v, args)
199199
end
200-
return require("fzf-lua.lib.serpent").block(args, { comment = false, sortkeys = false })
200+
return require("fzf-lua.lib.serpent").block(args, { name = "_", comment = false, sortkeys = false })
201201
end
202202

203203
return M

lua/fzf-lua/test/helpers.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ M.new_child_neovim = function()
131131
local child_lua = child.lua
132132
child.lua = function(code, arg)
133133
if type(code) == "string" then return child_lua(code, arg) end
134-
return require("fzf-lua.test.exec_lua").run(child, 2, code, arg)
134+
return require("fzf-lua.test.exec_lua").run(child_lua, 2, code, arg)
135135
end
136136

137137
-- TODO: support "function" upvalue

tests/headless_spec.lua

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,11 @@ local exec_term = function(c, cmd, args)
3838
cmd = cmd or {}
3939
args = args or {}
4040
args.term = true
41-
local serpent = require "fzf-lua.lib.serpent"
42-
cmd = serpent.block(cmd, { comment = false, sortkeys = false })
43-
args = serpent.block(args, { comment = false, sortkeys = false })
44-
local id = c.lua_get(string.format("vim.fn.jobstart(%s, %s)", cmd, args))
45-
eq(tonumber(id) > 0, true)
46-
c.lua(string.format("vim.fn.jobwait({%s})", tostring(id)))
41+
c.lua(function()
42+
local id = vim.fn.jobstart(cmd, args)
43+
assert(tonumber(id) > 0, true)
44+
vim.fn.jobwait({ id })
45+
end)
4746
c.wait_until(function()
4847
if FzfLua.utils.__HAS_NVIM_012 then
4948
-- https://github.com/neovim/neovim/pull/37987
@@ -92,7 +91,7 @@ T["headless"]["file_icons"]["server"] = new_set({ parametrize = { { "devicons" }
9291
local fzf_lua_server = child.lua_get("vim.g.fzf_lua_server")
9392
eq(#fzf_lua_server > 0, true)
9493
local new_child = helpers.new_child_neovim()
95-
new_child.start()
94+
new_child.init()
9695
new_child.o.statusline = "fzf://"
9796
exec_term(new_child, {
9897
vim.fs.abspath("./scripts/headless_fd.sh"),

tests/libuv_spec.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,17 @@ describe("Testing libuv module", function()
198198
vim.api.nvim_win_close(splitwin, true)
199199
FzfLua.utils.send_ctrl_c()
200200
end)
201+
202+
it("serialize/deserialize with shared function reference", function()
203+
local func = function() return "test" end
204+
local obj = { fn1 = func, fn2 = func }
205+
local serialized = libuv.serialize(obj, false)
206+
local deserialized = libuv.deserialize(serialized, false)
207+
eq(type(deserialized.fn1), "function")
208+
eq(type(deserialized.fn2), "function")
209+
-- also verify both return the same value as original
210+
eq(deserialized.fn1, deserialized.fn2)
211+
eq(deserialized.fn1(), "test")
212+
eq(deserialized.fn2(), "test")
213+
end)
201214
end)

0 commit comments

Comments
 (0)