You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.7 KiB

  1. // This file is part of Jiffy released under the MIT license.
  2. // See the LICENSE file for more information.
  3. #include "jiffy.h"
  4. static int
  5. load(ErlNifEnv* env, void** priv, ERL_NIF_TERM info)
  6. {
  7. jiffy_st* st = enif_alloc(sizeof(jiffy_st));
  8. if(st == NULL) {
  9. return 1;
  10. }
  11. st->atom_ok = make_atom(env, "ok");
  12. st->atom_error = make_atom(env, "error");
  13. st->atom_null = make_atom(env, "null");
  14. st->atom_true = make_atom(env, "true");
  15. st->atom_false = make_atom(env, "false");
  16. st->atom_bignum = make_atom(env, "bignum");
  17. st->atom_bignum_e = make_atom(env, "bignum_e");
  18. st->atom_bigdbl = make_atom(env, "bigdbl");
  19. st->atom_partial = make_atom(env, "partial");
  20. st->atom_uescape = make_atom(env, "uescape");
  21. st->atom_pretty = make_atom(env, "pretty");
  22. st->atom_force_utf8 = make_atom(env, "force_utf8");
  23. // Markers used in encoding
  24. st->ref_object = make_atom(env, "$object_ref$");
  25. st->ref_array = make_atom(env, "$array_ref$");
  26. st->res_dec = enif_open_resource_type(
  27. env,
  28. NULL,
  29. "decoder",
  30. dec_destroy,
  31. ERL_NIF_RT_CREATE | ERL_NIF_RT_TAKEOVER,
  32. NULL
  33. );
  34. *priv = (void*) st;
  35. return 0;
  36. }
  37. static int
  38. reload(ErlNifEnv* env, void** priv, ERL_NIF_TERM info)
  39. {
  40. return 0;
  41. }
  42. static int
  43. upgrade(ErlNifEnv* env, void** priv, void** old_priv, ERL_NIF_TERM info)
  44. {
  45. return load(env, priv, info);
  46. }
  47. static void
  48. unload(ErlNifEnv* env, void* priv)
  49. {
  50. enif_free(priv);
  51. return;
  52. }
  53. static ErlNifFunc funcs[] =
  54. {
  55. {"nif_decode_init", 1, decode_init},
  56. {"nif_decode_iter", 4, decode_iter},
  57. {"nif_encode", 2, encode}
  58. };
  59. ERL_NIF_INIT(jiffy, funcs, &load, &reload, &upgrade, &unload);