Wednesday, October 5, 2011

From vala to js - with emscripten.

Stumbling over emscripten again. A fun tool to compile llvm bytecode to JavaScript.
After installing some basic dependencies via
$ sudo yum install vala clang js
and a bit of glue (see below) and you can quickly run code written in vala using a JavaScript interpreter. All this can be done with all code clang compiles, so there is no more magic than normal involved.

$ cd /tmp
$ git clone
$ wget
$ unzip

Now that all software is in place, do some configurations by editing ~/.emscripten:

$ cat <<EOF ~/.emscripten
# path where you cloned emscripten
# I needed to link EMSCRIPTEN_ROOT/src/tmp to this path e.g. 

#LLVM_GCC=... # we don't have that on fedora
COMPILER_OPTS = ['-m64'] # Change to your arch!

SPIDERMONKEY_ENGINE = [os.path.expanduser('/usr/bin/js'), '-m']
CLOSURE_COMPILER = os.path.expanduser('~/tmp/emscripten/closure-compiler.jar')


Basically now everything is in place. Using the following snippet
struct FooBar
	int m;
	public void wtf ()
		stdout.printf ("Wtf!\n"); // The \n is important.

void main()
	FooBar f = FooBar (); ();

... we can compile and execute the whoole thing using the following lines:

$ valac --profile=posix --ccode vaem.vala
$ clang -emit-llvm -S -o vaem.ll vaem.c
$ ./ va/vaem.ll -o vaem.js
$ js -m vaem.js

We need to use the posix profile, otherwise we need to compile the whole glib/gobject system using emscripten. Another way of creating JavaScript from vala code is using maja.

No comments:

Post a Comment