diff options
author | Lukas Larsson <[email protected]> | 2015-12-17 14:14:54 +0100 |
---|---|---|
committer | Lukas Larsson <[email protected]> | 2016-03-29 14:57:11 +0200 |
commit | 1bd56e2b5141a3afdca4e854e9b667807bf4e2f3 (patch) | |
tree | 0171c0a573a08068d7155ac73bcd6a6d3fc941d6 /erts/emulator/test/nif_SUITE_data | |
parent | 348f3f2ee2d2707e30658c3600e05309ad0e72bf (diff) | |
download | otp-1bd56e2b5141a3afdca4e854e9b667807bf4e2f3.tar.gz otp-1bd56e2b5141a3afdca4e854e9b667807bf4e2f3.tar.bz2 otp-1bd56e2b5141a3afdca4e854e9b667807bf4e2f3.zip |
erts: Add enif_term_to_binary and enif_binary_to_term
Diffstat (limited to 'erts/emulator/test/nif_SUITE_data')
-rw-r--r-- | erts/emulator/test/nif_SUITE_data/nif_SUITE.c | 53 |
1 files changed, 52 insertions, 1 deletions
diff --git a/erts/emulator/test/nif_SUITE_data/nif_SUITE.c b/erts/emulator/test/nif_SUITE_data/nif_SUITE.c index 2ce0168788..9db7614405 100644 --- a/erts/emulator/test/nif_SUITE_data/nif_SUITE.c +++ b/erts/emulator/test/nif_SUITE_data/nif_SUITE.c @@ -2030,6 +2030,55 @@ static ERL_NIF_TERM is_port_alive(ErlNifEnv* env, int argc, const ERL_NIF_TERM a return atom_false; } +static ERL_NIF_TERM term_to_binary(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) +{ + ErlNifBinary bin; + ErlNifPid pid; + ErlNifEnv *msg_env = env; + ERL_NIF_TERM term; + + if (enif_get_local_pid(env, argv[1], &pid)) + msg_env = enif_alloc_env(); + + if (!enif_term_to_binary(msg_env, argv[0], &bin)) + return enif_make_badarg(env); + + term = enif_make_binary(msg_env, &bin); + + if (msg_env != env) { + enif_send(env, &pid, msg_env, term); + enif_free_env(msg_env); + return atom_true; + } else { + return term; + } +} + +static ERL_NIF_TERM binary_to_term(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) +{ + ErlNifBinary bin; + ERL_NIF_TERM term; + ErlNifPid pid; + ErlNifEnv *msg_env = env; + + if (enif_get_local_pid(env, argv[1], &pid)) + msg_env = enif_alloc_env(); + + if (!enif_inspect_binary(env, argv[0], &bin)) + return enif_make_badarg(env); + + if (!enif_binary_to_term(env, &bin, &term)) + return enif_make_badarg(env); + + if (msg_env != env) { + enif_send(env, &pid, msg_env, term); + enif_free_env(msg_env); + return atom_true; + } else { + return term; + } +} + static ErlNifFunc nif_funcs[] = { {"lib_version", 0, lib_version}, @@ -2107,7 +2156,9 @@ static ErlNifFunc nif_funcs[] = {"cpu_time", 0, cpu_time}, {"unique_integer_nif", 1, unique_integer}, {"is_process_alive_nif", 1, is_process_alive}, - {"is_port_alive_nif", 1, is_port_alive} + {"is_port_alive_nif", 1, is_port_alive}, + {"term_to_binary_nif", 2, term_to_binary}, + {"binary_to_term_nif", 2, binary_to_term} }; ERL_NIF_INIT(nif_SUITE,nif_funcs,load,reload,upgrade,unload) |