erlang_js: A Friendly Erlang to Javascript Binding

Version: 0.1

Authors: Kevin Smith (ksmith@basho.com).

Description

erlang_js aims to be a simple and easy to use binding between Erlang and Javascript. erlang_js is packaged as an OTP application so it's easy to integrate.

Quick N' Dirty

  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.

Examples

Defining and calling a function

{ok, Port} = js_driver:new().
ok = js:define(Port, <<"function my_add(x, y) { return x + y; }">>).
js:call(Port, <<"my_add">>, [100, 50]).
{ok, 150}

Using a user-defined initializer

InitFun = fun(Port) -> js:define(Port, <<"function my_square(x) { return x * x; }">>), ok end.
{ok, Port} = js_driver:new(InitFun).
js:call(Port, <<"my_square">>, [3]).
{ok,9}

Using bindings

InitFun = fun(Port) -> js:define(Port, <<"var my_constant = 100; function constant_mult(x) { return x * my_constant; };">>), ok end.
{ok, Port} = js_driver:new(InitFun).
js:call(Port, <<"constant_mult">>, [5]).
{ok, 500}
js:call(Port, <<"constant_mult">>, [5], [{<<"my_constant">>, 200}]).
{ok, 1000}

Using ejsLog to debug

JS = <<"function my_add(x, y) { ejsLog(\"/tmp/foo.txt\", \"Hello, world!\"); return x + y; }">>.
ok = js:define(Port, JS).
%% Logging output is automatically timestamped and written to /tmp/foo.txt
%% when the function runs
{ok, 84} = js:call(Port, <<"my_add">>, [42, 42]).

Generated by EDoc, Mar 10 2019, 17:17:23.