Skip to content

Commit 51e9332

Browse files
committed
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.
1 parent 0995c81 commit 51e9332

8 files changed

Lines changed: 233 additions & 96 deletions

File tree

src/blob.c

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,33 @@ blob_get(blob_T *b, int idx)
125125
}
126126

127127
/*
128-
* Store one byte "c" in blob "b" at "idx".
128+
* Store one byte "byte" in blob "blob" at "idx".
129129
* Caller must make sure that "idx" is valid.
130130
*/
131131
void
132-
blob_set(blob_T *b, int idx, char_u c)
132+
blob_set(blob_T *blob, int idx, int byte)
133133
{
134-
((char_u*)b->bv_ga.ga_data)[idx] = c;
134+
((char_u*)blob->bv_ga.ga_data)[idx] = byte;
135+
}
136+
137+
/*
138+
* Store one byte "byte" in blob "blob" at "idx".
139+
* Append one byte if needed.
140+
*/
141+
void
142+
blob_set_append(blob_T *blob, int idx, int byte)
143+
{
144+
garray_T *gap = &blob->bv_ga;
145+
146+
// Allow for appending a byte. Setting a byte beyond
147+
// the end is an error otherwise.
148+
if (idx < gap->ga_len
149+
|| (idx == gap->ga_len && ga_grow(gap, 1) == OK))
150+
{
151+
blob_set(blob, idx, byte);
152+
if (idx == gap->ga_len)
153+
++gap->ga_len;
154+
}
135155
}
136156

