Skip to content

Commit 7180b98

Browse files
author
José Valim
committed
Support disabling autoload after compilation
Useful for delaying loading of modules that may depend on NIFs until necessary. Signed-off-by: José Valim <[email protected]>
1 parent 021cca4 commit 7180b98

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

lib/elixir/src/elixir_compiler.erl

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,8 @@ code_loading_compilation(Forms, Vars, #{line := Line} = E) ->
7878

7979
%% Pass {native, false} to speed up bootstrap
8080
%% process when native is set to true
81-
AllOpts = options(),
82-
FinalOpts = AllOpts -- [native, warn_missing_spec],
83-
inner_module(Form, FinalOpts, true, E, fun(_, Binary) ->
81+
ErlOpts = options() -- [native, warn_missing_spec],
82+
inner_module(Form, ErlOpts, [{bootstrap, true}], E, fun(_, Binary) ->
8483
%% If we have labeled locals, anonymous functions
8584
%% were created and therefore we cannot ditch the
8685
%% module
@@ -169,7 +168,7 @@ allows_fast_compilation(_) -> false.
169168
%% executes the callback in case of success. This automatically
170169
%% handles errors and warnings. Used by this module and elixir_module.
171170
module(Forms, Opts, E, Callback) ->
172-
Extra =
171+
ErlOpts =
173172
case proplists:get_value(debug_info, Opts) of
174173
true -> [debug_info];
175174
false -> [];
@@ -179,16 +178,22 @@ module(Forms, Opts, E, Callback) ->
179178
false -> []
180179
end
181180
end,
182-
inner_module(Forms, Extra ++ options(), false, E, Callback).
181+
inner_module(Forms, ErlOpts ++ options(), Opts, E, Callback).
183182

184-
inner_module(Forms, Options, Bootstrap, #{file := File} = E, Callback) when
185-
is_list(Forms), is_list(Options), is_boolean(Bootstrap), is_function(Callback) ->
183+
inner_module(Forms, ErlOpts, ExOpts, #{file := File} = E, Callback) when
184+
is_list(Forms), is_list(ErlOpts), is_list(ExOpts), is_function(Callback) ->
186185
Source = elixir_utils:characters_to_list(File),
186+
Autoload = proplists:get_value(autoload, ExOpts, true),
187+
Bootstrap = proplists:get_value(bootstrap, ExOpts, false),
187188

188-
case compile:noenv_forms([no_auto_import()|Forms], [return, {source, Source}|Options]) of
189+
case compile:noenv_forms([no_auto_import()|Forms], [return, {source, Source}|ErlOpts]) of
189190
{ok, Module, Binary, Warnings} ->
190191
format_warnings(Bootstrap, Warnings),
191-
{module, Module} = code:load_binary(Module, beam_location(E), Binary),
192+
{module, Module} =
193+
case Autoload of
194+
true -> code:load_binary(Module, beam_location(E), Binary);
195+
false -> {module, Module}
196+
end,
192197
Callback(Module, Binary);
193198
{error, Errors, Warnings} ->
194199
format_warnings(Bootstrap, Warnings),

lib/elixir/src/elixir_module.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ spec_for_macro(Else) -> Else.
331331

332332
compile_opts(Module) ->
333333
case ets:lookup(data_table(Module), compile) of
334-
[{compile, Opts}] when is_list(Opts) -> Opts;
334+
[{compile, Opts}] when is_list(Opts) -> lists:flatten(Opts);
335335
[] -> []
336336
end.
337337

lib/elixir/test/elixir/module_test.exs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,13 @@ defmodule ModuleTest do
144144
assert [3, 2, 1] = @other_attribute
145145
end
146146

147+
test "@compile autoload attribute" do
148+
defmodule NoAutoload do
149+
@compile {:autoload, false}
150+
end
151+
refute :code.is_loaded(NoAutoload)
152+
end
153+
147154
## Naming
148155

149156
test "concat" do

0 commit comments

Comments
 (0)