Skip to content

Commit c901afd

Browse files
committed
Add code:is_loaded/1 and code:which/1
Signed-off-by: Paul Guyot <pguyot@kallisys.net>
1 parent f016904 commit c901afd

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2929
- Added `erlang:unique_integer/0` and `erlang:unique_integer/1`
3030
- Added support for 'ets:delete/1'.
3131
- Added `lists:flatmap/2`
32+
- Added `code:is_loaded/1` and `code:which/1`
3233

3334
### Fixed
3435
- ESP32: improved sntp sync speed from a cold boot.

libs/estdlib/src/code.erl

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
all_loaded/0,
3030
load_abs/1,
3131
load_binary/3,
32-
ensure_loaded/1
32+
ensure_loaded/1,
33+
which/1,
34+
is_loaded/1
3335
]).
3436

3537
%%-----------------------------------------------------------------------------
@@ -58,7 +60,7 @@ all_loaded() ->
5860
erlang:nif_error(undefined).
5961

6062
%%-----------------------------------------------------------------------------
61-
%% @param Filename path to the beam to open, without .beams suffix
63+
%% @param Filename path to the beam to open, without .beam suffix
6264
%% @returns A tuple with the name of the module
6365
%% @doc Load a module from a path.
6466
%% Error return result type is different from Erlang/OTP.
@@ -98,3 +100,35 @@ load_binary(_Module, _Filename, _Binary) ->
98100
Module :: atom().
99101
ensure_loaded(_Module) ->
100102
erlang:nif_error(undefined).
103+
104+
%%-----------------------------------------------------------------------------
105+
%% @param Module module to test
106+
%% @returns Tuple `{file, preloaded}' if module is loaded or `false'
107+
%% @doc Determine if a module is loaded. AtomVM works in
108+
%% an embedded-like mode where modules are loaded at start-up but modules
109+
%% can be loaded explicitely as well (especially from a binary with `load_binary/3').
110+
%% @end
111+
%%-----------------------------------------------------------------------------
112+
is_loaded(Module) ->
113+
case ?MODULE:ensure_loaded(Module) of
114+
{module, _Module} ->
115+
{file, preloaded};
116+
{error, _} ->
117+
false
118+
end.
119+
120+
%%-----------------------------------------------------------------------------
121+
%% @param Module module to test
122+
%% @returns `preloaded' if module is loaded or `false'
123+
%% @doc Determine if a module is loaded. There currently is no way to
124+
%% distinguish a module that was loaded with `load_binary/3' or that was
125+
%% preloaded at startup.
126+
%% @end
127+
%%-----------------------------------------------------------------------------
128+
which(Module) ->
129+
case ?MODULE:ensure_loaded(Module) of
130+
{module, _Module} ->
131+
preloaded;
132+
{error, _} ->
133+
non_existing
134+
end.

0 commit comments

Comments
 (0)