mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
feat(api, lua): more conversions between LuaRef and Vim Funcref
This commit is contained in:
parent
70fe3ce004
commit
c8656e44d8
@ -10,6 +10,9 @@
|
|||||||
#include "nvim/api/private/helpers.h"
|
#include "nvim/api/private/helpers.h"
|
||||||
#include "nvim/assert.h"
|
#include "nvim/assert.h"
|
||||||
#include "nvim/eval/typval.h"
|
#include "nvim/eval/typval.h"
|
||||||
|
#include "nvim/eval/userfunc.h"
|
||||||
|
#include "nvim/lua/converter.h"
|
||||||
|
#include "nvim/lua/executor.h"
|
||||||
|
|
||||||
/// Helper structure for vim_to_object
|
/// Helper structure for vim_to_object
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@ -228,6 +231,13 @@ static inline void typval_encode_dict_end(EncodedData *const edata)
|
|||||||
/// @return The converted value
|
/// @return The converted value
|
||||||
Object vim_to_object(typval_T *obj)
|
Object vim_to_object(typval_T *obj)
|
||||||
{
|
{
|
||||||
|
if (obj->v_type == VAR_FUNC) {
|
||||||
|
ufunc_T *fp = find_func(obj->vval.v_string);
|
||||||
|
if (fp->uf_cb == nlua_CFunction_func_call) {
|
||||||
|
LuaRef ref = api_new_luaref(((LuaCFunctionState *)fp->uf_cb_state)->lua_callable.func_ref);
|
||||||
|
return LUAREF_OBJ(ref);
|
||||||
|
}
|
||||||
|
}
|
||||||
EncodedData edata;
|
EncodedData edata;
|
||||||
kvi_init(edata.stack);
|
kvi_init(edata.stack);
|
||||||
const int evo_ret = encode_vim_to_object(&edata, obj,
|
const int evo_ret = encode_vim_to_object(&edata, obj,
|
||||||
@ -340,6 +350,16 @@ bool object_to_vim(Object obj, typval_T *tv, Error *err)
|
|||||||
tv->vval.v_dict = dict;
|
tv->vval.v_dict = dict;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case kObjectTypeLuaRef: {
|
||||||
|
LuaCFunctionState *state = xmalloc(sizeof(LuaCFunctionState));
|
||||||
|
state->lua_callable.func_ref = api_new_luaref(obj.data.luaref);
|
||||||
|
char_u *name = register_cfunc(&nlua_CFunction_func_call, &nlua_CFunction_func_free, state);
|
||||||
|
tv->v_type = VAR_FUNC;
|
||||||
|
tv->vval.v_string = vim_strsave(name);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
|
@ -617,6 +617,13 @@ bool nlua_push_typval(lua_State *lstate, typval_T *const tv, bool special)
|
|||||||
semsg(_("E1502: Lua failed to grow stack to %i"), initial_size + 4);
|
semsg(_("E1502: Lua failed to grow stack to %i"), initial_size + 4);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if (tv->v_type == VAR_FUNC) {
|
||||||
|
ufunc_T *fp = find_func(tv->vval.v_string);
|
||||||
|
if (fp->uf_cb == nlua_CFunction_func_call) {
|
||||||
|
nlua_pushref(lstate, ((LuaCFunctionState *)fp->uf_cb_state)->lua_callable.func_ref);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
if (encode_vim_to_lua(lstate, tv, "nlua_push_typval argument") == FAIL) {
|
if (encode_vim_to_lua(lstate, tv, "nlua_push_typval argument") == FAIL) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -987,6 +987,38 @@ describe('lua stdlib', function()
|
|||||||
|
|
||||||
matches([[attempt to index .* nil value]],
|
matches([[attempt to index .* nil value]],
|
||||||
pcall_err(exec_lua, 'return vim.g[0].testing'))
|
pcall_err(exec_lua, 'return vim.g[0].testing'))
|
||||||
|
|
||||||
|
exec_lua [[
|
||||||
|
local counter = 0
|
||||||
|
vim.g.AddCounter = function() counter = counter + 1 end
|
||||||
|
vim.g.GetCounter = function() return counter end
|
||||||
|
]]
|
||||||
|
|
||||||
|
eq(0, eval('g:GetCounter()'))
|
||||||
|
eval('g:AddCounter()')
|
||||||
|
eq(1, eval('g:GetCounter()'))
|
||||||
|
eval('g:AddCounter()')
|
||||||
|
eq(2, eval('g:GetCounter()'))
|
||||||
|
exec_lua([[vim.g.AddCounter()]])
|
||||||
|
eq(3, exec_lua([[return vim.g.GetCounter()]]))
|
||||||
|
exec_lua([[vim.api.nvim_get_var('AddCounter')()]])
|
||||||
|
eq(4, exec_lua([[return vim.api.nvim_get_var('GetCounter')()]]))
|
||||||
|
|
||||||
|
exec_lua [[
|
||||||
|
local counter = 0
|
||||||
|
vim.api.nvim_set_var('AddCounter', function() counter = counter + 1 end)
|
||||||
|
vim.api.nvim_set_var('GetCounter', function() return counter end)
|
||||||
|
]]
|
||||||
|
|
||||||
|
eq(0, eval('g:GetCounter()'))
|
||||||
|
eval('g:AddCounter()')
|
||||||
|
eq(1, eval('g:GetCounter()'))
|
||||||
|
eval('g:AddCounter()')
|
||||||
|
eq(2, eval('g:GetCounter()'))
|
||||||
|
exec_lua([[vim.g.AddCounter()]])
|
||||||
|
eq(3, exec_lua([[return vim.g.GetCounter()]]))
|
||||||
|
exec_lua([[vim.api.nvim_get_var('AddCounter')()]])
|
||||||
|
eq(4, exec_lua([[return vim.api.nvim_get_var('GetCounter')()]]))
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it('vim.b', function()
|
it('vim.b', function()
|
||||||
@ -1021,6 +1053,38 @@ describe('lua stdlib', function()
|
|||||||
]]
|
]]
|
||||||
eq(NIL, funcs.luaeval "vim.b.to_delete")
|
eq(NIL, funcs.luaeval "vim.b.to_delete")
|
||||||
|
|
||||||
|
exec_lua [[
|
||||||
|
local counter = 0
|
||||||
|
vim.b.AddCounter = function() counter = counter + 1 end
|
||||||
|
vim.b.GetCounter = function() return counter end
|
||||||
|
]]
|
||||||
|
|
||||||
|
eq(0, eval('b:GetCounter()'))
|
||||||
|
eval('b:AddCounter()')
|
||||||
|
eq(1, eval('b:GetCounter()'))
|
||||||
|
eval('b:AddCounter()')
|
||||||
|
eq(2, eval('b:GetCounter()'))
|
||||||
|
exec_lua([[vim.b.AddCounter()]])
|
||||||
|
eq(3, exec_lua([[return vim.b.GetCounter()]]))
|
||||||
|
exec_lua([[vim.api.nvim_buf_get_var(0, 'AddCounter')()]])
|
||||||
|
eq(4, exec_lua([[return vim.api.nvim_buf_get_var(0, 'GetCounter')()]]))
|
||||||
|
|
||||||
|
exec_lua [[
|
||||||
|
local counter = 0
|
||||||
|
vim.api.nvim_buf_set_var(0, 'AddCounter', function() counter = counter + 1 end)
|
||||||
|
vim.api.nvim_buf_set_var(0, 'GetCounter', function() return counter end)
|
||||||
|
]]
|
||||||
|
|
||||||
|
eq(0, eval('b:GetCounter()'))
|
||||||
|
eval('b:AddCounter()')
|
||||||
|
eq(1, eval('b:GetCounter()'))
|
||||||
|
eval('b:AddCounter()')
|
||||||
|
eq(2, eval('b:GetCounter()'))
|
||||||
|
exec_lua([[vim.b.AddCounter()]])
|
||||||
|
eq(3, exec_lua([[return vim.b.GetCounter()]]))
|
||||||
|
exec_lua([[vim.api.nvim_buf_get_var(0, 'AddCounter')()]])
|
||||||
|
eq(4, exec_lua([[return vim.api.nvim_buf_get_var(0, 'GetCounter')()]]))
|
||||||
|
|
||||||
exec_lua [[
|
exec_lua [[
|
||||||
vim.cmd "vnew"
|
vim.cmd "vnew"
|
||||||
]]
|
]]
|
||||||
@ -1058,6 +1122,38 @@ describe('lua stdlib', function()
|
|||||||
]]
|
]]
|
||||||
eq(NIL, funcs.luaeval "vim.w.to_delete")
|
eq(NIL, funcs.luaeval "vim.w.to_delete")
|
||||||
|
|
||||||
|
exec_lua [[
|
||||||
|
local counter = 0
|
||||||
|
vim.w.AddCounter = function() counter = counter + 1 end
|
||||||
|
vim.w.GetCounter = function() return counter end
|
||||||
|
]]
|
||||||
|
|
||||||
|
eq(0, eval('w:GetCounter()'))
|
||||||
|
eval('w:AddCounter()')
|
||||||
|
eq(1, eval('w:GetCounter()'))
|
||||||
|
eval('w:AddCounter()')
|
||||||
|
eq(2, eval('w:GetCounter()'))
|
||||||
|
exec_lua([[vim.w.AddCounter()]])
|
||||||
|
eq(3, exec_lua([[return vim.w.GetCounter()]]))
|
||||||
|
exec_lua([[vim.api.nvim_win_get_var(0, 'AddCounter')()]])
|
||||||
|
eq(4, exec_lua([[return vim.api.nvim_win_get_var(0, 'GetCounter')()]]))
|
||||||
|
|
||||||
|
exec_lua [[
|
||||||
|
local counter = 0
|
||||||
|
vim.api.nvim_win_set_var(0, 'AddCounter', function() counter = counter + 1 end)
|
||||||
|
vim.api.nvim_win_set_var(0, 'GetCounter', function() return counter end)
|
||||||
|
]]
|
||||||
|
|
||||||
|
eq(0, eval('w:GetCounter()'))
|
||||||
|
eval('w:AddCounter()')
|
||||||
|
eq(1, eval('w:GetCounter()'))
|
||||||
|
eval('w:AddCounter()')
|
||||||
|
eq(2, eval('w:GetCounter()'))
|
||||||
|
exec_lua([[vim.w.AddCounter()]])
|
||||||
|
eq(3, exec_lua([[return vim.w.GetCounter()]]))
|
||||||
|
exec_lua([[vim.api.nvim_win_get_var(0, 'AddCounter')()]])
|
||||||
|
eq(4, exec_lua([[return vim.api.nvim_win_get_var(0, 'GetCounter')()]]))
|
||||||
|
|
||||||
exec_lua [[
|
exec_lua [[
|
||||||
vim.cmd "vnew"
|
vim.cmd "vnew"
|
||||||
]]
|
]]
|
||||||
@ -1090,6 +1186,38 @@ describe('lua stdlib', function()
|
|||||||
]]
|
]]
|
||||||
eq(NIL, funcs.luaeval "vim.t.to_delete")
|
eq(NIL, funcs.luaeval "vim.t.to_delete")
|
||||||
|
|
||||||
|
exec_lua [[
|
||||||
|
local counter = 0
|
||||||
|
vim.t.AddCounter = function() counter = counter + 1 end
|
||||||
|
vim.t.GetCounter = function() return counter end
|
||||||
|
]]
|
||||||
|
|
||||||
|
eq(0, eval('t:GetCounter()'))
|
||||||
|
eval('t:AddCounter()')
|
||||||
|
eq(1, eval('t:GetCounter()'))
|
||||||
|
eval('t:AddCounter()')
|
||||||
|
eq(2, eval('t:GetCounter()'))
|
||||||
|
exec_lua([[vim.t.AddCounter()]])
|
||||||
|
eq(3, exec_lua([[return vim.t.GetCounter()]]))
|
||||||
|
exec_lua([[vim.api.nvim_tabpage_get_var(0, 'AddCounter')()]])
|
||||||
|
eq(4, exec_lua([[return vim.api.nvim_tabpage_get_var(0, 'GetCounter')()]]))
|
||||||
|
|
||||||
|
exec_lua [[
|
||||||
|
local counter = 0
|
||||||
|
vim.api.nvim_tabpage_set_var(0, 'AddCounter', function() counter = counter + 1 end)
|
||||||
|
vim.api.nvim_tabpage_set_var(0, 'GetCounter', function() return counter end)
|
||||||
|
]]
|
||||||
|
|
||||||
|
eq(0, eval('t:GetCounter()'))
|
||||||
|
eval('t:AddCounter()')
|
||||||
|
eq(1, eval('t:GetCounter()'))
|
||||||
|
eval('t:AddCounter()')
|
||||||
|
eq(2, eval('t:GetCounter()'))
|
||||||
|
exec_lua([[vim.t.AddCounter()]])
|
||||||
|
eq(3, exec_lua([[return vim.t.GetCounter()]]))
|
||||||
|
exec_lua([[vim.api.nvim_tabpage_get_var(0, 'AddCounter')()]])
|
||||||
|
eq(4, exec_lua([[return vim.api.nvim_tabpage_get_var(0, 'GetCounter')()]]))
|
||||||
|
|
||||||
exec_lua [[
|
exec_lua [[
|
||||||
vim.cmd "tabnew"
|
vim.cmd "tabnew"
|
||||||
]]
|
]]
|
||||||
|
Loading…
Reference in New Issue
Block a user