Skip to content

Commit

Permalink
pass tbl when using __call
Browse files Browse the repository at this point in the history
  • Loading branch information
prabirshrestha committed Jun 19, 2020
1 parent eee2e0c commit 7b1a509
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
14 changes: 9 additions & 5 deletions src/if_lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ typedef void (*msgfunc_T)(char_u *);

typedef struct {
int index;
int tableindex; // 0 if non table
int tableindex; // LUA_NOREF if not __call
lua_State *L;
} luaV_CFuncState;

Expand Down Expand Up @@ -605,28 +605,31 @@ luaV_totypval(lua_State *L, int pos, typval_T *tv)
luaV_CFuncState *state = ALLOC_CLEAR_ONE(luaV_CFuncState);
state->index = luaL_ref(L, LUA_REGISTRYINDEX);
state->L = L;
state->tableindex = 0;
state->tableindex = LUA_NOREF;
char_u *name = register_cfunc(&luaV_call_lua_func, &luaV_call_lua_func_free, state);
tv->v_type = VAR_FUNC;
tv->vval.v_string = vim_strsave(name);
break;
}
case LUA_TTABLE:
{
lua_pushvalue(L, pos);
int tableindex = luaL_ref(L, LUA_REGISTRYINDEX);
if (lua_getmetatable(L, pos)) {
lua_getfield(L, -1, (char*)"__call");
if (lua_isfunction(L, -1)) {
int index = luaL_ref(L, LUA_REGISTRYINDEX);
luaV_CFuncState *state = ALLOC_CLEAR_ONE(luaV_CFuncState);
state->index = index;
state->L = L;
state->tableindex = 1; // TODO set proper tableindex
state->tableindex = tableindex;
char_u *name = register_cfunc(&luaV_call_lua_func, &luaV_call_lua_func_free, state);
tv->v_type = VAR_FUNC;
tv->vval.v_string = vim_strsave(name);
break;
}
}
luaL_unref(L, LUA_REGISTRYINDEX, tableindex);
tv->v_type = VAR_NUMBER;
tv->vval.v_number = 0;
status = FAIL;
Expand Down Expand Up @@ -2467,8 +2470,7 @@ luaV_call_lua_func(int argcount, typval_T *argvars, typval_T *rettv, void *state
if (funcstate->tableindex > 0)
{
luaargcount += 1;
// TODO: pass proper tbl as first args
lua_pushnil(L);
lua_rawgeti(funcstate->L, LUA_REGISTRYINDEX, funcstate->tableindex);
}

for (i = 0; i < argcount; ++i)
Expand All @@ -2490,6 +2492,8 @@ luaV_call_lua_func_free(void *state)
luaV_CFuncState *funcstate = (luaV_CFuncState*)state;
luaL_unref(L, LUA_REGISTRYINDEX, funcstate->index);
funcstate->L = NULL;
if (funcstate->tableindex != LUA_NOREF)
luaL_unref(L, LUA_REGISTRYINDEX, funcstate->tableindex);
VIM_CLEAR(funcstate);
}

Expand Down
2 changes: 1 addition & 1 deletion src/testdir/test_lua.vim
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ func Vim_func_call_metatable_lua_callback(Greet)
endfunc

func Test_pass_lua_metatable_callback_to_vim_from_lua()
let result = luaeval("vim.funcref('Vim_func_call_metatable_lua_callback')(setmetatable({}, { __call = function(tbl, msg) return 'hello ' .. msg end }) )")
let result = luaeval("vim.funcref('Vim_func_call_metatable_lua_callback')(setmetatable({ space = ' '}, { __call = function(tbl, msg) return 'hello' .. tbl.space .. msg end }) )")
call assert_equal("hello world", result)
endfunc

Expand Down

0 comments on commit 7b1a509

Please sign in to comment.