Skip to content

Commit

Permalink
Expand load data at compile time (#570)
Browse files Browse the repository at this point in the history
  • Loading branch information
evnu authored Oct 25, 2023
1 parent 85582eb commit 7cbfdd2
Showing 1 changed file with 24 additions and 22 deletions.
46 changes: 24 additions & 22 deletions rustler_mix/lib/rustler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -120,35 +120,21 @@ defmodule Rustler do
quote do
@on_load :rustler_init

@doc false
def rustler_init do
# Remove any old modules that may be loaded so we don't get
# {:error, {:upgrade, 'Upgrade not supported by this NIF library.'}}
:code.purge(__MODULE__)

{otp_app, path} = @load_from

load_path =
otp_app
|> Application.app_dir(path)
|> to_charlist()

load_data = _construct_load_data(@load_data, @load_data_fun)

:erlang.load_nif(load_path, load_data)
end

defp _construct_load_data(load_data, load_data_fun) do
defmacrop _construct_load_data do
default_load_data_value = unquote(default_load_data_value)
default_fun_value = unquote(default_fun_value)

case {load_data, load_data_fun} do
case {@load_data, @load_data_fun} do
{load_data, ^default_fun_value} ->
load_data
quote do
unquote(load_data)
end

{^default_load_data_value, {module, function}}
when is_atom(module) and is_atom(function) ->
apply(module, function, [])
quote do
apply(unquote(module), unquote(function), [])
end

{^default_load_data_value, provided_value} ->
raise """
Expand All @@ -164,6 +150,22 @@ defmodule Rustler do
"""
end
end

@doc false
def rustler_init do
# Remove any old modules that may be loaded so we don't get
# {:error, {:upgrade, 'Upgrade not supported by this NIF library.'}}
:code.purge(__MODULE__)

{otp_app, path} = @load_from

load_path =
otp_app
|> Application.app_dir(path)
|> to_charlist()

:erlang.load_nif(load_path, _construct_load_data())
end
end
end

Expand Down

0 comments on commit 7cbfdd2

Please sign in to comment.