Blog

Home / Blog

WebAssembly (WASM) and 12 language compilers riding on WASM

William Tsu
Data Analyst
Experienced data analyst working with data visualization, cloud computing and ETL solutions.
September 15, 2021


WebAssembly

WebAssembly or WASM, a binary instruction format for a stack-based virtual machine, is designed as a portable target for the compilation of high-level languages such as C#/C++. It enables deployment on the web for both client and server applications. The WASM is a set of primitive instructions, which gets compiled into executable code running on a platform-agnostic and stack-based virtual machine. As per webassembly.org, WebAssembly or WASM is a portable, size, and load-time-efficient format suitable for compilation to the web.

WebAssembly

The design goals of the WASM are the following as discussed below:
Fast – Can execute with near-native code performance taking advantage of various capabilities provided by the contemporary software.
Safe – The code gets validated & executed in a memory-safe sandbox environment, which prevents security breaches.
Well-Defined – Defines valid programs and their behavior in an easy manner.
Hardware-Independent – Gets compiled on all modern architecture, mobile devices, and embedded systems alike.
Language-Independent – No preferences for any particular coding language.
Platform-Independent – Gets embedded in browsers, runs as a stand-alone Virtual Machine, or gets integrated into other compatible environments.
Open – With WASM, programs get to interact with their environment simply and universally.

Along with these design goals, WASM is efficient and portable. It is compact, modular, efficient, streamable, parallelizable, and portable. WASM codes are easy to inspect and debug in environments like web browsers. At its very core, WebAssembly is Virtual Instruction Set Architecture (Virtual ISA) that gets embedded in many different environments.

To encompass the variety of WASM and enabling maximum reuse, the WebAssembly specification gets split into various documents. The document is concerned with the core ISA layer of WebAssembly. It defines the instruction set, binary encoding, validation, and execution semantics along with a textual representation.

The WASM does not allow ambient access to the computing environment in which the code gets executed. Any interaction with the environment is made possible via invoking functions provided by the embedder and imported into a WASM Module. An embedder establishes security policies suitable for a respective environment by controlling or limiting which functional capabilities it makes available for import.

The superlative features of WebAssembly are as follows:
Performance: near-native speed: Developers can run a code at a near-native speed that supports many other languages. It becomes possible to implement performance-critical stuff in various modern languages such as C++, Rust, and Golang, which get compiled to WASM. In general, WebAssembly is faster than JavaScript.

WASM does not need browser plug-ins as a majority of mainstream browsers get implemented with WASM Standard.

WebAssembly works perfectly with JavaScript. WASM manipulates a DOM object and events in a similar way to that of JavaScript. The JavaScript triggers the execution of an apt WASM Code just like restful API and RPC calls.

WASM helps with porting massive libraries of existing C and C++ apps to the web.

The WebAssembly Code defines an AST (Abstract Syntax Tree) represented in a binary format.

WebAssembly Runtime: Enhances performance and supports smart contract development across an increasing number of languages including C++, and Rust.

WebAssembly (WASM) ameliorates smart contract programming in Solidity with good support from EVM (Ethereum Virtual Machine). Hence, the programmers get to use whatever language they prefer such as Swift, Go, Rust, Ruby, C++, and OCaml for developing smart contracts. Herein, the programming language selected should comply with the WASM format, which is supported by different platforms. These include various browsers like Google Chrome, Mozilla Firefox, and different Operating Systems such as Microsoft Windows, and Linux. The WASM format is also supported by CPUs like x86 or ARM.

12 language compilers riding on WASM

With a brief snippet about the definition and features of WebAssembly or WASM, let us discuss 12 hot language projects that ride WebAssembly. The WASM gives a promise of quick & responsive net functions as fast as the native desktop functions. Without further ado, let us discuss the 12 language initiatives that showcase the huge potential of WebAssembly.

Binaryen : A compiler toolchain infrastructure library for WASM, written in C++. One can compile the WebAssembly Easy, Fast, and Effective. Easy – Binaryen has a simple C API in a single header. It easily accepts WASM-like form along with a general control flow graph for those compilers, which prefer that. Fast – The internal IR of Binaryen uses compact data structures. It is designed for parallel code generation and its optimization using all the available CPU Cores. The IR also compiles down to WASM extremely easily. Effective – The optimizer of Binaryen has many passes, which improves code size and speed. One specific area of focus is about WebAssembly specific optimizations. The WASM minification is similar to that of JavaScript, and CSS, which are language-specific. This Binaryen is easily obtainable from GitHub.

