diff src/vim9execute.c @ 31920:f1a5e67e9a1b v9.0.1292

patch 9.0.1292: :defer may call the wrong method for an object Commit: https://github.com/vim/vim/commit/313e4724c3b4f6d7454b45b89da08f83a2a0c77e Author: Bram Moolenaar <Bram@vim.org> Date: Wed Feb 8 20:55:27 2023 +0000 patch 9.0.1292: :defer may call the wrong method for an object Problem: :defer may call the wrong method for an object. (Ernie Rael) Solution: When en object is from a class that extends or implements, figure out the method to call at runtime. (closes #11910)
author Bram Moolenaar <Bram@vim.org>
date Wed, 08 Feb 2023 22:00:06 +0100
parents ffa11e2757e7
children 7b1cbb43506c
line wrap: on
line diff
--- a/src/vim9execute.c
+++ b/src/vim9execute.c
@@ -4291,7 +4291,24 @@ exec_instructions(ectx_T *ectx)
 			vim_free(pt);
 			goto theend;
 		    }
-		    if (extra == NULL || extra->fre_func_name == NULL)
+		    if (extra != NULL && extra->fre_class != NULL)
+		    {
+			tv = STACK_TV_BOT(-1);
+			if (tv->v_type != VAR_OBJECT)
+			{
+			    object_required_error(tv);
+			    vim_free(pt);
+			    goto on_error;
+			}
+			object_T *obj = tv->vval.v_object;
+			class_T *cl = obj->obj_class;
+
+			// convert the interface index to the object index
+			int idx = object_index_from_itf_index(extra->fre_class,
+					      TRUE, extra->fre_method_idx, cl);
+			ufunc = cl->class_obj_methods[idx];
+		    }
+		    else if (extra == NULL || extra->fre_func_name == NULL)
 		    {
 			dfunc_T	*pt_dfunc = ((dfunc_T *)def_functions.ga_data)
 						       + funcref->fr_dfunc_idx;
@@ -4299,7 +4316,9 @@ exec_instructions(ectx_T *ectx)
 			ufunc = pt_dfunc->df_ufunc;
 		    }
 		    else
+		    {
 			ufunc = find_func(extra->fre_func_name, FALSE);
+		    }
 		    if (ufunc == NULL)
 		    {
 			SOURCING_LNUM = iptr->isn_lnum;
@@ -6727,8 +6746,16 @@ list_instructions(char *pfx, isn_T *inst
 		    }
 		    else
 			name = extra->fre_func_name;
-		    if (extra == NULL || extra->fre_loopvar_info.lvi_depth == 0)
+		    if (extra != NULL && extra->fre_class != NULL)
+		    {
+			smsg("%s%4d FUNCREF %s.%s", pfx, current,
+					   extra->fre_class->class_name, name);
+		    }
+		    else if (extra == NULL
+				     || extra->fre_loopvar_info.lvi_depth == 0)
+		    {
 			smsg("%s%4d FUNCREF %s", pfx, current, name);
+		    }
 		    else
 		    {
 			char_u	*info = printable_loopvarinfo(