Skip to content

Commit

Permalink
Merge pull request #8 from HadrienMP/master
Browse files Browse the repository at this point in the history
Loading nif in escript
  • Loading branch information
sofuture authored Jun 13, 2020
2 parents 61bbba1 + 6a03dd2 commit 72b7558
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ c_src/*.o
ebin/
_build/
.rebar
*.swp
6 changes: 1 addition & 5 deletions src/encurses.erl
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,7 @@
%% =============================================================================

load_nif() ->
Dir = case code:priv_dir(encurses) of
{error, bad_name} ->
filename:dirname(code:which(?MODULE)) ++ "/../priv";
OtherDir -> OtherDir
end,
Dir = paths:nif_dir(),
erlang:load_nif(Dir ++ "/encurses", 0).

%% =============================================================================
Expand Down
17 changes: 17 additions & 0 deletions src/paths.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-module(paths).
-export([nif_dir/0, nif_dir/2]).

nif_dir() ->
Cpd = code:priv_dir(encurses),
MoCpd = filename:dirname(code:which(?MODULE)) ++ "/../priv",
nif_dir(Cpd, MoCpd).


nif_dir({error, _}, ModulePrivDir) -> handle_escript_format(ModulePrivDir);
nif_dir(Dir, _) -> handle_escript_format(Dir).

handle_escript_format(Dir) ->
Handled = re:replace(Dir,
"(_build/[^/]+)/bin/([^/]+)/encurses/priv",
"\\1/lib/encurses/priv"),
binary_to_list(iolist_to_binary(Handled)).
18 changes: 18 additions & 0 deletions test/nif_path_test.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
-module(nif_path_test).
-include_lib("eunit/include/eunit.hrl").

'use the module priv directory when code priv isnt known_test'() ->
NifDir = paths:nif_dir({error, bad_name}, "module dir"),
?assertEqual("module dir", NifDir).

'use the code priv directory_test'() ->
NifDir = paths:nif_dir("./priv", "module dir"),
?assertEqual("./priv", NifDir).

'find the lib dir when in a bin escript directory_test'() ->
NifDir = paths:nif_dir("/dir/_build/default/bin/app/encurses/priv", "module dir"),
?assertEqual("/dir/_build/default/lib/encurses/priv", NifDir).

'find the lib dir when in a bin escript directory when code priv was not found_test'() ->
NifDir = paths:nif_dir({error, bad_name}, "/dir/_build/default/bin/app/encurses/priv"),
?assertEqual("/dir/_build/default/lib/encurses/priv", NifDir).

0 comments on commit 72b7558

Please sign in to comment.