Compile Swift to Wasm WASI
Wasienv makes very easy to get started with Swift and WebAssembly.
wasienv install-swift
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
if CommandLine.arguments.count < 2 {
print("Hello, WASI!");
} else {
print("Hello, \(CommandLine.arguments[1])");
}
for i in 1...100
{
if i % 3 == 0 && i % 5 == 0 {
print("FizzBuzz")
} else if i % 3 == 0 {
print("Fizz")
} else if i % 5 == 0 {
print("Buzz")
} else {
print(i)
}
}
Now let's Compile it!
wasiswiftc example.swift -o example.wasm
Once the program finishes, you will have a new
example.wasm
file ready to be executed with your favorite WebAssembly runtime!$ wasmer example.wasm
Hello, WASI!
Last modified 1yr ago