Patch 9.0.1292
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)
Files:      src/vim9expr.c, src/vim9.h, src/vim9instr.c,
            src/proto/vim9instr.pro, src/vim9compile.c, src/vim9execute.c,
            src/testdir/test_vim9_class.vim


*** ../vim-9.0.1291/src/vim9expr.c      2023-01-28 15:19:36.956757160 +0000
--- src/vim9expr.c      2023-02-08 20:40:25.444855283 +0000
***************
*** 383,389 ****
            // 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);
        }
  
        semsg(_(e_member_not_found_on_object_str_str), cl->class_name, name);
--- 383,394 ----
            // uf_name[] only being 4 characters.
            char_u *ufname = (char_u *)fp->uf_name;
            if (STRNCMP(name, ufname, len) == 0 && ufname[len] == NUL)
!           {
!               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,1314 ****
        // 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);
      }
  
      func_ptr_unref(ufunc);
--- 1313,1319 ----
        // 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, 0, NULL);
      }
  
      func_ptr_unref(ufunc);
*** ../vim-9.0.1291/src/vim9.h  2023-01-28 15:19:36.956757160 +0000
--- src/vim9.h  2023-02-08 20:21:17.106359001 +0000
***************
*** 380,385 ****
--- 380,387 ----
  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
*** ../vim-9.0.1291/src/vim9instr.c     2023-01-29 14:11:21.558365399 +0000
--- src/vim9instr.c     2023-02-08 20:46:17.556520486 +0000
***************
*** 1328,1339 ****
--- 1328,1343 ----
  
  /*
   * 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,1365 ****
        *isnp = isn;
  
      has_vars = get_loop_var_info(cctx, &loopinfo);
!     if (ufunc->uf_def_status == UF_NOT_COMPILED || has_vars)
      {
        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 (ufunc->uf_def_status == UF_NOT_COMPILED)
        extra->fre_func_name = vim_strsave(ufunc->uf_name);
!     else
      {
        if (isnp == NULL && ufunc->uf_def_status == UF_TO_BE_COMPILED)
            // compile the function now, we need the uf_dfunc_idx value
--- 1353,1375 ----
        *isnp = isn;
  
      has_vars = get_loop_var_info(cctx, &loopinfo);
!     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 || cl != NULL)
        extra->fre_func_name = vim_strsave(ufunc->uf_name);
!     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,2489 ****
--- 2494,2501 ----
                        func_unref(name);
                        vim_free(name);
                    }
+                   if (extra->fre_class != NULL)
+                       class_unref(extra->fre_class);
                    vim_free(extra);
                }
            }
*** ../vim-9.0.1291/src/proto/vim9instr.pro     2023-01-28 15:19:36.956757160 
+0000
--- src/proto/vim9instr.pro     2023-02-08 20:22:33.210330370 +0000
***************
*** 44,50 ****
  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_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);
--- 44,50 ----
  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, 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);
*** ../vim-9.0.1291/src/vim9compile.c   2023-01-30 21:12:30.547422897 +0000
--- src/vim9compile.c   2023-02-08 20:18:12.766429159 +0000
***************
*** 1068,1074 ****
                                            ASSIGN_CONST, ufunc->uf_func_type);
        if (lvar == NULL)
            goto theend;
!       if (generate_FUNCREF(cctx, ufunc, &funcref_isn) == FAIL)
            goto theend;
        r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
      }
--- 1068,1074 ----
                                            ASSIGN_CONST, ufunc->uf_func_type);
        if (lvar == NULL)
            goto theend;
!       if (generate_FUNCREF(cctx, ufunc, NULL, 0, &funcref_isn) == FAIL)
            goto theend;
        r = generate_STORE(cctx, ISN_STORE, lvar->lv_idx, NULL);
      }
*** ../vim-9.0.1291/src/vim9execute.c   2023-01-28 15:19:36.960757169 +0000
--- src/vim9execute.c   2023-02-08 20:37:21.457069333 +0000
***************
*** 4291,4297 ****
                        vim_free(pt);
                        goto theend;
                    }
!                   if (extra == NULL || extra->fre_func_name == NULL)
                    {
                        dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
                                                       + funcref->fr_dfunc_idx;
--- 4291,4314 ----
                        vim_free(pt);
                        goto theend;
                    }
!                   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,4305 ****
--- 4316,4324 ----
                        ufunc = pt_dfunc->df_ufunc;
                    }
                    else
+                   {
                        ufunc = find_func(extra->fre_func_name, FALSE);
+                   }
                    if (ufunc == NULL)
                    {
                        SOURCING_LNUM = iptr->isn_lnum;
***************
*** 6727,6734 ****
                    }
                    else
                        name = extra->fre_func_name;
!                   if (extra == NULL || extra->fre_loopvar_info.lvi_depth == 0)
                        smsg("%s%4d FUNCREF %s", pfx, current, name);
                    else
                    {
                        char_u  *info = printable_loopvarinfo(
--- 6746,6761 ----
                    }
                    else
                        name = extra->fre_func_name;
!                   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(
*** ../vim-9.0.1291/src/testdir/test_vim9_class.vim     2023-02-04 
15:45:23.999159934 +0000
--- src/testdir/test_vim9_class.vim     2023-02-08 20:39:17.776929980 +0000
***************
*** 1480,1485 ****
--- 1480,1522 ----
    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
  
  
*** ../vim-9.0.1291/src/version.c       2023-02-07 19:37:26.482256426 +0000
--- src/version.c       2023-02-08 20:17:54.150436316 +0000
***************
*** 697,698 ****
--- 697,700 ----
  {   /* Add new patch number below this line */
+ /**/
+     1292,
  /**/

-- 
Contrary to popular belief, Unix is user friendly.
It just happens to be selective about who it makes friends with.
                                               -- Dave Parnas

 /// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net   \\\
///                                                                      \\\
\\\        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/20230208205556.A9D8D1C2487%40moolenaar.net.

Raspunde prin e-mail lui