Skip to content

Commit

Permalink
Merge pull request #38 from hrubi/nif-loading
Browse files Browse the repository at this point in the history
Load the NIFs in the on_load callback
  • Loading branch information
badlop committed Oct 13, 2021
2 parents 0870e08 + 47df84d commit b41b2d2
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 12 deletions.
7 changes: 6 additions & 1 deletion c_src/fxml.c
Expand Up @@ -53,6 +53,11 @@ static int load(ErlNifEnv* env, void** priv, ERL_NIF_TERM load_info)
return 0;
}

static int upgrade(ErlNifEnv* env, void** priv, void** old_priv, ERL_NIF_TERM load_info)
{
return load(env, priv, load_info);
}

static struct buf *init_buf(ErlNifEnv* env)
{
struct buf *rbuf = ENIF_ALLOC(sizeof(struct buf));
Expand Down Expand Up @@ -299,4 +304,4 @@ static ErlNifFunc nif_funcs[] =
{"element_to_header", 1, element_to_header}
};

ERL_NIF_INIT(fxml, nif_funcs, load, NULL, NULL, NULL)
ERL_NIF_INIT(fxml, nif_funcs, load, NULL, upgrade, NULL)
7 changes: 6 additions & 1 deletion c_src/fxml_stream.c
Expand Up @@ -810,6 +810,11 @@ static int load(ErlNifEnv* env, void** priv, ERL_NIF_TERM load_info)
return 0;
}

static int upgrade(ErlNifEnv* env, void** priv, void** old_priv, ERL_NIF_TERM load_info)
{
return load(env, priv, load_info);
}

static ERL_NIF_TERM make_parse_error(ErlNifEnv *env, XML_Parser parser)
{
enum XML_Error errcode = XML_GetErrorCode(parser);
Expand Down Expand Up @@ -1057,4 +1062,4 @@ static ErlNifFunc nif_funcs[] =
{"change_callback_pid", 2, change_callback_pid_nif}
};

ERL_NIF_INIT(fxml_stream, nif_funcs, load, NULL, NULL, NULL)
ERL_NIF_INIT(fxml_stream, nif_funcs, load, NULL, upgrade, NULL)
9 changes: 1 addition & 8 deletions src/fast_xml.erl
Expand Up @@ -49,14 +49,7 @@
%% @end
%%--------------------------------------------------------------------
start(_StartType, _StartArgs) ->
case {fxml:load_nif(), fxml_stream:load_nif()} of
{ok, ok} ->
fxml_sup:start_link();
{{error,_} = E1, _} ->
E1;
{_, {error,_} = E2} ->
E2
end.
fxml_sup:start_link().

%%--------------------------------------------------------------------
%% @private
Expand Down
5 changes: 5 additions & 0 deletions src/fxml.erl
Expand Up @@ -27,6 +27,8 @@

-compile(no_native).

-on_load(init/0).

-export([element_to_binary/1, element_to_header/1,
crypt/1, remove_cdata/1,
remove_subtags/3, get_cdata/1, get_tag_cdata/1,
Expand All @@ -41,6 +43,9 @@
-include("fxml.hrl").
-export_type([xmlel/0]).

init() ->
ok = load_nif().

%% Replace element_to_binary/1 with NIF
load_nif() ->
SOPath = p1_nif_utils:get_so_path(?MODULE, [fast_xml], "fxml"),
Expand Down
5 changes: 5 additions & 0 deletions src/fxml_stream.erl
Expand Up @@ -27,6 +27,8 @@

-compile(no_native).

-on_load(init/0).

-export([new/1, new/2, new/3, parse/2, close/1, reset/1,
change_callback_pid/2, parse_element/1]).

Expand All @@ -53,6 +55,9 @@

-export_type([xml_stream_state/0, xml_stream_el/0]).

init() ->
ok = load_nif().

load_nif() ->
SOPath = p1_nif_utils:get_so_path(?MODULE, [fast_xml], "fxml_stream"),
load_nif(SOPath).
Expand Down
6 changes: 4 additions & 2 deletions test/fxml_test.erl
Expand Up @@ -39,8 +39,10 @@ close(State) ->
?assertEqual(true, fxml_stream:close(State)).

start_test() ->
?assertEqual(ok, fxml:load_nif(p1_nif_utils:get_so_path(fxml, [], "fxml"))),
?assertEqual(ok, fxml_stream:load_nif(p1_nif_utils:get_so_path(fxml_stream, [], "fxml_stream"))).
?assertMatch({ok, _}, application:ensure_all_started(fast_xml)),
?assertMatch(ok, application:stop(fast_xml)),
?assertMatch({ok, _}, application:ensure_all_started(fast_xml)),
?assertMatch(ok, application:stop(fast_xml)).

tag_test() ->
?assertEqual(#xmlel{name = <<"root">>},
Expand Down

0 comments on commit b41b2d2

Please sign in to comment.