From 2e2c15ae77a9c649bfd20692aad5a677b3c71d69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 28 Jan 2025 00:04:52 +0100 Subject: [PATCH] Load NIFs explicitly --- .../dll_loader.ex => adbc_dll_loader_nif.ex} | 17 +++++++---------- lib/adbc_nif.ex | 12 +----------- 2 files changed, 8 insertions(+), 21 deletions(-) rename lib/{adbc/dll_loader.ex => adbc_dll_loader_nif.ex} (59%) diff --git a/lib/adbc/dll_loader.ex b/lib/adbc_dll_loader_nif.ex similarity index 59% rename from lib/adbc/dll_loader.ex rename to lib/adbc_dll_loader_nif.ex index a3c7097..b4e756e 100644 --- a/lib/adbc/dll_loader.ex +++ b/lib/adbc_dll_loader_nif.ex @@ -1,7 +1,10 @@ -defmodule Adbc.Nif.DLLLoader do +defmodule Adbc.DLLLoaderNif do + # We don't use on_load callbacks because this module + # needs to be invoked from Adbc.Nif and if both have + # @on_load the order is not guaranteed in a release. @moduledoc false - @on_load :__on_load__ - def __on_load__ do + + def init do case :os.type() do {:win32, _} -> priv_dir = :code.priv_dir(:adbc) @@ -16,12 +19,6 @@ defmodule Adbc.Nif.DLLLoader do end def add_dll_directory do - case :os.type() do - {:win32, _} -> - :erlang.nif_error(:not_loaded) - - _ -> - :ok - end + :erlang.nif_error(:not_loaded) end end diff --git a/lib/adbc_nif.ex b/lib/adbc_nif.ex index 4af6551..15195ea 100644 --- a/lib/adbc_nif.ex +++ b/lib/adbc_nif.ex @@ -1,21 +1,11 @@ defmodule Adbc.Nif do @moduledoc false - alias Adbc.Nif.DLLLoader - @on_load :load_nif def load_nif do + :ok = Adbc.DLLLoaderNif.init() nif_file = ~c"#{:code.priv_dir(:adbc)}/adbc_nif" - :ok = - case :os.type() do - {:win32, _} -> - DLLLoader.add_dll_directory() - - _ -> - :ok - end - case :erlang.load_nif(nif_file, 0) do :ok -> :ok {:error, {:reload, _}} -> :ok