changeset 31451:5804270d6e9b v9.0.1058

patch 9.0.1058: string value of class and object do not have information Commit: https://github.com/vim/vim/commit/91c9d6d772aaea9d8a8be6c6de0e9defe649f322 Author: Bram Moolenaar <Bram@vim.org> Date: Wed Dec 14 17:30:37 2022 +0000 patch 9.0.1058: string value of class and object do not have information Problem: String value of class and object do not have useful information. Solution: Add the class name and for the object the member values.
author Bram Moolenaar <Bram@vim.org>
date Wed, 14 Dec 2022 18:45:03 +0100
parents 548b0a3e0c97
children 3c80e2e69e52
files src/eval.c src/testdir/test_vim9_class.vim src/version.c
diffstat 3 files changed, 52 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/src/eval.c
+++ b/src/eval.c
@@ -5604,7 +5604,8 @@ set_ref_in_item(
 	}
 
 	case VAR_CLASS:
-	    // TODO: mark methods in class_obj_methods ?
+	    // TODO: Mark methods in class_obj_methods ?
+	    // Mark initializer expressions?
 	    break;
 
 	case VAR_OBJECT:
@@ -5863,13 +5864,42 @@ echo_string_core(
 	    break;
 
 	case VAR_CLASS:
-	    *tofree = NULL;
-	    r = (char_u *)"class";
+	    {
+		class_T *cl = tv->vval.v_class;
+		size_t len = 6 + (cl == NULL ? 9 : STRLEN(cl->class_name)) + 1;
+		r = *tofree = alloc(len);
+		vim_snprintf((char *)r, len, "class %s",
+			    cl == NULL ? "[unknown]" : (char *)cl->class_name);
+	    }
 	    break;
 
 	case VAR_OBJECT:
-	    *tofree = NULL;
-	    r = (char_u *)"object";
+	    garray_T ga;
+	    ga_init2(&ga, 1, 50);
+	    ga_concat(&ga, (char_u *)"object of ");
+	    object_T *obj = tv->vval.v_object;
+	    class_T *cl = obj == NULL ? NULL : obj->obj_class;
+	    ga_concat(&ga, cl == NULL ? (char_u *)"[unknown]" : cl->class_name);
+	    if (cl != NULL)
+	    {
+		ga_concat(&ga, (char_u *)" {");
+		for (int i = 0; i < cl->class_obj_member_count; ++i)
+		{
+		    if (i > 0)
+			ga_concat(&ga, (char_u *)", ");
+		    objmember_T *m = &cl->class_obj_members[i];
+		    ga_concat(&ga, m->om_name);
+		    ga_concat(&ga, (char_u *)": ");
+		    char_u *tf = NULL;
+		    ga_concat(&ga, echo_string_core((typval_T *)(obj + 1) + i,
+					       &tf, numbuf, copyID, echo_style,
+					       restore_copyID, composite_val));
+		    vim_free(tf);
+		}
+		ga_concat(&ga, (char_u *)"}");
+	    }
+
+	    *tofree = r = ga.ga_data;
 	    break;
 
 	case VAR_FLOAT:
--- a/src/testdir/test_vim9_class.vim
+++ b/src/testdir/test_vim9_class.vim
@@ -283,6 +283,21 @@ def Test_class_object_member_inits()
   v9.CheckScriptFailure(lines, 'E1330:')
 enddef
 
+def Test_class_object_to_string()
+  var lines =<< trim END
+      vim9script
+      class TextPosition
+        this.lnum = 1
+        this.col = 22
+      endclass
+
+      assert_equal("class TextPosition", string(TextPosition))
+
+      var pos = TextPosition.new()
+      assert_equal("object of TextPosition {lnum: 1, col: 22}", string(pos))
+  END
+  v9.CheckScriptSuccess(lines)
+enddef
 
 
 " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
--- a/src/version.c
+++ b/src/version.c
@@ -696,6 +696,8 @@ static char *(features[]) =
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    1058,
+/**/
     1057,
 /**/
     1056,