How to read stdin?
guest271314 opened this issue · 6 comments
guest271314 commented
Question
What I tried
const reader: ReadableStreamDefaultReader = process.stdin.getReader();
reader.read().then((data) => {
process.stdout.write(data);
});
Error message
node_modules/.bin/asc --config ./node_modules/@assemblyscript/wasi-shim/asconfig.json module.ts -o module.wasm
ERROR TS2304: Cannot find name 'ReadableStreamDefaultReader'.
:
71 │ const reader: ReadableStreamDefaultReader = process.stdin.getReader();
│ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
└─ in module.ts(71,15)
FAILURE 1 compile error(s)
CountBleck commented
Since you're using wasi-shim, looks like you're supposed to use process.stdin.read(buffer), where buffer is an ArrayBuffer of the desired size. AS doesn't have any kind of async support.
guest271314 commented
Thanks. I figured it out during you writing your answer reading this
@unmanaged
abstract class ReadableStream extends Stream {
read(buffer: ArrayBuffer, offset: isize = 0): i32 {
var end = <usize>buffer.byteLength;
if (offset < 0 || <usize>offset > end) {
throw new Error(E_INDEXOUTOFRANGE);
}
store<usize>(tempbuf, changetype<usize>(buffer) + offset);
store<usize>(tempbuf, end - offset, sizeof<usize>());
var err = fd_read(<u32>changetype<usize>(this), tempbuf, 1, tempbuf + 2 * sizeof<usize>());
if (err) throw new Error(errnoToString(err));
return <i32>load<isize>(tempbuf, 2 * sizeof<usize>());
}
}
let input: string = "0";
let lex: string = "0";
if (process.argv.length > 1) {
input = process.argv.at(-2);
lex = process.argv.at(-1);
} else {
let stdin = process.stdin;
let buffer = new ArrayBuffer(64);
let n: number = stdin.read(buffer);
// process.stdout.write(`${n}`);
if (n > 0) {
let data = String.UTF8.decode(buffer);
input = data.slice(0, data.indexOf(" "));
lex = data.slice(q.indexOf(" "), data.length);
process.stdout.write(input);
process.stdout.write(lex);
}
}
CountBleck commented
Yup, I was reading that exact snippet.
guest271314 commented
@CountBleck Any idea why Binaryen's wasm2js throws Fatal: error in validating input?
CountBleck commented
No, I have no idea. Have you tried looking at the output of wasm-opt --validate?
guest271314 commented
../binaryen/bin/wasm-opt --validate module.wasm
Unknown option '--validate'
This is at the top of wasm2js output
[wasm-validator error in function 82] unexpected false: all used features should be allowed, on
(i32.trunc_sat_f64_s
(local.get $23)
)
[wasm-validator error in function 132] unexpected false: all used features should be allowed, on
(i32.trunc_sat_f64_s
(call $118
(local.get $5)
(i32.const 0)
)
)
[wasm-validator error in function 132] unexpected false: all used features should be allowed, on
(i32.trunc_sat_f64_s
(call $118
(local.get $5)
(i32.const 0)
)
)
[wasm-validator error in function 132] unexpected false: all used features should be allowed, on
(i32.trunc_sat_f64_s
(call $118
(local.get $5)
(i32.const 0)
)
)
[wasm-validator error in function 132] unexpected false: all used features should be allowed, on
(i32.trunc_sat_f64_s
(call $118
(local.get $5)
(i32.const 0)
)
)