From 32b97d0e852650683c1ce6da1f70c3486eac34e6 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Mon, 4 Aug 2025 23:02:35 +0100 Subject: [PATCH] utils/wasm: Add Node.js WASI runner (#83524) This runner can be utilized as an alternative to WasmKit when running tests or executables from Swift packages compiled to Wasm, when better performance or improved coverage of WASI 0.1 ABI is needed. This script is meant to be running only locally at this time and doesn't assume presence of Node.js on CI, since no CI scripts were changed. --- utils/wasm/node-wasi-runner | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100755 utils/wasm/node-wasi-runner diff --git a/utils/wasm/node-wasi-runner b/utils/wasm/node-wasi-runner new file mode 100755 index 00000000000..ecb9e308140 --- /dev/null +++ b/utils/wasm/node-wasi-runner @@ -0,0 +1,36 @@ +#!/usr/bin/env node --disable-warning=ExperimentalWarning + +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift open source project +// +// Copyright (c) 2025 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +// +//===----------------------------------------------------------------------===// + +const { readFile } = require('node:fs/promises'); +const { WASI } = require('node:wasi'); +const { argv, env } = require('node:process'); + +const wasmFile = argv[2]; + +const wasi = new WASI({ + version: 'preview1', + args: argv.splice(2), + env, + preopens: { + }, +}); + +(async () => { + const wasm = await WebAssembly.compile( + await readFile(wasmFile) + ); + const instance = await WebAssembly.instantiate(wasm, wasi.getImportObject()); + + wasi.start(instance); +})();