patch 8.2.1054: not so easy to pass a lua function to Vim
Problem: Not so easy to pass a lua function to Vim.
Solution: Convert a Lua function and closure to a Vim funcref. (Prabir
Shrestha, closes #6246)
diff --git a/src/testdir/test_lua.vim b/src/testdir/test_lua.vim
index bd50bce..826a7bc 100644
--- a/src/testdir/test_lua.vim
+++ b/src/testdir/test_lua.vim
@@ -541,6 +541,35 @@
call assert_equal("hello from lua", luaeval("require('testluaplugin').hello()"))
endfunc
+func Vim_func_call_lua_callback(Concat, Cb)
+ let l:message = a:Concat("hello", "vim")
+ call a:Cb(l:message)
+endfunc
+
+func Test_pass_lua_callback_to_vim_from_lua()
+ lua pass_lua_callback_to_vim_from_lua_result = ""
+ call assert_equal("", luaeval("pass_lua_callback_to_vim_from_lua_result"))
+ lua <<EOF
+ vim.funcref('Vim_func_call_lua_callback')(
+ function(greeting, message)
+ return greeting .. " " .. message
+ end,
+ function(message)
+ pass_lua_callback_to_vim_from_lua_result = message
+ end)
+EOF
+ call assert_equal("hello vim", luaeval("pass_lua_callback_to_vim_from_lua_result"))
+endfunc
+
+func Vim_func_call_metatable_lua_callback(Greet)
+ return a:Greet("world")
+endfunc
+
+func Test_pass_lua_metatable_callback_to_vim_from_lua()
+ 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
+
" Test vim.line()
func Test_lua_line()
new