Skip to content

Commit 17cb28d

Browse files
authored
fix(stdlib)!: Sys/File reading and writing operate on Bytes (#1655)
1 parent 2ee1328 commit 17cb28d

File tree

4 files changed

+25
-18
lines changed

4 files changed

+25
-18
lines changed

compiler/test/stdlib/sys.file.test.gr

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module FileTest
22

33
include "sys/file" as Fs
4+
include "bytes"
45
include "result"
56

67
// fdRead
@@ -20,7 +21,7 @@ let (buf, nread) = Result.unwrap(Fs.fdRead(foo, 40))
2021

2122
Fs.fdClose(foo)
2223

23-
assert buf == "foo, bar, & baz"
24+
assert buf == Bytes.fromString("foo, bar, & baz")
2425
assert nread == 15
2526

2627
// fdWrite
@@ -36,7 +37,7 @@ let foo = Result.unwrap(
3637
)
3738
)
3839

39-
assert Fs.fdWrite(foo, "this and that") == Ok(13)
40+
assert Fs.fdWrite(foo, Bytes.fromString("this and that")) == Ok(13)
4041

4142
Fs.fdSeek(foo, 0L, Fs.Set)
4243

@@ -46,5 +47,5 @@ Fs.fdSetSize(foo, 0L)
4647

4748
Fs.fdClose(foo)
4849

49-
assert buf == "this and that"
50+
assert buf == Bytes.fromString("this and that")
5051
assert nread == 13