Blazor WebAssembly: A novel User Interface (UI) Technology from Microsoft. It got officially released with .NET Core 3.1. This language allows developers or programmers to create single-page applications (SPAs) using C# and .NET by utilizing a component-based architecture. Blazor WebAssembly is a client-side in-browser implementation of Blazor. It includes a .NET runtime implemented WASM. This Blazor WebAssembly is easily obtained from the website of Microsoft.

Cheerp: An enterprise C/C++ compiler for the web. It compiles C/C++ into WebAssembly and JavaScript. Cheerp is Open-Source with a dual licence. The enterprise-grade C/C++ compiler for the web is Performance-Oriented, and Production-Ready with First-Class Interoperability. It is facilitated with Enhanced Debug options along with designing it as Developer-Friendly. Its unique features include JavaScript Interoperability that exports C++ with [[cheerp::jsexport]], combines memory models, and maintains one codebase.

CheerpJ: The only solution for converting and migrating existing Java Client Applications to HTML 5. It includes Swing Clients, Java Applets, and Java Libraries. It allows organizations to extend the life of Java Applications. Thus, these applications become usable on modern browsers with no change to source codes. CheerpJ does not require server-side support as all application components are static. This LLVM (Low-Level Virtual Machine) based compiler converts Java shopper software into WASM, JavaScript, and HTML. CheerpJ works in three ways including an Ahead-Of-Time (AOT) Compilation, a highly optimized CheerpJ Runtime, and an On-line CheerpJ Java Compiler.

Emscripten: A complete Open-Source compiler toolchain to WebAssembly, it compiles C and C++ codes along with any other language used by LLVM (Low-Level Virtual Machine) into WASM. It runs these codes on the Web, Node.js, or other WASM Runtimes. Emscripten can generate small and fast code. Its default output format is WebAssembly, a highly optimized executable format. It runs almost as fast as native code and is safely portable. Emscripten does a lot of optimization work automatically. It does this by careful integration with LLVM, Binaryen, Closure Compiler, and other tools.

Forest-Compiler: A multi-syntax functional programming language that compiles into WebAssembly or WASM. Design principle: ease of collaboration. Features: Statically typed, pattern matching, immutable data-structures, multiple syntaxes, and automatic code formatting.

Grain: : The compiler compiles directly to WASM with its speed & efficiency. It has no runtime type errors and has roots in functional programming. It is flexible enough to accommodate different programming styles for various applications.

JWebAssembly: It is also referred to as Java Bytecode to WebAssembly Compiler. It uses Java class files as input to compile Java Bytecodes like Clojure, Groovy, JRuby, Jython, Kotlin, and Scala. As an output, it generates a binary format, .wasm file. It runs Java natively in the browser with WebAssembly.

Pyodide: It is Python with the scientific stack compiled to WebAssembly. Pyodide brings the Python 3.9 runtime to the browser via WebAssembly. Also, it provides a transparent conversion of objects between JavaScript and Python.

TeaVM: This ahead-of-time compiler for Java Bytecode emits WASM & JavaScript that runs in a browser. The TeaVM does not require a source code and need only compiled Class Files. The source code need not be Java and it successfully compiles both Kotlin and Scala. TeaVM is easy to use with an inclusive powerful web framework. It is quite efficient as well and utilizes less time than others while compiling.

Uno Platform: The first and only User Interface (UI) platform for single-codebase applications for WebAssembly, Windows, iOS, macOS, Android, and Linux. It designs pixel-perfect multi-platform applications with C# and WinUI. It works with all operating systems and browsers. UNO platform leverages the Mono-WASM runtime in .NET 5 to run C# Code in the entire main net browsers. It serves as a bridge for WinUI and UWP apps to run natively on WASM.

WASMCloud: It is a platform for writing portable business logic that runs anywhere from the edge to the cloud. It is secure-by-default through its boilerplate-free development experience via a rapid feedback loop. It is used for app development and platform building. WASMCloud helps developers build, test, and scale along with deploying and operating microservices at scale quickly.

Conclusion

Herein, we discussed WebAssembly and 12 hot-language projects riding on WASM. The WASM helps in simplifying the job of developers working to update all browsers, Operating Systems, and CPUs as discussed here. Feel free to read through before trying out any of these language compilers.