1. Cheerp

Cheerp is a fork of LLVM/Clang that allows compilation of arbitrary C++ code to the Web. It’s an open-source project, decently documented.

Powerful diagram, by me :)

It’s a command-line tool that works basically as a replacement for clang (it’s even invoked like /path/to/cheerp/directory/bin/clang++), accepting most of the clang options.

It will then produce a JavaScript file that will instantiate a Wasm module, and they will go hand in hand, with the JavaScript side interacting with browser APIs, inputs and outputs and the Wasm side churning whatever computation is required. (Other compilation modes like pure JS are available, but we concentrate on the main one)

Most small examples will compile out of the box, and the magic will work just so. On bigger codebases, it may or may not work with a single line, since the build system will need to be adapted and there are generally more chances of problems arising. Custom warnings and errors are well described and will guide in the right direction to a smooth solution.

Bonus part: memory model

Memory for Wasm is basically a big chunk of memory addressed via an integer, and this maps quite naturally with C++ memory management, but how things work on the JavaScript side? You may work with the “big chunk of memory” model, but how could you then interact with browser API’s or other JavaScript code? How would you know whether to keep the memory alive or free it? Either you restrict a lot what the possible interactions with other libraries are or you could rely on the compiler to instantiate C++ objects as regular JavaScript objects. This creates a lot of other possible problems, to name one JavaScript does not have the concept of pointers, so no pointer arithmetic/pointer ordering will be possible. But this and other problems are all solved by the compiler (and before that, by us 😺). So the Cheerp compiler will internally tag each class/struct as either living in one model or in the other. “JavaScript-like” objects than make for 0-overhead interoperability while “big chunk of memory” objects could be generally more performant.

You can now follow the wiki instruction and get started with experimenting, or I can show two demos:

A serverless multiplayer retro game where you can jump and shoot around.

Here it starts with a complex C++ codebase that requires audio, video, communication with other peers, performance tuning and many other problems.

The result is a fun gaming experience, on the browser, no installation required. And you can jump around and shoot at total strangers.

A fast BigInt in an evening

An article (link) of mine, where I show how to port a complex C library to pure JavaScript in a few hours of work. Since it’s just pure JavaScript, performances could be directly evaluated on your browser of choice here: benchmark me.

This should show that there is a lot of attention to the performances of the resulting programs, and we aim to be close to native speed.

This article also doubles down as a tutorial of a sort, and substituting BigInt with whatever library you may need should be a fun way to start playing with Cheerp.