Patch 9.0.1211
Problem:    Storing value in interface member does not always work.
Solution:   Convert the index on the interface to the index on the object.
Files:      src/vim9instr.c, src/vim9.h, src/vim9compile.c,
            src/vim9execute.c, src/testdir/test_vim9_class.vim


*** ../vim-9.0.1210/src/vim9instr.c     2023-01-16 19:43:43.386873678 +0000
--- src/vim9instr.c     2023-01-16 20:07:19.806606991 +0000
***************
*** 2521,2526 ****
--- 2521,2530 ----
            class_unref(isn->isn_arg.classmember.cm_class);
            break;
  
+       case ISN_STOREINDEX:
+           class_unref(isn->isn_arg.storeindex.si_class);
+           break;
+ 
        case ISN_TRY:
            vim_free(isn->isn_arg.tryref.try_ref);
            break;
***************
*** 2622,2628 ****
        case ISN_SLICE:
        case ISN_SOURCE:
        case ISN_STORE:
-       case ISN_STOREINDEX:
        case ISN_STORENR:
        case ISN_STOREOUTER:
        case ISN_STORE_THIS:
--- 2626,2631 ----
*** ../vim-9.0.1210/src/vim9.h  2023-01-16 19:43:43.386873678 +0000
--- src/vim9.h  2023-01-16 20:11:40.642572161 +0000
***************
*** 78,85 ****
      // ISN_STOREOTHER, // pop into other script variable isn_arg.other.
  
      ISN_STORENR,    // store number into local variable 
isn_arg.storenr.stnr_idx
!     ISN_STOREINDEX,   // store into list or dictionary, type isn_arg.vartype,
!                       // value/index/variable on stack
      ISN_STORERANGE,   // store into blob,
                        // value/index 1/index 2/variable on stack
  
--- 78,85 ----
      // ISN_STOREOTHER, // pop into other script variable isn_arg.other.
  
      ISN_STORENR,    // store number into local variable 
isn_arg.storenr.stnr_idx
!     ISN_STOREINDEX,   // store into list or dictionary, using
!                       // isn_arg.storeindex; value/index/variable on stack
      ISN_STORERANGE,   // store into blob,
                        // value/index 1/index 2/variable on stack
  
***************
*** 486,491 ****
--- 486,496 ----
      class_T   *cm_class;
      int               cm_idx;
  } classmember_T;
+ // arguments to ISN_STOREINDEX
+ typedef struct {
+     vartype_T si_vartype;
+     class_T   *si_class;
+ } storeindex_T;
  
  /*
   * Instruction
***************
*** 540,545 ****
--- 545,551 ----
        echowin_T           echowin;
        construct_T         construct;
        classmember_T       classmember;
+       storeindex_T        storeindex;
      } isn_arg;
  };
  
*** ../vim-9.0.1210/src/vim9compile.c   2023-01-16 19:43:43.390873675 +0000
--- src/vim9compile.c   2023-01-16 20:37:28.921647470 +0000
***************
*** 2178,2184 ****
  
                if (isn == NULL)
                    return FAIL;
!               isn->isn_arg.vartype = dest_type;
            }
        }
        else if (range)
--- 2178,2199 ----
  
                if (isn == NULL)
                    return FAIL;
!               isn->isn_arg.storeindex.si_vartype = dest_type;
!               isn->isn_arg.storeindex.si_class = NULL;
! 
!               if (dest_type == VAR_OBJECT)
!               {
!                   class_T *cl = (class_T *)lhs->lhs_type->tt_member;
! 
!                   if (cl->class_flags & CLASS_INTERFACE)
!                   {
!                       // "this.value": load "this" object and get the value
!                       // at index for an object or class member get the type
!                       // of the member
!                       isn->isn_arg.storeindex.si_class = cl;
!                       ++cl->class_refcount;
!                   }
!               }
            }
        }
        else if (range)
*** ../vim-9.0.1210/src/vim9execute.c   2023-01-16 19:43:43.390873675 +0000
--- src/vim9execute.c   2023-01-16 20:32:20.957909568 +0000
***************
*** 2108,2114 ****
      static int
  execute_storeindex(isn_T *iptr, ectx_T *ectx)
  {
!     vartype_T dest_type = iptr->isn_arg.vartype;
      typval_T  *tv;
      typval_T  *tv_idx = STACK_TV_BOT(-2);
      typval_T  *tv_dest = STACK_TV_BOT(-1);
--- 2108,2114 ----
      static int
  execute_storeindex(isn_T *iptr, ectx_T *ectx)
  {
!     vartype_T dest_type = iptr->isn_arg.storeindex.si_vartype;
      typval_T  *tv;
      typval_T  *tv_idx = STACK_TV_BOT(-2);
      typval_T  *tv_dest = STACK_TV_BOT(-1);
***************
*** 2243,2248 ****
--- 2243,2254 ----
            long            idx = (long)tv_idx->vval.v_number;
            object_T        *obj = tv_dest->vval.v_object;
            typval_T        *otv = (typval_T *)(obj + 1);
+ 
+           class_T         *itf = iptr->isn_arg.storeindex.si_class;
+           if (itf != NULL)
+               // convert interface member index to class member index
+               idx = object_index_from_itf_index(itf, idx, obj->obj_class);
+ 
            clear_tv(&otv[idx]);
            otv[idx] = *tv;
        }
***************
*** 6475,6481 ****
  
            case ISN_STOREINDEX:
                smsg("%s%4d STOREINDEX %s", pfx, current,
!                                         vartype_name(iptr->isn_arg.vartype));
                break;
  
            case ISN_STORERANGE:
--- 6481,6487 ----
  
            case ISN_STOREINDEX:
                smsg("%s%4d STOREINDEX %s", pfx, current,
!                           vartype_name(iptr->isn_arg.storeindex.si_vartype));
                break;
  
            case ISN_STORERANGE:
*** ../vim-9.0.1210/src/testdir/test_vim9_class.vim     2023-01-16 
19:43:43.390873675 +0000
--- src/testdir/test_vim9_class.vim     2023-01-16 20:27:41.970248850 +0000
***************
*** 876,889 ****
        vim9script
  
        interface Result
!         this.label: string
          this.errpos: number
        endinterface
  
        # order of members is opposite of interface
        class Failure implements Result
          this.errpos: number = 42
!         this.label: string = 'label'
        endclass
  
        def Test()
--- 876,889 ----
        vim9script
  
        interface Result
!         public this.label: string
          this.errpos: number
        endinterface
  
        # order of members is opposite of interface
        class Failure implements Result
          this.errpos: number = 42
!         public this.label: string = 'label'
        endclass
  
        def Test()
***************
*** 891,896 ****
--- 891,900 ----
  
          assert_equal('label', result.label)
          assert_equal(42, result.errpos)
+ 
+         result.label = 'different'
+         assert_equal('different', result.label)
+         assert_equal(42, result.errpos)
        enddef
  
        Test()
*** ../vim-9.0.1210/src/version.c       2023-01-16 19:50:59.594796232 +0000
--- src/version.c       2023-01-16 20:46:45.929320102 +0000
***************
*** 697,698 ****
--- 697,700 ----
  {   /* Add new patch number below this line */
+ /**/
+     1211,
  /**/

-- 
For a moment, nothing happened.
Then, after a second or so, nothing continued to happen.
                -- Douglas Adams, "The Hitchhiker's Guide to the Galaxy"

 /// 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/20230116204825.4C9DB1C0916%40moolenaar.net.

Raspunde prin e-mail lui