diff src/if_lua.c @ 21030:08e284594211 v8.2.1066

patch 8.2.1066: Lua arrays are zero based Commit: https://github.com/vim/vim/commit/bd84617d1a6766efd59c94aabebb044bef805b99 Author: Bram Moolenaar <Bram@vim.org> Date: Sat Jun 27 12:32:57 2020 +0200 patch 8.2.1066: Lua arrays are zero based Problem: Lua arrays are zero based. Solution: Make Lua arrays one based. (Prabir Shrestha, closes https://github.com/vim/vim/issues/6347) Note: this is not backwards compatible.
author Bram Moolenaar <Bram@vim.org>
date Sat, 27 Jun 2020 12:45:04 +0200
parents 380923b96878
children 89aba7895bb2
line wrap: on
line diff
--- a/src/if_lua.c
+++ b/src/if_lua.c
@@ -871,7 +871,13 @@ luaV_list_index(lua_State *L)
     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 @@ luaV_list_newindex(lua_State *L)
     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);