]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.1.0542: Vim9: confusing string() output for object functions v9.1.0542
authorErnie Rael <errael@raelity.com>
Sun, 7 Jul 2024 18:41:44 +0000 (20:41 +0200)
committerChristian Brabandt <cb@256bit.org>
Sun, 7 Jul 2024 18:41:44 +0000 (20:41 +0200)
Problem:  Vim9: confusing string() output for object functions
Solution: improve the output for object functions (Ernie Rael)

fixes: #15129
closes: #15143

Signed-off-by: Ernie Rael <errael@raelity.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
src/eval.c
src/testdir/test_vim9_class.vim
src/version.c

index 66c95629535cb0c5fb830170e02472819be164d4..049834650fa581d4b8a9b3b66fa0be051aa045ef 100644 (file)
@@ -5922,6 +5922,37 @@ func_tv2string(typval_T *tv, char_u **tofree, int echo_style)
     return r;
 }
 
+/*
+ * Return a textual representation of the object method in "tv", a VAR_PARTIAL.
+ * If the memory is allocated "tofree" is set to it, otherwise NULL.
+ * When "echo_style" is FALSE, put quotes around the function name as
+ * "function()", otherwise does not put quotes around function name.
+ * May return NULL.
+ */
+    static char_u *
+method_tv2string(typval_T *tv, char_u **tofree, int echo_style)
+{
+    char_u     buf[MAX_FUNC_NAME_LEN];
+    partial_T  *pt = tv->vval.v_partial;
+
+    size_t len = vim_snprintf((char *)buf, sizeof(buf), "<SNR>%d_%s.%s",
+                          pt->pt_func->uf_script_ctx.sc_sid,
+                          pt->pt_func->uf_class->class_name,
+                          pt->pt_func->uf_name);
+    if (len >= sizeof(buf))
+    {
+       if (echo_style)
+       {
+           *tofree = NULL;
+           return (char_u *)"function()";
+       }
+       else
+           return *tofree = string_quote((char_u*)"", TRUE);
+    }
+
+    return *tofree = echo_style ? vim_strsave(buf) : string_quote(buf, TRUE);
+}
+
 /*
  * Return a textual representation of a partial in "tv".
  * If the memory is allocated "tofree" is set to it, otherwise NULL.
@@ -6241,7 +6272,11 @@ echo_string_core(
            break;
 
        case VAR_PARTIAL:
-           r = partial_tv2string(tv, tofree, numbuf, copyID);
+           if (tv->vval.v_partial == NULL
+                   || tv->vval.v_partial->pt_obj == NULL)
+               r = partial_tv2string(tv, tofree, numbuf, copyID);
+           else
+               r = method_tv2string(tv, tofree, echo_style);
            break;
 
        case VAR_BLOB:
index 4f75a945ad9ec83bc09fdb4701805ee36a76833d..4654598465c7d68022c5b0f14f43f6c53e743108 100644 (file)
@@ -10466,6 +10466,20 @@ func Test_object_string()
   call v9.CheckSourceSuccess(lines)
 endfunc
 
+" Test for using the string() builtin method with an object's method
+def Test_method_string()
+  var lines =<< trim END
+    vim9script
+    class A
+      def F()
+      enddef
+    endclass
+    assert_match('function(''<SNR>\d\+_A\.F'')', string(A.new().F))
+  END
+  v9.CheckScriptSuccess(lines)
+enddef
+
+
 " Test for using a class in the class definition
 def Test_Ref_Class_Within_Same_Class()
   var lines =<< trim END
index f9efacf3c43b24aac04a9fa0bed0561d5ecf2465..dffc63c1f41abf8019edd9677a2da94436447314 100644 (file)
@@ -704,6 +704,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    542,
 /**/
     541,
 /**/