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:
Jonatan Pålsson 2011-02-04 19:09:41 +01:00
parent f5fac06849
commit 4c6d0987bd
3 changed files with 49 additions and 23 deletions

View file

@ -8,21 +8,35 @@ HOST = 'localhost' # The remote host
PORT = int(sys.argv[1]) # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
# Define ourselves a function!
s.send(
'Token: 1001\n\
"Token: 1001\n\
Command: define\n\
Content-Type: text\n\
Content-Length: 40\n\
Content-Length: 42\n\
\n\
function helloWorld(x) {return x + 2 ;}')
function myFun() {return 'Hello world!' ;}")
fs = s.makefile()
data = fs.readline()
token = data.split(" ")[0]
print "Token:", token
print "Data: ", ' '.join(data.split(" ")[1:])
s.send(token+" __echo 0 msg")
data = s.recv(1024)
print data
# Call that function!
s.send(
"Token: 1001\n\
Command: call\n\
Content-Type: text\n\
Content-Length: 6\n\
\n\
myFun()")
fs = s.makefile()
data = fs.readline()
token = data.split(" ")[0]
print "Token:", token
print "Data: ", ' '.join(data.split(" ")[1:])
s.close()

View file

@ -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

View file

@ -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.