Skip to content

Commit 8b294df

Browse files
committed
Forward port changes from v0.6 release branch
Merge several changes into main, including: - Add support for `erlang:size/1` bif - Add missing DEBUG_FAIL_NULL for gcbifs - CI adjustments
2 parents 98a0412 + 82ae2ad commit 8b294df

File tree

9 files changed

+112
-1
lines changed

9 files changed

+112
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2222
- Support for `maps:iterator/2` and `~kp` with `io_lib:format/2` that were introduced with OTP26.
2323
- Support for `erlang:apply/2`
2424
- Support for `lists:keystore/4`
25+
- Support for `erlang:size/1` bif
2526

2627
### Changed
2728

src/libAtomVM/bif.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,3 +1500,15 @@ term bif_erlang_max_2(Context *ctx, uint32_t fail_label, term arg1, term arg2)
15001500
}
15011501
return r == TermGreaterThan ? arg1 : arg2;
15021502
}
1503+
1504+
term bif_erlang_size_1(Context *ctx, uint32_t fail_label, int live, term arg1)
1505+
{
1506+
if (term_is_binary(arg1)) {
1507+
// For bitstrings, number of bytes is rounded down
1508+
return bif_erlang_byte_size_1(ctx, fail_label, live, arg1);
1509+
} else if (term_is_tuple(arg1)) {
1510+
return bif_erlang_tuple_size_1(ctx, fail_label, arg1);
1511+
}
1512+
1513+
RAISE_ERROR_BIF(fail_label, BADARG_ATOM);
1514+
}

src/libAtomVM/bif.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ term bif_erlang_get_1(Context *ctx, uint32_t fail_label, term arg1);
110110
term bif_erlang_min_2(Context *ctx, uint32_t fail_label, term arg1, term arg2);
111111
term bif_erlang_max_2(Context *ctx, uint32_t fail_label, term arg1, term arg2);
112112

113+
term bif_erlang_size_1(Context *ctx, uint32_t fail_label, int live, term arg1);
114+
113115
#ifdef __cplusplus
114116
}
115117
#endif

