To: vim_dev@googlegroups.com Subject: Patch 8.2.1066 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 8.2.1066 Problem: Lua arrays are zero based. Solution: Make Lua arrays one based. (Prabir Shrestha, closes #6347) Note: this is not backwards compatible. Files: runtime/doc/if_lua.txt, src/if_lua.c, src/testdir/test_lua.vim *** ../vim-8.2.1065/runtime/doc/if_lua.txt 2020-06-25 19:27:53.032387614 +0200 --- runtime/doc/if_lua.txt 2020-06-27 12:29:15.345494160 +0200 *************** *** 217,227 **** Lua are reflected in Vim and vice-versa. A list "l" has the following properties and methods: Properties ---------- o "#l" is the number of items in list "l", equivalent to "len(l)" in Vim. ! o "l[k]" returns the k-th item in "l"; "l" is zero-indexed, as in Vim. To modify the k-th item, simply do "l[k] = newitem"; in particular, "l[k] = nil" removes the k-th item from "l". o "l()" returns an iterator for "l". --- 217,231 ---- Lua are reflected in Vim and vice-versa. A list "l" has the following properties and methods: + NOTE: In patch 8.2.1066 array indexes were changed from zero-based to + one-based. You can check with: > + if has("patch-8.2.1066") + Properties ---------- o "#l" is the number of items in list "l", equivalent to "len(l)" in Vim. ! o "l[k]" returns the k-th item in "l"; "l" is one-indexed, as in Lua. To modify the k-th item, simply do "l[k] = newitem"; in particular, "l[k] = nil" removes the k-th item from "l". o "l()" returns an iterator for "l". *************** *** 237,247 **** :let l = [1, 'item'] :lua l = vim.eval('l') -- same 'l' :lua l:add(vim.list()) ! :lua l[0] = math.pi :echo l[0] " 3.141593 ! :lua l[0] = nil -- remove first item :lua l:insert(true, 1) ! :lua print(l, #l, l[0], l[1], l[-1]) :lua for item in l() do print(item) end < --- 241,251 ---- :let l = [1, 'item'] :lua l = vim.eval('l') -- same 'l' :lua l:add(vim.list()) ! :lua l[1] = math.pi :echo l[0] " 3.141593 ! :lua l[1] = nil -- remove first item :lua l:insert(true, 1) ! :lua print(l, #l, l[1], l[2]) :lua for item in l() do print(item) end < *** ../vim-8.2.1065/src/if_lua.c 2020-06-25 20:56:38.723128804 +0200 --- src/if_lua.c 2020-06-27 12:32:22.164244827 +0200 *************** *** 871,877 **** 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)); if (li == NULL) lua_pushnil(L); else --- 871,883 ---- list_T *l = luaV_unbox(L, luaV_List, 1); if (lua_isnumber(L, 2)) // list item? { ! 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,905 **** --- 906,915 ---- 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); *** ../vim-8.2.1065/src/testdir/test_lua.vim 2020-06-25 19:27:53.036387595 +0200 --- src/testdir/test_lua.vim 2020-06-27 12:24:13.893946785 +0200 *************** *** 327,334 **** 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: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) --- 327,334 ---- call assert_equal(7, luaeval('#l')) call assert_match('^list: \%(0x\)\?\x\+$', luaeval('tostring(l)')) ! 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,388 **** 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[2][0]')) ! call assert_equal(2, luaeval('l[2][1]')) ! call assert_equal(1, luaeval('l[2][2][0]')) ! call assert_equal(2, luaeval('l[2][2][1]')) 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('l'), luaeval('l[2]')) ! call assert_equal(luaeval('l'), luaeval('l[2][2]')) lua l = nil endfunc --- 367,388 ---- lua l = vim.list():add(1):add(2) lua l = l:add(l) ! call assert_equal(1, luaeval('l[1]')) ! call assert_equal(2, luaeval('l[2]')) ! call assert_equal(1, luaeval('l[3][1]')) ! call assert_equal(2, luaeval('l[3][2]')) ! 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[3])')) ! call assert_equal(luaeval('l'), luaeval('l[3]')) ! call assert_equal(luaeval('l'), luaeval('l[3][3]')) lua l = nil endfunc *** ../vim-8.2.1065/src/version.c 2020-06-26 22:46:23.233370940 +0200 --- src/version.c 2020-06-27 12:26:55.106969903 +0200 *************** *** 756,757 **** --- 756,759 ---- { /* Add new patch number below this line */ + /**/ + 1066, /**/ -- "I know that there are people who don't love their fellow man, and I hate those people!" - Tom Lehrer /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\ \\\ an exciting new programming language -- http://www.Zimbu.org /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///