137157
/*

src/errors.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,3 +403,5 @@ EXTERN char e_blob_required[]
403403
INIT(= N_("E1182: Blob required"));
404404
EXTERN char e_cannot_use_range_with_assignment_operator_str[]
405405
INIT(= N_("E1183: Cannot use a range with an assignment operator: %s"));
406+
EXTERN char e_blob_not_set[]
407+
INIT(= N_("E1184: Blob not set"));

src/proto/blob.pro

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ void blob_free(blob_T *b);
77
void blob_unref(blob_T *b);
88
long blob_len(blob_T *b);
99
int blob_get(blob_T *b, int idx);
10-
void blob_set(blob_T *b, int idx, char_u c);
10+
void blob_set(blob_T *blob, int idx, int byte);
11+
void blob_set_append(blob_T *blob, int idx, int byte);
1112
int blob_equal(blob_T *b1, blob_T *b2);
1213
int read_blob(FILE *fd, blob_T *blob);
1314
int write_blob(FILE *fd, blob_T *blob);

src/testdir/test_blob.vim

Lines changed: 147 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -120,88 +120,166 @@ func Test_blob_assign()
120120
endfunc
121121

122122
func Test_blob_get_range()
123+
let lines =<< trim END
124+
VAR b = 0z0011223344
125+
call assert_equal(0z2233, b[2 : 3])
126+
call assert_equal(0z223344, b[2 : -1])
127+
call assert_equal(0z00, b[0 : -5])
128+
call assert_equal(0z, b[0 : -11])
129+
call assert_equal(0z44, b[-1 :])
130+
call assert_equal(0z0011223344, b[:])
131+
call assert_equal(0z0011223344, b[: -1])
132+
call assert_equal(0z, b[5 : 6])
133+
call assert_equal(0z0011, b[-10 : 1])
134+
END
135+
call CheckLegacyAndVim9Success(lines)
136+
137+
" legacy script white space
123138
let b = 0z0011223344
124139
call assert_equal(0z2233, b[2:3])
125-
call assert_equal(0z223344, b[2:-1])
126-
call assert_equal(0z00, b[0:-5])
127-
call assert_equal(0z, b[0:-11])
128-
call assert_equal(0z44, b[-1:])
129-
call assert_equal(0z0011223344, b[:])
130-
call assert_equal(0z0011223344, b[:-1])
131-
call assert_equal(0z, b[5:6])
132-
call assert_equal(0z0011, b[-10:1])
133140
endfunc
134141

135142
func Test_blob_get()
136-
let b = 0z0011223344
137-
call assert_equal(0x00, get(b, 0))
138-
call assert_equal(0x22, get(b, 2, 999))
139-
call assert_equal(0x44, get(b, 4))
140-
call assert_equal(0x44, get(b, -1))
141-
call assert_equal(-1, get(b, 5))
142-
call assert_equal(999, get(b, 5, 999))
143-
call assert_equal(-1, get(b, -8))
144-
call assert_equal(999, get(b, -8, 999))
145-
call assert_equal(10, get(test_null_blob(), 2, 10))
146-
147-
call assert_equal(0x00, b[0])
148-
call assert_equal(0x22, b[2])
149-
call assert_equal(0x44, b[4])
150-
call assert_equal(0x44, b[-1])
151-
call assert_fails('echo b[5]', 'E979:')
152-
call assert_fails('echo b[-8]', 'E979:')
143+
let lines =<< trim END
144+
VAR b = 0z0011223344
145+
call assert_equal(0x00, get(b, 0))
146+
call assert_equal(0x22, get(b, 2, 999))
147+
call assert_equal(0x44, get(b, 4))
148+
call assert_equal(0x44, get(b, -1))
149+
call assert_equal(-1, get(b, 5))
150+
call assert_equal(999, get(b, 5, 999))
151+
call assert_equal(-1, get(b, -8))
152+
call assert_equal(999, get(b, -8, 999))
153+
call assert_equal(10, get(test_null_blob(), 2, 10))
154+
155+
call assert_equal(0x00, b[0])
156+
call assert_equal(0x22, b[2])
157+
call assert_equal(0x44, b[4])
158+
call assert_equal(0x44, b[-1])
159+
END
160+
call CheckLegacyAndVim9Success(lines)
161+
162+
let lines =<< trim END
163+
VAR b = 0z0011223344
164+
echo b[5]
165+
END
166+
call CheckLegacyAndVim9Failure(lines, 'E979:')
167+
168+
let lines =<< trim END
169+
VAR b = 0z0011223344
170+
echo b[-8]
171+
END
172+
call CheckLegacyAndVim9Failure(lines, 'E979:')
153173
endfunc
154174

155175
func Test_blob_to_string()
156-
let b = 0z00112233445566778899aabbccdd
157-
call assert_equal('0z00112233.44556677.8899AABB.CCDD', string(b))
158-
call assert_equal(b, eval(string(b)))
159-
call remove(b, 4, -1)
160-
call assert_equal('0z00112233', string(b))
161-
call remove(b, 0, 3)
162-
call assert_equal('0z', string(b))
163-
call assert_equal('0z', string(test_null_blob()))
176+
let lines =<< trim END
177+
VAR b = 0z00112233445566778899aabbccdd
178+
call assert_equal('0z00112233.44556677.8899AABB.CCDD', string(b))
179+
call assert_equal(b, eval(string(b)))
180+
call remove(b, 4, -1)
181+
call assert_equal('0z00112233', string(b))
182+
call remove(b, 0, 3)
183+
call assert_equal('0z', string(b))
184+
call assert_equal('0z', string(test_null_blob()))
185+
END
186+
call CheckLegacyAndVim9Success(lines)
164187
endfunc
165188

166189
func Test_blob_compare()
167-
let b1 = 0z0011
168-
let b2 = 0z1100
169-
let b3 = 0z001122
170-
call assert_true(b1 == b1)
171-
call assert_false(b1 == b2)
172-
call assert_false(b1 == b3)
173-
call assert_true(b1 != b2)
174-
call assert_true(b1 != b3)
175-
call assert_true(b1 == 0z0011)
176-
call assert_fails('echo b1 == 9', 'E977:')
177-
call assert_fails('echo b1 != 9', 'E977:')
178-
179-
call assert_false(b1 is b2)
180-
let b2 = b1
181-
call assert_true(b1 == b2)
182-
call assert_true(b1 is b2)
183-
let b2 = copy(b1)
184-
call assert_true(b1 == b2)
185-
call assert_false(b1 is b2)
186-
let b2 = b1[:]
187-
call assert_true(b1 == b2)
188-
call assert_false(b1 is b2)
189-
call assert_true(b1 isnot b2)
190-
191-
call assert_fails('let x = b1 > b2')
192-
call assert_fails('let x = b1 < b2')
193-
call assert_fails('let x = b1 - b2')
194-
call assert_fails('let x = b1 / b2')
195-
call assert_fails('let x = b1 * b2')
190+
let lines =<< trim END
191+
VAR b1 = 0z0011
192+
VAR b2 = 0z1100
193+
VAR b3 = 0z001122
194+
call assert_true(b1 == b1)
195+
call assert_false(b1 == b2)
196+
call assert_false(b1 == b3)
197+
call assert_true(b1 != b2)
198+
call assert_true(b1 != b3)
199+
call assert_true(b1 == 0z0011)
200+
201+
call assert_false(b1 is b2)
202+
LET b2 = b1
203+
call assert_true(b1 == b2)
204+
call assert_true(b1 is b2)
205+
LET b2 = copy(b1)
206+
call assert_true(b1 == b2)
207+
call assert_false(b1 is b2)
208+
LET b2 = b1[:]
209+
call assert_true(b1 == b2)
210+
call assert_false(b1 is b2)
211+
call assert_true(b1 isnot b2)
212+
END
213+
call CheckLegacyAndVim9Success(lines)
214+
215+
let lines =<< trim END
216+
VAR b1 = 0z0011
217+
echo b1 == 9
218+
END
219+
call CheckLegacyAndVim9Failure(lines, ['E977:', 'E1072', 'E1072'])
220+
221+
let lines =<< trim END
222+
VAR b1 = 0z0011
223+
echo b1 != 9
224+
END
225+
call CheckLegacyAndVim9Failure(lines, ['E977:', 'E1072', 'E1072'])
226+
227+
let lines =<< trim END
228+
VAR b1 = 0z0011
229+
VAR b2 = 0z1100
230+
VAR x = b1 > b2
231+
END
232+
call CheckLegacyAndVim9Failure(lines, ['E978:', 'E1072:', 'E1072:'])
233+
234+
let lines =<< trim END
235+
VAR b1 = 0z0011
236+
VAR b2 = 0z1100
237+
VAR x = b1 < b2
238+
END
239+
call CheckLegacyAndVim9Failure(lines, ['E978:', 'E1072:', 'E1072:'])
240+
241+
let lines =<< trim END
242+
VAR b1 = 0z0011
243+
VAR b2 = 0z1100
244+
VAR x = b1 - b2
245+
END
246+
call CheckLegacyAndVim9Failure(lines, ['E974:', 'E1036:', 'E974:'])
247+
248+
let lines =<< trim END
249+
VAR b1 = 0z0011
250+
VAR b2 = 0z1100
251+
VAR x = b1 / b2
252+
END
253+
call CheckLegacyAndVim9Failure(lines, ['E974:', 'E1036:', 'E974:'])
254+
255+
let lines =<< trim END
256+
VAR b1 = 0z0011
257+
VAR b2 = 0z1100
258+
VAR x = b1 * b2
259+
END
260+
call CheckLegacyAndVim9Failure(lines, ['E974:', 'E1036:', 'E974:'])
196261
endfunc
197262

198-
" test for range assign
199-
func Test_blob_range_assign()
200-
let b = 0z00
201-
let b[1] = 0x11
202-
let b[2] = 0x22
203-
call assert_equal(0z001122, b)
204-
call assert_fails('let b[4] = 0x33', 'E979:')
263+
func Test_blob_index_assign()
264+
let lines =<< trim END
265+
VAR b = 0z00
266+
LET b[1] = 0x11
267+
LET b[2] = 0x22
268+
call assert_equal(0z001122, b)
269+
END
270+
call CheckLegacyAndVim9Success(lines)
271+
272+
let lines =<< trim END
273+
VAR b = 0z00
274+
LET b[2] = 0x33
275+
END
276+
call CheckLegacyAndVim9Failure(lines, 'E979:')
277+
278+
let lines =<< trim END
279+
VAR b = 0z00
280+
LET b[-2] = 0x33
281+
END
282+
call CheckLegacyAndVim9Failure(lines, 'E979:')
205283
endfunc
206284

207285
func Test_blob_for_loop()

src/testdir/test_vim9_disassemble.vim

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ def s:ScriptFuncStoreMember()
246246
locallist[0] = 123
247247
var localdict: dict<number> = {}
248248
localdict["a"] = 456
249+
var localblob: blob = 0z1122
250+
localblob[1] = 33
249251
enddef
250252

251253
def Test_disassemble_store_member()
@@ -259,7 +261,7 @@ def Test_disassemble_store_member()
259261
'\d PUSHNR 123\_s*' ..
260262
'\d PUSHNR 0\_s*' ..
261263
'\d LOAD $0\_s*' ..
262-
'\d STORELIST\_s*' ..
264+
'\d STOREINDEX list\_s*' ..
263265
'var localdict: dict<number> = {}\_s*' ..
264266
'\d NEWDICT size 0\_s*' ..
265267
'\d SETTYPE dict<number>\_s*' ..
@@ -268,7 +270,15 @@ def Test_disassemble_store_member()
268270
'\d\+ PUSHNR 456\_s*' ..
269271
'\d\+ PUSHS "a"\_s*' ..
270272
'\d\+ LOAD $1\_s*' ..
271-
'\d\+ STOREDICT\_s*' ..
273+
'\d\+ STOREINDEX dict\_s*' ..
274+
'var localblob: blob = 0z1122\_s*' ..
275+
'\d\+ PUSHBLOB 0z1122\_s*' ..
276+
'\d\+ STORE $2\_s*' ..
277+
'localblob\[1\] = 33\_s*' ..
278+
'\d\+ PUSHNR 33\_s*' ..
279+
'\d\+ PUSHNR 1\_s*' ..
280+
'\d\+ LOAD $2\_s*' ..
281+
'\d\+ STOREINDEX blob\_s*' ..
272282
'\d\+ RETURN 0',
273283
res)
274284
enddef
@@ -291,7 +301,7 @@ def Test_disassemble_store_index()
291301
'\d PUSHNR 0\_s*' ..
292302
'\d LOAD $0\_s*' ..
293303
'\d MEMBER dd\_s*' ..
294-
'\d STOREINDEX\_s*' ..
304+
'\d STOREINDEX any\_s*' ..
295305
'\d\+ RETURN 0',
296306
res)
297307
enddef

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,8 @@ static char *(features[]) =
750750

751751
static int included_patches[] =
752752
{ /* Add new patch number below this line */
753+
/**/
754+
2777,
753755
/**/
754756
2776,
755757
/**/

