From d3a7bdefb0782179433f42f8be8a35816a04f68e Mon Sep 17 00:00:00 2001 From: Björn Linse Date: Sat, 29 Jun 2019 20:15:12 +0200 Subject: lua: immediate-callback safe print() --- test/functional/lua/overrides_spec.lua | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) (limited to 'test/functional/lua') diff --git a/test/functional/lua/overrides_spec.lua b/test/functional/lua/overrides_spec.lua index 8f318e3503..c0541eeae9 100644 --- a/test/functional/lua/overrides_spec.lua +++ b/test/functional/lua/overrides_spec.lua @@ -14,6 +14,7 @@ local command = helpers.command local write_file = helpers.write_file local redir_exec = helpers.redir_exec local alter_slashes = helpers.alter_slashes +local exec_lua = helpers.exec_lua local screen @@ -53,11 +54,11 @@ describe('print', function() v_tblout = setmetatable({}, meta_tblout) ]]) eq('', redir_exec('luafile ' .. fname)) - eq('\nE5114: Error while converting print argument #2: [NULL]', + eq('\nE5105: Error while calling lua chunk: E5114: Error while converting print argument #2: [NULL]', redir_exec('lua print("foo", v_nilerr, "bar")')) - eq('\nE5114: Error while converting print argument #2: Xtest-functional-lua-overrides-luafile:2: abc', + eq('\nE5105: Error while calling lua chunk: E5114: Error while converting print argument #2: Xtest-functional-lua-overrides-luafile:2: abc', redir_exec('lua print("foo", v_abcerr, "bar")')) - eq('\nE5114: Error while converting print argument #2: ', + eq('\nE5105: Error while calling lua chunk: E5114: Error while converting print argument #2: ', redir_exec('lua print("foo", v_tblout, "bar")')) end) it('prints strings with NULs and NLs correctly', function() @@ -76,6 +77,29 @@ describe('print', function() eq('\nabc ', redir_exec('lua print("abc", "")')) eq('\nabc def', redir_exec('lua print("abc", "", "def")')) end) + it('defers printing in luv event handlers', function() + exec_lua([[ + local cmd = ... + function test() + local timer = vim.loop.new_timer() + local done = false + timer:start(10, 0, function() + print("very fast") + timer:close() + done = true + end) + -- be kind to slow travis OS X jobs: + -- loop until we know for sure the callback has been executed + while not done do + os.execute(cmd) + vim.loop.run("nowait") -- fake os_breakcheck() + end + print("very slow") + vim.api.nvim_command("sleep 1m") -- force deferred event processing + end + ]], (iswin() and "timeout 1") or "sleep 0.1") + eq('\nvery slow\nvery fast',redir_exec('lua test()')) + end) end) describe('debug.debug', function() -- cgit From e6d77993d1167f4c15c9f67c0c3281444b1d18c2 Mon Sep 17 00:00:00 2001 From: Björn Linse Date: Sun, 4 Aug 2019 19:42:31 +0200 Subject: lua: do not crash on syntax error in debug.debug() --- test/functional/lua/overrides_spec.lua | 75 ++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) (limited to 'test/functional/lua') diff --git a/test/functional/lua/overrides_spec.lua b/test/functional/lua/overrides_spec.lua index c0541eeae9..f6439001ac 100644 --- a/test/functional/lua/overrides_spec.lua +++ b/test/functional/lua/overrides_spec.lua @@ -207,6 +207,81 @@ describe('debug.debug', function() {cr:Press ENTER or type command to continue}^ | ]]) end) + + it("can be safely exited with 'cont'", function() + feed('') + feed(':lua debug.debug() print("x")') + screen:expect{grid=[[ + | + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + lua_debug> ^ | + ]]} + + feed("conttt") -- misspelled cont; invalid syntax + screen:expect{grid=[[ + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + lua_debug> conttt | + {E:E5115: Error while loading debug string: (debug comma}| + {E:nd):1: '=' expected near ''} | + lua_debug> ^ | + ]]} + + feed("cont") -- exactly "cont", exit now + screen:expect{grid=[[ + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + lua_debug> conttt | + {E:E5115: Error while loading debug string: (debug comma}| + {E:nd):1: '=' expected near ''} | + lua_debug> cont | + x | + {cr:Press ENTER or type command to continue}^ | + ]]} + + feed('') + screen:expect{grid=[[ + ^ | + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + | + ]]} + end) end) describe('package.path/package.cpath', function() -- cgit From 88938634e7418ced4cfb074c48867523460dcc84 Mon Sep 17 00:00:00 2001 From: Björn Linse Date: Sun, 4 Aug 2019 21:56:29 +0200 Subject: lua: add vim.in_fast_event() to check if we are in a luv callback --- test/functional/lua/loop_spec.lua | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'test/functional/lua') diff --git a/test/functional/lua/loop_spec.lua b/test/functional/lua/loop_spec.lua index b96214738a..992d1666f6 100644 --- a/test/functional/lua/loop_spec.lua +++ b/test/functional/lua/loop_spec.lua @@ -75,6 +75,7 @@ describe('vim.loop', function() exec_lua([[ local timer = vim.loop.new_timer() timer:start(20, 0, function () + _G.is_fast = vim.in_fast_event() timer:close() vim.api.nvim_set_var("valid", true) vim.api.nvim_command("echomsg 'howdy'") @@ -89,18 +90,20 @@ describe('vim.loop', function() {1:~ }| {2: }| {3:Error executing luv callback:} | - {3:[string ""]:4: E5560: nvim_set_var must not }| + {3:[string ""]:5: E5560: nvim_set_var must not }| {3:be called in a lua loop callback} | {4:Press ENTER or type command to continue}^ | ]]) feed('') eq(false, eval("get(g:, 'valid', v:false)")) + eq(true, exec_lua("return _G.is_fast")) -- callbacks can be scheduled to be executed in the main event loop -- where the entire API is available exec_lua([[ local timer = vim.loop.new_timer() timer:start(20, 0, vim.schedule_wrap(function () + _G.is_fast = vim.in_fast_event() timer:close() vim.api.nvim_set_var("valid", true) vim.api.nvim_command("echomsg 'howdy'") @@ -120,6 +123,7 @@ describe('vim.loop', function() howdy | ]]) eq(true, eval("get(g:, 'valid', v:false)")) + eq(false, exec_lua("return _G.is_fast")) -- fast (not deferred) API functions are allowed to be called directly exec_lua([[ -- cgit