summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2021-08-15 16:08:36 +0200
committerBram Moolenaar <Bram@vim.org>2021-08-15 16:08:36 +0200
commitb033ee2ddfa8513b57111a56d6af9e45c41f7d1e (patch)
treee9c9f0d88d53dfb5ddfd116d9a0eb4c48a2e639b
parenta401bba0805856e295c213aa504648084dae7e24 (diff)
patch 8.2.3351: Vim9: using a function by name may delete itv8.2.3351
Problem: Vim9: using a function by name may delete it. (Naohiro Ono) Solution: Increment the reference count when using a function by name. (closes #8760)
-rw-r--r--src/evalvars.c4
-rw-r--r--src/testdir/test_vim9_func.vim17
-rw-r--r--src/version.c2
3 files changed, 23 insertions, 0 deletions
diff --git a/src/evalvars.c b/src/evalvars.c
index 2de7999a0f..1af9ec5628 100644
--- a/src/evalvars.c
+++ b/src/evalvars.c
@@ -2636,6 +2636,8 @@ eval_variable(
{
ufunc_T *ufunc = find_func(name, FALSE, NULL);
+ // In Vim9 script we can get a function reference by using the
+ // function name.
if (ufunc != NULL)
{
found = TRUE;
@@ -2643,6 +2645,8 @@ eval_variable(
{
rettv->v_type = VAR_FUNC;
rettv->vval.v_string = vim_strsave(ufunc->uf_name);
+ if (rettv->vval.v_string != NULL)
+ func_ref(ufunc->uf_name);
}
}
}
diff --git a/src/testdir/test_vim9_func.vim b/src/testdir/test_vim9_func.vim
index cc7132aac9..c42188e7a8 100644
--- a/src/testdir/test_vim9_func.vim
+++ b/src/testdir/test_vim9_func.vim
@@ -3089,6 +3089,23 @@ def Test_closing_brace_at_start_of_line()
call CheckDefAndScriptSuccess(lines)
enddef
+func CreateMydict()
+ let g:mydict = {}
+ func g:mydict.afunc()
+ let g:result = self.key
+ endfunc
+endfunc
+
+def Test_numbered_function_reference()
+ CreateMydict()
+ var output = execute('legacy func g:mydict.afunc')
+ var funcName = 'g:' .. substitute(output, '.*function \(\d\+\).*', '\1', '')
+ execute 'function(' .. funcName .. ', [], {key: 42})()'
+ # check that the function still exists
+ assert_equal(output, execute('legacy func g:mydict.afunc'))
+ unlet g:mydict
+enddef
+
if has('python3')
def Test_python3_heredoc()
py3 << trim EOF
diff --git a/src/version.c b/src/version.c
index f7e16365da..22022c815c 100644
--- a/src/version.c
+++ b/src/version.c
@@ -756,6 +756,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 3351,
+/**/
3350,
/**/
3349,