josephrw/wasm-compiler-bridge
0
WASM Compiler Bridge
A web-based compiler bridge that compiles C++ and Swift source code to WebAssembly (WASM) and runs it in the browser.
Features
- C++ -> WASM via Emscripten (single-file JS+WASM glue, runs directly in browser)
- Swift -> WASM via SwiftWasm (generates WASI-target WASM binary)
- Monaco code editor with error/warning squiggles
- Examples dropdown with built-in code snippets
- Compiler flags input (e.g.
-O3 -s WASM=1) - Export compiled output (
.jsfor C++,.wasmfor Swift) - WASM Inspector panel (exports, imports, memory)
- Resizable editor/output panels
- Settings modal (theme, font size, auto-compile)
- URL hash sharing + localStorage persistence
- Keyboard shortcuts (
Ctrl+Entercompile,Ctrl+Rrun, etc.)
Architecture
Browser (Monaco Editor) <--HTTP--> Express API --> emcc / swiftc
(temp files)Prerequisites (Local Development)
- Node.js 18+ (for the bridge server)
- macOS or Linux with ~3GB free disk space (for toolchains)
Quick Start (Local)
1. Install dependencies
cd wasm-compiler-bridge
npm install
npm run build # compiles TypeScript2. Install compiler toolchains
./scripts/install-toolchains.shThis installs:
- Emscripten to
~/wasm-toolchains/emsdk - SwiftWasm to
~/wasm-toolchains/swiftwasm
Note: This downloads ~1-2GB.
3. Start the bridge
npm startOpen http://localhost:3456 in your browser.
Deploy to Hugging Face Space
The project includes a Dockerfile with both toolchains pre-installed.
- Create a new HF Space with Docker as the SDK
- Push this repo to the Space (or use the Dockerfile)
- The container installs Emscripten + SwiftWasm at build time
- The server listens on port
7860(HF default)
# Clone your HF Space repo
git clone https://huggingface.co/spaces/YOURNAME/wasm-compiler-bridge
cd wasm-compiler-bridge
# Copy project files
cp -r /path/to/wasm-compiler-bridge/* .
git add .
git commit -m "Initial deploy"
git pushNote: First build will take 10-20 minutes as it downloads and installs Emscripten + SwiftWasm inside the container.
API Endpoints
Environment Variables
C++ Example
#include <emscripten.h>
#include <stdio.h>
extern "C" {
EMSCRIPTEN_KEEPALIVE
int add(int a, int b) { return a + b; }
}
int main() {
printf("Hello from C++ WASM\n");
return 0;
}Swift Example
@_cdecl("add")
public func add(a: Int32, b: Int32) -> Int32 {
return a + b
}
print("Swift WASM module compiled")Limitations
- Swift browser execution requires a full WASI runtime (the bridge shows module info/exports). For full browser execution, use SwiftWasm's JavaScriptKit.
- Security: This is a dev tool. Do not expose to untrusted networks without sandboxing.
