]> granicus.if.org Git - vim/commitdiff
patch 8.2.3793: using "g:Func" as a funcref does not work in script context v8.2.3793
authorBram Moolenaar <Bram@vim.org>
Sun, 12 Dec 2021 21:02:03 +0000 (21:02 +0000)
committerBram Moolenaar <Bram@vim.org>
Sun, 12 Dec 2021 21:02:03 +0000 (21:02 +0000)
Problem:    Using "g:Func" as a funcref does not work in script context
            because "g:" is dropped.
Solution:   Keep "g:" in the name.  Also add parenthesis to avoid confusing
            operator prececence. (closes #9336)

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

index a12b9bd23ba9379e66300368c93be6b73da1f7f2..6aa2e4205f045d59433454dab3cdf07a4eaf8af5 100644 (file)
@@ -1414,7 +1414,7 @@ ex_let_option(
                n = (long)tv_get_number(tv);
        }
 
-       if (opt_p_flags & P_FUNC && (tv->v_type == VAR_PARTIAL
+       if ((opt_p_flags & P_FUNC) && (tv->v_type == VAR_PARTIAL
                                                || tv->v_type == VAR_FUNC))
        {
            // If the option can be set to a function reference or a lambda
@@ -2723,7 +2723,12 @@ eval_variable(
                if (rettv != NULL)
                {
                    rettv->v_type = VAR_FUNC;
-                   rettv->vval.v_string = vim_strsave(ufunc->uf_name);
+                   if (STRNCMP(name, "g:", 2) == 0)
+                       // Keep the "g:", otherwise script-local may be
+                       // assumed.
+                       rettv->vval.v_string = vim_strsave(name);
+                   else
+                       rettv->vval.v_string = vim_strsave(ufunc->uf_name);
                    if (rettv->vval.v_string != NULL)
                        func_ref(ufunc->uf_name);
                }
index 2c871d33e5f505ae7a9e42057f8cf08d6eb41df4..6d098ffda5371b5dab1295a36d9716db5ca02ea9 100644 (file)
@@ -1224,6 +1224,25 @@ def Test_set_opfunc_to_lambda()
   CheckScriptSuccess(lines)
 enddef
 
+def Test_set_opfunc_to_global_function()
+  var lines =<< trim END
+    vim9script
+    def g:CountSpaces(type = ''): string
+      normal! '[V']y
+      g:result = getreg('"')->count(' ')
+      return ''
+    enddef
+    &operatorfunc = g:CountSpaces
+    new
+    'a b c d e'->setline(1)
+    feedkeys("g@_", 'x')
+    assert_equal(4, g:result)
+    bwipe!
+  END
+  CheckScriptSuccess(lines)
+  &operatorfunc = ''
+enddef
+
 def Test_lambda_type_allocated()
   # Check that unreferencing a partial using a lambda can use the variable type
   # after the lambda has been freed and does not leak memory.
index 7ee0e170c18487ecd94a9367e0792d401f4008e3..971510a5f82ef174dd64cefded4a6bae4efe881d 100644 (file)
@@ -753,6 +753,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    3793,
 /**/
     3792,
 /**/