Generating asm.js using Emscripten
We will use Emscripten to port C/C++ programs into asm.js or the WebAssembly binary and then run them inside the JavaScript engine.
Note
Programming languages such as Lua and Python have a C/C++ runtime. With Emscripten, we can port the runtime as a WebAssembly module and execute them inside the JavaScript engine. This makes it easy to run Lua/Python code on the JavaScript engine. Thus, Emscripten and WebAssembly allow the running of native code in the JavaScript engine.
First, let's create a sum.cpp
file:
// sum.cpp extern "C" { unsigned sum(unsigned a, unsigned b) { return a + b; } }
Consider extern "C"
as something like an export mechanism. All the functions inside are available as an exported function without any changes to their name. Then, we define the normal sum
function that takes in two numbers and returns a number.
In order to generate...