]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 7.4.1577 v7.4.1577
authorBram Moolenaar <Bram@vim.org>
Tue, 15 Mar 2016 18:33:34 +0000 (19:33 +0100)
committerBram Moolenaar <Bram@vim.org>
Tue, 15 Mar 2016 18:33:34 +0000 (19:33 +0100)
Problem:    Cannot pass "dict.Myfunc" around as a partial.
Solution:   Create a partial when expected.

src/eval.c
src/testdir/test_partial.vim
src/version.c

index 7776cc6c33ec68c1aaf750ee3e430a2bdd9ebfd4..38c192ed1b328436313a77db474d981161c55a2f 100644 (file)
@@ -110,6 +110,7 @@ static char *e_illvar = N_("E461: Illegal variable name: %s");
 #ifdef FEAT_FLOAT
 static char *e_float_as_string = N_("E806: using Float as a String");
 #endif
+static char *e_dict_both = N_("E924: can't have both a \"self\" dict and a partial: %s");
 
 #define NAMESPACE_CHAR (char_u *)"abglstvw"
 
@@ -8912,8 +8913,7 @@ call_func(
                                                                        name);
                    break;
            case ERROR_BOTH:
-                   emsg_funcname(N_("E924: can't have both a \"self\" dict and a partial: %s"),
-                                                                       name);
+                   emsg_funcname(e_dict_both, name);
                    break;
        }
     }
@@ -11782,12 +11782,29 @@ f_function(typval_T *argvars, typval_T *rettv)
 {
     char_u     *s;
     char_u     *name;
+    int                use_string = FALSE;
 
-    s = get_tv_string(&argvars[0]);
-    if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
+    if (argvars[0].v_type == VAR_FUNC)
+    {
+       /* function(MyFunc, [arg], dict) */
+       s = argvars[0].vval.v_string;
+    }
+    else if (argvars[0].v_type == VAR_PARTIAL
+                                        && argvars[0].vval.v_partial != NULL)
+       /* function(dict.MyFunc, [arg]) */
+       s = argvars[0].vval.v_partial->pt_name;
+    else
+    {
+       /* function('MyFunc', [arg], dict) */
+       s = get_tv_string(&argvars[0]);
+       use_string = TRUE;
+    }
+
+    if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
        EMSG2(_(e_invarg2), s);
     /* Don't check an autoload name for existence here. */
-    else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
+    else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
+                                                      && !function_exists(s))
        EMSG2(_("E700: Unknown function: %s"), s);
     else
     {
@@ -11837,6 +11854,12 @@ f_function(typval_T *argvars, typval_T *rettv)
                    vim_free(name);
                    return;
                }
+               if (argvars[0].v_type == VAR_PARTIAL)
+               {
+                   EMSG2(_(e_dict_both), name);
+                   vim_free(name);
+                   return;
+               }
                if (argvars[dict_idx].vval.v_dict == NULL)
                    dict_idx = 0;
            }
@@ -11880,7 +11903,12 @@ f_function(typval_T *argvars, typval_T *rettv)
                    }
                }
 
-               if (dict_idx > 0)
+               if (argvars[0].v_type == VAR_PARTIAL)
+               {
+                   pt->pt_dict = argvars[0].vval.v_partial->pt_dict;
+                   ++pt->pt_dict->dv_refcount;
+               }
+               else if (dict_idx > 0)
                {
                    pt->pt_dict = argvars[dict_idx].vval.v_dict;
                    ++pt->pt_dict->dv_refcount;
@@ -21533,7 +21561,7 @@ handle_subscript(
                rettv->v_type = VAR_UNKNOWN;
 
                /* Invoke the function.  Recursive! */
-               if (rettv->v_type == VAR_PARTIAL)
+               if (functv.v_type == VAR_PARTIAL)
                {
                    pt = functv.vval.v_partial;
                    s = pt->pt_name;
@@ -21582,6 +21610,23 @@ handle_subscript(
            }
        }
     }
+
+    if (rettv->v_type == VAR_FUNC && selfdict != NULL)
+    {
+       partial_T       *pt = (partial_T *)alloc_clear(sizeof(partial_T));
+
+       /* Turn "dict.Func" into a partial for "Func" with "dict". */
+       if (pt != NULL)
+       {
+           pt->pt_dict = selfdict;
+           selfdict = NULL;
+           pt->pt_name = rettv->vval.v_string;
+           func_ref(pt->pt_name);
+           rettv->v_type = VAR_PARTIAL;
+           rettv->vval.v_partial = pt;
+       }
+    }
+
     dict_unref(selfdict);
     return ret;
 }
index b1a52db03ba64bc68319fc21ee24fdeb850a8012..ddc85e0ef990f003657e52b565a9a879d726cc6f 100644 (file)
@@ -50,3 +50,21 @@ func Test_partial_dict()
   call assert_equal("hello/xxx/yyy", Cb("xxx", "yyy"))
   call assert_fails('Cb("fff")', 'E492:')
 endfunc
+
+func Test_partial_implicit()
+  let dict = {'name': 'foo'}
+  func dict.MyFunc(arg) dict
+    return self.name . '/' . a:arg
+  endfunc
+
+  call assert_equal('foo/bar',  dict.MyFunc('bar'))
+
+  call assert_fails('let func = dict.MyFunc', 'E704:')
+  let Func = dict.MyFunc
+  call assert_equal('foo/aaa', Func('aaa'))
+
+  let Func = function(dict.MyFunc, ['bbb'])
+  call assert_equal('foo/bbb', Func())
+
+  call assert_fails('call function(dict.MyFunc, ["bbb"], dict)', 'E924:')
+endfunc
index 5cc53c8a78bfecb2fd0dde14cf24368381b74997..5c26ce61081ffee13ff732e813efbebefdf1c91d 100644 (file)
@@ -743,6 +743,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1577,
 /**/
     1576,
 /**/