function_args.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include "napi.h"
  2. static napi_value NoArgFunction_Core(napi_env env, napi_callback_info info) {
  3. (void) env;
  4. (void) info;
  5. return nullptr;
  6. }
  7. static napi_value OneArgFunction_Core(napi_env env, napi_callback_info info) {
  8. size_t argc = 1;
  9. napi_value argv;
  10. if (napi_get_cb_info(env, info, &argc, &argv, nullptr, nullptr) != napi_ok) {
  11. return nullptr;
  12. }
  13. (void) argv;
  14. return nullptr;
  15. }
  16. static napi_value TwoArgFunction_Core(napi_env env, napi_callback_info info) {
  17. size_t argc = 2;
  18. napi_value argv[2];
  19. if (napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr) != napi_ok) {
  20. return nullptr;
  21. }
  22. (void) argv[0];
  23. (void) argv[1];
  24. return nullptr;
  25. }
  26. static napi_value ThreeArgFunction_Core(napi_env env, napi_callback_info info) {
  27. size_t argc = 3;
  28. napi_value argv[3];
  29. if (napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr) != napi_ok) {
  30. return nullptr;
  31. }
  32. (void) argv[0];
  33. (void) argv[1];
  34. (void) argv[2];
  35. return nullptr;
  36. }
  37. static napi_value FourArgFunction_Core(napi_env env, napi_callback_info info) {
  38. size_t argc = 4;
  39. napi_value argv[4];
  40. if (napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr) != napi_ok) {
  41. return nullptr;
  42. }
  43. (void) argv[0];
  44. (void) argv[1];
  45. (void) argv[2];
  46. (void) argv[3];
  47. return nullptr;
  48. }
  49. static void NoArgFunction(const Napi::CallbackInfo& info) {
  50. (void) info;
  51. }
  52. static void OneArgFunction(const Napi::CallbackInfo& info) {
  53. Napi::Value argv0 = info[0]; (void) argv0;
  54. }
  55. static void TwoArgFunction(const Napi::CallbackInfo& info) {
  56. Napi::Value argv0 = info[0]; (void) argv0;
  57. Napi::Value argv1 = info[1]; (void) argv1;
  58. }
  59. static void ThreeArgFunction(const Napi::CallbackInfo& info) {
  60. Napi::Value argv0 = info[0]; (void) argv0;
  61. Napi::Value argv1 = info[1]; (void) argv1;
  62. Napi::Value argv2 = info[2]; (void) argv2;
  63. }
  64. static void FourArgFunction(const Napi::CallbackInfo& info) {
  65. Napi::Value argv0 = info[0]; (void) argv0;
  66. Napi::Value argv1 = info[1]; (void) argv1;
  67. Napi::Value argv2 = info[2]; (void) argv2;
  68. Napi::Value argv3 = info[3]; (void) argv3;
  69. }
  70. static Napi::Object Init(Napi::Env env, Napi::Object exports) {
  71. napi_value no_arg_function, one_arg_function, two_arg_function,
  72. three_arg_function, four_arg_function;
  73. napi_status status;
  74. status = napi_create_function(env,
  75. "noArgFunction",
  76. NAPI_AUTO_LENGTH,
  77. NoArgFunction_Core,
  78. nullptr,
  79. &no_arg_function);
  80. NAPI_THROW_IF_FAILED(env, status, Napi::Object());
  81. status = napi_create_function(env,
  82. "oneArgFunction",
  83. NAPI_AUTO_LENGTH,
  84. OneArgFunction_Core,
  85. nullptr,
  86. &one_arg_function);
  87. NAPI_THROW_IF_FAILED(env, status, Napi::Object());
  88. status = napi_create_function(env,
  89. "twoArgFunction",
  90. NAPI_AUTO_LENGTH,
  91. TwoArgFunction_Core,
  92. nullptr,
  93. &two_arg_function);
  94. NAPI_THROW_IF_FAILED(env, status, Napi::Object());
  95. status = napi_create_function(env,
  96. "threeArgFunction",
  97. NAPI_AUTO_LENGTH,
  98. ThreeArgFunction_Core,
  99. nullptr,
  100. &three_arg_function);
  101. NAPI_THROW_IF_FAILED(env, status, Napi::Object());
  102. status = napi_create_function(env,
  103. "fourArgFunction",
  104. NAPI_AUTO_LENGTH,
  105. FourArgFunction_Core,
  106. nullptr,
  107. &four_arg_function);
  108. NAPI_THROW_IF_FAILED(env, status, Napi::Object());
  109. Napi::Object core = Napi::Object::New(env);
  110. core["noArgFunction"] = Napi::Value(env, no_arg_function);
  111. core["oneArgFunction"] = Napi::Value(env, one_arg_function);
  112. core["twoArgFunction"] = Napi::Value(env, two_arg_function);
  113. core["threeArgFunction"] = Napi::Value(env, three_arg_function);
  114. core["fourArgFunction"] = Napi::Value(env, four_arg_function);
  115. exports["core"] = core;
  116. Napi::Object cplusplus = Napi::Object::New(env);
  117. cplusplus["noArgFunction"] = Napi::Function::New(env, NoArgFunction);
  118. cplusplus["oneArgFunction"] = Napi::Function::New(env, OneArgFunction);
  119. cplusplus["twoArgFunction"] = Napi::Function::New(env, TwoArgFunction);
  120. cplusplus["threeArgFunction"] = Napi::Function::New(env, ThreeArgFunction);
  121. cplusplus["fourArgFunction"] = Napi::Function::New(env, FourArgFunction);
  122. exports["cplusplus"] = cplusplus;
  123. Napi::Object templated = Napi::Object::New(env);
  124. templated["noArgFunction"] = Napi::Function::New<NoArgFunction>(env);
  125. templated["oneArgFunction"] = Napi::Function::New<OneArgFunction>(env);
  126. templated["twoArgFunction"] = Napi::Function::New<TwoArgFunction>(env);
  127. templated["threeArgFunction"] = Napi::Function::New<ThreeArgFunction>(env);
  128. templated["fourArgFunction"] = Napi::Function::New<FourArgFunction>(env);
  129. exports["templated"] = templated;
  130. return exports;
  131. }
  132. NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init)