Skip to content

Commit 8a1d529

Browse files
committed
PoC to allow NIFs to be loaded from escripts
1 parent 9522518 commit 8a1d529

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

rustler_mix/lib/rustler.ex

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,29 @@ defmodule Rustler do
158158
:code.purge(__MODULE__)
159159

160160
{otp_app, path} = @load_from
161+
app_dir = Application.app_dir(otp_app, path)
161162

162163
load_path =
163-
otp_app
164-
|> Application.app_dir(path)
165-
|> to_charlist()
164+
case System.get_env("ESCRIPT_NAME") do
165+
nil ->
166+
app_dir
167+
168+
escript_path ->
169+
if String.starts_with?(app_dir, escript_path) do
170+
tmp = System.tmp_dir!()
171+
172+
:zip.extract(escript_path,
173+
file_list: [Path.join(otp_app |> Atom.to_string(), path)],
174+
cwd: tmp
175+
)
176+
177+
Path.join(tmp, path)
178+
else
179+
app_dir
180+
end
181+
end
166182

167-
:erlang.load_nif(load_path, _construct_load_data())
183+
:erlang.load_nif(load_path |> to_charlist(), _construct_load_data())
168184
end
169185
end
170186
end

0 commit comments

Comments
 (0)