os: fix File.read() in JS backends (fix #20501) (#20633)

This commit is contained in:
GGRei 2024-01-29 14:07:25 +01:00 committed by GitHub
parent cae40ad673
commit 2d68230681
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 2 deletions

View File

@ -84,9 +84,10 @@ pub fn (f &File) read(mut buf []u8) !int {
} }
mut nbytes := 0 mut nbytes := 0
#try { #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); } #catch (e) { return error('' + e); }

View File

@ -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])
}