Hello, World!

This section will show you how to compile a simple Swift code into WebAssembly and run the produced binary on WASI supported WebAssembly runtime.

1. Create a Swift file

$ echo 'print("Hello, world!")' > hello.swift

2. Compile Swift code into WebAssembly with WASI

$ TOOLCHAIN_PATH=$(dirname $(swiftenv which swiftc))/../
$ swiftc \
    -target wasm32-unknown-wasi \
    -sdk $TOOLCHAIN_PATH/share/wasi-sysroot \
    hello.swift -o hello.wasm

3. Run the produced binary on WebAssembly runtime

You can the run the produced binary with wasmer (or other WebAssembly runtime):

$ wasmer hello.wasm

The produced binary depends on WASI which is an interface of system call for WebAssembly. So you need to use WASI supported runtime and when you run the binary on browser, you need WASI polyfill library like @wasmer/wasi.