Skip to content

Commit bd84617

Browse files
committed
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.
1 parent 7147820 commit bd84617

File tree

4 files changed

+32
-16
lines changed

4 files changed

+32
-16
lines changed

runtime/doc/if_lua.txt

+8-4
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,15 @@ Vim's syntax for lists. Since lists are objects, changes in list references in
217217
Lua are reflected in Vim and vice-versa. A list "l" has the following
218218
properties and methods:
219219

220+
NOTE: In patch 8.2.1066 array indexes were changed from zero-based to
221+
one-based. You can check with: >
222+
if has("patch-8.2.1066")
223+
220224
Properties
221225
----------
222226
o "#l" is the number of items in list "l", equivalent to "len(l)"
223227
in Vim.
224-
o "l[k]" returns the k-th item in "l"; "l" is zero-indexed, as in Vim.
228+
o "l[k]" returns the k-th item in "l"; "l" is one-indexed, as in Lua.
225229
To modify the k-th item, simply do "l[k] = newitem"; in
226230
particular, "l[k] = nil" removes the k-th item from "l".
227231
o "l()" returns an iterator for "l".
@@ -237,11 +241,11 @@ Examples:
237241
:let l = [1, 'item']
238242
:lua l = vim.eval('l') -- same 'l'
239243
:lua l:add(vim.list())
240-
:lua l[0] = math.pi
244+
:lua l[1] = math.pi
241245
:echo l[0] " 3.141593
242-
:lua l[0] = nil -- remove first item
246+
:lua l[1] = nil -- remove first item
243247
:lua l:insert(true, 1)
244-
:lua print(l, #l, l[0], l[1], l[-1])
248+
:lua print(l, #l, l[1], l[2])
245249
:lua for item in l() do print(item) end
246250
<
247251

src/if_lua.c

+11-1
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,13 @@ luaV_list_index(lua_State *L)
871871
list_T *l = luaV_unbox(L, luaV_List, 1);
872872
if (lua_isnumber(L, 2)) // list item?
873873
{
874-
listitem_T *li = list_find(l, (long) luaL_checkinteger(L, 2));
874+
long n = (long) luaL_checkinteger(L, 2);
875+
listitem_T *li;
876+
877+
// Lua array index starts with 1 while Vim uses 0, subtract 1 to
878+
// normalize.
879+
n -= 1;
880+
li = list_find(l, n);
875881
if (li == NULL)
876882
lua_pushnil(L);
877883
else
@@ -900,6 +906,10 @@ luaV_list_newindex(lua_State *L)
900906
list_T *l = luaV_unbox(L, luaV_List, 1);
901907
long n = (long) luaL_checkinteger(L, 2);
902908
listitem_T *li;
909+
910+
// Lua array index starts with 1 while Vim uses 0, subtract 1 to normalize.
911+
n -= 1;
912+
903913
if (l->lv_lock)
904914
luaL_error(L, "list is locked");
905915
li = list_find(l, n);

src/testdir/test_lua.vim

+11-11
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,8 @@ func Test_lua_list()
327327
call assert_equal(7, luaeval('#l'))
328328
call assert_match('^list: \%(0x\)\?\x\+$', luaeval('tostring(l)'))
329329

330-
lua l[0] = 124
331-
lua l[5] = nil
330+
lua l[1] = 124
331+
lua l[6] = nil
332332
lua l:insert('first')
333333
lua l:insert('xx', 3)
334334
call assert_equal(['first', 124, 'abc', 'xx', v:true, v:false, v:null, {'a': 1, 'b': 2, 'c': 3}], l)
@@ -367,22 +367,22 @@ func Test_lua_recursive_list()
367367
lua l = vim.list():add(1):add(2)
368368
lua l = l:add(l)
369369

370-
call assert_equal(1, luaeval('l[0]'))
371-
call assert_equal(2, luaeval('l[1]'))
370+
call assert_equal(1, luaeval('l[1]'))
371+
call assert_equal(2, luaeval('l[2]'))
372372

373-
call assert_equal(1, luaeval('l[2][0]'))
374-
call assert_equal(2, luaeval('l[2][1]'))
373+
call assert_equal(1, luaeval('l[3][1]'))
374+
call assert_equal(2, luaeval('l[3][2]'))
375375

376-
call assert_equal(1, luaeval('l[2][2][0]'))
377-
call assert_equal(2, luaeval('l[2][2][1]'))
376+
call assert_equal(1, luaeval('l[3][3][1]'))
377+
call assert_equal(2, luaeval('l[3][3][2]'))
378378

379379
call assert_equal('[1, 2, [...]]', string(luaeval('l')))
380380

381381
call assert_match('^list: \%(0x\)\?\x\+$', luaeval('tostring(l)'))
382-
call assert_equal(luaeval('tostring(l)'), luaeval('tostring(l[2])'))
382+
call assert_equal(luaeval('tostring(l)'), luaeval('tostring(l[3])'))
383383

384-
call assert_equal(luaeval('l'), luaeval('l[2]'))
385-
call assert_equal(luaeval('l'), luaeval('l[2][2]'))
384+
call assert_equal(luaeval('l'), luaeval('l[3]'))
385+
call assert_equal(luaeval('l'), luaeval('l[3][3]'))
386386

387387
lua l = nil
388388
endfunc

src/version.c

+2
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,8 @@ static char *(features[]) =
754754

755755
static int included_patches[] =
756756
{ /* Add new patch number below this line */
757+
/**/
758+
1066,
757759
/**/
758760
1065,
759761
/**/

0 commit comments

Comments
 (0)