Skip to content

Commit c1a0923

Browse files
authoredMay 11, 2022
Test with Node.js's WASI implementation (#192)
1 parent e6b326d commit c1a0923

File tree

4 files changed

+91
-39
lines changed

4 files changed

+91
-39
lines changed
 

‎.github/workflows/test.yml

+18-7
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,35 @@ jobs:
88
name: Build and Test
99
strategy:
1010
matrix:
11-
os: [macos-10.15, macos-11, macos-12, ubuntu-18.04, ubuntu-20.04]
12-
toolchain:
13-
- wasm-5.5.0-RELEASE
14-
- wasm-5.6.0-RELEASE
15-
runs-on: ${{ matrix.os }}
11+
entry:
12+
# Ensure that all host can install toolchain, build project, and run tests
13+
- { os: macos-10.15, toolchain: wasm-5.6.0-RELEASE, wasi-backend: Node }
14+
- { os: macos-11, toolchain: wasm-5.6.0-RELEASE, wasi-backend: Node }
15+
- { os: macos-12, toolchain: wasm-5.6.0-RELEASE, wasi-backend: Node }
16+
- { os: ubuntu-18.04, toolchain: wasm-5.6.0-RELEASE, wasi-backend: Node }
17+
18+
# Ensure that test succeeds with all toolchains and wasi backend combinations
19+
- { os: ubuntu-20.04, toolchain: wasm-5.5.0-RELEASE, wasi-backend: Node }
20+
- { os: ubuntu-20.04, toolchain: wasm-5.6.0-RELEASE, wasi-backend: Node }
21+
- { os: ubuntu-20.04, toolchain: wasm-5.5.0-RELEASE, wasi-backend: Wasmer }
22+
- { os: ubuntu-20.04, toolchain: wasm-5.6.0-RELEASE, wasi-backend: Wasmer }
23+
24+
runs-on: ${{ matrix.entry.os }}
1625
steps:
1726
- name: Checkout
1827
uses: actions/checkout@master
1928
with:
2029
fetch-depth: 1
2130
- name: Run Test
31+
env:
32+
JAVASCRIPTKIT_WASI_BACKEND: ${{ matrix.entry.wasi-backend }}
2233
run: |
2334
git clone https://github.com/kylef/swiftenv.git ~/.swiftenv
2435
export SWIFTENV_ROOT="$HOME/.swiftenv"
2536
export PATH="$SWIFTENV_ROOT/bin:$PATH"
2637
eval "$(swiftenv init -)"
27-
SWIFT_VERSION=${{ matrix.toolchain }} make bootstrap
28-
echo ${{ matrix.toolchain }} > .swift-version
38+
SWIFT_VERSION=${{ matrix.entry.toolchain }} make bootstrap
39+
echo ${{ matrix.entry.toolchain }} > .swift-version
2940
make test
3041
native-build:
3142
# Check native build to make it easy to develop applications by Xcode

‎IntegrationTests/Makefile

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
CONFIGURATION ?= debug
22
SWIFT_BUILD_FLAGS ?=
33

4+
NODEJS = node --experimental-wasi-unstable-preview1
5+
46
FORCE:
57
TestSuites/.build/$(CONFIGURATION)/%.wasm: FORCE
68
swift build --package-path TestSuites \
@@ -27,18 +29,18 @@ benchmark_setup: build_rt dist/BenchmarkTests.wasm
2729

2830
.PHONY: run_benchmark
2931
run_benchmark:
30-
node bin/benchmark-tests.js
32+
$(NODEJS) bin/benchmark-tests.js
3133

3234
.PHONY: benchmark
3335
benchmark: benchmark_setup run_benchmark
3436

3537
.PHONY: primary_test
3638
primary_test: build_rt dist/PrimaryTests.wasm
37-
node bin/primary-tests.js
39+
$(NODEJS) bin/primary-tests.js
3840

3941
.PHONY: concurrency_test
4042
concurrency_test: build_rt dist/ConcurrencyTests.wasm
41-
node bin/concurrency-tests.js
43+
$(NODEJS) bin/concurrency-tests.js
4244

4345
.PHONY: test
4446
test: concurrency_test primary_test

‎IntegrationTests/bin/primary-tests.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ global.callThrowingClosure = (c) => {
9595
}
9696
};
9797

