Skip to content

Commit

Permalink
PoC to allow NIFs to be loaded from escripts
Browse files Browse the repository at this point in the history
  • Loading branch information
filmor committed Dec 17, 2024
1 parent 9522518 commit 8a1d529
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions rustler_mix/lib/rustler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,29 @@ defmodule Rustler do
:code.purge(__MODULE__)

{otp_app, path} = @load_from
app_dir = Application.app_dir(otp_app, path)

load_path =
otp_app
|> Application.app_dir(path)
|> to_charlist()
case System.get_env("ESCRIPT_NAME") do
nil ->
app_dir

escript_path ->
if String.starts_with?(app_dir, escript_path) do
tmp = System.tmp_dir!()

:zip.extract(escript_path,
file_list: [Path.join(otp_app |> Atom.to_string(), path)],
cwd: tmp
)

Path.join(tmp, path)
else
app_dir
end
end

:erlang.load_nif(load_path, _construct_load_data())
:erlang.load_nif(load_path |> to_charlist(), _construct_load_data())
end
end
end
Expand Down

0 comments on commit 8a1d529

Please sign in to comment.