Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learn WebAssembly

You're reading from   Learn WebAssembly Build web applications with native performance using Wasm and C/C++

Arrow left icon
Product type Paperback
Published in Sep 2018
Publisher Packt
ISBN-13 9781788997379
Length 328 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Mike Rourke Mike Rourke
Author Profile Icon Mike Rourke
Mike Rourke
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Preface 1. What is WebAssembly? 2. Elements of WebAssembly - Wat, Wasm, and the JavaScript API FREE CHAPTER 3. Setting Up a Development Environment 4. Installing the Required Dependencies 5. Creating and Loading a WebAssembly Module 6. Interacting with JavaScript and Debugging 7. Creating an Application from Scratch 8. Porting a Game with Emscripten 9. Integrating with Node.js 10. Advanced Tools and Upcoming Features 11. Other Books You May Enjoy

What languages are supported?

WebAssembly's high-level goals for their MVP was to provide roughly the same functionality as asm.js. The two technologies are very closely related. C, C++, and Rust are very popular languages that support manual memory allocation, which made them ideal candidates for the initial implementation. In this section, we're going to provide a brief overview of each programming language.

C and C++

C and C++ are low-level programming languages that have been around for over 30 years. C is procedural and doesn't inherently support object-oriented programming concepts such as classes and inheritance, but it's fast, portable, and widely used. 

C++ was built to fill the gaps in C by adding features such as operator overloading and improved type checking. Both languages consistently rank in the top 10 most popular programming languages, which make them ideally suited for the MVP:

TIOBE Very Long Term History of the top 10 programming languages

C and C++ support is also baked into Emscripten, so in addition to simplifying the compilation process, it allows you to take advantage of WebAssembly's full capabilities. It is also possible to compile C/C++ code down to a .wasm file using LLVM. LLVM is a collection of modular and reusable compiler and toolchain technologies. In a nutshell, it's a framework that simplifies the configuration of a compilation process from source code to machine code. If you made your own programming language and would like to build a compiler, LLVM has tools to simplify the process. I'll cover how to compile C/C++ into .wasm files using LLVM in Chapter 10, Advanced Tools and Upcoming Features.

The following snippet demonstrates how to print Hello World! to the console using C++:

#include <iostream>

int main() {
std::cout << "Hello, World!\n";
return 0;
}

Rust

C and C++ were intended to be the primary languages used for WebAssembly, but Rust is a perfectly suitable substitute. Rust is a systems programming language that is syntactically similar to C++. It was designed with memory safety in mind, but still retains the performance advantages of C and C++. The current nightly build of Rust's compiler can generate .wasm files from Rust source code, so if you prefer Rust and are familiar with C++, you should be able to use Rust for most of the examples in this book.

The following snippet demonstrates how to print Hello World! to the console using Rust:

fn main() {
println!("Hello World!");
}

Other languages

Various tooling exists to enable the use of WebAssembly with some of the other popular programming languages, although they are mostly experimental:

  • C# via Blazor
  • Haxe via WebIDL
  • Java via TeaVM or Bytecoder
  • Kotlin via TeaVM
  • TypeScript via AssemblyScript

It is also technically possible to transpile a language to C and consequently compile that to a Wasm module, but the success of compilation is contingent on the output of the transpiler. More than likely, you'd have to make significant changes to the code to get it to work.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image