# HG changeset patch # User Bram Moolenaar # Date 1618685104 -7200 # Node ID 96905804bf5a529fc52620cb666c1e6e4903e8ef # Parent 2336518ba1662f9f905a63780a6bdc5afbdd2e9f patch 8.2.2777: Vim9: blob operations not tested in all ways Commit: https://github.com/vim/vim/commit/51e933261b984db014e858d79387a826d2626fb6 Author: Bram Moolenaar Date: Sat Apr 17 20:44:56 2021 +0200 patch 8.2.2777: Vim9: blob operations not tested in all ways Problem: Vim9: blob operations not tested in all ways. Solution: Run tests with CheckLegacyAndVim9Success(). Make blob assign with index work. diff --git a/src/blob.c b/src/blob.c --- a/src/blob.c +++ b/src/blob.c @@ -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*)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) { - ((char_u*)b->bv_ga.ga_data)[idx] = c; + 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; + } } /* diff --git a/src/errors.h b/src/errors.h --- a/src/errors.h +++ b/src/errors.h @@ -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")); diff --git a/src/proto/blob.pro b/src/proto/blob.pro --- a/src/proto/blob.pro +++ b/src/proto/blob.pro @@ -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); diff --git a/src/testdir/test_blob.vim b/src/testdir/test_blob.vim --- a/src/testdir/test_blob.vim +++ b/src/testdir/test_blob.vim @@ -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)) + 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]) - call assert_fails('echo b[5]', 'E979:') - call assert_fails('echo b[-8]', 'E979:') + 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:') + 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']) - 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) + 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:']) - 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 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() diff --git a/src/testdir/test_vim9_disassemble.vim b/src/testdir/test_vim9_disassemble.vim --- a/src/testdir/test_vim9_disassemble.vim +++ b/src/testdir/test_vim9_disassemble.vim @@ -246,6 +246,8 @@ def s:ScriptFuncStoreMember() locallist[0] = 123 var localdict: dict = {} 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 = {}\_s*' .. '\d NEWDICT size 0\_s*' .. '\d SETTYPE dict\_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 diff --git a/src/version.c b/src/version.c --- a/src/version.c +++ b/src/version.c @@ -751,6 +751,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ /**/ + 2777, +/**/ 2776, /**/ 2775, diff --git a/src/vim9compile.c b/src/vim9compile.c --- a/src/vim9compile.c +++ b/src/vim9compile.c @@ -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 (range - && need_type(((type_T **)stack->ga_data)[stack->ga_len - 2], - &t_number, -1, 0, cctx, FALSE, FALSE) == FAIL) + if (dest_type == VAR_LIST || dest_type == VAR_BLOB) + { + 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; } } diff --git a/src/vim9execute.c b/src/vim9execute.c --- a/src/vim9execute.c +++ b/src/vim9execute.c @@ -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: