From 9e3640a7797bcc5f6015548842572a6ce951a861 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Wed, 18 Oct 2023 18:27:50 +0800 Subject: vim-patch:9.0.2044: Vim9: exceptions confuse defered functions Problem: Vim9: exceptions confuse defered functions Solution: save and restore exception state when calling defered functions closes: vim/vim#13364 closes: vim/vim#13372 https://github.com/vim/vim/commit/0672595fd50e9ae668676a40e28ebf66d7f52392 Co-authored-by: Yegappan Lakshmanan --- src/nvim/eval/userfunc.c | 15 +++++++++++++++ test/old/testdir/test_user_func.vim | 26 ++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/nvim/eval/userfunc.c b/src/nvim/eval/userfunc.c index ca98aad6bc..f789c53870 100644 --- a/src/nvim/eval/userfunc.c +++ b/src/nvim/eval/userfunc.c @@ -3296,8 +3296,23 @@ static void handle_defer_one(funccall_T *funccal) char *name = dr->dr_name; dr->dr_name = NULL; + // If the deferred function is called after an exception, then only the + // first statement in the function will be executed. Save and restore + // the try/catch/throw exception state. + const int save_trylevel = trylevel; + const bool save_did_throw = did_throw; + const bool save_need_rethrow = need_rethrow; + + trylevel = 0; + did_throw = false; + need_rethrow = false; + call_func(name, -1, &rettv, dr->dr_argcount, dr->dr_argvars, &funcexe); + trylevel = save_trylevel; + did_throw = save_did_throw; + need_rethrow = save_need_rethrow; + tv_clear(&rettv); xfree(name); for (int i = dr->dr_argcount - 1; i >= 0; i--) { diff --git a/test/old/testdir/test_user_func.vim b/test/old/testdir/test_user_func.vim index dc36ab98cb..ee1fd4ec5b 100644 --- a/test/old/testdir/test_user_func.vim +++ b/test/old/testdir/test_user_func.vim @@ -793,5 +793,31 @@ func Test_defer_wrong_arguments() call v9.CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected string but got number') endfunc +" Test for calling a deferred function after an exception +func Test_defer_after_exception() + let g:callTrace = [] + func Defer() + let g:callTrace += ['a'] + let g:callTrace += ['b'] + let g:callTrace += ['c'] + let g:callTrace += ['d'] + endfunc + + func Foo() + defer Defer() + throw "TestException" + endfunc + + try + call Foo() + catch /TestException/ + let g:callTrace += ['e'] + endtry + call assert_equal(['a', 'b', 'c', 'd', 'e'], g:callTrace) + + delfunc Defer + delfunc Foo + unlet g:callTrace +endfunc " vim: shiftwidth=2 sts=2 expandtab -- cgit From 09fb243bdd0c2cde76e360bb98118932fda5d77b Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Wed, 18 Oct 2023 18:29:51 +0800 Subject: vim-patch:9.0.2045: tests: checking for swap files takes time Problem: tests: checking for swap files takes time Solution: don't check for swap files when test has been skipped Check for swap files takes a considerable about of time, so don't do that for skipped tests to avoid wasting time. closes: vim/vim#13371 https://github.com/vim/vim/commit/a0e1f06f04da3444e278ddf47e2ea3d5857a7dec --- test/old/testdir/runtest.vim | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/test/old/testdir/runtest.vim b/test/old/testdir/runtest.vim index 6be6fd0fd3..97ef96424a 100644 --- a/test/old/testdir/runtest.vim +++ b/test/old/testdir/runtest.vim @@ -231,6 +231,8 @@ func RunTheTest(test) endtry endif + let skipped = v:false + au VimLeavePre * call EarlyExit(g:testfunc) if a:test =~ 'Test_nocatch_' " Function handles errors itself. This avoids skipping commands after the @@ -240,6 +242,7 @@ func RunTheTest(test) if g:skipped_reason != '' call add(s:messages, ' Skipped') call add(s:skipped, 'SKIPPED ' . a:test . ': ' . g:skipped_reason) + let skipped = v:true endif else try @@ -247,6 +250,7 @@ func RunTheTest(test) catch /^\cskipped/ call add(s:messages, ' Skipped') call add(s:skipped, 'SKIPPED ' . a:test . ': ' . substitute(v:exception, '^\S*\s\+', '', '')) + let skipped = v:true catch call add(v:errors, 'Caught exception in ' . a:test . ': ' . v:exception . ' @ ' . v:throwpoint) endtry @@ -342,14 +346,16 @@ func RunTheTest(test) endif endwhile - " Check if the test has left any swap files behind. Delete them before - " running tests again, they might interfere. - let swapfiles = s:GetSwapFileList() - if len(swapfiles) > 0 - call add(s:messages, "Found swap files: " .. string(swapfiles)) - for name in swapfiles - call delete(name) - endfor + if !skipped + " Check if the test has left any swap files behind. Delete them before + " running tests again, they might interfere. + let swapfiles = s:GetSwapFileList() + if len(swapfiles) > 0 + call add(s:messages, "Found swap files: " .. string(swapfiles)) + for name in swapfiles + call delete(name) + endfor + endif endif endfunc -- cgit