forked from basho/erlang_js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
overview.edoc
52 lines (49 loc) · 1.77 KB
/
overview.edoc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
@author Kevin Smith ([email protected])
@version 0.1
@title erlang_js: A Friendly Erlang to Javascript Binding
@doc
<p>
<h3>Description</h3>
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.
</p>
<p>
<h3>Quick N' Dirty</h3>
<ol>
<li>Make sure the <code>sasl</code> application is running.</li>
<li>Start <code>erlang_js</code>.</li>
<li>Create a Javascript VM via <code>js_driver:new/0</code>.</li>
<li>Use the <code>js</code> module to execute Javascript.</li>
</ol>
</p>
<h3>Examples</h3>
<h4>Defining and calling a function</h4>
<pre>
{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}
</pre>
<h4>Using a user-defined initializer</h4>
<pre>
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}
</pre>
<h4>Using bindings</h4>
<pre>
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}
</pre>
<h4>Using ejsLog to debug</h4>
<pre>
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]).
</pre>