Skip to content

erlang-mozjs/erlang-mozjs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

erlang-mozjs

Overview

Travis-CI :: https://secure.travis-ci.org/erlang-mozjs/erlang-mozjs.png

erlang-mozjs is a NIF library, and API that embeds Mozilla’s Spidermonkey Javascript Virtual Machine in Erlang. Originally created to facilitate usage of Riak’s MapReduce by non-Erlang programmers, it supports multiple concurrent Javascript VMs, runtime evaluation of Javascript code, and invocation of Javascript functions.

erlang-mozjs builds and executes only on Unix-based platforms, including Linux, Mac OS/X, and Solaris. It requires Spidermonkey 52 and includes Douglas Crockford’s json2.js JSON parser. More information about the features available to Javascript inside erlang-mozjs are detailed on the Mozilla Developer Center.

Quick Start

You must have Erlang/OTP 18 or later and a GNU-style make system to compile and run erlang-mozjs.

git clone git://github.com/erlang-mozjs/erlang-mozjs.git
make all test

Start up an Erlang shell with the path to erlang-mozjs included.

erl -pa path/to/erlang-mozjs/ebin -boot start_sasl

Create a Javascript VM.

1> {ok, JS} = js_driver:new().
{ok,#Ref<0.557298074.1409679362.105432>}

Now you can evaluate Javascript expressions and call Javascript functions.

2> js:define(JS, <<"var addOne = function(a){ return a + 1; }">>).
ok
3> js:call(JS, <<"addOne">>, [3]).
{ok,4}

Release JS resource when no longer needed.

4> js_driver:destroy(JS).
ok

Documentation

Documentation about the API is extensive and can be generated by edoc.

$ make docs
$ open docs/index.html

Contributing

We encourage contributions to erlang-mozjs from the community.

  1. Fork the erlang-mozjs repository on Github.
  2. Clone your fork or add the remote if you already have a clone of the repository.
git clone git@github.com:yourusername/erlang-mozjs.git
# or
git remote add mine git@github.com:yourusername/erlang-mozjs.git
  1. Create a topic branch for your change.
git checkout -b some-topic-branch
  1. Make your change and commit. Use a clear and descriptive commit message, spanning multiple lines if detailed explanation is needed.
  2. Push to your fork of the repository and then send a pull-request through Github.
git push mine some-topic-branch
  1. A community maintainer will review your patch and merge it into the main repository or send you feedback.