Skip to content

Commit

Permalink
port: add $call atom to message tuple, and use gen_server like format
Browse files Browse the repository at this point in the history
Change message format from `{Pid, MonitorRef, Message}` to
`{'$call', {self(), MonitorRef}, Message}`, for consistency and
future-proofing.

Signed-off-by: Davide Bettio <davide@uninstall.it>
  • Loading branch information
bettio committed Dec 25, 2023
1 parent 76d4f8c commit 47c8c60
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion libs/eavmlib/src/port.erl
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ call(Port, Message) ->
-spec call(pid(), Message :: term(), Timeout :: timeout()) -> term() | {error, timeout}.
call(Port, Message, Timeout) ->
MonitorRef = monitor(port, Port),
Port ! {self(), MonitorRef, Message},
Port ! {'$call', {self(), MonitorRef}, Message},
Result =
receive
{'DOWN', MonitorRef, port, Port, normal} ->
Expand Down
18 changes: 14 additions & 4 deletions src/libAtomVM/port.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,27 @@ enum GenMessageParseResult port_parse_gen_message(term msg, GenMessage *gen_mess
return GenMessageParseError;
}

gen_message->pid = term_get_tuple_element(msg, 0);
if (UNLIKELY(!term_is_pid(gen_message->pid))) {
gen_message->message_type = term_get_tuple_element(msg, 0);
if (UNLIKELY(!term_is_atom(gen_message->message_type))) {
return GenMessageParseError;
}

gen_message->ref = term_get_tuple_element(msg, 1);
if (UNLIKELY(!term_is_reference(gen_message->ref))) {
gen_message->from = term_get_tuple_element(msg, 1);
if (UNLIKELY(
!term_is_tuple(gen_message->from) || term_get_tuple_arity(gen_message->from) != 2)) {
return GenMessageParseError;
}

gen_message->req = term_get_tuple_element(msg, 2);

gen_message->pid = term_get_tuple_element(gen_message->from, 0);
if (UNLIKELY(!term_is_pid(gen_message->pid))) {
return GenMessageParseError;
}
gen_message->ref = term_get_tuple_element(gen_message->from, 1);
if (UNLIKELY(!term_is_reference(gen_message->ref))) {
return GenMessageParseError;
}

return GenMessageParseOk;
}
1 change: 1 addition & 0 deletions src/libAtomVM/port.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ static inline void port_send_reply(Context *ctx, term pid, term ref, term payloa

typedef struct
{
term message_type;
term from;
term req;

Expand Down

0 comments on commit 47c8c60

Please sign in to comment.