Skip to content

Commit 74c35de

Browse files
Support latest nightly snapshot (swiftwasm#246)
* Allow async main entry point with JavaScriptEventLoop This change allows the Swift program to have an async main entry point when the JavaScriptEventLoop is installed as the global executor. * Remove Wasmer WASI tests We no longer use it in carton * Support the latest nightly snapshot * Migrate to ESM * Fix host build
1 parent 68376c5 commit 74c35de

File tree

21 files changed

+200
-412
lines changed

21 files changed

+200
-412
lines changed

Diff for: .github/workflows/test.yml

+1-3
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@ jobs:
1818
# Ensure that test succeeds with all toolchains and wasi backend combinations
1919
- { os: ubuntu-20.04, toolchain: wasm-5.7.3-RELEASE, wasi-backend: Node }
2020
- { os: ubuntu-20.04, toolchain: wasm-5.8.0-RELEASE, wasi-backend: Node }
21-
- { os: ubuntu-20.04, toolchain: wasm-5.7.3-RELEASE, wasi-backend: Wasmer }
22-
- { os: ubuntu-20.04, toolchain: wasm-5.8.0-RELEASE, wasi-backend: Wasmer }
23-
- { os: ubuntu-20.04, toolchain: wasm-5.9.1-RELEASE, wasi-backend: Wasmer }
2421
- { os: ubuntu-20.04, toolchain: wasm-5.7.3-RELEASE, wasi-backend: MicroWASI }
2522
- { os: ubuntu-20.04, toolchain: wasm-5.8.0-RELEASE, wasi-backend: MicroWASI }
2623
- { os: ubuntu-20.04, toolchain: wasm-5.9.1-RELEASE, wasi-backend: MicroWASI }
24+
- { os: ubuntu-22.04, toolchain: wasm-DEVELOPMENT-SNAPSHOT-2024-05-02-a, wasi-backend: Node }
2725

2826
runs-on: ${{ matrix.entry.os }}
2927
env:

Diff for: IntegrationTests/Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ TestSuites/.build/$(CONFIGURATION)/%.wasm: FORCE
1111
--triple wasm32-unknown-wasi \
1212
--configuration $(CONFIGURATION) \
1313
-Xswiftc -Xclang-linker -Xswiftc -mexec-model=reactor \
14-
-Xlinker --export=main \
14+
-Xlinker --export-if-defined=main -Xlinker --export-if-defined=__main_argc_argv \
15+
--static-swift-stdlib -Xswiftc -static-stdlib \
1516
$(SWIFT_BUILD_FLAGS)
1617

1718
dist/%.wasm: TestSuites/.build/$(CONFIGURATION)/%.wasm

Diff for: IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ try test("Call Function With This") {
400400
let setName = try expectFunction(getJSValue(this: cat1, name: "setName"))
401401

402402
// Direct call without this
403-
try expectEqual(getIsCat(), .undefined)
403+
_ = try expectThrow(try getIsCat.throws())
404404

405405
// Call with this
406406
let gotIsCat = getIsCat(this: cat1)

Diff for: IntegrationTests/bin/benchmark-tests.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const { startWasiTask } = require("../lib");
2-
const { performance } = require("perf_hooks");
1+
import { startWasiTask } from "../lib.js";
2+
import { performance } from "perf_hooks";
33

44
const SAMPLE_ITERATION = 1000000
55

Diff for: IntegrationTests/bin/concurrency-tests.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { startWasiTask } = require("../lib");
1+
import { startWasiTask } from "../lib.js";
22

33
Error.stackTraceLimit = Infinity;
44

Diff for: IntegrationTests/bin/primary-tests.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ global.objectDecodingTest = {
102102
bi: BigInt(3)
103103
};
104104

105-
const { startWasiTask, WASI } = require("../lib");
105+
import { startWasiTask } from "../lib.js";
106106

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

Diff for: IntegrationTests/lib.js

+14-53
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,9 @@
1-
const SwiftRuntime = require("javascript-kit-swift").SwiftRuntime;
2-
const WasmerWASI = require("@wasmer/wasi").WASI;
3-
const WasmFs = require("@wasmer/wasmfs").WasmFs;
4-
const NodeWASI = require("wasi").WASI;
5-
const { WASI: MicroWASI, useAll } = require("uwasi");
6-
7-
const promisify = require("util").promisify;
8-
const fs = require("fs");
9-
const readFile = promisify(fs.readFile);
1+
import { SwiftRuntime } from "javascript-kit-swift"
2+
import { WASI as NodeWASI } from "wasi"
3+
import { WASI as MicroWASI, useAll } from "uwasi"
4+
import * as fs from "fs/promises"
105

116
const WASI = {
12-
Wasmer: ({ programName }) => {
13-
// Instantiate a new WASI Instance
14-
const wasmFs = new WasmFs();
15-
// Output stdout and stderr to console
16-
const originalWriteSync = wasmFs.fs.writeSync;
17-
wasmFs.fs.writeSync = (fd, buffer, offset, length, position) => {
18-
const text = new TextDecoder("utf-8").decode(buffer);
19-
switch (fd) {
20-
case 1:
21-
console.log(text);
22-
break;
23-
case 2:
24-
console.error(text);
25-
break;
26-
}
27-
return originalWriteSync(fd, buffer, offset, length, position);
28-
};
29-
const wasi = new WasmerWASI({
30-
args: [programName],
31-
env: {},
32-
bindings: {
33-
...WasmerWASI.defaultBindings,
34-
fs: wasmFs.fs,
35-
},
36-
});
37-
38-
return {
39-
wasiImport: wasi.wasiImport,
40-
start(instance) {
41-
wasi.start(instance);
42-
instance.exports._initialize();
43-
instance.exports.main();
44-
}
45-
}
46-
},
477
MicroWASI: ({ programName }) => {
488
const wasi = new MicroWASI({
499
args: [programName],
@@ -53,25 +13,28 @@ const WASI = {
5313

5414
return {
5515
wasiImport: wasi.wasiImport,
56-
start(instance) {
16+
start(instance, swift) {
5717
wasi.initialize(instance);
58-
instance.exports.main();
18+
swift.main();
5919
}
6020
}
6121
},
6222
Node: ({ programName }) => {
6323
const wasi = new NodeWASI({
6424
args: [programName],
6525
env: {},
26+
preopens: {
27+
"/": "./",
28+
},
6629
returnOnExit: false,
6730
version: "preview1",
6831
})
6932

7033
return {
7134
wasiImport: wasi.wasiImport,
72-
start(instance) {
35+
start(instance, swift) {
7336
wasi.initialize(instance);
74-
instance.exports.main();
37+
swift.main();
7538
}
7639
}
7740
},
@@ -88,10 +51,10 @@ const selectWASIBackend = () => {
8851
return WASI.Node;
8952
};
9053

91-
const startWasiTask = async (wasmPath, wasiConstructor = selectWASIBackend()) => {
54+
export const startWasiTask = async (wasmPath, wasiConstructor = selectWASIBackend()) => {
9255
const swift = new SwiftRuntime();
9356
// Fetch our Wasm File
94-
const wasmBinary = await readFile(wasmPath);
57+
const wasmBinary = await fs.readFile(wasmPath);
9558
const wasi = wasiConstructor({ programName: wasmPath });
9659

9760
// Instantiate the WebAssembly file
@@ -106,7 +69,5 @@ const startWasiTask = async (wasmPath, wasiConstructor = selectWASIBackend()) =>
10669

10770
swift.setInstance(instance);
10871
// Start the WebAssembly WASI instance!
109-
wasi.start(instance);
72+
wasi.start(instance, swift);
11073
};
111-
112-
module.exports = { startWasiTask, WASI };

0 commit comments

Comments
 (0)