Friday, January 21, 2011

Using erlang_js

This post concerns installing and using erlang_js, together with some sample code to demonstrate how to run and extract data from SpiderMonkey using erlang_js. When installing erlang_js, you need a current version of Erlang. R14b04 (PPA) according to Sean Cribbs (#riak @ Freenode), this is the version I used, previous versions from 2009 do not seem to work. Initially, I used a mercurial repo to fetch the erlang_js code, but I was later informed that I should use the GitHub repo instead, perhaps this is more up to date. In order to execute javascript, we need (according to erlang_js docs):
  1. Make sure the sasl application is running.
  2. Start erlang_js.
  3. Create a Javascript VM via js_driver:new/0.
  4. Use the js module to execute Javascript.
For (1), this means we should supply -boot start_sasl to the erl commandline.(2) means we should add the path to the erlang_js binary to the erl path with -pa, and then execute erlang_js:start(). This amounts to: erl -boot start_sasl -pa ebin -eval 'erlang_js:start().' Now we are ready to run javascript! To run this code, start erlang with: erl -boot start_sasl -pa ebin, compile the example code with c() and simply run js_test:run(). Oh, by the way. I have not found the erlang_js docs online, you should build them with build_docs.sh in the erlang_js source directory.

1 comment: