WebAssembly
WebAssembly (sometimes abbreviated Wasm) defines a portable binary-code format and a corresponding text format for executable programs and software interfaces for facilitating interactions between such programs and their host environment. WebAssembly
Assembly languages in general consists of simple and atomic operations that the processor can follow.
For example, let's consider the following add.c
C code.
#include <stdio.h>
int add(int x, int y) { return x + y; }
int main() {
printf("Hello World\n");
printf("1 + 2 = %d \n", add(1, 2));
return 0;
}
❯ gcc add.c
❯ ./a.out
Hello World
1 + 2 = 3
It is a very straightforward C code that returns the sum of the two integers provided as input.
We can convert this C code into WebAssembly with a build tool called Emscripten.
After running the following command, we can get the WASM version of the function int add(int x, int y)
.
emcc add.c -o index.html
We can now create a local server to test this example.
❯ npx serve .
┌───────────────────────────────────────────────────┐
│ │
│ Serving! │
│ │
│ - Local: http://localhost:3000 │
│ - On Your Network: http://192.168.8.100:3000 │
│ │
│ Copied local address to clipboard! │
│ │
└───────────────────────────────────────────────────┘
And we can confirm that it is working as expected.
Interesting Implementations
SQLite Wasm in the browser backed by the Origin Private File System - Chrome Developers
- SQLite based on Web Assembly
- Binding a low-level sqlite3 API which is as close to the C one as feasible in terms of usage.
- A higher-level object-oriented API, more akin to sql. js and Node. Js-style implementations speak directly to the low-level API. This API must be used from the same thread as the low-level API.
- A Worker-based API that speaks to the previous APIs via Worker messages. This one is intended for use in the main thread, with the lower-level APIs installed in a Worker thread and talking to them via Worker messages.
- A Promise-based variant of the Worker API that entirely hides the cross-thread communication aspects from the user.
- Support persistent client-side storage using JavaScript APIs, including the Origin Private File System (OPFS).