CoolFace
Apppublic

josephrw/wasm-compiler-bridge

sourceHugging Faceupdated 3mo agoView on Hugging Face
0likes
App README

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 (.js for C++, .wasm for 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+Enter compile, Ctrl+R run, 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

bash
cd wasm-compiler-bridge
npm install
npm run build   # compiles TypeScript

2. Install compiler toolchains

bash
./scripts/install-toolchains.sh

This installs:

  • —Emscripten to ~/wasm-toolchains/emsdk
  • —SwiftWasm to ~/wasm-toolchains/swiftwasm
Note: This downloads ~1-2GB.

3. Start the bridge

bash
npm start

Open http://localhost:3456 in your browser.

Deploy to Hugging Face Space

The project includes a Dockerfile with both toolchains pre-installed.

  1. 1.Create a new HF Space with Docker as the SDK
  2. 2.Push this repo to the Space (or use the Dockerfile)
  3. 3.The container installs Emscripten + SwiftWasm at build time
  4. 4.The server listens on port 7860 (HF default)
bash
# 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 push
Note: First build will take 10-20 minutes as it downloads and installs Emscripten + SwiftWasm inside the container.

API Endpoints

EndpointMethodDescription
GET /api/statusGETCheck toolchain availability
GET /api/examplesGETBuilt-in code examples for C++ and Swift
POST /api/compile/cppPOSTCompile C++ code to WASM (accepts { code, flags })
POST /api/compile/swiftPOSTCompile Swift code to WASM (accepts { code, flags })
POST /api/inspectPOSTInspect a WASM binary ({ wasmBase64 })

Environment Variables

VariableDefaultDescription
PORT3456Server port
EMSDK_PATH~/wasm-toolchains/emsdkPath to Emscripten SDK
SWIFTWASM_PATH~/wasm-toolchains/swiftwasmPath to SwiftWasm toolchain

C++ Example

cpp
#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

swift
@_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.