From 7358e206d4691ad862a2b96e8050ad3ceeedffda Mon Sep 17 00:00:00 2001 From: Davide Bettio Date: Sun, 24 Dec 2023 01:32:05 +0100 Subject: [PATCH] port: add $call atom to message tuple, and use gen_server like format Change message format from `{Pid, MonitorRef, Message}` to `{'$call', {self(), MonitorRef}, Message}`, for consistency and future-proofing. Signed-off-by: Davide Bettio --- libs/eavmlib/src/port.erl | 2 +- src/libAtomVM/port.c | 18 ++++++++++++++---- src/libAtomVM/port.h | 1 + 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/libs/eavmlib/src/port.erl b/libs/eavmlib/src/port.erl index 934208cc90..2bcfc6897d 100644 --- a/libs/eavmlib/src/port.erl +++ b/libs/eavmlib/src/port.erl @@ -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} -> diff --git a/src/libAtomVM/port.c b/src/libAtomVM/port.c index 2bcaf774d6..f1a726fd50 100644 --- a/src/libAtomVM/port.c +++ b/src/libAtomVM/port.c @@ -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; } diff --git a/src/libAtomVM/port.h b/src/libAtomVM/port.h index 0fa3d24971..94f9b64b32 100644 --- a/src/libAtomVM/port.h +++ b/src/libAtomVM/port.h @@ -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;