Skip to main content

WASM

WASM stands for WebAssembly. Another topic that's confusing when we take the name literally.

It's not a programming language. WASM means assembly language for the web. It's a low-level assembly like language that runs in the browser.

WASM isn't a programming language
  1. WASM is a compiler target. It takes code written in other languages and compiles it to WASM.
  2. It's similar to JVM. Write code in Java, Kotlin or Scala and compile it to JVM bytecode.
  3. Similarly, WASM also produces a bytecode that can be executed in any runtime.
wasm-generation

WASM Compile Time​

An application written in any application is compiled into WASM bytecode. This is why WASM is called a compiler target.

WASM Compiler generates byte code similar to java. The runtime engine does minimal work to generate the native code at runtime.

WASM isn't a programming language

Nobody writes code in WASM. It's always generated from other languages.

WASM Runtime​

WASM is compiled to a bytecode that needs a runtime which then executes the bytecode. For example, if we want to run WASM in JVM, we need a WASM runtime that can run inside JVM.

Chicory is one such example. It's Java code that can parse WASM bytecode and create java bytecode for it.

WASM aimed initially at web

The main goal of WASM was to run code that's already written in different languages in browser. There was no point in porting the entire code to JS. WASM compiler instead took the original code and generated WSAM code for it.

Browser then provided two runtimes. One for JS and one for WASM.

WASM as sandbox​

WASM can do only computational work. All IO operations must be provided by the host environment.

WASM is a sandbox

When executed, WASM is only an engine since it only knows how to parse WSAM files. The engine only runs computational code. Anything else for example, loading a file must come from the host environment.

This is exactly why we say WASM is a sandbox by default.

what's host environment?

Host environment is the parent runtime that runs the WASM engine. For example, in a browser, the host environment is the browser itself. In Java, JVM itself is the host environment. It provides all other functions that the WASM compiled code needs.

Using Host Functions​

It's important to understand how the host functions exchange data with the WASM engine. It happens all via the memory.

wasm-host-function
How host functions are linked?

When the WSAM engine is instantiated, all the allowed functions of the host is added to a list that's passed to the engine's constructor.

These are nothing but the function references. When the function is called, the engine knows that it's the scope of the host and pauses. The engine then schedules the host function in the instruction pointer to run.

WASI specifications are only names

WASI is an agreed set of names of host functions that every host environment can provide. These aren't mandatory. For example, a function for reading file can be called anything. But WASI provides a standard name for it. The compiler generates standard names for the host functions. All runtime implementations will provide implementations for these standard names.

QuickJS​

QuickJS is what makes things even more interesting and confusing.

QuickJS is a JS runtime. It can parse JS code and execute. It's written in C and compiled to WASM. Meaning, we pass the actual JS code to QuickJS, and it will execute it for us inside the WASM sandbox.

QuickJS and Monty

Whatever QuickJS does for JS, is done by Monty for python. Provides a secure Python interpreter.

Even Monty provides a WASM compiled version of the interpreter.

wasm-generation