Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 38 ยท Compiling from C, Rust and Others

Other Languages for WebAssembly

Compare the languages that compile to WebAssembly - AssemblyScript, Go and TinyGo, Zig, C# Blazor, Kotlin, Swift - with runnable BigInt and feature-detection examples.

C, C++ and Rust get most of the attention, but WebAssembly is a compile target, not a language. Any language whose compiler can emit a .wasm file can run in the browser, on a server or in a plugin host. The interesting question is not "can language X do it?" but "what does it cost, and what does it bring along?" A language with a garbage collector and a big runtime produces a bigger module than one without.

This lesson tours the main options, explains the trade-offs you will meet, and uses runnable examples to show two things that matter regardless of the source language: how 64-bit integers behave at the JavaScript boundary, and how to detect at runtime which WebAssembly features a browser supports.

The big picture

Languages that target Wasm

Rust Tiny modules, great tooling.
C and C++ Emscripten, huge existing code.
Zig Small output, easy cross-compile.
AssemblyScript TypeScript-like syntax.

No runtime means small files.

1 / 2

Two families emerge. Ahead-of-time compiled, no runtime languages (C, C++, Rust, Zig, AssemblyScript) produce code that is almost exactly what you wrote, with a small allocator. Runtime-carrying languages (Go, C#, Python, Ruby, Java via TeaVM or CheerpJ) either ship their garbage collector and scheduler inside the module, or they ship a whole interpreter compiled to Wasm and run your source on top of it.

AssemblyScript

AssemblyScript looks like TypeScript, but it is a stricter language with concrete numeric types (i32, f64, u8) and compiles straight to WebAssembly. It is the easiest route for a front-end developer who wants a first module without learning C or Rust.

TypeScript
// assembly/index.ts
export function factorial(n: i64): i64 {
  return n <= 1 ? 1 : n * factorial(n - 1);
}

export function hypot(a: f64, b: f64): f64 {
  return Math.sqrt(a * a + b * b);
}
Terminal
npm install --save-dev assemblyscript
npx asc assembly/index.ts --outFile build/release.wasm --optimize

It has a built-in memory manager and reference-counted garbage collection, but you can strip it down (--runtime stub) for tiny numeric modules.

Go and TinyGo

The standard Go toolchain can target the browser with GOOS=js GOARCH=wasm. The module includes Go's runtime, scheduler and garbage collector, so even a "hello world" is about two megabytes, and it needs a JavaScript support file called wasm_exec.js. TinyGo is a separate compiler built on LLVM that targets small devices and WebAssembly; the same program can be a few tens of kilobytes, at the cost of supporting less of the standard library and of reflection.

Terminal
# standard Go, large but complete
GOOS=js GOARCH=wasm go build -o main.wasm

# TinyGo, small
tinygo build -o main.wasm -target wasm ./main.go
# or for WASI hosts
tinygo build -o main.wasm -target wasip1 ./main.go

Go also supports GOOS=wasip1 GOARCH=wasm for WASI hosts, which is the right choice for server-side plugins.

Zig

Zig is a systems language with no hidden allocations and first-class cross compilation. Its wasm output is small and predictable:

ZIG
// add.zig
export fn add(a: i32, b: i32) i32 {
    return a + b;
}
Terminal
zig build-exe add.zig -target wasm32-freestanding -fno-entry -rdynamic -O ReleaseSmall

The export keyword makes the function part of the module's interface, and -rdynamic keeps exported symbols.

C# and .NET (Blazor)

Blazor WebAssembly runs C# in the browser. It ships the .NET runtime compiled to Wasm and your assemblies as data on top of it, so the first download is several megabytes, then cached. Ahead-of-time compilation (AOT) turns your own methods into native Wasm for speed at the cost of size. Blazor is a good fit for apps whose team already writes C#, and a poor fit for a small widget on a content page.

Kotlin, Swift and others

  • Kotlin/Wasm uses the WebAssembly garbage collection proposal (struct and array types), so it does not need to ship its own collector.
  • Swift has an official WebAssembly target (swift build --swift-sdk ...-wasm) mainly used for WASI and server tools.
  • Dart/Flutter compile to WasmGC for web builds.
  • Python, Ruby, PHP, Lua run by compiling their interpreters to Wasm (Pyodide is the best-known example, and it powers the Python runner on this site).

What every language must handle: 64-bit integers

WebAssembly has an i64 type, and languages use it constantly (long long, u64, int64_t, AssemblyScript i64). JavaScript numbers are doubles with 53 bits of integer precision, so an i64 crossing the boundary becomes a BigInt. Passing a regular number where an i64 is expected throws a TypeError. Try it with a module that exports fact(n: i64) -> i64 and hypot(a: f64, b: f64) -> f64:

Node.js i64 is BigInt, f64 is Number

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Notice the overflow: WebAssembly integer arithmetic wraps around (modulo 2 to the power of the bit width) instead of trapping, just like C. If you need exact big numbers you must use a bignum library in your language of choice.

Detecting features at runtime

New WebAssembly features (SIMD, threads, exception handling, garbage collection, tail calls) ship in browsers at different times. A page that wants to use them should check support and fall back gracefully. There is no navigator.wasm.simd property; the reliable test is to ask the engine to validate a tiny module that uses the feature. If WebAssembly.validate says yes, the feature is supported.

Node.js Feature detection by validating tiny modules

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The popular wasm-feature-detect package is exactly a collection of such probe modules. Always ship a baseline build alongside an optimized one when a feature is not yet universal.

Choosing a language

If you want... Consider
The smallest, fastest modules and strong safety Rust
To reuse an existing C or C++ library Emscripten
A gentle start from TypeScript AssemblyScript
Small output and simple cross-compiling Zig
To reuse a Go or C# team and skill set TinyGo, Blazor
A server-side plugin in a language you know Any language with a WASI target

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What type does an i64 result have when it reaches JavaScript?

    JavaScript numbers cannot hold every 64-bit integer, so the JS API uses BigInt for i64 values and expects BigInt arguments.
  2. 2. Why is a standard Go hello-world module much larger than a Rust or Zig one?

    Runtime-carrying languages ship their own support code inside the module. TinyGo cuts it down by supporting less.
  3. 3. What is the dependable way to test whether a browser supports SIMD in WebAssembly?

    Engines validate modules against the features they support. If a probe module using the feature validates, the feature is available.
  4. 4. What happens when i64 arithmetic overflows in WebAssembly?

    Integer operations in WebAssembly wrap, exactly like unsigned arithmetic in C. Division by zero and overflow in division are the traps.

Summary

  • WebAssembly is a compile target; many languages can produce it.
  • No-runtime languages (C, C++, Rust, Zig, AssemblyScript) give small modules; runtime-carrying languages (Go, C#, Python) ship extra support code.
  • i64 values cross the JavaScript boundary as BigInt; arithmetic wraps on overflow.
  • Detect newer features with WebAssembly.validate on probe modules and serve a fallback build.
  • Pick the language by what you need to reuse: libraries, team skills or minimum size.

Next: Passing Strings and Structs Across the Boundary