compiler/test/suites/stdlib.re

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,12 @@ describe("stdlib", ({test, testSkip}) => {
6464
// logging to the stdout file descriptor
6565
assertRun(
6666
"stdlib_file_stdout",
67-
{|include "sys/file"; ignore(File.fdWrite(File.stdout, "enterthe")); print(void)|},
67+
{|
68+
include "bytes"
69+
include "sys/file"
70+
ignore(File.fdWrite(File.stdout, Bytes.fromString("enterthe")))
71+
print(void)
72+
|},
6873
"enterthevoid\n",
6974
);
7075
assertStdlib("array.test");

stdlib/sys/file.gr

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ include "runtime/dataStructures"
3232
from DataStructures use {
3333
tagSimpleNumber,
3434
allocateArray,
35+
allocateBytes,
3536
allocateString,
3637
newInt64,
3738
allocateInt64,
@@ -525,7 +526,7 @@ provide let fdRead = (fd: FileDescriptor, size: Number) => {
525526
let n = WasmI32.fromGrain(size) >> 1n
526527

527528
let iovs = Memory.malloc(3n * 4n)
528-
let strPtr = allocateString(n)
529+
let strPtr = allocateBytes(n)
529530

530531
WasmI32.store(iovs, strPtr + 2n * 4n, 0n)
531532
WasmI32.store(iovs, n, 4n)
@@ -542,7 +543,7 @@ provide let fdRead = (fd: FileDescriptor, size: Number) => {
542543
nread = WasmI32.load(nread, 0n)
543544
WasmI32.store(strPtr, nread, 4n)
544545
Memory.free(iovs)
545-
return Ok((WasmI32.toGrain(strPtr): String, tagSimpleNumber(nread)))
546+
return Ok((WasmI32.toGrain(strPtr): Bytes, tagSimpleNumber(nread)))
546547
}
547548

548549
/**
@@ -564,7 +565,7 @@ provide let fdPread = (fd: FileDescriptor, offset: Int64, size: Number) => {
564565
let n = WasmI32.fromGrain(size) >> 1n
565566

566567
let iovs = Memory.malloc(3n * 4n)
567-
let strPtr = allocateString(n)
568+
let strPtr = allocateBytes(n)
568569

569570
WasmI32.store(iovs, strPtr + 2n * 4n, 0n)
570571
WasmI32.store(iovs, n, 4n)
@@ -581,7 +582,7 @@ provide let fdPread = (fd: FileDescriptor, offset: Int64, size: Number) => {
581582
nread = WasmI32.load(nread, 0n)
582583
WasmI32.store(strPtr, nread, 4n)
583584
Memory.free(iovs)
584-
return Ok((WasmI32.toGrain(strPtr): String, tagSimpleNumber(nread)))
585+
return Ok((WasmI32.toGrain(strPtr): Bytes, tagSimpleNumber(nread)))
585586
}
586587

587588
/**
@@ -592,7 +593,7 @@ provide let fdPread = (fd: FileDescriptor, offset: Int64, size: Number) => {
592593
* @returns `Ok(numBytes)` of the number of bytes written if successful or `Err(Exception)` otherwise
593594
*/
594595
@unsafe
595-
provide let fdWrite = (fd: FileDescriptor, data: String) => {
596+
provide let fdWrite = (fd: FileDescriptor, data: Bytes) => {
596597
let fdArg = fd
597598
let fd = match (fd) { FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n }
598599

@@ -624,7 +625,7 @@ provide let fdWrite = (fd: FileDescriptor, data: String) => {
624625
* @returns `Ok(numBytes)` of the number of bytes written if successful or `Err(exception)` otherwise
625626
*/
626627
@unsafe
627-
provide let fdPwrite = (fd: FileDescriptor, data: String, offset: Int64) => {
628+
provide let fdPwrite = (fd: FileDescriptor, data: Bytes, offset: Int64) => {
628629
let fdArg = fd
629630
let offsetArg = offset
630631
let fd = match (fd) { FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n }

stdlib/sys/file.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ Returns:
238238
### File.**fdRead**
239239

240240
```grain
241-
fdRead : (FileDescriptor, Number) -> Result<(String, Number), Exception>
241+
fdRead : (FileDescriptor, Number) -> Result<(Bytes, Number), Exception>
242242
```
243243

244244
Read from a file descriptor.
@@ -254,13 +254,13 @@ Returns:
254254

255255
|type|description|
256256
|----|-----------|
257-
|`Result<(String, Number), Exception>`|`Ok((contents, numBytes))` of bytes read and the number of bytes read if successful or `Err(exception)` otherwise|
257+
|`Result<(Bytes, Number), Exception>`|`Ok((contents, numBytes))` of bytes read and the number of bytes read if successful or `Err(exception)` otherwise|
258258

259259
### File.**fdPread**
260260

261261
```grain
262262
fdPread :
263-
(FileDescriptor, Int64, Number) -> Result<(String, Number), Exception>
263+
(FileDescriptor, Int64, Number) -> Result<(Bytes, Number), Exception>
264264
```
265265

266266
Read from a file descriptor without updating the file descriptor's offset.
@@ -277,12 +277,12 @@ Returns:
277277

278278
|type|description|
279279
|----|-----------|
280-
|`Result<(String, Number), Exception>`|`Ok((contents, numBytes))` of bytes read and the number of bytes read if successful or `Err(exception)` otherwise|
280+
|`Result<(Bytes, Number), Exception>`|`Ok((contents, numBytes))` of bytes read and the number of bytes read if successful or `Err(exception)` otherwise|
281281

282282
### File.**fdWrite**
283283

284284
```grain
285-
fdWrite : (FileDescriptor, String) -> Result<Number, Exception>
285+
fdWrite : (FileDescriptor, Bytes) -> Result<Number, Exception>
286286
```
287287

288288
Write to a file descriptor.
@@ -292,7 +292,7 @@ Parameters:
292292
|param|type|description|
293293
|-----|----|-----------|
294294
|`fd`|`FileDescriptor`|The file descriptor to which data will be written|
295-
|`data`|`String`|The data to be written|
295+
|`data`|`Bytes`|The data to be written|
296296

297297
Returns:
298298

@@ -303,7 +303,7 @@ Returns:
303303
### File.**fdPwrite**
304304

305305
```grain
306-
fdPwrite : (FileDescriptor, String, Int64) -> Result<Number, Exception>
306+
fdPwrite : (FileDescriptor, Bytes, Int64) -> Result<Number, Exception>
307307
```
308308

309309
Write to a file descriptor without updating the file descriptor's offset.
@@ -313,7 +313,7 @@ Parameters:
313313
|param|type|description|
314314
|-----|----|-----------|
315315
|`fd`|`FileDescriptor`|The file descriptor to which data will be written|
316-
|`data`|`String`|The data to be written|
316+
|`data`|`Bytes`|The data to be written|
317317
|`offset`|`Int64`|The position within the file to begin writing|
318318

319319
Returns:

0 commit comments

Comments
 (0)