]> granicus.if.org Git - vim/commitdiff
patch 8.2.2777: Vim9: blob operations not tested in all ways v8.2.2777
authorBram Moolenaar <Bram@vim.org>
Sat, 17 Apr 2021 18:44:56 +0000 (20:44 +0200)
committerBram Moolenaar <Bram@vim.org>
Sat, 17 Apr 2021 18:44:56 +0000 (20:44 +0200)
Problem:    Vim9: blob operations not tested in all ways.
Solution:   Run tests with CheckLegacyAndVim9Success().  Make blob assign with
            index work.

src/blob.c
src/errors.h
src/proto/blob.pro
src/testdir/test_blob.vim
src/testdir/test_vim9_disassemble.vim
src/version.c
src/vim9compile.c
src/vim9execute.c

index 114dacdec2d1c378f95f8167cfde80009865500b..9f59777073226e120d68d3a73f46bdf2d5399a1a 100644 (file)
@@ -125,13 +125,33 @@ blob_get(blob_T *b, int idx)
 }
 
 /*
- * Store one byte "c" in blob "b" at "idx".
+ * Store one byte "byte" in blob "blob" at "idx".
  * Caller must make sure that "idx" is valid.
  */
     void
-blob_set(blob_T *b, int idx, char_u c)
+blob_set(blob_T *blob, int idx, int byte)
 {
-    ((char_u*)b->bv_ga.ga_data)[idx] = c;
+    ((char_u*)blob->bv_ga.ga_data)[idx] = byte;
+}
+
+/*
+ * Store one byte "byte" in blob "blob" at "idx".
+ * Append one byte if needed.
+ */
+    void
+blob_set_append(blob_T *blob, int idx, int byte)
+{
+    garray_T *gap = &blob->bv_ga;
+
+    // Allow for appending a byte.  Setting a byte beyond
+    // the end is an error otherwise.
+    if (idx < gap->ga_len
+           || (idx == gap->ga_len && ga_grow(gap, 1) == OK))
+    {
+       blob_set(blob, idx, byte);
+       if (idx == gap->ga_len)
+           ++gap->ga_len;
+    }
 }
 
 /*
index 99f3aaff5a551d208784872ee9297105adec2218..1e091867f211bd00495d145701213334a5ab68c7 100644 (file)
@@ -403,3 +403,5 @@ 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"));
+EXTERN char e_blob_not_set[]
+       INIT(= N_("E1184: Blob not set"));
index 3adaf0ffe5fe7dcbbaea96436e355633b837d2ee..157f15bbcdf3b95c71dcd02dc7a505652788da48 100644 (file)
@@ -7,7 +7,8 @@ void blob_free(blob_T *b);
 void blob_unref(blob_T *b);
 long blob_len(blob_T *b);
 int blob_get(blob_T *b, int idx);
-void blob_set(blob_T *b, int idx, char_u c);
+void blob_set(blob_T *blob, int idx, int byte);
+void blob_set_append(blob_T *blob, int idx, int byte);
 int blob_equal(blob_T *b1, blob_T *b2);
 int read_blob(FILE *fd, blob_T *blob);
 int write_blob(FILE *fd, blob_T *blob);
index 3699f3bb194abf2269e0396d3849009b9366a89d..21f90efb18cd33a338288d8a37f2d82107c1596c 100644 (file)
@@ -120,88 +120,166 @@ func Test_blob_assign()
 endfunc
 
 func Test_blob_get_range()
+  let lines =<< trim END
+      VAR b = 0z0011223344
+      call assert_equal(0z2233, b[2 : 3])
+      call assert_equal(0z223344, b[2 : -1])
+      call assert_equal(0z00, b[0 : -5])
+      call assert_equal(0z, b[0 : -11])
+      call assert_equal(0z44, b[-1 :])
+      call assert_equal(0z0011223344, b[:])
+      call assert_equal(0z0011223344, b[: -1])
+      call assert_equal(0z, b[5 : 6])
+      call assert_equal(0z0011, b[-10 : 1])
+  END
+  call CheckLegacyAndVim9Success(lines)
+
+  " legacy script white space
   let b = 0z0011223344
   call assert_equal(0z2233, b[2:3])
-  call assert_equal(0z223344, b[2:-1])
-  call assert_equal(0z00, b[0:-5])
-  call assert_equal(0z, b[0:-11])
-  call assert_equal(0z44, b[-1:])
-  call assert_equal(0z0011223344, b[:])
-  call assert_equal(0z0011223344, b[:-1])
-  call assert_equal(0z, b[5:6])
-  call assert_equal(0z0011, b[-10:1])
 endfunc
 
 func Test_blob_get()
-  let b = 0z0011223344
-  call assert_equal(0x00, get(b, 0))
-  call assert_equal(0x22, get(b, 2, 999))
-  call assert_equal(0x44, get(b, 4))
-  call assert_equal(0x44, get(b, -1))
-  call assert_equal(-1, get(b, 5))
-  call assert_equal(999, get(b, 5, 999))
-  call assert_equal(-1, get(b, -8))
-  call assert_equal(999, get(b, -8, 999))
-  call assert_equal(10, get(test_null_blob(), 2, 10))
-
-  call assert_equal(0x00, b[0])
-  call assert_equal(0x22, b[2])
-  call assert_equal(0x44, b[4])
-  call assert_equal(0x44, b[-1])
-  call assert_fails('echo b[5]', 'E979:')
-  call assert_fails('echo b[-8]', 'E979:')
+  let lines =<< trim END
+      VAR b = 0z0011223344
+      call assert_equal(0x00, get(b, 0))
+      call assert_equal(0x22, get(b, 2, 999))
+      call assert_equal(0x44, get(b, 4))
+      call assert_equal(0x44, get(b, -1))
+      call assert_equal(-1, get(b, 5))
+      call assert_equal(999, get(b, 5, 999))
+      call assert_equal(-1, get(b, -8))
+      call assert_equal(999, get(b, -8, 999))
+      call assert_equal(10, get(test_null_blob(), 2, 10))
+
+      call assert_equal(0x00, b[0])
+      call assert_equal(0x22, b[2])
+      call assert_equal(0x44, b[4])
+      call assert_equal(0x44, b[-1])
+  END
+  call CheckLegacyAndVim9Success(lines)
+
+  let lines =<< trim END
+      VAR b = 0z0011223344
+      echo b[5]
+  END
+  call CheckLegacyAndVim9Failure(lines, 'E979:')
+
+  let lines =<< trim END
+      VAR b = 0z0011223344
+      echo b[-8]
+  END
+  call CheckLegacyAndVim9Failure(lines, 'E979:')
 endfunc
 
 func Test_blob_to_string()
-  let b = 0z00112233445566778899aabbccdd
-  call assert_equal('0z00112233.44556677.8899AABB.CCDD', string(b))
-  call assert_equal(b, eval(string(b)))
-  call remove(b, 4, -1)
-  call assert_equal('0z00112233', string(b))
-  call remove(b, 0, 3)
-  call assert_equal('0z', string(b))
-  call assert_equal('0z', string(test_null_blob()))
+  let lines =<< trim END
+      VAR b = 0z00112233445566778899aabbccdd
+      call assert_equal('0z00112233.44556677.8899AABB.CCDD', string(b))
+      call assert_equal(b, eval(string(b)))
+      call remove(b, 4, -1)
+      call assert_equal('0z00112233', string(b))
+      call remove(b, 0, 3)
+      call assert_equal('0z', string(b))
+      call assert_equal('0z', string(test_null_blob()))
+  END
+  call CheckLegacyAndVim9Success(lines)
 endfunc
 
 func Test_blob_compare()
-  let b1 = 0z0011
-  let b2 = 0z1100
-  let b3 = 0z001122
-  call assert_true(b1 == b1)
-  call assert_false(b1 == b2)
-  call assert_false(b1 == b3)
-  call assert_true(b1 != b2)
-  call assert_true(b1 != b3)
-  call assert_true(b1 == 0z0011)
-  call assert_fails('echo b1 == 9', 'E977:')
-  call assert_fails('echo b1 != 9', 'E977:')
-
-  call assert_false(b1 is b2)
-  let b2 = b1
-  call assert_true(b1 == b2)
-  call assert_true(b1 is b2)
-  let b2 = copy(b1)
-  call assert_true(b1 == b2)
-  call assert_false(b1 is b2)
-  let b2 = b1[:]
-  call assert_true(b1 == b2)
-  call assert_false(b1 is b2)
-  call assert_true(b1 isnot b2)
-
-  call assert_fails('let x = b1 > b2')
-  call assert_fails('let x = b1 < b2')
-  call assert_fails('let x = b1 - b2')
-  call assert_fails('let x = b1 / b2')
-  call assert_fails('let x = b1 * b2')
+  let lines =<< trim END
+      VAR b1 = 0z0011
+      VAR b2 = 0z1100
+      VAR b3 = 0z001122
+      call assert_true(b1 == b1)
+      call assert_false(b1 == b2)
+      call assert_false(b1 == b3)
+      call assert_true(b1 != b2)
+      call assert_true(b1 != b3)
+      call assert_true(b1 == 0z0011)
+
+      call assert_false(b1 is b2)
+      LET b2 = b1
+      call assert_true(b1 == b2)
+      call assert_true(b1 is b2)
+      LET b2 = copy(b1)
+      call assert_true(b1 == b2)
+      call assert_false(b1 is b2)
+      LET b2 = b1[:]
+      call assert_true(b1 == b2)
+      call assert_false(b1 is b2)
+      call assert_true(b1 isnot b2)
+  END
+  call CheckLegacyAndVim9Success(lines)
+
+  let lines =<< trim END
+      VAR b1 = 0z0011
+      echo b1 == 9
+  END
+  call CheckLegacyAndVim9Failure(lines, ['E977:', 'E1072', 'E1072'])
+
+  let lines =<< trim END
+      VAR b1 = 0z0011
+      echo b1 != 9
+  END
+  call CheckLegacyAndVim9Failure(lines, ['E977:', 'E1072', 'E1072'])
+
+  let lines =<< trim END
+      VAR b1 = 0z0011
+      VAR b2 = 0z1100
+      VAR x = b1 > b2
+  END
+  call CheckLegacyAndVim9Failure(lines, ['E978:', 'E1072:', 'E1072:'])
+
+  let lines =<< trim END
+      VAR b1 = 0z0011
+      VAR b2 = 0z1100
+      VAR x = b1 < b2
+  END
+  call CheckLegacyAndVim9Failure(lines, ['E978:', 'E1072:', 'E1072:'])
+
+  let lines =<< trim END
+      VAR b1 = 0z0011
+      VAR b2 = 0z1100
+      VAR x = b1 - b2
+  END
+  call CheckLegacyAndVim9Failure(lines, ['E974:', 'E1036:', 'E974:'])
+
+  let lines =<< trim END
+      VAR b1 = 0z0011
+      VAR b2 = 0z1100
+      VAR x = b1 / b2
+  END
+  call CheckLegacyAndVim9Failure(lines, ['E974:', 'E1036:', 'E974:'])
+
+  let lines =<< trim END
+      VAR b1 = 0z0011
+      VAR b2 = 0z1100
+      VAR x = b1 * b2
+  END
+  call CheckLegacyAndVim9Failure(lines, ['E974:', 'E1036:', 'E974:'])
 endfunc
 
-" test for range assign
-func Test_blob_range_assign()
-  let b = 0z00
-  let b[1] = 0x11
-  let b[2] = 0x22
-  call assert_equal(0z001122, b)
-  call assert_fails('let b[4] = 0x33', 'E979:')
+func Test_blob_index_assign()
+  let lines =<< trim END
+      VAR b = 0z00
+      LET b[1] = 0x11
+      LET b[2] = 0x22
+      call assert_equal(0z001122, b)
+  END
+  call CheckLegacyAndVim9Success(lines)
+
+  let lines =<< trim END
+      VAR b = 0z00
+      LET b[2] = 0x33
+  END
+  call CheckLegacyAndVim9Failure(lines, 'E979:')
+
+  let lines =<< trim END
+      VAR b = 0z00
+      LET b[-2] = 0x33
+  END
+  call CheckLegacyAndVim9Failure(lines, 'E979:')
 endfunc
 
 func Test_blob_for_loop()
index 3ea2ad9a4086c4e6006e83a940109179f3f8fbc3..e6b3751452cbd26ffd47b8e5955a10b44b75c33d 100644 (file)
@@ -246,6 +246,8 @@ def s:ScriptFuncStoreMember()
   locallist[0] = 123
   var localdict: dict<number> = {}
   localdict["a"] = 456
+  var localblob: blob = 0z1122
+  localblob[1] = 33
 enddef
 
 def Test_disassemble_store_member()
@@ -259,7 +261,7 @@ def Test_disassemble_store_member()
         '\d PUSHNR 123\_s*' ..
         '\d PUSHNR 0\_s*' ..
         '\d LOAD $0\_s*' ..
-        '\d STORELIST\_s*' ..
+        '\d STOREINDEX list\_s*' ..
         'var localdict: dict<number> = {}\_s*' ..
         '\d NEWDICT size 0\_s*' ..
         '\d SETTYPE dict<number>\_s*' ..
@@ -268,7 +270,15 @@ def Test_disassemble_store_member()
         '\d\+ PUSHNR 456\_s*' ..
         '\d\+ PUSHS "a"\_s*' ..
         '\d\+ LOAD $1\_s*' ..
-        '\d\+ STOREDICT\_s*' ..
+        '\d\+ STOREINDEX dict\_s*' ..
+        'var localblob: blob = 0z1122\_s*' ..
+        '\d\+ PUSHBLOB 0z1122\_s*' ..
+        '\d\+ STORE $2\_s*' ..
+        'localblob\[1\] = 33\_s*' ..
+        '\d\+ PUSHNR 33\_s*' ..
+        '\d\+ PUSHNR 1\_s*' ..
+        '\d\+ LOAD $2\_s*' ..
+        '\d\+ STOREINDEX blob\_s*' ..
         '\d\+ RETURN 0',
         res)
 enddef
@@ -291,7 +301,7 @@ def Test_disassemble_store_index()
         '\d PUSHNR 0\_s*' ..
         '\d LOAD $0\_s*' ..
         '\d MEMBER dd\_s*' ..
-        '\d STOREINDEX\_s*' ..
+        '\d STOREINDEX any\_s*' ..
         '\d\+ RETURN 0',
         res)
 enddef
index e1f15264ef3025a2225bbaea0346b70c1b5210d0..17afa34a6a5614731c9cfe129a64d006b5db7fc0 100644 (file)
@@ -750,6 +750,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    2777,
 /**/
     2776,
 /**/
