What is Web Assembly?

Introduction

Web Assembly (or commonly Wasm) is a binary instruction format designed as a portable compilation target for high-level programming languages like C, C++, and Rust. It allows these languages to be used for web development while providing performance close to that of native code. WebAssembly is designed to be executed in a virtual machine that runs in web browsers, making it a key component of web technologies.

Key Advantages

Getting Started

To get your feet wet and jump into WebAssembly, we will provide a simple example for running C code on the browser using Wasm. Once you’ve written a code module in C, we can use a tool like Emscripten to compile it into WebAssembly.

Setup this prerequisite by following the instructions here: https://emscripten.org/docs/getting_started/downloads.html

Firstly, we’ll need an example C file to compile. Create a hello.c file with the following sample code:

#include <stdio.h>

int main() {
    printf("Hello World\n");
    return 0;
}

Then, using the terminal window you used to enter the Emscripten compiler environment, navigate to the same directory as your hello.c file and run the following:

emcc hello.c -o hello.html

The -o flag tells emcc to generate a HTML file to run our code in.

Running Your Example

You need to run your example .html file through a web server, and cannot just open it on your local hard drive. To do this, follow the instructions on this page: https://developer.mozilla.org/en-US/docs/Learn/Common_questions/Tools_and_setup/set_up_a_local_testing_server

Once you have run your server, if everything works as expected you should see “Hello World” output in your browser’s console.

./WebAssembly_Graphics/wasm_example.png

Sources & Additional Reading

Emscripten: https://emscripten.org/ Emscripten Downloads and Instructions: https://emscripten.org/docs/getting_started/downloads.html WASM Example: https://developer.mozilla.org/en-US/docs/WebAssembly/C_to_wasm
WASM Project and Documentation: https://webassembly.org/