Skip to content

Commit

Permalink
lua: fix completion retrieval for cdata objects
Browse files Browse the repository at this point in the history
Cdata objects cannot have an `__autocomplete` metamethod (it's our custom
metamethod). To work this around, we should also lookup the
`__autocomplete` method using an object's `__index` metamethod.

Part of tarantool#9107

NO_CHANGELOG=<not a reported bug>
NO_DOC=<bugfix>
  • Loading branch information
CuriousGeorgiy committed Mar 5, 2024
1 parent ee08747 commit ff8fc55
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/box/lua/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions test/app-luatest/gh_6305_autocomplete_metamethod_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit ff8fc55

Please sign in to comment.