2019-04-08 12:45:08 -07:00
|
|
|
#! /bin/sh
|
|
|
|
|
2021-02-18 15:33:43 -07:00
|
|
|
unset LDFLAGS
|
|
|
|
unset CFLAGS
|
|
|
|
|
2019-10-22 08:16:54 -07:00
|
|
|
if command -v wasm-opt >/dev/null; then
|
2021-11-18 06:15:02 -07:00
|
|
|
wasm-opt -O4 -o "${1}.tmp" "$1" && mv -f "${1}.tmp" "$1"
|
2019-10-22 08:16:54 -07:00
|
|
|
fi
|
|
|
|
|
2022-12-17 13:46:00 -07:00
|
|
|
if [ -z "$WASI_RUNTIME" ] || [ "$WASI_RUNTIME" = "wasmedge" ]; then
|
|
|
|
if command -v wasmedgec >/dev/null && command -v wasmedge >/dev/null; then
|
2022-12-18 13:06:44 -07:00
|
|
|
wasmedgec "$1" "${1}.so" >/dev/null &&
|
2022-12-17 13:46:00 -07:00
|
|
|
wasmedge --dir=.:. "${1}.so" &&
|
|
|
|
rm -f "${1}.so" &&
|
|
|
|
exit 0
|
2019-10-21 23:59:24 -07:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2019-10-11 08:31:57 -07:00
|
|
|
if [ -z "$WASI_RUNTIME" ] || [ "$WASI_RUNTIME" = "wasmer" ]; then
|
|
|
|
if command -v wasmer >/dev/null; then
|
2021-02-18 15:33:43 -07:00
|
|
|
wasmer run "$1" "--${WASMER_BACKEND:-cranelift}" --dir=. && exit 0
|
2019-10-11 08:31:57 -07:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2022-12-17 13:46:00 -07:00
|
|
|
if [ -z "$WASI_RUNTIME" ] || [ "$WASI_RUNTIME" = "wasmtime" ]; then
|
|
|
|
if command -v wasmtime >/dev/null; then
|
|
|
|
wasmtime run --dir=. "$1" && exit 0
|
2021-02-18 15:33:43 -07:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2022-12-17 13:46:00 -07:00
|
|
|
if [ -z "$WASI_RUNTIME" ] || [ "$WASI_RUNTIME" = "wavm" ]; then
|
|
|
|
if command -v wavm >/dev/null; then
|
|
|
|
wavm run --abi=wasi "$1" && exit 0
|
2021-02-18 15:33:43 -07:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2022-12-19 02:14:56 -07:00
|
|
|
if [ -z "$WASI_RUNTIME" ] || [ "$WASI_RUNTIME" = "bun" ]; then
|
|
|
|
if echo | bun help >/dev/null 2>&1; then
|
|
|
|
{
|
|
|
|
echo "import fs from 'fs'; import { init, WASI } from '@wasmer/wasi';"
|
|
|
|
echo "await init();"
|
|
|
|
echo "const wasi = new WASI({args: process.argv, env: process.env, preopens: {'.':'.'}});"
|
|
|
|
echo "await (async function() {"
|
|
|
|
echo " const wasm = await WebAssembly.compile(fs.readFileSync('${1}'));"
|
|
|
|
echo " await wasi.instantiate(wasm, {});"
|
|
|
|
echo " wasi.start();"
|
|
|
|
echo "})().catch(e => { console.error(e); process.exit(1); });"
|
|
|
|
} >"${1}.mjs"
|
|
|
|
bun run "${1}.mjs" 2>/tmp/err &&
|
|
|
|
rm -f "${1}.mjs" && exit 0
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2021-02-20 13:24:29 -07:00
|
|
|
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 };"
|
2022-12-19 02:14:56 -07:00
|
|
|
echo "await (async function() {"
|
2022-12-18 13:36:35 -07:00
|
|
|
echo " const wasm = await WebAssembly.compile(fs.readFileSync('${1}'));"
|
|
|
|
echo " const instance = await WebAssembly.instantiate(wasm, importObject);"
|
|
|
|
echo " wasi.start(instance);"
|
|
|
|
echo "})().catch(e => { console.error(e); process.exit(1); });"
|
2021-02-20 13:24:29 -07:00
|
|
|
} >"${1}.mjs"
|
2022-12-18 13:36:35 -07:00
|
|
|
node --experimental-wasm-bigint --experimental-wasi-unstable-preview1 "${1}.mjs" 2>/tmp/err &&
|
2021-02-20 13:24:29 -07:00
|
|
|
rm -f "${1}.mjs" && exit 0
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2022-12-17 13:46:00 -07:00
|
|
|
if [ -z "$WASI_RUNTIME" ] || [ "$WASI_RUNTIME" = "wasm3" ]; then
|
|
|
|
if command -v wasm3 >/dev/null; then
|
|
|
|
wasm3 "$1" && exit 0
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z "$WASI_RUNTIME" ] || [ "$WASI_RUNTIME" = "iwasm" ]; then
|
|
|
|
if iwasm | grep -qi wasi >/dev/null 2>&1; then
|
|
|
|
iwasm "$1" && exit 0
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z "$WASI_RUNTIME" ] || [ "$WASI_RUNTIME" = "wazero" ]; then
|
|
|
|
if command -v wazero >/dev/null; then
|
|
|
|
wazero run -mount .:/ "$1" && exit 0
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2021-02-20 13:24:29 -07:00
|
|
|
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
|
|
|
|
|
2019-04-22 16:23:41 -07:00
|
|
|
echo "WebAssembly runtime failed" >&2
|
|
|
|
exit 1
|