Fixed ggs_vm_runner

This commit is contained in:
Jonatan Pålsson 2011-02-14 19:32:46 +01:00
parent 1e08fe0015
commit bf98e1fee2
2 changed files with 30 additions and 14 deletions

View file

@ -104,8 +104,8 @@ handle_cast({define, Token, SourceCode}, State) ->
% Handle javascript calls
handle_cast({call, Token, Command}, State) ->
GameVM = getJSVM(Token, State),
ggs_vm_runner:user_command(GameVM, "User", Command, []),
%send(State#state.lsock, Token, "JS says:", binary_to_list(Ret)), Unessecary
Ret = ggs_vm_runner:user_command(GameVM, "User", Command, []),
send(State#state.lsock, Token, "JS says:", binary_to_list(Ret)),
{noreply, State};
% Set the new state to the reference generated, and JSVM associated

View file

@ -4,25 +4,41 @@
%Mattias
start_link() ->
erlang_js:start(),
PortPid = spawn_link( fun() ->
process_flag(trap_exit, true),
{ok, Port} = js_driver:new(),
js:define(Port, <<"function userCommand(user, command, args){}">>),
PortPid = spawn_link(fun() -> loop(Port) end ),
js:define(Port, <<"function userCommand(user, command, args){return 'Hello world';}">>),
loop(Port)
end ),
PortPid.
loop(Port) ->
receive
io:format("I am PID"),
erlang:display(self()),
receive
{define, SourceCode} ->
ok = js:define(Port, list_to_binary(SourceCode)),
loop(Port);
{user_command, User, Command, Args} ->
{ok, Ret} = js:call(Port, <<"userCommand">>, list_to_binary([User,Command,Args])),
{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.
end.
define(GameVM, SourceCode) ->
GameVM ! {define,SourceCode}.
user_command(GameVM, User, Command, Args) ->
GameVM ! {user_command, User, Command, Args}.
Ref = make_ref(),
GameVM ! {user_command, User, Command, Args, self(), Ref},
receive
{Ref, RetVal} ->
RetVal;
Other -> Other
end.