mirror of
https://github.com/neovim/neovim.git
synced 2025-10-21 17:21:49 +00:00
lua: Add ability to pass tables with __call
vim-patch:8.2.1054: not so easy to pass a lua function to Vim vim-patch:8.2.1084: Lua: registering function has useless code I think I have also opened up the possibility for people to use these callbacks elsewhere, since I've added a new struct that we should be able to use. Also, this should allow us to determine what the state of a list is in Lua or a dictionary in Lua, since we now can track the luaref as we go.
This commit is contained in:
@@ -295,13 +295,12 @@ describe('luaeval()', function()
|
||||
]])
|
||||
end)
|
||||
|
||||
-- TODO(tjdevries): Need to figure
|
||||
pending('should work with metatables using __call', function()
|
||||
eq(true, exec_lua [[
|
||||
it('should work with metatables using __call', function()
|
||||
eq(1, exec_lua [[
|
||||
local this_is_local_variable = false
|
||||
local callable_table = setmetatable({}, {
|
||||
__call = function(...)
|
||||
this_is_local_variable = true
|
||||
local callable_table = setmetatable({x = 1}, {
|
||||
__call = function(t, ...)
|
||||
this_is_local_variable = t.x
|
||||
end
|
||||
})
|
||||
|
||||
@@ -315,6 +314,73 @@ describe('luaeval()', function()
|
||||
]])
|
||||
end)
|
||||
|
||||
it('should handle being called from a timer once.', function()
|
||||
eq(3, exec_lua [[
|
||||
local this_is_local_variable = false
|
||||
local callable_table = setmetatable({5, 4, 3, 2, 1}, {
|
||||
__call = function(t, ...) this_is_local_variable = t[3] end
|
||||
})
|
||||
|
||||
vim.fn.timer_start(5, callable_table)
|
||||
vim.wait(1000, function()
|
||||
return this_is_local_variable
|
||||
end)
|
||||
|
||||
return this_is_local_variable
|
||||
]])
|
||||
end)
|
||||
|
||||
it('should call functions once with __call metamethod', function()
|
||||
eq(true, exec_lua [[
|
||||
local this_is_local_variable = false
|
||||
local callable_table = setmetatable({a = true, b = false}, {
|
||||
__call = function(t, ...) this_is_local_variable = t.a end
|
||||
})
|
||||
|
||||
assert(getmetatable(callable_table).__call)
|
||||
vim.fn.call(callable_table, {})
|
||||
|
||||
return this_is_local_variable
|
||||
]])
|
||||
end)
|
||||
|
||||
it('should work with lists using __call', function()
|
||||
eq(3, exec_lua [[
|
||||
local this_is_local_variable = false
|
||||
local mt = {
|
||||
__call = function(t, ...)
|
||||
this_is_local_variable = t[3]
|
||||
end
|
||||
}
|
||||
local callable_table = setmetatable({5, 4, 3, 2, 1}, mt)
|
||||
|
||||
-- Call it once...
|
||||
vim.fn.timer_start(5, callable_table)
|
||||
vim.wait(1000, function()
|
||||
return this_is_local_variable
|
||||
end)
|
||||
|
||||
assert(this_is_local_variable)
|
||||
this_is_local_variable = false
|
||||
|
||||
vim.fn.timer_start(5, callable_table)
|
||||
vim.wait(1000, function()
|
||||
return this_is_local_variable
|
||||
end)
|
||||
|
||||
return this_is_local_variable
|
||||
]])
|
||||
end)
|
||||
|
||||
it('should not work with tables not using __call', function()
|
||||
eq({false, 'Vim:E921: Invalid callback argument'}, exec_lua [[
|
||||
local this_is_local_variable = false
|
||||
local callable_table = setmetatable({x = 1}, {})
|
||||
|
||||
return {pcall(function() vim.fn.timer_start(5, callable_table) end)}
|
||||
]])
|
||||
end)
|
||||
|
||||
it('correctly converts containers with type_idx', function()
|
||||
eq(5, eval('type(luaeval("{[vim.type_idx]=vim.types.float, [vim.val_idx]=0}"))'))
|
||||
eq(4, eval([[type(luaeval('{[vim.type_idx]=vim.types.dictionary}'))]]))
|
||||
|
Reference in New Issue
Block a user