The New Coolest Hack: emscripten - an LLVM bitcode to JavaScript Compiler
Shlomi Fish
shlomif at shlomifish.org
Sun Jan 6 12:59:43 IST 2013
Hi all,
[Note to Muli: prepare the groggers].
The executive summary is that you should probably check out
https://github.com/kripken/emscripten which is an LLVM bitcode to JavaScript
compiler, which allows one to compile any language that has a front-end to
compile to LLVM bitcode (including C, C++, Objective C, etc. via clang or
llvm-gcc) into JavaScript code that can run on the browser. It's great and
incredibly useful.
-----
Here's the longer story from my POV. As you probably now I am the creator and
maintainer of Freecell Solver ( http://fc-solve.shlomifish.org/ ), which is a C
library (-std=gnu99) and some command line programs to solve Freecell and
similar solitaire games. Now, as well as the source (which is open source under
the MIT/X11 licence), I provide some Win32 binaries, and there are also some
GUI frontends that can make use of it to display the cards and moved in an
animated manner (see for instance
http://www.youtube.com/watch?v=hYdqNuX4WJc ). The Freecell Solver download page
contains a huge note with a stop sign at the top saying that you probably want
one of these interfaces, and the Win32 binary also emits a message to STDERR to
avoid people asking about an empty dos box which does nothing (but in fact
waits for standard input.).
The problem is that for some users it was not enough, and they asked me
at least two times by E-mail to provide them with a solution to a Freecell
layout with which they had difficulties. I complied with this request, but
having read http://www.joelonsoftware.com/articles/customerservice.html , I
knew that I should also find a way to fix such questions permanently, and the
natural way was to provide a web-interface for the solver.
So how to do that? One way would have been to write a server-side CGI (or
FastCGI or whatever) program that runs on the server and emits solution.
However, keeping a CGI program online, updated and secure requires some effort,
especially when large parts of it are written in C, and quite a clever one and
very optimised to speed at that. So I've contemplated somehow creating a
version in JavaScript that will run entirely, or almost entirely, on the
client-side. But how?
I thought that maybe somehow have written a C->JavaScript compiler by now and
so searched the web for it and the first hit was indeed
https://github.com/kripken/emscripten . I cloned the repository and tried out
several demos and they worked nicely. However, I then ran into this bug -
https://github.com/kripken/emscripten/issues/766 - and reported it. The
maintainer tried to help, but he was unable to follow my instructions at
first, so I had to fix them and make my code's build system more
idiot-proof. Eventually after he was able to try everything, it worked there,
and then it worked for me after I did "git pull" to update the code, and so the
bug was fixed independently of my bug report. So I was able to proceed.
Since I'm a lazy bottom-up learner, who does not always take the time to read
all the documentation right at first, it took me some time and trial and error
to get everything right, and in the process discovered what seems to be another
bug - https://github.com/kripken/emscripten/issues/774 (which was not fixed
yet) - but I eventually got the code to work.
The generated JavaScript is large:
* 2,326,040 bytes originally.
* 1,271,024 after minification by jsmin (though there may be better minifiers)
* 250,897 after being minified and compressed with gzip -9 (I could not
serve it this way on HostGator, but it may be possible).
This is in comparison to libfreecell-solver.so.0.5.0 which is 376,122 bytes
with debugging symbols and 163,976 bytes after being stripped (and it's an
x86-64 system and I didn't use -Os).
Nevertheless, it was easier than writing an entirely new code in JavaScript and
maintaining it, and you can see the beta demo in its temporary location here:
* http://www.shlomifish.org/Files/files/code/fc-solve/js-port/web_fc_solve.html
Emscripten has many more, much cooler, demos here:
https://github.com/kripken/emscripten/wiki including ports of Python, Ruby and
Lua and some graphical games.
You can also read this announcement about the fc-solve demo on fc-solve-discuss:
http://tech.groups.yahoo.com/group/fc-solve-discuss/message/1331
----------
So if you're looking to convert C, C++, etc. code to JavaScript so people can
run it in the convenience of their browsers, then look no further than
Emscripten. Another compiler with a JS backend is
https://github.com/fglock/Perlito which compiles a subset of Perl 5 and Perl 6
to JavaScript (and to themselves), but is still work under progress. Here is the
Perl 5 compiler demo - http://perlcabal.org/~fglock/perlito5.html .
You can use this makefile as a reference or skeleton for using emscripten:
https://bitbucket.org/shlomif/fc-solve/src/42ee84534876f728edd30b845c8799440a25af88/fc-solve/source/Makefile.to-javascript.mak?at=master
(short URL - http://is.gd/JmPCKe ).
-----
Regards,
Shlomi Fish
--
-----------------------------------------------------------------
Shlomi Fish http://www.shlomifish.org/
My Public Domain Photos - http://www.flickr.com/photos/shlomif/
If the mountain will not come to Muhammad, then Muhammad will go to the
mountain. If the mountain will not come to Chuck Norris, then the mountain
will suffer Norris’s wrath for not complying with his whims.
Please reply to list if it's a mailing list post - http://shlom.in/reply .
More information about the Linux-il
mailing list