rewrite to gen_server and added tests

This commit is contained in:
Jeena Paradies 2011-02-17 20:05:12 +01:00
parent 47017d3afc
commit 44d26278cc

View file

@ -1,24 +1,34 @@
-module(ggs_gamevm).
-export([start_link/0, define/2, user_command/4]).
%% @doc This module is responsible for running the game VM:s. You can issue
%% commands to a vm using this module.
-module(ggs_gamevm).
-behaviour(gen_server).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
-record(state, { port, table } ).
%% API
-export([start_link/1, define/2, user_command/4, stop/1, call_js/2]).
-include_lib("eunit/include/eunit.hrl").
%% ----------------------------------------------------------------------
% API implementation
%% @doc Create a new VM process. The process ID is returned and can be used
%% with for example the define method of this module.
start_link() ->
start_link(Table) ->
erlang_js:start(), %% @TODO: should only be done once
PortPid = spawn_link( fun() ->
process_flag(trap_exit, true),
{ok, Port} = js_driver:new(),
js:define(Port, <<"function userCommand(user, command, args){return 'Hello world';}">>),
loop(Port)
end ),
PortPid.
{ok, Pid} = gen_server:start_link(?MODULE, [Table], []),
Pid.
%% @doc Define some new code on the specified VM, returns the atom ok.
define(GameVM, SourceCode) ->
GameVM ! {define,SourceCode},
ok.
gen_server:cast(GameVM, {define, SourceCode}).
%% @doc Execute a user command on the specified VM. This function is
%% asynchronous, and returns ok.
@ -28,23 +38,81 @@ define(GameVM, SourceCode) ->
%% Command = a game command to run
%% Args = arguments for the Command parameter
user_command(GameVM, Player, Command, Args) ->
Ref = make_ref(),
GameVM ! {user_command, Player, Command, Args, self(), Ref},
gen_server:cast(GameVM, {user_command, Player, Command, Args}).
%% @private
% only for tests
call_js(GameVM, SourceCode) ->
gen_server:call(GameVM, {eval, SourceCode}).
% @doc stops the gamevm process
stop(GameVM) ->
gen_server:cast(GameVM, stop).
%% ----------------------------------------------------------------------
%% @private
init([Table]) ->
process_flag(trap_exit, true),
{ok, Port} = js_driver:new(),
%% @TODO: add here default JS API instead
{ok, #state { port = Port, table = Table }}.
%% private
% only needed for the tests
handle_call({eval, SourceCode}, _From, #state { port = Port } = State) ->
{ok, Ret} = js:eval(Port, list_to_binary(SourceCode)),
{reply, Ret, State}.
%% @private
handle_cast({define, SourceCode}, #state { port = Port } = State) ->
ok = js:define(Port, list_to_binary(SourceCode)),
{noreply, State};
handle_cast({user_command, Player, Command, Args}, #state { port = Port } = State) ->
Arguments = string:sub(string:join([Player, Command, Args], ","), "'", "\'"),
Js = list_to_binary(string:concat(string:concat("userCommand(", Arguments), ");")),
js_driver:define_js(Port, Js),
{noreply, State};
handle_cast(stop, State) ->
{stop, normal, State};
handle_cast(Msg, S) ->
error_logger:error_report([unknown_msg, Msg]),
{noreply, S}.
%% @private
handle_info(Msg, S) ->
error_logger:error_report([unknown_msg, Msg]),
{noreply, S}.
%% @private
terminate(_Reason, _State) ->
ok.
%% Helper functions
%% @private
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
loop(Port) ->
receive
{define, SourceCode} ->
ok = js:define(Port, list_to_binary(SourceCode)),
loop(Port);
{user_command, User, Command, Args, From, Ref} ->
{ok, Ret} = js:call(Port, <<"userCommand">>,
[ list_to_binary(User),
list_to_binary(Command),
list_to_binary(Args)
]),
From ! {Ref, Ret},
loop(Port)
end.
%% ----------------------------------------------------------------------
% Tests
start_link_test() ->
erlang_js:start(), %% @TODO: should only be done once
GameVM = start_link(test_table),
?assertNot(GameVM =:= undefined).
define_test() ->
GameVM = start_link(test_table),
define(GameVM, "function hello(test) { return test; }"),
?assertMatch(<<"jeena">>, gen_server:call(GameVM, {eval, "hello('jeena')"})).
stop_test() ->
GameVM = start_link(test_table),
ok = stop(GameVM).
user_command_test() ->
GameVM = start_link(test_table),
define(GameVM, "var t = '';\nfunction userCommand(user, command, args) { t = user + command + args; }\n"),
user_command(GameVM, "'jeena'", "'thecommand'", "'theargs'"),
?assertMatch(<<"jeenathecommandtheargs">>, gen_server:call(GameVM, {eval, "t;"})).