Skip to content

Commit

Permalink
Allow sending message to registered process name
Browse files Browse the repository at this point in the history
Changes `erlang:send/2` to accept a pid or registered process name. Updates
`tests/erlang_tests/test_send_nif_and_echo.erl` to also test sending directly to the registered
name, and `tests/erlang_tests/test_send.erl` to test sending to an an unregistered `atom` name.

Fixes issue atomvm#98

Signed-off-by: Winford <winford@object.stream>
  • Loading branch information
UncleGrumpy committed Jun 10, 2023
1 parent 29825e4 commit 0251f93
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed a bug in when putting integers in bit syntax with integer field sizes
- Fixed numerous bugs in memory allocations that could crash the VM
- Fixed SNTP support that had been broken in IDF 4.x builds
- Fixed `erlang:send/2` not sending to registered name

### Breaking Changes

Expand Down
19 changes: 16 additions & 3 deletions src/libAtomVM/nifs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1285,10 +1285,23 @@ static term nif_erlang_send_2(Context *ctx, int argc, term argv[])
{
UNUSED(argc);

term pid_term = argv[0];
VALIDATE_VALUE(pid_term, term_is_pid);
term dest = argv[0];
int local_process_id;
if (term_is_pid(dest) == 1) {
local_process_id = term_to_local_process_id(dest);
} else if (term_is_atom(dest) == true) {
int atom_index = term_to_atom_index(dest);
int local_pid = globalcontext_get_registered_process(ctx->global, atom_index);
bool is_active = globalcontext_process_exists(ctx->global, local_pid);
if (UNLIKELY(is_active != true)) {
RAISE_ERROR(BADARG_ATOM);
} else {
local_process_id = local_pid;
}
} else {
RAISE_ERROR(BADARG_ATOM);
}

int local_process_id = term_to_local_process_id(pid_term);
globalcontext_send_message(ctx->global, local_process_id, argv[1]);

return argv[1];
Expand Down
19 changes: 14 additions & 5 deletions tests/erlang_tests/test_send.erl
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,25 @@

-module(test_send).

-export([start/0, send2/2]).
-export([start/0]).

start() ->
send2(5, 1) + send2(self(), -1).
send_fail(5, 0) + send(self(), 0) + send_fail(bogus, 0).

send2(A, B) ->
send(A, B) ->
try erlang:send(A, B) of
B -> -1;
Any -> Any
B -> B;
_Any -> -1
catch
error:badarg -> -2;
_:_ -> -4
end.

send_fail(A, B) ->
try erlang:send(A, B) of
B -> -1;
_Any -> -2
catch
error:badarg -> 0;
_:_ -> -4
end.
11 changes: 9 additions & 2 deletions tests/erlang_tests/test_send_nif_and_echo.erl
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,20 @@

start() ->
register(echo, do_open_port(<<"echo">>, [])),
byte_size(echo(<<"Hello World">>)).
byte_size(echo(<<"Hello World">>)) + byte_size(to_pid(erlang:whereis(echo), <<"Hello World">>)).

do_open_port(PortName, Param) ->
open_port({spawn, PortName}, Param).

echo(SendValue) ->
erlang:send(whereis(echo), {self(), SendValue}),
erlang:send(echo, {self(), SendValue}),
receive
Value ->
Value
end.

to_pid(Pid, SendValue) ->
erlang:send(Pid, {self(), SendValue}),
receive
Value ->
Value
Expand Down
4 changes: 2 additions & 2 deletions tests/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ struct Test tests[] = {
TEST_CASE_EXPECTED(long_atoms, 4),
TEST_CASE_EXPECTED(test_concat_badarg, 4),
TEST_CASE_EXPECTED(register_and_whereis_badarg, 333),
TEST_CASE_EXPECTED(test_send, -3),
TEST_CASE(test_send),
TEST_CASE_EXPECTED(test_open_port_badargs, -21),
TEST_CASE_EXPECTED(prime_ext, 1999),
TEST_CASE_EXPECTED(test_try_case_end, 256),
Expand Down Expand Up @@ -477,7 +477,7 @@ struct Test tests[] = {
TEST_CASE_ATOMVM_ONLY(test_close_console_driver, 0),
TEST_CASE_ATOMVM_ONLY(test_close_echo_driver, 0),
TEST_CASE_ATOMVM_ONLY(test_regecho_driver, 11),
TEST_CASE_ATOMVM_ONLY(test_send_nif_and_echo, 11),
TEST_CASE_ATOMVM_ONLY(test_send_nif_and_echo, 22),

TEST_CASE_EXPECTED(test_code_load_binary, 24),
TEST_CASE_EXPECTED(test_code_load_abs, 24),
Expand Down

0 comments on commit 0251f93

Please sign in to comment.