Patch 8.2.2672
Problem:    Vim9: cannot use :lockvar and :unlockvar in compiled script.
Solution:   Implement locking support.
Files:      src/vim9compile.c, src/errors.h, src/testdir/test_vim9_cmd.vim


*** ../vim-8.2.2671/src/vim9compile.c   2021-03-26 20:41:24.773620612 +0100
--- src/vim9compile.c   2021-03-28 20:18:26.463502080 +0200
***************
*** 6708,6729 ****
  }
  
  /*
!  * compile "unlet var", "lock var" and "unlock var"
!  * "arg" points to "var".
   */
!     static char_u *
! compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
  {
!     char_u *p = arg;
  
!     if (eap->cmdidx != CMD_unlet)
      {
!       emsg("Sorry, :lock and unlock not implemented yet");
!       return NULL;
      }
  
!     ex_unletlock(eap, p, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
!                                                         compile_unlet, cctx);
      return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
  }
  
--- 6708,6774 ----
  }
  
  /*
!  * Callback passed to ex_unletlock().
   */
!     static int
! compile_lock_unlock(
!     lval_T  *lvp,
!     char_u  *name_end,
!     exarg_T *eap,
!     int           deep UNUSED,
!     void    *coookie)
  {
!     cctx_T    *cctx = coookie;
!     int               cc = *name_end;
!     char_u    *p = lvp->ll_name;
!     int               ret = OK;
!     size_t    len;
!     char_u    *buf;
! 
!     if (cctx->ctx_skip == SKIP_YES)
!       return OK;
! 
!     // Cannot use :lockvar and :unlockvar on local variables.
!     if (p[1] != ':')
!     {
!       char_u *end = skip_var_one(p, FALSE);
! 
!       if (lookup_local(p, end - p, NULL, cctx) == OK)
!       {
!           emsg(_(e_cannot_lock_unlock_local_variable));
!           return FAIL;
!       }
!     }
  
!     // Checking is done at runtime.
!     *name_end = NUL;
!     len = name_end - p + 20;
!     buf = alloc(len);
!     if (buf == NULL)
!       ret = FAIL;
!     else
      {
!       vim_snprintf((char *)buf, len, "%s %s",
!               eap->cmdidx == CMD_lockvar ? "lockvar" : "unlockvar",
!               p);
!       ret = generate_EXEC(cctx, buf);
! 
!       vim_free(buf);
!       *name_end = cc;
      }
+     return ret;
+ }
  
! /*
!  * compile "unlet var", "lock var" and "unlock var"
!  * "arg" points to "var".
!  */
!     static char_u *
! compile_unletlock(char_u *arg, exarg_T *eap, cctx_T *cctx)
! {
!     ex_unletlock(eap, arg, 0, GLV_NO_AUTOLOAD | GLV_COMPILING,
!           eap->cmdidx == CMD_unlet ? compile_unlet : compile_lock_unlock,
!           cctx);
      return eap->nextcmd == NULL ? (char_u *)"" : eap->nextcmd;
  }
  
*** ../vim-8.2.2671/src/errors.h        2021-03-26 20:41:24.773620612 +0100
--- src/errors.h        2021-03-28 20:17:55.667566727 +0200
***************
*** 391,393 ****
--- 391,395 ----
        INIT(= N_("E1176: Misplaced command modifier"));
  EXTERN char e_for_loop_on_str_not_supported[]
        INIT(= N_("E1177: For loop on %s not supported"));
+ EXTERN char e_cannot_lock_unlock_local_variable[]
+       INIT(= N_("E1178: Cannot lock or unlock a local variable"));
*** ../vim-8.2.2671/src/testdir/test_vim9_cmd.vim       2021-03-25 
22:15:24.404073755 +0100
--- src/testdir/test_vim9_cmd.vim       2021-03-28 20:37:23.056764057 +0200
***************
*** 1135,1138 ****
--- 1135,1176 ----
    CheckDefExecFailure(lines, 'E171:', 1)
  enddef
  
+ let s:theList = [1, 2, 3]
+ 
+ def Test_lockvar()
+   s:theList[1] = 22
+   assert_equal([1, 22, 3], s:theList)
+   lockvar s:theList
+   assert_fails('theList[1] = 77', 'E741:')
+   unlockvar s:theList
+   s:theList[1] = 44
+   assert_equal([1, 44, 3], s:theList)
+ 
+   var lines =<< trim END
+       vim9script
+       var theList = [1, 2, 3]
+       def SetList()
+         theList[1] = 22
+         assert_equal([1, 22, 3], theList)
+         lockvar theList
+         theList[1] = 77
+       enddef
+       SetList()
+   END
+   CheckScriptFailure(lines, 'E1119', 4)
+ 
+   lines =<< trim END
+       var theList = [1, 2, 3]
+       lockvar theList
+   END
+   CheckDefFailure(lines, 'E1178', 2)
+ 
+   lines =<< trim END
+       var theList = [1, 2, 3]
+       unlockvar theList
+   END
+   CheckDefFailure(lines, 'E1178', 2)
+ enddef
+ 
+ 
  " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
*** ../vim-8.2.2671/src/version.c       2021-03-28 16:26:37.504193381 +0200
--- src/version.c       2021-03-28 20:01:43.570062797 +0200
***************
*** 752,753 ****
--- 752,755 ----
  {   /* Add new patch number below this line */
+ /**/
+     2672,
  /**/

-- 
hundred-and-one symptoms of being an internet addict:
32. You don't know what sex three of your closest friends are, because they
    have neutral nicknames and you never bothered to ask.

 /// 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/202103281838.12SIcx6T938529%40masaka.moolenaar.net.

Raspunde prin e-mail lui