index 801f9c193cbdf2e13d17664d4a21cd7e1faac0e0..9cb71295cd63c85dcee540ce81077565ec9fb8f6 100644 (file)
@@ -6209,14 +6209,21 @@ compile_assign_unlet(
        }
        if (dest_type == VAR_DICT && may_generate_2STRING(-1, cctx) == FAIL)
            return FAIL;
-       if (dest_type == VAR_LIST)
+       if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
        {
-           if (range
-                 && need_type(((type_T **)stack->ga_data)[stack->ga_len - 2],
-                                &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
+           type_T *type;
+
+           if (range)
+           {
+               type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
+               if (need_type(type, &t_number,
+                                           -1, 0, cctx, FALSE, FALSE) == FAIL)
                return FAIL;
-           if (need_type(((type_T **)stack->ga_data)[stack->ga_len - 1],
-                                &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
+           }
+           type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
+           if ((dest_type != VAR_BLOB || type != &t_special)
+                   && need_type(type, &t_number,
+                                           -1, 0, cctx, FALSE, FALSE) == FAIL)
                return FAIL;
        }
     }
index abcbce06829c5800eecb2de712885d218fa70cb5..8a985214c1ed7ca93604b67393c419845ba16dd7 100644 (file)
@@ -2221,7 +2221,35 @@ call_def_function(
                    }
                    else if (status == OK && dest_type == VAR_BLOB)
                    {
-                       // TODO
+                       long        lidx = (long)tv_idx->vval.v_number;
+                       blob_T      *blob = tv_dest->vval.v_blob;
+                       varnumber_T nr;
+                       int         error = FALSE;
+                       int         len;
+
+                       if (blob == NULL)
+                       {
+                           emsg(_(e_blob_not_set));
+                           goto on_error;
+                       }
+                       len = blob_len(blob);
+                       if (lidx < 0 && len + lidx >= 0)
+                           // negative index is relative to the end
+                           lidx = len + lidx;
+
+                       // Can add one byte at the end.
+                       if (lidx < 0 || lidx > len)
+                       {
+                           semsg(_(e_blobidx), lidx);
+                           goto on_error;
+                       }
+                       if (value_check_lock(blob->bv_lock,
+                                                     (char_u *)"blob", FALSE))
+                           goto on_error;
+                       nr = tv_get_number_chk(tv, &error);
+                       if (error)
+                           goto on_error;
+                       blob_set_append(blob, lidx, nr);
                    }
                    else
                    {
@@ -4415,19 +4443,8 @@ ex_disassemble(exarg_T *eap)
                break;
 
            case ISN_STOREINDEX:
-               switch (iptr->isn_arg.vartype)
-               {
-                   case VAR_LIST:
-                           smsg("%4d STORELIST", current);
-                           break;
-                   case VAR_DICT:
-                           smsg("%4d STOREDICT", current);
-                           break;
-                   case VAR_ANY:
-                           smsg("%4d STOREINDEX", current);
-                           break;
-                   default: break;
-               }
+               smsg("%4d STOREINDEX %s", current,
+                                         vartype_name(iptr->isn_arg.vartype));
                break;
 
            case ISN_STORERANGE: