diff --git a/src/box/lua/console.c b/src/box/lua/console.c index 493f414e3c8f..0eb65630556c 100644 --- a/src/box/lua/console.c +++ b/src/box/lua/console.c @@ -1108,6 +1108,17 @@ lua_rl_getcompletion(lua_State *L) /* use __autocomplete metamethod if it's present */ lua_pushstring(L, "__autocomplete"); lua_rawget(L, -2); + if (lua_isnil(L, -1)) { + lua_pop(L, 1); + /* + * __autocomplete may not always be part of the metatable (e.g., + * in case of cdata), so we also want to trigger the __index + * metamethod instead of simply doing a raw lookup in the + * metatable. + */ + lua_pushstring(L, "__autocomplete"); + lua_gettable(L, -3); + } if (lua_isfunction(L, -1)) { lua_replace(L, -2); lua_insert(L, -2); diff --git a/test/app-luatest/gh_6305_autocomplete_metamethod_test.lua b/test/app-luatest/gh_6305_autocomplete_metamethod_test.lua index d5dd710ad62f..5f3aa8d37cf6 100644 --- a/test/app-luatest/gh_6305_autocomplete_metamethod_test.lua +++ b/test/app-luatest/gh_6305_autocomplete_metamethod_test.lua @@ -45,6 +45,14 @@ g.test__autocomplete = function() }) -- the right way - should add 'auto' comletion setmetatable(tab, {__autocomplete = function() return {auto = true} end}) + t.assert_items_equals(tabcomplete('table11.'), {'table11.', + 'table11.first', + 'table11.second', + 'table11.auto', + }) + -- __autocomplete should also be looked up in __index + local autocompletion = function() return {auto = true} end + setmetatable(tab, {__index = {__autocomplete = autocompletion}}) t.assert_items_equals(tabcomplete('table11.'), {'table11.', 'table11.first', 'table11.second',