diff options
author | Bram Moolenaar <Bram@vim.org> | 2021-04-14 20:35:23 +0200 |
---|---|---|
committer | Bram Moolenaar <Bram@vim.org> | 2021-04-14 20:35:23 +0200 |
commit | 0e3ff1919603ee4c4a347fdf761dbdbdeb068015 (patch) | |
tree | db3dc3df238da3a1c44116827796157cdd3346f0 /src | |
parent | b47bed2f7ada4dfae78f76f27473b83507e40315 (diff) | |
download | vim-git-0e3ff1919603ee4c4a347fdf761dbdbdeb068015.tar.gz |
patch 8.2.2765: Vim9: not all blob operations workv8.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.
Diffstat (limited to 'src')
-rw-r--r-- | src/blob.c | 30 | ||||
-rw-r--r-- | src/errors.h | 2 | ||||
-rw-r--r-- | src/eval.c | 16 | ||||
-rw-r--r-- | src/proto/blob.pro | 2 | ||||
-rw-r--r-- | src/testdir/test_blob.vim | 51 | ||||
-rw-r--r-- | src/testdir/vim9.vim | 47 | ||||
-rw-r--r-- | src/version.c | 2 | ||||
-rw-r--r-- | src/vim9execute.c | 14 |
8 files changed, 140 insertions, 24 deletions
diff --git a/src/blob.c b/src/blob.c index d758beb19..f020966ae 100644 --- a/src/blob.c +++ b/src/blob.c @@ -337,6 +337,36 @@ blob_slice_or_index( } /* + * 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. diff --git a/src/errors.h b/src/errors.h index d38562cfa..99f3aaff5 100644 --- a/src/errors.h +++ b/src/errors.h @@ -401,3 +401,5 @@ EXTERN char e_cannot_use_underscore_here[] 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")); diff --git a/src/eval.c b/src/eval.c index 99768ee28..d8607f2be 100644 --- a/src/eval.c +++ b/src/eval.c @@ -1175,12 +1175,9 @@ get_lval( 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 (check_blob_index(bloblen, lp->ll_n1, lp->ll_range, quiet) + == FAIL) { - if (!quiet) - semsg(_(e_blobidx), lp->ll_n1); clear_tv(&var2); return NULL; } @@ -1188,14 +1185,9 @@ get_lval( { 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); + 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; diff --git a/src/proto/blob.pro b/src/proto/blob.pro index 7da269c35..0b7cbd3a8 100644 --- a/src/proto/blob.pro +++ b/src/proto/blob.pro @@ -14,6 +14,8 @@ int write_blob(FILE *fd, blob_T *blob); 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 : */ diff --git a/src/testdir/test_blob.vim b/src/testdir/test_blob.vim index 34ed0caf2..3699f3bb1 100644 --- a/src/testdir/test_blob.vim +++ b/src/testdir/test_blob.vim @@ -76,16 +76,47 @@ func Test_blob_assign() 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:') + 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() diff --git a/src/testdir/vim9.vim b/src/testdir/vim9.vim index e05f8590b..9efa65552 100644 --- a/src/testdir/vim9.vim +++ b/src/testdir/vim9.vim @@ -144,8 +144,23 @@ func CheckLegacySuccess(lines) 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,3 +183,35 @@ def CheckLegacyAndVim9Success(lines: list<string>) 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 diff --git a/src/version.c b/src/version.c index 6ad07f7d7..0127c2c51 100644 --- 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 */ /**/ + 2765, +/**/ 2764, /**/ 2763, diff --git a/src/vim9execute.c b/src/vim9execute.c index 026a9ee45..bdf4a02b7 100644 --- a/src/vim9execute.c +++ b/src/vim9execute.c @@ -2278,8 +2278,18 @@ call_def_function( if (error) status = FAIL; else - status = blob_set_range(tv_dest->vval.v_blob, - n1, n2, tv); + { + 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); + } } } |