src/vim9compile.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6209,14 +6209,21 @@ compile_assign_unlet(
62096209
}
62106210
if (dest_type == VAR_DICT && may_generate_2STRING(-1, cctx) == FAIL)
62116211
return FAIL;
6212-
if (dest_type == VAR_LIST)
6212+
if (dest_type == VAR_LIST || dest_type == VAR_BLOB)
62136213
{
6214-
if (range
6215-
&& need_type(((type_T **)stack->ga_data)[stack->ga_len - 2],
6216-
&t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
6214+
type_T *type;
6215+
6216+
if (range)
6217+
{
6218+
type = ((type_T **)stack->ga_data)[stack->ga_len - 2];
6219+
if (need_type(type, &t_number,
6220+
-1, 0, cctx, FALSE, FALSE) == FAIL)
62176221
return FAIL;
6218-
if (need_type(((type_T **)stack->ga_data)[stack->ga_len - 1],
6219-
&t_number, -1, 0, cctx, FALSE, FALSE) == FAIL)
6222+
}
6223+
type = ((type_T **)stack->ga_data)[stack->ga_len - 1];
6224+
if ((dest_type != VAR_BLOB || type != &t_special)
6225+
&& need_type(type, &t_number,
6226+
-1, 0, cctx, FALSE, FALSE) == FAIL)
62206227
return FAIL;
62216228
}
62226229
}

0 commit comments

Comments
 (0)