Compile Swift to Wasm WASI
Thanks to the great work of Swift Wasm we can now compile Swift to WebAssembly WASI.
Wasienv makes very easy to get started with Swift and WebAssembly.
1
wasienv install-swift
Copied!
You will need the latest version of version of Wasienv installed.
If you don't have wasienv installed in your system, or want to upgrade it please follow the instructions here
Once that's done, just create an example Swift file:
example.swift
fizzbuzz.swift
1
if CommandLine.arguments.count < 2 {
2
print("Hello, WASI!");
3
} else {
4
print("Hello, \(CommandLine.arguments[1])");
5
}
Copied!
1
for i in 1...100
2
{
3
if i % 3 == 0 && i % 5 == 0 {
4
print("FizzBuzz")
5
} else if i % 3 == 0 {
6
print("Fizz")
7
} else if i % 5 == 0 {
8
print("Buzz")
9
} else {
10
print(i)
11
}
12
}
Copied!
Now let's Compile it!
1
wasiswiftc example.swift -o example.wasm
Copied!
Once the program finishes, you will have a new example.wasm file ready to be executed with your favorite WebAssembly runtime!
1
$ wasmer example.wasm
2
Hello, WASI!
Copied!
Last modified 8mo ago
Copy link