src/libAtomVM/bifs.gperf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,4 @@ erlang:map_size/1, {.gcbif.base.type = GCBIFFunctionType, .gcbif.gcbif1_ptr = bi
9090
erlang:map_get/2, {.bif.base.type = BIFFunctionType, .bif.bif2_ptr = bif_erlang_map_get_2}
9191
erlang:min/2, {.bif.base.type = BIFFunctionType, .bif.bif2_ptr = bif_erlang_min_2}
9292
erlang:max/2, {.bif.base.type = BIFFunctionType, .bif.bif2_ptr = bif_erlang_max_2}
93+
erlang:size/1, {.gcbif.base.type = GCBIFFunctionType, .gcbif.gcbif1_ptr = bif_erlang_size_1}

src/libAtomVM/opcodesswitch.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5273,6 +5273,7 @@ HOT_FUNC int scheduler_entry_point(GlobalContext *glb)
52735273

52745274
const struct ExportedFunction *exported_bif = mod->imported_funcs[bif];
52755275
GCBifImpl1 func = EXPORTED_FUNCTION_TO_GCBIF(exported_bif)->gcbif1_ptr;
5276+
DEBUG_FAIL_NULL(func);
52765277
term ret = func(ctx, fail_label, live, arg1);
52775278
if (UNLIKELY(term_is_invalid_term(ret))) {
52785279
if (fail_label) {
@@ -5320,6 +5321,7 @@ HOT_FUNC int scheduler_entry_point(GlobalContext *glb)
53205321

53215322
const struct ExportedFunction *exported_bif = mod->imported_funcs[bif];
53225323
GCBifImpl2 func = EXPORTED_FUNCTION_TO_GCBIF(exported_bif)->gcbif2_ptr;
5324+
DEBUG_FAIL_NULL(func);
53235325
term ret = func(ctx, fail_label, live, arg1, arg2);
53245326
if (UNLIKELY(term_is_invalid_term(ret))) {
53255327
if (fail_label) {
@@ -5393,6 +5395,7 @@ HOT_FUNC int scheduler_entry_point(GlobalContext *glb)
53935395

53945396
const struct ExportedFunction *exported_bif = mod->imported_funcs[bif];
53955397
GCBifImpl3 func = EXPORTED_FUNCTION_TO_GCBIF(exported_bif)->gcbif3_ptr;
5398+
DEBUG_FAIL_NULL(func);
53965399
term ret = func(ctx, fail_label, live, arg1, arg2, arg3);
53975400
if (UNLIKELY(term_is_invalid_term(ret))) {
53985401
if (fail_label) {

tests/erlang_tests/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ compile_erlang(test_bitwise2)
120120
compile_erlang(test_boolean)
121121
compile_erlang(test_gt_and_le)
122122
compile_erlang(test_tuple_size)
123+
compile_erlang(test_size)
123124
compile_erlang(test_element)
124125
compile_erlang(test_setelement)
125126
compile_erlang(test_insert_element)
@@ -582,6 +583,7 @@ add_custom_target(erlang_test_modules DEPENDS
582583
test_boolean.beam
583584
test_gt_and_le.beam
584585
test_tuple_size.beam
586+
test_size.beam
585587
test_element.beam
586588
test_setelement.beam
587589
test_insert_element.beam

tests/erlang_tests/test_size.erl

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
%
2+
% This file is part of AtomVM.
3+
%
4+
% Copyright 2024 Paul Guyot <pguyot@kallisys.net>
5+
%
6+
% Licensed under the Apache License, Version 2.0 (the "License");
7+
% you may not use this file except in compliance with the License.
8+
% You may obtain a copy of the License at
9+
%
10+
% http://www.apache.org/licenses/LICENSE-2.0
11+
%
12+
% Unless required by applicable law or agreed to in writing, software
13+
% distributed under the License is distributed on an "AS IS" BASIS,
14+
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
% See the License for the specific language governing permissions and
16+
% limitations under the License.
17+
%
18+
% SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
19+
%
20+
21+
-module(test_size).
22+
23+
-export([start/0, test_size/2, test_size_guard/2]).
24+
25+
start() ->
26+
gt = ?MODULE:test_size_guard({1, 2, 3}, 4),
27+
lt = ?MODULE:test_size_guard({1, 2, 3}, 2),
28+
eq = ?MODULE:test_size_guard({1, 2, 3}, 3),
29+
gt = ?MODULE:test_size_guard(<<1, 2, 3>>, 4),
30+
lt = ?MODULE:test_size_guard(<<1, 2, 3>>, 2),
31+
eq = ?MODULE:test_size_guard(<<1, 2, 3>>, 3),
32+
gt = ?MODULE:test_size_guard(<<3, 14, 1, 2, 3>>, 4),
33+
lt = ?MODULE:test_size_guard(<<3, 14, 1, 2, 3>>, 2),
34+
eq = ?MODULE:test_size_guard(<<3, 14, 1, 2, 3>>, 3),
35+
false = ?MODULE:test_size_guard(#{a => 1, b => 2, c => 3}, 4),
36+
false = ?MODULE:test_size_guard([1, 2, 3], 2),
37+
38+
gt = ?MODULE:test_size({1, 2, 3}, 4),
39+
lt = ?MODULE:test_size({1, 2, 3}, 2),
40+
eq = ?MODULE:test_size({1, 2, 3}, 3),
41+
gt = ?MODULE:test_size(<<1, 2, 3>>, 4),
42+
lt = ?MODULE:test_size(<<1, 2, 3>>, 2),
43+
eq = ?MODULE:test_size(<<1, 2, 3>>, 3),
44+
gt = ?MODULE:test_size(<<3, 14, 1, 2, 3>>, 4),
45+
lt = ?MODULE:test_size(<<3, 14, 1, 2, 3>>, 2),
46+
eq = ?MODULE:test_size(<<3, 14, 1, 2, 3>>, 3),
47+
ok =
48+
try
49+
?MODULE:test_size([1, 2, 3], 2),
50+
fail
51+
catch
52+
error:badarg ->
53+
ok
54+
end,
55+
ok =
56+
try
57+
?MODULE:test_size(#{a => 1, b => 2, c => 3}, 4),
58+
fail
59+
catch
60+
error:badarg ->
61+
ok
62+
end,
63+
0.
64+
65+
% OTP-27 encodes this as a call to byte_size/1, passing it the matching state
66+
% OTP-21 encodes this as a call to size/1, but encodes bs_context_to_binary
67+
% first.
68+
test_size_guard(<<3, 14, Elem/binary>>, S) when size(Elem) < S -> gt;
69+
test_size_guard(<<3, 14, Elem/binary>>, S) when size(Elem) > S -> lt;
70+
test_size_guard(<<3, 14, Elem/binary>>, S) when size(Elem) =:= S -> eq;
71+
test_size_guard(Elem, S) when size(Elem) < S -> gt;
72+
test_size_guard(Elem, S) when size(Elem) > S -> lt;
73+
test_size_guard(Elem, S) when size(Elem) =:= S -> eq;
74+
test_size_guard(_Elem, _S) -> false.
75+
76+
test_size(<<3, 14, Elem/binary>>, S) ->
77+
Size = size(Elem),
78+
if
79+
Size < S -> gt;
80+
Size > S -> lt;
81+
true -> eq
82+
end;
83+
test_size(Elem, S) ->
84+
Size = size(Elem),
85+
if
86+
Size < S -> gt;
87+
Size > S -> lt;
88+
true -> eq
89+
end.

tests/erlang_tests/whereis_dead_process.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ test_names(Names) ->
3838
ok =
3939
receive
4040
{'DOWN', Monitor, process, Pid, _} -> ok
41-
after 500 -> timeout
41+
after 1000 -> timeout
4242
end,
4343
ok = test_unregistered(Names).
4444

tests/test.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ struct Test tests[] = {
143143
TEST_CASE(test_boolean),
144144
TEST_CASE_EXPECTED(test_gt_and_le, 255),
145145
TEST_CASE_EXPECTED(test_tuple_size, 6),
146+
TEST_CASE(test_size),
146147
TEST_CASE_EXPECTED(test_element, 7),
147148
TEST_CASE_EXPECTED(test_setelement, 121),
148149
TEST_CASE_EXPECTED(test_insert_element, 121),

0 commit comments

Comments
 (0)