Skip to content 2.0

Compiling and running WebAssembly code

Author: Natalia Venditto

Date First Published: Wed Jan 04 2023

Wasm as a compilation target

But don’t despair! We don’t need to learn to write WebAssembly Text in order to create Wasm code. In fact, WebAssembly wasn’t really designed to be created in this way. It was designed to be a portable compilation target for higher-level languages.

Can we compile JavaScript to WebAssembly?

JavaScript toolchain to compile to WebAssembly is currently in experimental or early stages, but as we already discussed, they’re interoperable given you can import and execute Wasm modules as part of your scripts. (Although in terms of speed, it may make sense to compile JavaScript to binary, so it doesn’t have to be interpreted. But that’s another story. And this work is in progress.)

Languages that compile to .wasm

Having other programming languages compile to binary code that can be ran in the browser, and has access to Web Platform APIs, is the most appealing feature of this technology, and why it is so successful. It means that developers that were restricted to writing server-side only code, can now run their code in the client.

The following high-level languages, compile to ´.wasm´ that can be executed in the browser, today. It is very likely that in the future, more languages compile to this code format, like JavaScript, since efforts to make that possible, are in progress.

  • Java
  • PHP
  • C# and .NET
  • C++
  • Ruby
  • C
  • Swift
  • R
  • Go
  • Kotlin
  • Rust
  • Haskell
  • Lua
  • Perl
  • Zig

There are non-browser scenarios where .wasm code is potentially executable, that we will describe in the next page.

I will try to keep this list up to date as much as possible, but you can also visit Fermyon’s site where for sure they keep much better track.

Wasm compilation toolchains

What makes it possible for languages to compile to Wasm, is called the toolchain that supports conformance with the target language.

service workers

For exmaple the Rust toolchain is described here.

It basically includes cargo, which is the default Rust CLI, npm for dependency management and wasm-pack to do the transformation to binary.