Source

v8erl / erl / v8.erl

-module(v8).
-export([load/0,
         context/0,
         script/2]).
-define(LIB, "v8erl").

load() ->
    case whereis(v8erl) of
        undefined ->
            case erl_ddll:load_driver("lib", ?LIB) of
                ok ->
                    ok;
                {error, already_loaded} ->
                    ok;
                {error, Err} ->
		    {error, erl_ddll:format_error(Err)}
            end;
        _ ->
            ok
    end.


context() ->
    load(),
    open_port({spawn, ?LIB}, []).


script(Context, Script) when is_port(Context) andalso 
                             is_binary(Script) ->
    Context ! Script,
    receive
	{Context, {data, Data}} ->
	    Data;
	{Context, closed} ->
	    closed
    end.