I currently have the following. For some reason the wasi program keeps running fd_readdir
again with bigger and bigger buffers, but with the same 0 cookie. What am I doing wrong?
buffer32[bufused / 4] = 0;
let cookie = Number(big_cookie);
console.log(cookie, Object.keys(fds[fd].directory).slice(cookie));
if (buf_len > 5276240) throw "too big buf"; // FIXME
if (cookie >= Object.keys(fds[fd].directory).length) {
console.log("end of dir");
return -1;
}
let next_cookie = cookie + 1;
for (let name of Object.keys(fds[fd].directory).slice(cookie)) {
let entry = fds[fd].directory[name];
console.log(name, entry);
let encoded_name = new TextEncoder("utf-8").encode(name + "\0");
let offset = 16 + encoded_name.length;
if ((buf_len - buffer32[bufused / 4]) < offset) {
console.log("too small buf");
break;
} else {
console.log(next_cookie, buf);
write_le_u32(buffer8, buf, next_cookie);
next_cookie += 1;
write_le_u32(buffer8, buf + 4, 0); // inode
write_le_u32(buffer8, buf + 8, encoded_name.length); // name
write_le_u32(buffer8, buf + 12, entry.file_type);
buffer8.set(encoded_name, buf + 16);
console.log(buffer8[buf], buffer8.slice(buf, buf + offset));
buf += offset;
buffer32[bufused / 4] += offset;
}
}
I think I found the problem: cookie is 64bit, not 32bit
It doesn't get stuck in an infinite loop anymore, but it still doesn't show any files:
buffer32[bufused / 4] = 0;
console.log(cookie, Object.keys(fds[fd].directory).slice(Number(cookie)));
if (cookie >= BigInt(Object.keys(fds[fd].directory).length)) {
console.log("end of dir");
return 0;
}
let next_cookie = cookie + 1n;
for (let name of Object.keys(fds[fd].directory).slice(Number(cookie))) {
let entry = fds[fd].directory[name];
console.log(name, entry);
let encoded_name = new TextEncoder("utf-8").encode(name);
let offset = 24 + encoded_name.length;
if ((buf_len - buffer32[bufused / 4]) < offset) {
console.log("too small buf");
break;
} else {
console.log("next_cookie =", next_cookie, buf);
write_le_u64(buffer8, buf, next_cookie);
next_cookie += 1n;
write_le_u64(buffer8, buf + 8, 1n); // inode
write_le_u32(buffer8, buf + 16, encoded_name.length); // name
buffer8[buf + 20] = entry.file_type;
buffer8.set(encoded_name, buf + 24);
console.log("buffer =", buffer8.slice(buf, buf + offset));
buf += offset;
buffer32[bufused / 4] += offset;
console.log();
}
}
console.log("used =", buffer32[bufused / 4]);
return 0;
This is the test program: echo 'fn main() { for item in std::fs::read_dir("/sysroot/lib/rustlib/x86_64-unknown-linux-gnu/lib/").unwrap() { println!("{:?}", item); } }' | rustc --target wasm32-wasi -
Never mind. I was overwriting the program output when the program completed successfully.
I got rustc in the browser working: https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/rustc.20on.20wasm/near/205695027
Last updated: Dec 23 2024 at 12:05 UTC