98-
const { startWasiTask } = require("../lib");
98+
const { startWasiTask, WASI } = require("../lib");
9999

100100
startWasiTask("./dist/PrimaryTests.wasm").catch((err) => {
101101
console.log(err);

‎IntegrationTests/lib.js

+67-28
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,81 @@
11
const SwiftRuntime = require("javascript-kit-swift").SwiftRuntime;
2-
const WASI = require("@wasmer/wasi").WASI;
2+
const WasmerWASI = require("@wasmer/wasi").WASI;
33
const WasmFs = require("@wasmer/wasmfs").WasmFs;
4+
const NodeWASI = require("wasi").WASI;
45

56
const promisify = require("util").promisify;
67
const fs = require("fs");
78
const readFile = promisify(fs.readFile);
89

9-
const startWasiTask = async (wasmPath) => {
10-
// Instantiate a new WASI Instance
11-
const wasmFs = new WasmFs();
12-
// Output stdout and stderr to console
13-
const originalWriteSync = wasmFs.fs.writeSync;
14-
wasmFs.fs.writeSync = (fd, buffer, offset, length, position) => {
15-
const text = new TextDecoder("utf-8").decode(buffer);
16-
switch (fd) {
17-
case 1:
18-
console.log(text);
19-
break;
20-
case 2:
21-
console.error(text);
22-
break;
10+
const WASI = {
11+
Wasmer: () => {
12+
// Instantiate a new WASI Instance
13+
const wasmFs = new WasmFs();
14+
// Output stdout and stderr to console
15+
const originalWriteSync = wasmFs.fs.writeSync;
16+
wasmFs.fs.writeSync = (fd, buffer, offset, length, position) => {
17+
const text = new TextDecoder("utf-8").decode(buffer);
18+
switch (fd) {
19+
case 1:
20+
console.log(text);
21+
break;
22+
case 2:
23+
console.error(text);
24+
break;
25+
}
26+
return originalWriteSync(fd, buffer, offset, length, position);
27+
};
28+
const wasi = new WasmerWASI({
29+
args: [],
30+
env: {},
31+
bindings: {
32+
...WasmerWASI.defaultBindings,
33+
fs: wasmFs.fs,
34+
},
35+
});
36+
37+
return {
38+
wasiImport: wasi.wasiImport,
39+
start(instance) {
40+
wasi.start(instance);
41+
instance.exports._initialize();
42+
instance.exports.main();
43+
}
2344
}
24-
return originalWriteSync(fd, buffer, offset, length, position);
25-
};
26-
let wasi = new WASI({
27-
args: [],
28-
env: {},
29-
bindings: {
30-
...WASI.defaultBindings,
31-
fs: wasmFs.fs,
32-
},
33-
});
45+
},
46+
Node: () => {
47+
const wasi = new NodeWASI({
48+
args: [],
49+
env: {},
50+
returnOnExit: true,
51+
})
52+
53+
return {
54+
wasiImport: wasi.wasiImport,
55+
start(instance) {
56+
wasi.initialize(instance);
57+
instance.exports.main();
58+
}
59+
}
60+
},
61+
};
62+
63+
const selectWASIBackend = () => {
64+
const value = process.env["JAVASCRIPTKIT_WASI_BACKEND"]
65+
if (value) {
66+
const backend = WASI[value];
67+
if (backend) {
68+
return backend;
69+
}
70+
}
71+
return WASI.Node;
72+
};
3473

74+
const startWasiTask = async (wasmPath, wasiConstructor = selectWASIBackend()) => {
3575
const swift = new SwiftRuntime();
3676
// Fetch our Wasm File
3777
const wasmBinary = await readFile(wasmPath);
78+
const wasi = wasiConstructor();
3879

3980
// Instantiate the WebAssembly file
4081
let { instance } = await WebAssembly.instantiate(wasmBinary, {
@@ -45,8 +86,6 @@ const startWasiTask = async (wasmPath) => {
4586
swift.setInstance(instance);
4687
// Start the WebAssembly WASI instance!
4788
wasi.start(instance);
48-
instance.exports._initialize();
49-
instance.exports.main();
5089
};
5190

52-
module.exports = { startWasiTask };
91+
module.exports = { startWasiTask, WASI };

0 commit comments

Comments
 (0)