Patch 8.2.2765
Problem:    Vim9: not all blob operations work.
Solution:   Run more tests also with Vim9 script and :def functions.  Fix what
            doesn't work.
Files:      src/eval.c, src/blob.c, src/proto/blob.pro, src/vim9execute.c,
            src/errors.h, src/testdir/vim9.vim, src/testdir/test_blob.vim


*** ../vim-8.2.2764/src/eval.c  2021-04-13 21:47:59.540306700 +0200
--- src/eval.c  2021-04-14 20:04:07.812671043 +0200
***************
*** 1175,1186 ****
                lp->ll_n1 = (long)tv_get_number(&var1);
            clear_tv(&var1);
  
!           if (lp->ll_n1 < 0
!                   || lp->ll_n1 > bloblen
!                   || (lp->ll_range && lp->ll_n1 == bloblen))
            {
-               if (!quiet)
-                   semsg(_(e_blobidx), lp->ll_n1);
                clear_tv(&var2);
                return NULL;
            }
--- 1175,1183 ----
                lp->ll_n1 = (long)tv_get_number(&var1);
            clear_tv(&var1);
  
!           if (check_blob_index(bloblen, lp->ll_n1, lp->ll_range, quiet)
!                                                                      == FAIL)
            {
                clear_tv(&var2);
                return NULL;
            }
***************
*** 1188,1201 ****
            {
                lp->ll_n2 = (long)tv_get_number(&var2);
                clear_tv(&var2);
!               if (lp->ll_n2 < 0
!                       || lp->ll_n2 >= bloblen
!                       || lp->ll_n2 < lp->ll_n1)
!               {
!                   if (!quiet)
!                       semsg(_(e_blobidx), lp->ll_n2);
                    return NULL;
-               }
            }
            lp->ll_blob = lp->ll_tv->vval.v_blob;
            lp->ll_tv = NULL;
--- 1185,1193 ----
            {
                lp->ll_n2 = (long)tv_get_number(&var2);
                clear_tv(&var2);
!               if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
!                                                                      == FAIL)
                    return NULL;
            }
            lp->ll_blob = lp->ll_tv->vval.v_blob;
            lp->ll_tv = NULL;
*** ../vim-8.2.2764/src/blob.c  2021-04-12 21:20:58.634708976 +0200
--- src/blob.c  2021-04-14 20:16:00.478952273 +0200
***************
*** 337,342 ****
--- 337,372 ----
  }
  
  /*
+  * Check if "n1"- is a valid index for a blobl with length "bloblen".
+  */
+     int
+ check_blob_index(long bloblen, varnumber_T n1, int is_range, int quiet)
+ {
+     if (n1 < 0 || n1 > bloblen)
+     {
+       if (!quiet)
+           semsg(_(e_blobidx), n1);
+       return FAIL;
+     }
+     return OK;
+ }
+ 
+ /*
+  * Check if "n1"-"n2" is a valid range for a blob with length "bloblen".
+  */
+     int
+ check_blob_range(long bloblen, varnumber_T n1, varnumber_T n2, int quiet)
+ {
+     if (n2 < 0 || n2 >= bloblen || n2 < n1)
+     {
+       if (!quiet)
+           semsg(_(e_blobidx), n2);
+       return FAIL;
+     }
+     return OK;
+ }
+ 
+ /*
   * Set bytes "n1" to "n2" (inclusive) in "dest" to the value of "src".
   * Caller must make sure "src" is a blob.
   * Returns FAIL if the number of bytes does not match.
*** ../vim-8.2.2764/src/proto/blob.pro  2021-04-12 21:20:58.634708976 +0200
--- src/proto/blob.pro  2021-04-14 20:03:43.424733990 +0200
***************
*** 14,19 ****
--- 14,21 ----
  char_u *blob2string(blob_T *blob, char_u **tofree, char_u *numbuf);
  blob_T *string2blob(char_u *str);
  int blob_slice_or_index(blob_T *blob, int is_range, varnumber_T n1, 
varnumber_T n2, int exclusive, typval_T *rettv);
+ int check_blob_index(long bloblen, varnumber_T n1, int is_range, int quiet);
+ int check_blob_range(long bloblen, varnumber_T n1, varnumber_T n2, int quiet);
  int blob_set_range(blob_T *dest, long n1, long n2, typval_T *src);
  void blob_remove(typval_T *argvars, typval_T *rettv);
  /* vim: set ft=c : */
