swiftwasm-pad is a online playground to help developers learn about Swift on Web.
https://swiftwasm-pad.netlify.app
-
./CompileAPI
- A backend service which compiles.swift
into WebAssembly object file. Implemented by Swift AWS Lambda Runtime. -
./Frontend
- A web frontend editor application. Implemented by SwiftWasm and Tokamak. -
./PreviewSystem
- A shared package to provide prebuilt.swiftmodule
and wasm library.
$ docker-compose up
# Build PreviewSystem
$ ./PreviewSystem/build-script.sh
# Start frontend
$ cd Frontend
$ npm install
$ npm run start
# Start backend
$ cd CompileAPI
$ export LOCAL_LAMBDA_SERVER_ENABLED=true
$ export LAMBDA_PREVIEW_STUB_PACKAGE=$(pwd)/../PreviewSystem/distribution/PreviewStub
$ export LAMBDA_SWIFTC=$(dirname $(pwd))/.toolchain/darwin/$(cat ../.swift-version)/usr/bin/swiftc
$ swift run CompileSwiftWasm
CompileAPI is deployed with SwiftWasm toolchain built on Amazon Linux 2 and PreviewSystem
.
.swiftmodule
files in PreviewSystem are used to compile user input code that can use Tokamak
or JavaScriptKit
. This API returns not an executable wasm binary but an object file.
The object file will be linked with prebuilt library on browser.
Frontend web application sends requests to CompilerAPI, link compiled object files and shared library and run linked executable wasm.
library.so.wasm
is the shared library combined with Swift Standard Library, Tokamak and JavaScriptKit. The library is built by ./PreviewSystem/build-script.sh
This frontend application uses WebAssembly linker implemented by Swift named chibi-link to reduce CompileAPI's load and also reduce transfer data size. The linker is executed in Web Worker to avoid blocking UI.