summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTuncer Ayaz <tuncer.ayaz@gmail.com>2011-07-17 18:38:33 +0200
committerTuncer Ayaz <tuncer.ayaz@gmail.com>2011-07-17 18:38:33 +0200
commit734d30f96e2390330234e10b0157d6d1e3741a7e (patch)
tree5d060252114ffccfc0442d0904accecba6bdff95
parent4e0ab4065f3fdf81574d60d26e072386f6244c37 (diff)
Modernize basicnif template code
-rw-r--r--priv/templates/basicnif.erl26
1 files changed, 16 insertions, 10 deletions
diff --git a/priv/templates/basicnif.erl b/priv/templates/basicnif.erl
index 5f1ce11..342a5d3 100644
--- a/priv/templates/basicnif.erl
+++ b/priv/templates/basicnif.erl
@@ -5,24 +5,30 @@
-on_load(init/0).
+-define(nif_stub, nif_stub_error(?LINE)).
+nif_stub_error(Line) ->
+ erlang:nif_error({nif_not_loaded,module,?MODULE,line,Line}).
+
-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
-endif.
init() ->
- case code:priv_dir({{module}}) of
- {error, bad_name} ->
- SoName = filename:join("../priv", {{module}});
- Dir ->
- SoName = filename:join(Dir, {{module}})
- end,
- erlang:load_nif(SoName, 0).
+ PrivDir = case code:priv_dir(?MODULE) of
+ {error, bad_name} ->
+ EbinDir = filename:dirname(code:which(?MODULE)),
+ AppPath = filename:dirname(EbinDir),
+ filename:join(AppPath, "priv");
+ Path ->
+ Path
+ end,
+ erlang:load_nif(filename:join(PrivDir, ?MODULE), 0).
new() ->
- "NIF library not loaded".
+ ?nif_stub.
myfunction(Ref) ->
- "NIF library not loaded".
+ ?nif_stub.
%% ===================================================================
%% EUnit tests
@@ -31,6 +37,6 @@ myfunction(Ref) ->
basic_test() ->
{ok, Ref} = new(),
- ok = myfunction(Ref).
+ ?assertEqual(ok, myfunction(Ref)).
-endif.