*** ../vim-8.2.2764/src/vim9execute.c   2021-04-12 21:20:58.634708976 +0200
--- src/vim9execute.c   2021-04-14 20:18:14.670665403 +0200
***************
*** 2278,2285 ****
                            if (error)
                                status = FAIL;
                            else
!                               status = blob_set_range(tv_dest->vval.v_blob,
!                                                                  n1, n2, tv);
                        }
                    }
  
--- 2278,2295 ----
                            if (error)
                                status = FAIL;
                            else
!                           {
!                               long    bloblen = 
blob_len(tv_dest->vval.v_blob);
! 
!                               if (check_blob_index(bloblen,
!                                                      n1, TRUE, FALSE) == FAIL
!                                       || check_blob_range(bloblen,
!                                                       n1, n2, FALSE) == FAIL)
!                                   status = FAIL;
!                               else
!                                   status = blob_set_range(
!                                            tv_dest->vval.v_blob, n1, n2, tv);
!                           }
                        }
                    }
  
*** ../vim-8.2.2764/src/errors.h        2021-04-12 21:20:58.634708976 +0200
--- src/errors.h        2021-04-14 20:30:56.120879793 +0200
***************
*** 401,403 ****
--- 401,405 ----
        INIT(= N_("E1181: Cannot use an underscore here"));
  EXTERN char e_blob_required[]
        INIT(= N_("E1182: Blob required"));
+ EXTERN char e_cannot_use_range_with_assignment_operator_str[]
+       INIT(= N_("E1183: Cannot use a range with an assignment operator: %s"));
*** ../vim-8.2.2764/src/testdir/vim9.vim        2021-04-12 21:20:58.638708968 
+0200
--- src/testdir/vim9.vim        2021-04-14 19:31:52.721567593 +0200
***************
*** 144,151 ****
--- 144,166 ----
    try
      exe 'so ' .. fname
      call Func()
+   finally
      delfunc! Func
+     call chdir(cwd)
+     call delete(fname)
+   endtry
+ endfunc
+ 
+ " Check that "lines" inside a legacy function results in the expected error
+ func CheckLegacyFailure(lines, error)
+   let cwd = getcwd()
+   let fname = 'XlegacyFails' .. s:sequence
+   let s:sequence += 1
+   call writefile(['func Func()'] + a:lines + ['endfunc', 'call Func()'], 
fname)
+   try
+     call assert_fails('so ' .. fname, a:error)
    finally
+     delfunc! Func
      call chdir(cwd)
      call delete(fname)
    endtry
***************
*** 168,170 ****
--- 183,217 ----
    CheckDefSuccess(vim9lines)
    CheckScriptSuccess(['vim9script'] + vim9lines)
  enddef
