Fixed ggs_vm_runner
This commit is contained in:
parent
1e08fe0015
commit
bf98e1fee2
2 changed files with 30 additions and 14 deletions
|
@ -104,8 +104,8 @@ handle_cast({define, Token, SourceCode}, State) ->
|
||||||
% Handle javascript calls
|
% Handle javascript calls
|
||||||
handle_cast({call, Token, Command}, State) ->
|
handle_cast({call, Token, Command}, State) ->
|
||||||
GameVM = getJSVM(Token, State),
|
GameVM = getJSVM(Token, State),
|
||||||
ggs_vm_runner:user_command(GameVM, "User", Command, []),
|
Ret = ggs_vm_runner:user_command(GameVM, "User", Command, []),
|
||||||
%send(State#state.lsock, Token, "JS says:", binary_to_list(Ret)), Unessecary
|
send(State#state.lsock, Token, "JS says:", binary_to_list(Ret)),
|
||||||
{noreply, State};
|
{noreply, State};
|
||||||
|
|
||||||
% Set the new state to the reference generated, and JSVM associated
|
% Set the new state to the reference generated, and JSVM associated
|
||||||
|
|
|
@ -4,25 +4,41 @@
|
||||||
%Mattias
|
%Mattias
|
||||||
start_link() ->
|
start_link() ->
|
||||||
erlang_js:start(),
|
erlang_js:start(),
|
||||||
{ok, Port} = js_driver:new(),
|
PortPid = spawn_link( fun() ->
|
||||||
js:define(Port, <<"function userCommand(user, command, args){}">>),
|
process_flag(trap_exit, true),
|
||||||
PortPid = spawn_link(fun() -> loop(Port) end ),
|
{ok, Port} = js_driver:new(),
|
||||||
|
js:define(Port, <<"function userCommand(user, command, args){return 'Hello world';}">>),
|
||||||
|
loop(Port)
|
||||||
|
end ),
|
||||||
PortPid.
|
PortPid.
|
||||||
|
|
||||||
|
|
||||||
loop(Port) ->
|
loop(Port) ->
|
||||||
receive
|
io:format("I am PID"),
|
||||||
{define, SourceCode} ->
|
erlang:display(self()),
|
||||||
ok = js:define(Port, list_to_binary(SourceCode)),
|
receive
|
||||||
loop(Port);
|
{define, SourceCode} ->
|
||||||
{user_command, User, Command, Args} ->
|
ok = js:define(Port, list_to_binary(SourceCode)),
|
||||||
{ok, Ret} = js:call(Port, <<"userCommand">>, list_to_binary([User,Command,Args])),
|
loop(Port);
|
||||||
loop(Port)
|
{user_command, User, Command, Args, From, Ref} ->
|
||||||
end.
|
{ok, Ret} = js:call(Port, <<"userCommand">>,
|
||||||
|
[ list_to_binary(User),
|
||||||
|
list_to_binary(Command),
|
||||||
|
list_to_binary(Args)
|
||||||
|
]),
|
||||||
|
From ! {Ref, Ret},
|
||||||
|
loop(Port)
|
||||||
|
end.
|
||||||
|
|
||||||
|
|
||||||
define(GameVM, SourceCode) ->
|
define(GameVM, SourceCode) ->
|
||||||
GameVM ! {define,SourceCode}.
|
GameVM ! {define,SourceCode}.
|
||||||
|
|
||||||
user_command(GameVM, User, Command, Args) ->
|
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.
|
||||||
|
|
Reference in a new issue