Closed
Description
Question
I've compiled Rust source code to WASM, then compiled that WASM to JavaScript with wasm2js
, then used a WASI implementation from here https://gitlab.com/-/snippets/4782260 to execute the JavaScript output by wasm2js
.
// ...
import WASI from "./wasi.js";
let wasi = new WASI();
var retasmFunc = asmFunc({
"wasi_snapshot_preview1": wasi.exports,
});
export var memory = retasmFunc.memory;
export var _start = retasmFunc._start;
export var __main_void = retasmFunc.__main_void;
wasi.memory = memory;
_start();
echo '3 4' | node permutations-rust.js
4 of 5 (0-indexed, factorial 6) => [2, 0, 1]
I've done something similar with Bytecode Alliance's Javy, and Facebook's Static Hermes.
Here either arguments or stdin
is read and passed to the relevant function
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);
if (n > 0) {
let data = String.UTF8.decode(buffer);
input = data.slice(0, data.indexOf(" "));
lex = data.slice(data.indexOf(" "), data.length);
}
}
input = input.trim();
lex = lex.trim();
if (<i32> parseInt(input) < 2 || <i32> parseInt(lex) < 0) {
process.stdout.write(`Expected n > 2, m >= 0, got ${input}, ${lex}`); // eval(input)
process.exit(1);
}
array_nth_permutation(<i32> parseInt(input), <i32> parseInt(lex));
// ...
function asmFunc(imports) {
var buffer = new ArrayBuffer(65536);
var HEAP8 = new Int8Array(buffer);
var HEAP16 = new Int16Array(buffer);
var HEAP32 = new Int32Array(buffer);
var HEAPU8 = new Uint8Array(buffer);
var HEAPU16 = new Uint16Array(buffer);
var HEAPU32 = new Uint32Array(buffer);
var HEAPF32 = new Float32Array(buffer);
var HEAPF64 = new Float64Array(buffer);
var Math_imul = Math.imul;
var Math_fround = Math.fround;
var Math_abs = Math.abs;
var Math_clz32 = Math.clz32;
var Math_min = Math.min;
var Math_max = Math.max;
var Math_floor = Math.floor;
var Math_ceil = Math.ceil;
var Math_trunc = Math.trunc;
var Math_sqrt = Math.sqrt;
var wasi_snapshot_preview1 = imports.wasi_snapshot_preview1;
var fimport$0 = wasi_snapshot_preview1.args_sizes_get;
var fimport$1 = wasi_snapshot_preview1.fd_write;
var fimport$2 = wasi_snapshot_preview1.proc_exit;
var fimport$3 = wasi_snapshot_preview1.args_get;
var fimport$4 = wasi_snapshot_preview1.fd_read;
console.log("We get here...");
// ...
function $131($0_1, $1_1) {
console.log($0_1, $1_1);
// ...
echo '7 8' | node --no-warnings module.js
We get here...
7 8
The resulting JavaScript from the AssemblyScript source to WASM then to JavaScript hangs. Why? And how to fix this?