Skip to content

Commit 7beca96

Browse files
committed
Add mock for erlang:fun_info(F, env)
Signed-off-by: Jakub Gonet <[email protected]>
1 parent 8eae957 commit 7beca96

File tree

3 files changed

+34
-12
lines changed

3 files changed

+34
-12
lines changed

src/libAtomVM/defaultatoms.def

+1
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ X(NAME_ATOM, "\x4", "name")
163163
X(ARITY_ATOM, "\x5", "arity")
164164
X(EXTERNAL_ATOM, "\x8", "external")
165165
X(LOCAL_ATOM, "\x5", "local")
166+
X(ENV_ATOM, "\x3", "env")
166167

167168
X(REGISTERED_NAME_ATOM, "\xF", "registered_name")
168169

src/libAtomVM/nifs.c

+7-2
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,7 @@ static const struct Nif unicode_characters_to_binary_nif =
801801
.base.type = NIFFunctionType,
802802
.nif_ptr = nif_unicode_characters_to_binary
803803
};
804-
static const struct Nif erlang_lists_subtract_nif =
804+
static const struct Nif erlang_lists_subtract_nif =
805805
{
806806
.base.type = NIFFunctionType,
807807
.nif_ptr = nif_erlang_lists_subtract
@@ -3647,11 +3647,16 @@ static term nif_erlang_fun_info_2(Context *ctx, int argc, term argv[])
36473647
value = term_is_external_fun(fun) ? EXTERNAL_ATOM : LOCAL_ATOM;
36483648
break;
36493649

3650+
case ENV_ATOM:
3651+
// TODO: implement env: env is mocked here and always return []
3652+
value = term_nil();
3653+
break;
3654+
36503655
default:
36513656
RAISE_ERROR(BADARG_ATOM);
36523657
}
36533658

3654-
if (UNLIKELY(memory_ensure_free_with_roots(ctx, TUPLE_SIZE(2), 2, (term[]) { key, value }, MEMORY_CAN_SHRINK) != MEMORY_GC_OK)) {
3659+
if (UNLIKELY(memory_ensure_free_with_roots(ctx, TUPLE_SIZE(2), 2, (term[]){ key, value }, MEMORY_CAN_SHRINK) != MEMORY_GC_OK)) {
36553660
RAISE_ERROR(OUT_OF_MEMORY_ATOM);
36563661
}
36573662
term fun_info_tuple = term_alloc_tuple(2, &ctx->heap);

tests/erlang_tests/test_fun_info.erl

+26-10
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,33 @@
1919
%
2020

2121
-module(test_fun_info).
22+
2223
-export([start/0, get_fun/1]).
2324

24-
-define(SUCCESS, (0)).
25-
-define(ERROR, (1)).
25+
-define(SUCCESS, 0).
26+
-define(ERROR, 1).
2627

2728
start() ->
2829
try test_funs() of
29-
ok -> ?SUCCESS
30+
ok ->
31+
?SUCCESS
3032
catch
3133
_:E:S ->
3234
erlang:display({E, S}),
3335
?ERROR
3436
end.
3537

36-
f(_X, _Y, _Z) -> ok.
38+
f(_X, _Y, _Z) ->
39+
ok.
3740

38-
get_fun(local) -> fun(B) -> not B end;
39-
get_fun(local_ref) -> fun f/3;
40-
get_fun(external_ref) -> fun erlang:apply/2;
41-
get_fun(not_existing_ref) -> fun erlang:undef/8.
41+
get_fun(local) ->
42+
fun(B) -> not B end;
43+
get_fun(local_ref) ->
44+
fun f/3;
45+
get_fun(external_ref) ->
46+
fun erlang:apply/2;
47+
get_fun(not_existing_ref) ->
48+
fun erlang:undef/8.
4249

4350
test_funs() ->
4451
LocalFun = ?MODULE:get_fun(local),
@@ -52,6 +59,8 @@ test_funs() ->
5259
true = atom_contains(LocalFunName, "get_fun"),
5360
{arity, 1} = erlang:fun_info(LocalFun, arity),
5461
{type, local} = erlang:fun_info(LocalFun, type),
62+
% TODO: implement env: env is mocked here and always return []
63+
{env, []} = erlang:fun_info(LocalFun, env),
5564

5665
{module, test_fun_info} = erlang:fun_info(LocalFunRef, module),
5766
{name, LocalFunRefName} = erlang:fun_info(LocalFunRef, name),
@@ -62,25 +71,32 @@ test_funs() ->
6271
true = Format1 or Format2 or Format3,
6372
{arity, 3} = erlang:fun_info(LocalFunRef, arity),
6473
{type, local} = erlang:fun_info(LocalFunRef, type),
74+
% TODO: implement env: env is mocked here and always return []
75+
{env, []} = erlang:fun_info(LocalFunRef, env),
6576

6677
{module, erlang} = erlang:fun_info(ExternalFunRef, module),
6778
{name, apply} = erlang:fun_info(ExternalFunRef, name),
6879
{arity, 2} = erlang:fun_info(ExternalFunRef, arity),
6980
{type, external} = erlang:fun_info(ExternalFunRef, type),
81+
{env, []} = erlang:fun_info(ExternalFunRef, env),
7082

7183
{module, erlang} = erlang:fun_info(NotExistingFunRef, module),
7284
{name, undef} = erlang:fun_info(NotExistingFunRef, name),
7385
{arity, 8} = erlang:fun_info(NotExistingFunRef, arity),
7486
{type, external} = erlang:fun_info(NotExistingFunRef, type),
87+
% TODO: implement env: env is mocked here and always return []
88+
{env, []} = erlang:fun_info(NotExistingFunRef, env),
7589

7690
ok.
7791

7892
atom_contains(Atom, Pattern) when is_atom(Atom) ->
7993
atom_contains(atom_to_list(Atom), Pattern);
8094
atom_contains([_C | Rest] = String, Pattern) ->
8195
case prefix_match(String, Pattern) of
82-
true -> true;
83-
false -> atom_contains(Rest, Pattern)
96+
true ->
97+
true;
98+
false ->
99+
atom_contains(Rest, Pattern)
84100
end;
85101
atom_contains([], _Pattern) ->
86102
false.

0 commit comments

Comments
 (0)