]> granicus.if.org Git - vim/commitdiff
patch 9.0.1292: :defer may call the wrong method for an object v9.0.1292
authorBram Moolenaar <Bram@vim.org>
Wed, 8 Feb 2023 20:55:27 +0000 (20:55 +0000)
committerBram Moolenaar <Bram@vim.org>
Wed, 8 Feb 2023 20:55:27 +0000 (20:55 +0000)
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)

src/proto/vim9instr.pro
src/testdir/test_vim9_class.vim
src/version.c
src/vim9.h
src/vim9compile.c
src/vim9execute.c
src/vim9expr.c
src/vim9instr.c

index ff132831cc401968b3fe9bb9c6921226f52ac874..9372b1fc67c917ea8d3456875ff580f902602399 100644 (file)
@@ -44,7 +44,7 @@ int generate_OLDSCRIPT(cctx_T *cctx, isntype_T isn_type, char_u *name, int sid,
 int generate_VIM9SCRIPT(cctx_T *cctx, isntype_T isn_type, int sid, int idx, type_T *type);
 int generate_NEWLIST(cctx_T *cctx, int count, int use_null);
 int generate_NEWDICT(cctx_T *cctx, int count, int use_null);
-int generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc, isn_T **isnp);
+int generate_FUNCREF(cctx_T *cctx, ufunc_T *ufunc, class_T *cl, int fi, isn_T **isnp);
 int generate_NEWFUNC(cctx_T *cctx, char_u *lambda_name, char_u *func_name);
 int generate_DEF(cctx_T *cctx, char_u *name, size_t len);
 int generate_JUMP(cctx_T *cctx, jumpwhen_T when, int where);
index 184a61f8177b3026823649b48dadcfe33a7be76c..0f812f1cd859ec67a241b1404381b44c7d440229 100644 (file)
@@ -1480,6 +1480,43 @@ def Test_defer_with_object()
   END
   v9.CheckScriptSuccess(lines)
   unlet g:result
+
+  lines =<< trim END
+      vim9script
+
+      class BaseWithEE
+        def Enter()
+          g:result ..= "entered-base/"
+        enddef
+        def Exit()
+          g:result ..= "exited-base"
+        enddef
+      endclass
+
+      class CWithEE extends BaseWithEE
+        def Enter()
+          g:result ..= "entered-child/"
+        enddef
+        def Exit()
+          g:result ..= "exited-child"
+        enddef
+      endclass
+
+      def With(ee: BaseWithEE, F: func)
+        ee.Enter()
+        defer ee.Exit()
+        F()
+      enddef
+
+      g:result = ''
+      var obj = CWithEE.new()
+      obj->With(() => {
+        g:result ..= "called/"
+      })
+      assert_equal('entered-child/called/exited-child', g:result)
+  END
+  v9.CheckScriptSuccess(lines)
+  unlet g:result
 enddef
 
 
index a98d4c3f2671c40302f01ae6c2678feae1fccbaa..2c86c48c202641484a938a85c35d8d2c63c93edc 100644 (file)
@@ -695,6 +695,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1292,
 /**/
     1291,
 /**/
index fd98bb502c3ab3322bcbbb02b23a72d2484a5d0c..4df97c6447b309950b71b83f3492686b75d87a23 100644 (file)
@@ -380,6 +380,8 @@ typedef struct {
 typedef struct {
     char_u       *fre_func_name;       // function name for legacy function
     loopvarinfo_T fre_loopvar_info;    // info about variables inside loops
+    class_T      *fre_class;           // class for a method
+    int                  fre_method_idx;       // method index on "fre_class"
 } funcref_extra_T;
 
 // arguments to ISN_FUNCREF
index a3bb411e87f606afeac849a81cdcad82e0ed6856..0f002f946b0fdbf80ff54d73347d1a82335cd9b2 100644 (file)
@@ -1068,7 +1068,7 @@ compile_nested_function(exarg_T *eap, cctx_T *cctx, garray_T *lines_to_free)
                                            ASSIGN_CONST, ufunc->uf_func_type);
        if (lvar == NULL)
            goto theend;
