Skip to content

Commit b033ee2

Browse files
committed
patch 8.2.3351: Vim9: using a function by name may delete it
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)
1 parent a401bba commit b033ee2

3 files changed

Lines changed: 23 additions & 0 deletions

File tree

src/evalvars.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2636,13 +2636,17 @@ eval_variable(
26362636
{
26372637
ufunc_T *ufunc = find_func(name, FALSE, NULL);
26382638

2639+
// In Vim9 script we can get a function reference by using the
2640+
// function name.
26392641
if (ufunc != NULL)
26402642
{
26412643
found = TRUE;
26422644
if (rettv != NULL)
26432645
{
26442646
rettv->v_type = VAR_FUNC;
26452647
rettv->vval.v_string = vim_strsave(ufunc->uf_name);
2648+
if (rettv->vval.v_string != NULL)
2649+
func_ref(ufunc->uf_name);
26462650
}
26472651
}
26482652
}

src/testdir/test_vim9_func.vim

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3089,6 +3089,23 @@ def Test_closing_brace_at_start_of_line()
30893089
call CheckDefAndScriptSuccess(lines)
30903090
enddef
30913091

3092+
func CreateMydict()
3093+
let g:mydict = {}
3094+
func g:mydict.afunc()
3095+
let g:result = self.key
3096+
endfunc
3097+
endfunc
3098+
3099+
def Test_numbered_function_reference()
3100+
CreateMydict()
3101+
var output = execute('legacy func g:mydict.afunc')
3102+
var funcName = 'g:' .. substitute(output, '.*function \(\d\+\).*', '\1', '')
3103+
execute 'function(' .. funcName .. ', [], {key: 42})()'
3104+
# check that the function still exists
3105+
assert_equal(output, execute('legacy func g:mydict.afunc'))
3106+
unlet g:mydict
3107+
enddef
3108+
30923109
if has('python3')
30933110
def Test_python3_heredoc()
30943111
py3 << trim EOF

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,8 @@ static char *(features[]) =
755755

756756
static int included_patches[] =
757757
{ /* Add new patch number below this line */
758+
/**/
759+
3351,
758760
/**/
759761
3350,
760762
/**/

0 commit comments

Comments
 (0)