home about

You Got C++ In My JavaScript

May 24, 2011

Someone recently told me about Emscripten. I didn’t know whether to laugh or cry.

Either way, I couldn’t resist giving it a try. I dusted off a very simple Mandelbrot fractal viewer I hacked up a few years ago to see if Emscripten could make it run in a browser.

Did it work? Well, see for yourself.

It wasn’t easy, though. In fact, I had to hack up the source code so badly that only the actual math was left untouched. Everything else had to be completely rewritten or removed because it didn’t work for one reason or another. (For example, Emscripten emulates pointers as JavaScript array keys, not true offsets into contiguous data, which can produce … well, “interesting” results when you’re manipulating pixel buffers directly.)

And of course, the generated JavaScript also runs orders of magnitude slower than the native version.

As a hack, it’s pretty amusing. As a serious development tool, well … I just hope they’re not serious.