From 51069af59f86f95348063c9d4f3e4b9ad7db9eb3 Mon Sep 17 00:00:00 2001 From: v0idpwn Date: Mon, 26 Dec 2022 06:35:46 +0100 Subject: [PATCH 1/3] Add Code.loaded?/1 --- lib/elixir/lib/code.ex | 20 ++++++++++++++++++++ lib/elixir/test/elixir/code_test.exs | 5 +++++ 2 files changed, 25 insertions(+) diff --git a/lib/elixir/lib/code.ex b/lib/elixir/lib/code.ex index dbef73f753c..a11a1ac5793 100644 --- a/lib/elixir/lib/code.ex +++ b/lib/elixir/lib/code.ex @@ -1687,6 +1687,26 @@ defmodule Code do end end + @doc """ + Returns true if the module is loaded. + + This function doesn't attempt to load the module. For such behaviour, + `ensure_loaded?/1` can be used. + + ## Examples + + iex> Code.loaded?(Atom) + true + + iex> Code.loaded?(NotYetLoaded) + false + + """ + @doc since: "1.15.0" + def loaded?(module) do + :erlang.module_loaded(module) + end + @doc """ Returns true if the current process can await for module compilation. diff --git a/lib/elixir/test/elixir/code_test.exs b/lib/elixir/test/elixir/code_test.exs index 95123cb2095..45f6e3eb8f6 100644 --- a/lib/elixir/test/elixir/code_test.exs +++ b/lib/elixir/test/elixir/code_test.exs @@ -358,6 +358,11 @@ defmodule CodeTest do end end + test "loaded?/1" do + assert Code.loaded?(__MODULE__) + refute Code.loaded?(Code.NoFile) + end + test "put_compiler_option/2 validates options" do message = "unknown compiler option: :not_a_valid_option" From e1fe877bb11fde87d6e571e8ff4d85f9f3d4be8d Mon Sep 17 00:00:00 2001 From: Andrea Leopardi Date: Mon, 26 Dec 2022 08:45:11 +0100 Subject: [PATCH 2/3] Apply suggestions from code review Co-authored-by: felipe stival <14948182+v0idpwn@users.noreply.github.com> --- lib/elixir/lib/code.ex | 2 +- lib/elixir/test/elixir/code_test.exs | 5 ----- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/lib/elixir/lib/code.ex b/lib/elixir/lib/code.ex index a11a1ac5793..ee638c9d497 100644 --- a/lib/elixir/lib/code.ex +++ b/lib/elixir/lib/code.ex @@ -1688,7 +1688,7 @@ defmodule Code do end @doc """ - Returns true if the module is loaded. + Returns `true` if the module is loaded. This function doesn't attempt to load the module. For such behaviour, `ensure_loaded?/1` can be used. diff --git a/lib/elixir/test/elixir/code_test.exs b/lib/elixir/test/elixir/code_test.exs index 45f6e3eb8f6..95123cb2095 100644 --- a/lib/elixir/test/elixir/code_test.exs +++ b/lib/elixir/test/elixir/code_test.exs @@ -358,11 +358,6 @@ defmodule CodeTest do end end - test "loaded?/1" do - assert Code.loaded?(__MODULE__) - refute Code.loaded?(Code.NoFile) - end - test "put_compiler_option/2 validates options" do message = "unknown compiler option: :not_a_valid_option" From 35239add08a99ccd9aad6b79ba06cae2bbfd46a9 Mon Sep 17 00:00:00 2001 From: Andrea Leopardi Date: Mon, 26 Dec 2022 08:45:41 +0100 Subject: [PATCH 3/3] Update lib/elixir/lib/code.ex --- lib/elixir/lib/code.ex | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/elixir/lib/code.ex b/lib/elixir/lib/code.ex index ee638c9d497..875db5b5000 100644 --- a/lib/elixir/lib/code.ex +++ b/lib/elixir/lib/code.ex @@ -1703,6 +1703,7 @@ defmodule Code do """ @doc since: "1.15.0" + @spec loaded?(module) :: boolean def loaded?(module) do :erlang.module_loaded(module) end