Carlo Piovesan
Oct 4 · 5 min read

During the last few weeks, I had a few occasions in which I had to explain what I do at work.

Explaining how lucky I feel like and how rewarding it is to work on interesting problems is easy.

xkcd #301, obvious reference

Telling that I spend most of the time at the coffee machine waiting either for recompilations of our codebase or for test results is somehow accurate but a bit too micro. I do also some work on optimizing our compiler results, data-structures, working on the testing infrastructure, or writing documentation.

But what do we do as a company?


There is an overlap between this and a business pitch, clients are always welcome, but the focus is on convincing you that it’s fun and rewarding spending time toying with our technology. And it may be even useful.


And what we do: a compiler, so a tool to translate from one programming language into another. In our case, the input is either C/C++/Java/.jar files. And the output is a mixture of JavaScript and WebAssembly.


Lots of nice buzzwords, but what does that mean / what can I show?

  • Cheerp: a compiler from C/C++ codebases to JS and/or Wasm
  • CheerpJ: a compiler form Java to JavaScript
  • our new project, CheerpX 🚀
  • few selected articles
  • a retro multiplayer shooter game

With some hand waving, it’s generally fine to use our products for private use or for an open-source project. That should cover most non-profit usage of the product. And you can always double-check with us, in the end, you will find how to get in touch.


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:

Teeworlds

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.


2. CheerpJ

CheerpJ is a compiler from Java to JavaScript.

There are 3 ways to experiment with it:

Java Fiddle

An online Java editor, that allows you to compile and run arbitrary Java code directly in your browser.

The site is in need of a redesign (want to help?) but should be intuitive enough. You basically code any Java on the left, click “compile and run” and to the right, you will see the console output + any visual element you may have drawn.

CheerpJ Applet Runner

A Chrome extension to run any Java applet.

This is mainly thought for users of websites where there are legacy java applets, and it’s a safe alternative to messing with the security options of your browser.

You just add the extension and whatever content present on the sites you browse will properly execute.

Here an article on how it solves a problem for a high-school professor: Java Applet archaeology.

Installing CheerpJSlides of the talks are not up yet, but should hopefully be up soon here: https://2019.viewsourceconf.org/#.

To next year, ViewSource!

CheerpJ is also a command-line tool: it takes a .jar file, and produce an equivalent JavaScript file. Check out the documentation for getting started!


3. CheerpX

SpaceX is cool, so why not CheerpX?

It’s unclear whether the name of the technology is temporary or not, but you can read more of what surprises are we working on directly on our CTO Alessandro’s article:


4. Articles

To keep all these technologies running better and better, there is a lot of interesting work done behind the scenes. Sometimes we then find time and energy to write small articles about what we do at work.

Here are two great ones by Yuri our Compiler Engineer:


Contact info

If you have any doubt, there are two chats for developers at https://gitter.im/leaningtech/cheerp or https://gitter.im/leaningtech/cheerpj, we are friendly and sort of responsive for any query, bug report, or just for pointing you in the right direction.

Or on the website: contact us page.

leaningtech

Leaning Technologies' Blog - everything Cheerp, CheerpJ, compile-to-JavaScript and compile-to-WebAssembly

Carlo Piovesan

Written by

at Leaning Technologies

leaningtech

Leaning Technologies' Blog - everything Cheerp, CheerpJ, compile-to-JavaScript and compile-to-WebAssembly

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade