patch 8.2.1066: Lua arrays are zero based
Problem: Lua arrays are zero based.
Solution: Make Lua arrays one based. (Prabir Shrestha, closes #6347)
Note: this is not backwards compatible.
diff --git a/src/if_lua.c b/src/if_lua.c
index 4cab3f4..2d02f7c 100644
--- a/src/if_lua.c
+++ b/src/if_lua.c
@@ -871,7 +871,13 @@
list_T *l = luaV_unbox(L, luaV_List, 1);
if (lua_isnumber(L, 2)) // list item?
{
- listitem_T *li = list_find(l, (long) luaL_checkinteger(L, 2));
+ long n = (long) luaL_checkinteger(L, 2);
+ listitem_T *li;
+
+ // Lua array index starts with 1 while Vim uses 0, subtract 1 to
+ // normalize.
+ n -= 1;
+ li = list_find(l, n);
if (li == NULL)
lua_pushnil(L);
else
@@ -900,6 +906,10 @@
list_T *l = luaV_unbox(L, luaV_List, 1);
long n = (long) luaL_checkinteger(L, 2);
listitem_T *li;
+
+ // Lua array index starts with 1 while Vim uses 0, subtract 1 to normalize.
+ n -= 1;
+
if (l->lv_lock)
luaL_error(L, "list is locked");
li = list_find(l, n);
diff --git a/src/testdir/test_lua.vim b/src/testdir/test_lua.vim
index 826a7bc..9d5ad68 100644
--- a/src/testdir/test_lua.vim
+++ b/src/testdir/test_lua.vim
@@ -327,8 +327,8 @@
call assert_equal(7, luaeval('#l'))
call assert_match('^list: \%(0x\)\?\x\+$', luaeval('tostring(l)'))
- lua l[0] = 124
- lua l[5] = nil
+ lua l[1] = 124
+ lua l[6] = nil
lua l:insert('first')
lua l:insert('xx', 3)
call assert_equal(['first', 124, 'abc', 'xx', v:true, v:false, v:null, {'a': 1, 'b': 2, 'c': 3}], l)
@@ -367,22 +367,22 @@
lua l = vim.list():add(1):add(2)
lua l = l:add(l)
- call assert_equal(1, luaeval('l[0]'))
- call assert_equal(2, luaeval('l[1]'))
+ call assert_equal(1, luaeval('l[1]'))
+ call assert_equal(2, luaeval('l[2]'))
- call assert_equal(1, luaeval('l[2][0]'))
- call assert_equal(2, luaeval('l[2][1]'))
+ call assert_equal(1, luaeval('l[3][1]'))
+ call assert_equal(2, luaeval('l[3][2]'))
- call assert_equal(1, luaeval('l[2][2][0]'))
- call assert_equal(2, luaeval('l[2][2][1]'))
+ call assert_equal(1, luaeval('l[3][3][1]'))
+ call assert_equal(2, luaeval('l[3][3][2]'))
call assert_equal('[1, 2, [...]]', string(luaeval('l')))
call assert_match('^list: \%(0x\)\?\x\+$', luaeval('tostring(l)'))
- call assert_equal(luaeval('tostring(l)'), luaeval('tostring(l[2])'))
+ call assert_equal(luaeval('tostring(l)'), luaeval('tostring(l[3])'))
- call assert_equal(luaeval('l'), luaeval('l[2]'))
- call assert_equal(luaeval('l'), luaeval('l[2][2]'))
+ call assert_equal(luaeval('l'), luaeval('l[3]'))
+ call assert_equal(luaeval('l'), luaeval('l[3][3]'))
lua l = nil
endfunc
diff --git a/src/version.c b/src/version.c
index 0743833..d152f9e 100644
--- a/src/version.c
+++ b/src/version.c
@@ -755,6 +755,8 @@
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 1066,
+/**/
1065,
/**/
1064,