From b2d471ab337e56f660eb7c89ae24f447f7b7a165 Mon Sep 17 00:00:00 2001 From: Gregory Anders <8965202+gpanders@users.noreply.github.com> Date: Thu, 7 Dec 2023 08:01:27 -0800 Subject: fix(lua): allow nil values in serialized Lua arrays (#26329) When we convert a Lua table to an Object, we consider the table a "dictionary" if it contains only string keys, and an array if it contains all numeric indices with no gaps. While rare, Lua tables can have both strictly numeric indices and gaps (e.g. { [2] = 2 }). These currently cannot be serialized because it is not considered an array. However, we know the maximum index of the table and as long as all of the keys in the table are numeric, it is still possible to serialize this table as an array. The missing indices will have nil values. --- test/functional/lua/api_spec.lua | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'test') diff --git a/test/functional/lua/api_spec.lua b/test/functional/lua/api_spec.lua index d808693a9e..8c03eb60ec 100644 --- a/test/functional/lua/api_spec.lua +++ b/test/functional/lua/api_spec.lua @@ -245,4 +245,8 @@ describe('luaeval(vim.api.…)', function() eq('', funcs.luaeval('vim.api.nvim_replace_termcodes("", 0, 1.5, "test")')) eq('', funcs.luaeval('vim.api.nvim_replace_termcodes("", true, {}, {[vim.type_idx]=vim.types.array})')) end) + + it('serializes sparse arrays in Lua', function() + eq({ [1] = vim.NIL, [2] = 2 }, exec_lua [[ return { [2] = 2 } ]]) + end) end) -- cgit