About Emscripten

Emscripten is an Open Source LLVM to JavaScript compiler. Using Emscripten you can:

  • Compile C and C++ code into JavaScript
  • Compile any other code that can be translated into LLVM bitcode into JavaScript.
  • Compile the C/C++ runtimes of other languages into JavaScript, and then run code in those other languages in an indirect way (this has been done for Python and Lua)!

Tip

Emscripten makes native code immediately available on the Web: a platform that is standards-based, has numerous independent compatible implementations, and runs everywhere from PCs to iPads.

With Emscripten, C/C++ developers don’t have the high cost of porting code manually to JavaScript — or having to learn JavaScript at all. Web developers also benefit, as they can use the many thousands of pre-existing native utilities and libraries in their sites.

Practically any portable C or C++ codebase can be compiled into JavaScript using Emscripten, ranging from high performance games that need to render graphics, play sounds, and load and process files, through to application frameworks like Qt. Emscripten has already been used to convert a very long list of real-world codebases to JavaScript, including large projects like CPython, Poppler and the Bullet Physics Engine, as well as commercial projects like the Unreal Engine 4 and the Unity engine. Here are two demos using Unity:

Angrybots game logo
Dead Trigger 2 Game logo

For more demos, see the list on the wiki.

Emscripten generates fast code! Its default output format is asm.js , a highly optimizable subset of JavaScript that can execute at close to native speed in many cases (check out the current benchmark results or run the benchmark tests yourself). Optimized Emscripten code has also been shown to have a similar effective size to native code, when both are gzipped.

For a better understanding of just how fast and fluid Emscripten-ported code can be, check out the Dead Trigger 2 and Angrybots demos above.

Emscripten Toolchain

A high level view of the Emscripten toolchain is given below. The main tool is the Emscripten Compiler Frontend (emcc). This is a drop-in replacement for a standard compiler like gcc.

../../_images/EmscriptenToolchain.png

Emcc uses Clang to convert C/C++ files to LLVM bitcode, and Fastcomp (Emscripten’s Compiler Core — an LLVM backend) to compile the bitcode to JavaScript. The output JavaScript can be executed by node.js, or from within HTML in a browser.

The Emscripten SDK (emsdk) is used to manage multiple SDKs and tools, and to specify the particular SDK/set of tools currently being used to compile code (the Active Tool/SDK). It can even “install” (download and build) the latest toolchain from Github!

Emsdk writes the “active” configuration to the Emscripten Compiler Configuration File (.emscripten). This file is used by emcc to get the correct current toolchain for building.

A number of other tools are not shown — for example, Java can optionally be used by emcc to run the closure compiler, which can further decrease code size.

The whole toolchain is delivered in the Emscripten SDK, and can be used on Linux, Windows or Mac OS X.

Porting code to use Emscripten

Emscripten support for portable C/C++ code is fairly comprehensive. Support for the C standard library, C++ standard library, C++ exceptions, etc. is very good. SDL support is sufficient to run quite a lot of code. OpenGL support in Emscripten support is excellent for OpenGL ES 2.0-type code, and acceptable for other types.

There are differences between the native and Emscripten Runtime Environment, which mean some changes usually need to be made to the native code. That said, many applications will only need to change the way they define their main loop, and also modify their file handling to adapt to the limitations of the browser/JavaScript.

There are also limitations that can make some code easier to port — read Portability Guidelines to determine where you may need to spend more effort.