]> granicus.if.org Git - vim/commitdiff
patch 8.2.3351: Vim9: using a function by name may delete it v8.2.3351
authorBram Moolenaar <Bram@vim.org>
Sun, 15 Aug 2021 14:08:36 +0000 (16:08 +0200)
committerBram Moolenaar <Bram@vim.org>
Sun, 15 Aug 2021 14:08:36 +0000 (16:08 +0200)
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)

src/evalvars.c
src/testdir/test_vim9_func.vim
src/version.c

index 2de7999a0f4f2c6912b6a8e9601b5c9590448378..1af9ec562843e86ae4fcff1b66dac3bb459df21b 100644 (file)
@@ -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);
                }
            }
        }
index cc7132aac990e49e0d91af7e3d9a2f6b3d0d0e4f..c42188e7a83b79df7ca47a9997151684edeb705f 100644 (file)
@@ -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
index f7e16365da86e00c825892b7af8af91a8cb16f63..22022c815ce3af4a0a994b8c1a7809a6dd170a9a 100644 (file)
@@ -755,6 +755,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    3351,
 /**/
     3350,
 /**/