+ 
+ " Execute "lines" in a legacy function, :def function and Vim9 script.
+ " Use 'VAR' for a declaration.
+ " Use 'LET' for an assignment
+ " Use ' #"' for a comment
+ def CheckLegacyAndVim9Failure(lines: list<string>, error: any)
+   var legacyError: string
+   var defError: string
+   var scriptError: string
+ 
+   if type(error) == type('string')
+     legacyError = error
+     defError = error
+     scriptError = error
+   else
+     legacyError = error[0]
+     defError = error[1]
+     scriptError = error[2]
+   endif
+ 
+   var legacylines = lines->mapnew((_, v) =>
+                               v->substitute('\<VAR\>', 'let', 'g')
+                                ->substitute('\<LET\>', 'let', 'g')
+                                ->substitute('#"', ' "', 'g'))
+   CheckLegacyFailure(legacylines, legacyError)
+ 
+   var vim9lines = lines->mapnew((_, v) =>
+                               v->substitute('\<VAR\>', 'var', 'g')
+                                ->substitute('\<LET ', '', 'g'))
+   CheckDefExecFailure(vim9lines, defError)
+   CheckScriptFailure(['vim9script'] + vim9lines, scriptError)
+ enddef
*** ../vim-8.2.2764/src/testdir/test_blob.vim   2021-04-12 21:20:58.638708968 
+0200
--- src/testdir/test_blob.vim   2021-04-14 20:31:08.980848397 +0200
***************
*** 76,91 ****
    END
    call CheckLegacyAndVim9Success(lines)
  
!   " TODO: move to above once it works
!   let b = 0zDEADBEEF
!   call assert_fails('let b[2 : 3] = 0z112233', 'E972:')
!   call assert_fails('let b[2 : 3] = 0z11', 'E972:')
!   call assert_fails('let b[3 : 2] = 0z', 'E979:')
! 
!   call assert_fails('let b ..= 0z33', 'E734:')
!   call assert_fails('let b ..= "xx"', 'E734:')
!   call assert_fails('let b += "xx"', 'E734:')
!   call assert_fails('let b[1 : 1] ..= 0z55', 'E734:')
  endfunc
  
  func Test_blob_get_range()
--- 76,122 ----
    END
    call CheckLegacyAndVim9Success(lines)
  
!   let lines =<< trim END
!       VAR b = 0zDEADBEEF
!       LET b[2 : 3] = 0z112233
!   END
!   call CheckLegacyAndVim9Failure(lines, 'E972:')
! 
!   let lines =<< trim END
!       VAR b = 0zDEADBEEF
!       LET b[2 : 3] = 0z11
!   END
!   call CheckLegacyAndVim9Failure(lines, 'E972:')
! 
!   let lines =<< trim END
!       VAR b = 0zDEADBEEF
!       LET b[3 : 2] = 0z
!   END
!   call CheckLegacyAndVim9Failure(lines, 'E979:')
! 
!   let lines =<< trim END
!       VAR b = 0zDEADBEEF
!       LET b ..= 0z33
!   END
!   call CheckLegacyAndVim9Failure(lines, ['E734:', 'E1019:', 'E734:'])
! 
!   let lines =<< trim END
!       VAR b = 0zDEADBEEF
!       LET b ..= "xx"
!   END
!   call CheckLegacyAndVim9Failure(lines, ['E734:', 'E1019:', 'E734:'])
! 
!   let lines =<< trim END
!       VAR b = 0zDEADBEEF
!       LET b += "xx"
!   END
!   call CheckLegacyAndVim9Failure(lines, ['E734:', 'E1012:', 'E734:'])
! 
!   let lines =<< trim END
!       VAR b = 0zDEADBEEF
!       LET b[1 : 1] ..= 0z55
!   END
!   call CheckLegacyAndVim9Failure(lines, ['E734:', 'E1183:', 'E734:'])
  endfunc
  
  func Test_blob_get_range()
*** ../vim-8.2.2764/src/version.c       2021-04-14 17:06:39.928955007 +0200
--- src/version.c       2021-04-14 19:14:54.360429391 +0200
***************
*** 752,753 ****
--- 752,755 ----
  {   /* Add new patch number below this line */
+ /**/
+     2765,
  /**/

-- 
hundred-and-one symptoms of being an internet addict:
93. New mail alarm on your palmtop annoys other churchgoers.

 /// Bram Moolenaar -- b...@moolenaar.net -- 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 vim_dev+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/202104141835.13EIZo4t1361793%40masaka.moolenaar.net.

Raspunde prin e-mail lui