WebAssembly is a binary instruction format for a stack-based virtual machine and usually abbreviated as WASM and it is designed as a portable target for compilation of high-level languages like C/C++/Rust, enabling deployment on the web for client and server applications.
Why do we need WebAssembly?
isn’t capable of delivering near-native performance.
WebAssembly is a low-level, assembly-like language with a compact binary format. This makes it possible for WebAssembly to deliver
With more and more cloud native, mobile-first development going on, near-native performance has become crucial for some applications to function in a way that is viable to consumers.
Here are some benefits which shows why we need WebAssembly
WebAssembly is efficient and fast
Wasm bytecode is designed to be encoded in a size and load-time-efficient binary format. WebAssembly aims to execute at native speed by taking advantage of common hardware capabilities available on a wide range of platforms.
WebAssembly is safe
WebAssembly is open and debuggable
WebAssembly is designed to be pretty-printed in a textual format for debugging, testing, experimenting, optimizing, learning, teaching, and writing programs by hand. The textual format will be used when viewing the source of wasm modules on the web.
WebAssembly is part of the open web platform
Prerequisites of WebAssembly
How Web Assembly Works ?
an Abstract Syntax Tree.
Then the engine interpreter takes the AST and turns it into Bytecode and finally the engine compiler takes the Bytecode and turns it into a code a machine can run on its processor.
How WASM Works
The reason WASM is faster is because WASM code goes directly to the compiler, effectively skipping a few steps such as parsing and the transformation to Intermediate Representation as seen in flow diagram.
The reason why
In contrast, statically-typed languages such as C/C++ require to declare the types in advance, therefore types are known and are checked at compile time.
So the way WASM works is:
- We write code with its types, usually in a statically typed-language such as C/C++, or Rust.
- Then we generate a pre-compiled WASM module.
- Then we can run this code straight by the engine compiler, skipping the parsing and the transformation to Intermediate Representation.
To compile statically-typed language such as C/C++ we need to install Emscripten SDK which is a Low level virtual machine.
The steps to install Emscripten are as such:
- Clone the repository using command
git clone https://github.com/emscripten-core/emsdk.git
- Navigate to the
- make sure clones repository is updated with remote one:
- Install the latest version of Emscripten
./emsdk install latest
- Activate the latest version
./emsdk activate latest
- Set the various environmental variables
- Check Installation
- To Compile C/C++ programs
emcc hello.c -s WASM=1 -o hello.html
em++ hello.cpp -s WASM=1 -o hello.html
- For only wasm file
emcc hello.c -s STANDALONE_WASM
em++ hello.cpp -s STANDALONE_WASM
Serving compiled code
There are many way to serve custom or default generated
html page using Emscripten
emrunis a command line tool inbuild in Emscripten used to launch Emscripten-generated HTML page in a web browser from the command line.
- Using Node server Start a server using node server
- Starting own server
- Starting an Apache server
- Starting a simple server via command line utility provided in
python -m http.server- etc.
Next we will see some example of webassembly for browsers
Latest comments (0)