Skip to content

Commit

Permalink
n-api: re-write test_make_callback
Browse files Browse the repository at this point in the history
This re-writes the test in C by dropping std::vector<napi_value> in
favour of a C99 variable length array, and by dropping the anonymous
namespace in favour of static function declarations.

PR-URL: #19448
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
  • Loading branch information
Gabriel Schulhof authored and targos committed Mar 24, 2018
1 parent 926214a commit fd8523f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#include <node_api.h>
#include "../common.h"
#include <vector>

namespace {
#define MAX_ARGUMENTS 10

static
napi_value MakeCallback(napi_env env, napi_callback_info info) {
const int kMaxArgs = 10;
size_t argc = kMaxArgs;
napi_value args[kMaxArgs];
size_t argc = MAX_ARGUMENTS;
size_t n;
napi_value args[MAX_ARGUMENTS];
// NOLINTNEXTLINE (readability/null_usage)
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));

Expand All @@ -16,9 +16,9 @@ napi_value MakeCallback(napi_env env, napi_callback_info info) {
napi_value recv = args[0];
napi_value func = args[1];

std::vector<napi_value> argv;
for (size_t n = 2; n < argc; n += 1) {
argv.push_back(args[n]);
napi_value argv[MAX_ARGUMENTS - 2];
for (n = 2; n < argc; n += 1) {
argv[n - 2] = args[n];
}

napi_valuetype func_type;
Expand All @@ -35,7 +35,7 @@ napi_value MakeCallback(napi_env env, napi_callback_info info) {
napi_value result;
if (func_type == napi_function) {
NAPI_CALL(env, napi_make_callback(
env, context, recv, func, argv.size(), argv.data(), &result));
env, context, recv, func, argc - 2, argv, &result));
} else {
NAPI_ASSERT(env, false, "Unexpected argument type");
}
Expand All @@ -45,6 +45,7 @@ napi_value MakeCallback(napi_env env, napi_callback_info info) {
return result;
}

static
napi_value Init(napi_env env, napi_value exports) {
napi_value fn;
NAPI_CALL(env, napi_create_function(
Expand All @@ -54,6 +55,4 @@ napi_value Init(napi_env env, napi_value exports) {
return exports;
}

} // anonymous namespace

NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)
2 changes: 1 addition & 1 deletion test/addons-napi/test_make_callback/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
'target_name': 'binding',
'defines': [ 'V8_DEPRECATION_WARNINGS=1' ],
'sources': [ 'binding.cc' ]
'sources': [ 'binding.c' ]
}
]
}

0 comments on commit fd8523f

Please sign in to comment.