-       if (generate_FUNCREF(cctx, ufunc, &funcref_isn) == FAIL)
+       if (generate_FUNCREF(cctx, ufunc, NULL, 0, &funcref_isn) == FAIL)
            goto theend;
        r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
     }
index 3498e547e2a5638055853cc013de8f3de7ecd52e..605cfb25c8af1fddb96525563252fa79bd808df7 100644 (file)
@@ -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 *instr, int instr_count, ufunc_T *ufunc)
                    }
                    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(
index c65fc79fd591b1d85825c89394e65a06cd671dc5..88d9ec18d62ba5aac3bf8cc332492d6141422ffd 100644 (file)
@@ -383,7 +383,12 @@ compile_class_object_index(cctx_T *cctx, char_u **arg, type_T *type)
            // uf_name[] only being 4 characters.
            char_u *ufname = (char_u *)fp->uf_name;
            if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
-               return generate_FUNCREF(cctx, fp, NULL);
+           {
+               if (type->tt_type == VAR_OBJECT
+                    && (cl->class_flags & (CLASS_INTERFACE | CLASS_EXTENDED)))
+                   return generate_FUNCREF(cctx, fp, cl, i, NULL);
+               return generate_FUNCREF(cctx, fp, NULL, 0, NULL);
+           }
        }
 
        semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
@@ -1308,7 +1313,7 @@ compile_lambda(char_u **arg, cctx_T *cctx)
        // The function reference count will be 1.  When the ISN_FUNCREF
        // instruction is deleted the reference count is decremented and the
        // function is freed.
-       return generate_FUNCREF(cctx, ufunc, NULL);
+       return generate_FUNCREF(cctx, ufunc, NULL, 0, NULL);
     }
 
     func_ptr_unref(ufunc);
index ce7342b7b76c1888b324ed4a843e1fbc60979303..cdf037942c71a599796ba1cd8c8df919ac5b4ca2 100644 (file)
@@ -1328,12 +1328,16 @@ generate_NEWDICT(cctx_T *cctx, int count, int use_null)
 
 /*
  * Generate an ISN_FUNCREF instruction.
+ * For "obj.Method" "cl" is the class of the object (can be an interface or a
+ * base class) and "fi" the index of the method on that class.
  * "isnp" is set to the instruction, so that fr_dfunc_idx can be set later.
  */
     int
 generate_FUNCREF(
        cctx_T      *cctx,
        ufunc_T     *ufunc,
+       class_T     *cl,
+       int         fi,
        isn_T       **isnp)
 {
     isn_T          *isn;
@@ -1349,17 +1353,23 @@ generate_FUNCREF(
        *isnp = isn;
 
     has_vars = get_loop_var_info(cctx, &loopinfo);
-    if (ufunc->uf_def_status == UF_NOT_COMPILED || has_vars)
+    if (ufunc->uf_def_status == UF_NOT_COMPILED || has_vars || cl != NULL)
     {
        extra = ALLOC_CLEAR_ONE(funcref_extra_T);
        if (extra == NULL)
            return FAIL;
        isn->isn_arg.funcref.fr_extra = extra;
        extra->fre_loopvar_info = loopinfo;
+       if (cl != NULL)
+       {
+           extra->fre_class = cl;
+           ++cl->class_refcount;
+           extra->fre_method_idx = fi;
+       }
     }
-    if (ufunc->uf_def_status == UF_NOT_COMPILED)
+    if (ufunc->uf_def_status == UF_NOT_COMPILED || cl != NULL)
        extra->fre_func_name = vim_strsave(ufunc->uf_name);
-    else
+    if (ufunc->uf_def_status != UF_NOT_COMPILED && cl == NULL)
     {
        if (isnp == NULL && ufunc->uf_def_status == UF_TO_BE_COMPILED)
            // compile the function now, we need the uf_dfunc_idx value
@@ -2484,6 +2494,8 @@ delete_instr(isn_T *isn)
                        func_unref(name);
                        vim_free(name);
                    }
+                   if (extra->fre_class != NULL)
+                       class_unref(extra->fre_class);
                    vim_free(extra);
                }
            }