Skip to content

Commit be70ce8

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 _ ```
1 parent bd281b9 commit be70ce8

5 files changed

Lines changed: 18 additions & 8 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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

tests/headless_spec.lua

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,9 @@ 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))
41+
local id = c.lua(function()
42+
return vim.fn.jobstart(cmd, args)
43+
end)
4544
eq(tonumber(id) > 0, true)
4645
c.lua(string.format("vim.fn.jobwait({%s})", tostring(id)))
4746
c.wait_until(function()

tests/libuv_spec.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,16 @@ 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(), "test")
211+
eq(deserialized.fn2(), "test")
212+
end)
201213
end)

0 commit comments

Comments
 (0)