From 88a591050e0a05dd8bfe3762cf935a319acf6dd5 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Sat, 20 Feb 2021 21:24:29 +0100 Subject: [PATCH] Add support for Node without wasmer-js --- test/default/wasi-test-wrapper.sh | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/test/default/wasi-test-wrapper.sh b/test/default/wasi-test-wrapper.sh index 4f22b2f9..c454fe02 100755 --- a/test/default/wasi-test-wrapper.sh +++ b/test/default/wasi-test-wrapper.sh @@ -27,12 +27,6 @@ if [ -z "$WASI_RUNTIME" ] || [ "$WASI_RUNTIME" = "wasmer" ]; then fi fi -if [ -z "$WASI_RUNTIME" ] || [ "$WASI_RUNTIME" = "wasmer-js" ]; then - if command -v wasmer-js >/dev/null; then - wasmer-js run "$1" --dir=. && exit 0 - fi -fi - if [ -z "$WASI_RUNTIME" ] || [ "$WASI_RUNTIME" = "wasm3" ]; then if command -v wasm3 >/dev/null; then wasm3 "$1" && exit 0 @@ -65,5 +59,27 @@ if [ -z "$WASI_RUNTIME" ] || [ "$WASI_RUNTIME" = "lucet" ]; then fi fi +if [ -z "$WASI_RUNTIME" ] || [ "$WASI_RUNTIME" = "node" ]; then + if echo | node --experimental-wasi-unstable-preview1 >/dev/null 2>&1; then + { + echo "import fs from 'fs'; import { WASI } from 'wasi';" + echo "const wasi = new WASI({args: process.argv, env: process.env, preopens: {'.':'.'}});" + echo "const importObject = { wasi_snapshot_preview1: wasi.wasiImport };" + echo "const wasm = await WebAssembly.compile(fs.readFileSync('${1}'));" + echo "const instance = await WebAssembly.instantiate(wasm, importObject);" + echo "wasi.start(instance);" + } >"${1}.mjs" + cat "${1}.mjs" >/tmp/a + node --experimental-wasi-unstable-preview1 "${1}.mjs" 2>/tmp/err && + rm -f "${1}.mjs" && exit 0 + fi +fi + +if [ -z "$WASI_RUNTIME" ] || [ "$WASI_RUNTIME" = "wasmer-js" ]; then + if command -v wasmer-js >/dev/null; then + wasmer-js run "$1" --dir=. && exit 0 + fi +fi + echo "WebAssembly runtime failed" >&2 exit 1