Skip to content

Commit

Permalink
fix(stdlib)!: Sys/File reading and writing operate on Bytes (#1655)
Browse files Browse the repository at this point in the history
  • Loading branch information
ospencer committed Feb 5, 2023
1 parent 2ee1328 commit 17cb28d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
7 changes: 4 additions & 3 deletions compiler/test/stdlib/sys.file.test.gr
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module FileTest

include "sys/file" as Fs
include "bytes"
include "result"

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

Fs.fdClose(foo)

assert buf == "foo, bar, & baz"
assert buf == Bytes.fromString("foo, bar, & baz")
assert nread == 15

// fdWrite
Expand All @@ -36,7 +37,7 @@ let foo = Result.unwrap(
)
)

assert Fs.fdWrite(foo, "this and that") == Ok(13)
assert Fs.fdWrite(foo, Bytes.fromString("this and that")) == Ok(13)

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

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

Fs.fdClose(foo)

assert buf == "this and that"
assert buf == Bytes.fromString("this and that")
assert nread == 13
7 changes: 6 additions & 1 deletion compiler/test/suites/stdlib.re
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ describe("stdlib", ({test, testSkip}) => {
// logging to the stdout file descriptor
assertRun(
"stdlib_file_stdout",
{|include "sys/file"; ignore(File.fdWrite(File.stdout, "enterthe")); print(void)|},
{|
include "bytes"
include "sys/file"
ignore(File.fdWrite(File.stdout, Bytes.fromString("enterthe")))
print(void)
|},
"enterthevoid\n",
);
assertStdlib("array.test");
Expand Down
13 changes: 7 additions & 6 deletions stdlib/sys/file.gr
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ include "runtime/dataStructures"
from DataStructures use {
tagSimpleNumber,
allocateArray,
allocateBytes,
allocateString,
newInt64,
allocateInt64,
Expand Down Expand Up @@ -525,7 +526,7 @@ provide let fdRead = (fd: FileDescriptor, size: Number) => {
let n = WasmI32.fromGrain(size) >> 1n

let iovs = Memory.malloc(3n * 4n)
let strPtr = allocateString(n)
let strPtr = allocateBytes(n)

WasmI32.store(iovs, strPtr + 2n * 4n, 0n)
WasmI32.store(iovs, n, 4n)
Expand All @@ -542,7 +543,7 @@ provide let fdRead = (fd: FileDescriptor, size: Number) => {
nread = WasmI32.load(nread, 0n)
WasmI32.store(strPtr, nread, 4n)
Memory.free(iovs)
return Ok((WasmI32.toGrain(strPtr): String, tagSimpleNumber(nread)))
return Ok((WasmI32.toGrain(strPtr): Bytes, tagSimpleNumber(nread)))
}

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

let iovs = Memory.malloc(3n * 4n)
let strPtr = allocateString(n)
let strPtr = allocateBytes(n)

WasmI32.store(iovs, strPtr + 2n * 4n, 0n)
WasmI32.store(iovs, n, 4n)
Expand All @@ -581,7 +582,7 @@ provide let fdPread = (fd: FileDescriptor, offset: Int64, size: Number) => {
nread = WasmI32.load(nread, 0n)
WasmI32.store(strPtr, nread, 4n)
Memory.free(iovs)
return Ok((WasmI32.toGrain(strPtr): String, tagSimpleNumber(nread)))
return Ok((WasmI32.toGrain(strPtr): Bytes, tagSimpleNumber(nread)))
}

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

Expand Down Expand Up @@ -624,7 +625,7 @@ provide let fdWrite = (fd: FileDescriptor, data: String) => {
* @returns `Ok(numBytes)` of the number of bytes written if successful or `Err(exception)` otherwise
*/
@unsafe
provide let fdPwrite = (fd: FileDescriptor, data: String, offset: Int64) => {
provide let fdPwrite = (fd: FileDescriptor, data: Bytes, offset: Int64) => {
let fdArg = fd
let offsetArg = offset
let fd = match (fd) { FileDescriptor(n) => WasmI32.fromGrain(n) >> 1n }
Expand Down
16 changes: 8 additions & 8 deletions stdlib/sys/file.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ Returns:
### File.**fdRead**

```grain
fdRead : (FileDescriptor, Number) -> Result<(String, Number), Exception>
fdRead : (FileDescriptor, Number) -> Result<(Bytes, Number), Exception>
```

Read from a file descriptor.
Expand All @@ -254,13 +254,13 @@ Returns:

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

### File.**fdPread**

```grain
fdPread :
(FileDescriptor, Int64, Number) -> Result<(String, Number), Exception>
(FileDescriptor, Int64, Number) -> Result<(Bytes, Number), Exception>
```

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

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

### File.**fdWrite**

```grain
fdWrite : (FileDescriptor, String) -> Result<Number, Exception>
fdWrite : (FileDescriptor, Bytes) -> Result<Number, Exception>
```

Write to a file descriptor.
Expand All @@ -292,7 +292,7 @@ Parameters:
|param|type|description|
|-----|----|-----------|
|`fd`|`FileDescriptor`|The file descriptor to which data will be written|
|`data`|`String`|The data to be written|
|`data`|`Bytes`|The data to be written|

Returns:

Expand All @@ -303,7 +303,7 @@ Returns:
### File.**fdPwrite**

```grain
fdPwrite : (FileDescriptor, String, Int64) -> Result<Number, Exception>
fdPwrite : (FileDescriptor, Bytes, Int64) -> Result<Number, Exception>
```

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

Returns:
Expand Down

0 comments on commit 17cb28d

Please sign in to comment.