Skip to content

Commit fde3064

Browse files
authored
feat(stdlib)!: Provide empty byte array from Bytes.make (#1729)
1 parent bec0cb7 commit fde3064

File tree

2 files changed

+21
-39
lines changed

2 files changed

+21
-39
lines changed

compiler/test/stdlib/string.test.gr

Lines changed: 17 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -271,11 +271,8 @@ assert processBytes(String.encode("𐐷", String.UTF32_BE)) == [> 0x00us, 0x01us
271271
// formatter-ignore
272272
assert processBytes(String.encode("𐐷", String.UTF32_LE)) == [> 0x37us, 0x04us, 0x01us, 0x00us]
273273

274-
let buf = Bytes.make(12)
275-
// ensure buffer is clear for our assertions on contents
276-
Bytes.clear(buf)
277274
// formatter-ignore
278-
assert processBytes(String.encodeAt("𐐷", String.UTF16_BE, buf, 5)) == [> 0x00us, 0x00us, 0x00us, 0x00us, 0x00us, 0xD8us, 0x01us, 0xDCus, 0x37us, 0x00us, 0x00us, 0x00us]
275+
assert processBytes(String.encodeAt("𐐷", String.UTF16_BE, Bytes.make(12), 5)) == [> 0x00us, 0x00us, 0x00us, 0x00us, 0x00us, 0xD8us, 0x01us, 0xDCus, 0x37us, 0x00us, 0x00us, 0x00us]
279276
// test that BOM is prepended:
280277
// formatter-ignore
281278
assert processBytes(String.encodeWithBom("𐐷", String.UTF8)) == [> 0xEFus, 0xBBus, 0xBFus, 0xF0us, 0x90us, 0x90us, 0xB7us]
@@ -319,137 +316,119 @@ assert String.decode(String.encode(emojis, String.UTF32_BE), String.UTF32_BE) ==
319316
assert String.decode(String.encode(emojis, String.UTF32_LE), String.UTF32_LE) ==
320317
emojis
321318

322-
let buf = Bytes.make(500)
323-
// ensure buffer is clear for our assertions on contents
324-
Bytes.clear(buf)
325319
// decodeRange
326320
// 51 is chosen to stress-test these functions, since it's not an aligned offset
327321
assert String.decodeRange(
328-
String.encodeAt(emojis, String.UTF8, buf, 51),
322+
String.encodeAt(emojis, String.UTF8, Bytes.make(500), 51),
329323
String.UTF8,
330324
51,
331325
98
332326
) ==
333327
emojis
334-
Bytes.clear(buf)
335328
assert String.decodeRange(
336-
String.encodeAt(emojis, String.UTF16_LE, buf, 51),
329+
String.encodeAt(emojis, String.UTF16_LE, Bytes.make(500), 51),
337330
String.UTF16_LE,
338331
51,
339332
164
340333
) ==
341334
emojis
342-
Bytes.clear(buf)
343335
assert String.decodeRange(
344-
String.encodeAt(emojis, String.UTF16_BE, buf, 51),
336+
String.encodeAt(emojis, String.UTF16_BE, Bytes.make(500), 51),
345337
String.UTF16_BE,
346338
51,
347339
164
348340
) ==
349341
emojis
350-
Bytes.clear(buf)
351342
assert String.decodeRange(
352-
String.encodeAt(emojis, String.UTF32_LE, buf, 51),
343+
String.encodeAt(emojis, String.UTF32_LE, Bytes.make(500), 51),
353344
String.UTF32_LE,
354345
51,
355346
296
356347
) ==
357348
emojis
358-
Bytes.clear(buf)
359349
assert String.decodeRange(
360-
String.encodeAt(emojis, String.UTF32_BE, buf, 51),
350+
String.encodeAt(emojis, String.UTF32_BE, Bytes.make(500), 51),
361351
String.UTF32_BE,
362352
51,
363353
296
364354
) ==
365355
emojis
366356

367357
// keepBom should be no-op, since there is no BOM
368-
Bytes.clear(buf)
369358
assert String.decodeRangeKeepBom(
370-
String.encodeAt(emojis, String.UTF8, buf, 51),
359+
String.encodeAt(emojis, String.UTF8, Bytes.make(500), 51),
371360
String.UTF8,
372361
51,
373362
98
374363
) ==
375364
emojis
376-
Bytes.clear(buf)
377365
assert String.decodeRangeKeepBom(
378-
String.encodeAt(emojis, String.UTF16_LE, buf, 51),
366+
String.encodeAt(emojis, String.UTF16_LE, Bytes.make(500), 51),
379367
String.UTF16_LE,
380368
51,
381369
164
382370
) ==
383371
emojis
384-
Bytes.clear(buf)
385372
assert String.decodeRangeKeepBom(
386-
String.encodeAt(emojis, String.UTF16_BE, buf, 51),
373+
String.encodeAt(emojis, String.UTF16_BE, Bytes.make(500), 51),
387374
String.UTF16_BE,
388375
51,
389376
164
390377
) ==
391378
emojis
392-
Bytes.clear(buf)
393379
assert String.decodeRangeKeepBom(
394-
String.encodeAt(emojis, String.UTF32_LE, buf, 51),
380+
String.encodeAt(emojis, String.UTF32_LE, Bytes.make(500), 51),
395381
String.UTF32_LE,
396382
51,
397383
296
398384
) ==
399385
emojis
400-
Bytes.clear(buf)
401386
assert String.decodeRangeKeepBom(
402-
String.encodeAt(emojis, String.UTF32_BE, buf, 51),
387+
String.encodeAt(emojis, String.UTF32_BE, Bytes.make(500), 51),
403388
String.UTF32_BE,
404389
51,
405390
296
406391
) ==
407392
emojis
408393
// but, when we include it, it should preserve it:
409-
Bytes.clear(buf)
410394
assert String.decodeRangeKeepBom(
411-
String.encodeAtWithBom(emojis, String.UTF16_LE, buf, 51),
395+
String.encodeAtWithBom(emojis, String.UTF16_LE, Bytes.make(500), 51),
412396
String.UTF16_LE,
413397
51,
414398
166
415399
) !=
416400
emojis
417401
// BOM-skipping should be default:
418-
Bytes.clear(buf)
419402
assert String.decodeRange(
420-
String.encodeAtWithBom(emojis, String.UTF8, buf, 51),
403+
String.encodeAtWithBom(emojis, String.UTF8, Bytes.make(500), 51),
421404
String.UTF8,
422405
51,
423406
101
424407
) ==
425408
emojis
426-
Bytes.clear(buf)
427409
assert String.decodeRange(
428-
String.encodeAtWithBom(emojis, String.UTF16_LE, buf, 51),
410+
String.encodeAtWithBom(emojis, String.UTF16_LE, Bytes.make(500), 51),
429411
String.UTF16_LE,
430412
51,
431413
166
432414
) ==
433415
emojis
434-
Bytes.clear(buf)
435416
assert String.decodeRange(
436-
String.encodeAtWithBom(emojis, String.UTF16_BE, buf, 51),
417+
String.encodeAtWithBom(emojis, String.UTF16_BE, Bytes.make(500), 51),
437418
String.UTF16_BE,
438419
51,
439420
166
440421
) ==
441422
emojis
442-
Bytes.clear(buf)
443423
assert String.decodeRange(
444-
String.encodeAtWithBom(emojis, String.UTF32_BE, buf, 51),
424+
String.encodeAtWithBom(emojis, String.UTF32_BE, Bytes.make(500), 51),
445425
String.UTF32_BE,
446426
51,
447427
300
448428
) ==
449429
emojis
450-
Bytes.clear(buf)
451430
assert String.decodeRange(
452-
String.encodeAtWithBom(emojis, String.UTF32_LE, buf, 51),
431+
String.encodeAtWithBom(emojis, String.UTF32_LE, Bytes.make(500), 51),
453432
String.UTF32_LE,
454433
51,
455434
300

stdlib/bytes.gr

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ let getSize = ptr => WasmI32.load(ptr, _SIZE_OFFSET)
7575
*/
7676
@unsafe
7777
provide let make = (size: Number) => {
78-
let bytes = allocateBytes(coerceNumberToWasmI32(size))
78+
from WasmI32 use { add as (+) }
79+
let size = coerceNumberToWasmI32(size)
80+
let bytes = allocateBytes(size)
81+
Memory.fill(bytes + 8n, 0n, size)
7982
WasmI32.toGrain(bytes): Bytes
8083
}
8184

0 commit comments

Comments
 (0)