Skip to content

Commit

Permalink
os: fix File.read() in JS backends (fix #20501) (#20633)
Browse files Browse the repository at this point in the history
  • Loading branch information
GGRei committed Jan 29, 2024
1 parent cae40ad commit 2d68230
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
5 changes: 3 additions & 2 deletions vlib/os/file.js.v
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ pub fn (f &File) read(mut buf []u8) !int {
}
mut nbytes := 0
#try {
#let buffer = $fs.readFileSync(f.val.fd.valueOf());
#let readBuffer = Buffer.alloc(buf.val.len.valueOf());
#nbytes = $fs.readSync(f.val.fd.valueOf(), readBuffer, 0, buf.val.len.valueOf(), null);
#
#for (const val of buffer.values()) { buf.arr[nbytes++] = val; }
#for (let i = 0; i < nbytes; i++) { buf.val.arr.arr[i] = new u8(readBuffer[i]); }
#}
#catch (e) { return error('' + e); }

Expand Down
8 changes: 8 additions & 0 deletions vlib/os/open_and_read_from_file_test.js.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import os

fn test_read_from_file() {
mut buf := []u8{len: 10}
f := os.open(@FILE)!
n := f.read(mut &buf)!
println(buf[..n])
}

0 comments on commit 2d68230

Please sign in to comment.