Now we can define and run functions, but not in the same JSVM at the moment. A lookup is needed
This commit is contained in:
parent
f5fac06849
commit
4c6d0987bd
3 changed files with 49 additions and 23 deletions
|
@ -12,15 +12,27 @@ parse(Data) ->
|
|||
), Message),
|
||||
% Hacky way to build a tuple, filter out not_found later on
|
||||
Processed = {
|
||||
case lists:keysearch("Command", 1, MsgKV) of
|
||||
{value,{_, "define"}} ->
|
||||
define;
|
||||
{value,{_, "call"}} ->
|
||||
call;
|
||||
false ->
|
||||
not_found
|
||||
end,
|
||||
case lists:keysearch("Token", 1, MsgKV) of
|
||||
{value,{_, Value}} ->
|
||||
Value;
|
||||
false ->
|
||||
not_found
|
||||
end,
|
||||
case lists:keysearch("Command", 1, MsgKV) of
|
||||
{value,{_, "define"}} ->
|
||||
define;
|
||||
case lists:keysearch("Content-Length", 1, MsgKV) of
|
||||
{value,{_, Value}} ->
|
||||
{Length, _} = string:to_integer(Value),
|
||||
[_|Cont] = re:split(Data, "\n\n",[{return,list}]),
|
||||
Content = string:join(Cont, "\n\n"),
|
||||
Payload = string:substr(Content,1,Length),
|
||||
Payload;
|
||||
false ->
|
||||
not_found
|
||||
end
|
||||
|
|
|
@ -79,6 +79,9 @@ handle_info({tcp, Socket, RawData}, State) ->
|
|||
io:format("Old map: ~p NewState: ~p~n", [OldMap, NewState]),
|
||||
{noreply, State#state{client_vm_map = OldMap ++ [NewState]}};
|
||||
|
||||
handle_info({tcp_closed, _}, State) ->
|
||||
{stop, "Client closed socket", State};
|
||||
|
||||
handle_info(timeout, #state{lsock = LSock} = State) ->
|
||||
{ok, _Sock} = gen_tcp:accept(LSock),
|
||||
{noreply, State}.
|
||||
|
@ -95,19 +98,16 @@ code_change(_OldVsn, State, _Extra) ->
|
|||
|
||||
do_JSCall(Socket, Data, State) ->
|
||||
JSVM = js_runner:boot(),
|
||||
js_runner:define(JSVM, "function userCommand(cmd, par) {return cmd+' '+ par}"),
|
||||
Parsed = ggs_protocol:parse(Data),
|
||||
NewState = case Parsed of
|
||||
{Token, define} ->
|
||||
io:format("Got define!!\n"),
|
||||
[];
|
||||
{cmd, Command, Parameter} ->
|
||||
% Set the new state to []
|
||||
Ret = js_runner:call(JSVM, "userCommand",
|
||||
[list_to_binary(Command),
|
||||
list_to_binary(Parameter)]),
|
||||
send(Socket, "RefID", "JS says: ", Ret),
|
||||
{define, Token, Payload} ->
|
||||
js_runner:define(JSVM, Payload),
|
||||
send(Socket, "RefID", "Okay, defined that for you!"),
|
||||
[];
|
||||
{call, Token, Payload} ->
|
||||
Ret = js_runner:call(JSVM, Payload, []),%Payload, []),
|
||||
send(Socket, "RefID", "JS says: ", Ret);
|
||||
|
||||
% Set the new state to the reference generated, and JSVM associated
|
||||
{hello} ->
|
||||
Client = getRef(),
|
||||
|
@ -120,11 +120,11 @@ do_JSCall(Socket, Data, State) ->
|
|||
{crash, Zero} ->
|
||||
10/Zero;
|
||||
{vms} ->
|
||||
send(Socket, "RefID", State);
|
||||
send(Socket, "RefID", State)
|
||||
% Set the new state to []
|
||||
Other ->
|
||||
send(Socket, "RefID", "__error"),
|
||||
[]
|
||||
% Other ->
|
||||
% send(Socket, "RefID", "__error"),
|
||||
% []
|
||||
end,
|
||||
% Return the new state
|
||||
NewState.
|
||||
|
|
Reference in a new issue