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.

Backport-PR-URL: #19447
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 MylesBorins committed Apr 16, 2018
1 parent 49cd4fa commit c16a705
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
@@ -1,23 +1,23 @@
#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];
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));

NAPI_ASSERT(env, argc > 0, "Wrong number of arguments");

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 @@ -34,7 +34,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 @@ -44,6 +44,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 @@ -52,6 +53,4 @@ napi_value Init(napi_env env, napi_value exports) {
return exports;
}

} // namespace

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

0 comments on commit c16a705

Please sign in to comment.