From 24de28fe0e9b4b4ce0212beea5f1590f8a315613 Mon Sep 17 00:00:00 2001 From: Benedikt Reinartz Date: Wed, 16 Oct 2024 00:14:49 +0200 Subject: [PATCH] PoC to allow NIFs to be loaded from escripts --- rustler_mix/lib/rustler.ex | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/rustler_mix/lib/rustler.ex b/rustler_mix/lib/rustler.ex index 487d8b4d..c5d7e336 100644 --- a/rustler_mix/lib/rustler.ex +++ b/rustler_mix/lib/rustler.ex @@ -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