Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand load data at compile time #570

Merged
merged 1 commit into from
Oct 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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