vim-patch:8.2.2765: Vim9: not all blob operations work

Problem:    Vim9: not all blob operations work.
Solution:   Run more tests also with Vim9 script and :def functions.  Fix what
            doesn't work.

0e3ff19196

Co-authored-by: Bram Moolenaar <Bram@vim.org>
This commit is contained in:
zeertzjq 2023-02-28 13:30:08 +08:00
parent adfa55ba99
commit c554e98978
4 changed files with 68 additions and 20 deletions

View File

@ -1498,21 +1498,14 @@ char *get_lval(char *const name, typval_T *const rettv, lval_T *const lp, const
tv_clear(&var1); tv_clear(&var1);
const int bloblen = tv_blob_len(lp->ll_tv->vval.v_blob); const int bloblen = tv_blob_len(lp->ll_tv->vval.v_blob);
if (lp->ll_n1 < 0 || lp->ll_n1 > bloblen if (tv_blob_check_index(bloblen, lp->ll_n1, lp->ll_range, quiet) == FAIL) {
|| (lp->ll_range && lp->ll_n1 == bloblen)) {
if (!quiet) {
semsg(_(e_blobidx), (int64_t)lp->ll_n1);
}
tv_clear(&var2); tv_clear(&var2);
return NULL; return NULL;
} }
if (lp->ll_range && !lp->ll_empty2) { if (lp->ll_range && !lp->ll_empty2) {
lp->ll_n2 = (long)tv_get_number(&var2); lp->ll_n2 = (long)tv_get_number(&var2);
tv_clear(&var2); tv_clear(&var2);
if (lp->ll_n2 < 0 || lp->ll_n2 >= bloblen || lp->ll_n2 < lp->ll_n1) { if (tv_blob_check_range(bloblen, lp->ll_n1, lp->ll_n2, quiet) == FAIL) {
if (!quiet) {
semsg(_(e_blobidx), (int64_t)lp->ll_n2);
}
return NULL; return NULL;
} }
} }

View File

@ -2710,6 +2710,30 @@ bool tv_blob_equal(const blob_T *const b1, const blob_T *const b2)
return true; return true;
} }
/// Check if "n1" is a valid index for a blob with length "bloblen".
int tv_blob_check_index(int bloblen, varnumber_T n1, int is_range, bool 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 tv_blob_check_range(int bloblen, varnumber_T n1, varnumber_T n2, bool 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". /// Set bytes "n1" to "n2" (inclusive) in "dest" to the value of "src".
/// Caller must make sure "src" is a blob. /// Caller must make sure "src" is a blob.
/// Returns FAIL if the number of bytes does not match. /// Returns FAIL if the number of bytes does not match.

View File

@ -76,16 +76,47 @@ func Test_blob_assign()
END END
call CheckLegacyAndVim9Success(lines) call CheckLegacyAndVim9Success(lines)
" TODO: move to above once it works let lines =<< trim END
let b = 0zDEADBEEF VAR b = 0zDEADBEEF
call assert_fails('let b[2 : 3] = 0z112233', 'E972:') LET b[2 : 3] = 0z112233
call assert_fails('let b[2 : 3] = 0z11', 'E972:') END
call assert_fails('let b[3 : 2] = 0z', 'E979:') call CheckLegacyAndVim9Failure(lines, 'E972:')
call assert_fails('let b ..= 0z33', 'E734:') let lines =<< trim END
call assert_fails('let b ..= "xx"', 'E734:') VAR b = 0zDEADBEEF
call assert_fails('let b += "xx"', 'E734:') LET b[2 : 3] = 0z11
call assert_fails('let b[1 : 1] ..= 0z55', 'E734:') 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 endfunc
func Test_blob_get_range() func Test_blob_get_range()

View File

@ -98,9 +98,9 @@ endfunc
" Use ' #"' for a comment " Use ' #"' for a comment
func CheckLegacyAndVim9Failure(lines, error) func CheckLegacyAndVim9Failure(lines, error)
if type(a:error) == type('string') if type(a:error) == type('string')
let legacyError = error let legacyError = a:error
else else
let legacyError = error[0] let legacyError = a:error[0]
endif endif
let legacylines = a:lines->deepcopy()->map({_, v -> let legacylines = a:lines->deepcopy()->map({_, v ->