Files
node/test/addons-napi/test_function/test_function.c
T
Jason Ginchereau 2bbabb1f85 n-api: napi_get_cb_info should fill array
When the number of args requested is greater than the actual number of
args supplied to the function call, the remainder of the args array
should be filled in with `undefined` values. Because of this bug, the
remainder of the array was left uninitialized, which could cause a
crash.

Refer to the documentation for the `argv` parameter at
https://github.com/nodejs/node/blob/master/doc/api/n-api.md#napi_get_cb_info

PR-URL: https://github.com/nodejs/node/pull/12863
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
2017-05-09 15:12:15 -04:00

36 lines
1015 B
C

#include <node_api.h>
#include "../common.h"
napi_value Test(napi_env env, napi_callback_info info) {
size_t argc = 10;
napi_value args[10];
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
NAPI_ASSERT(env, argc > 0, "Wrong number of arguments");
napi_valuetype valuetype0;
NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0));
NAPI_ASSERT(env, valuetype0 == napi_function,
"Wrong type of arguments. Expects a function as first argument.");
napi_value* argv = args + 1;
argc = argc - 1;
napi_value global;
NAPI_CALL(env, napi_get_global(env, &global));
napi_value result;
NAPI_CALL(env, napi_call_function(env, global, args[0], argc, argv, &result));
return result;
}
void Init(napi_env env, napi_value exports, napi_value module, void* priv) {
napi_value fn;
NAPI_CALL_RETURN_VOID(env, napi_create_function(env, NULL, Test, NULL, &fn));
NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, exports, "Test", fn));
}
NAPI_MODULE(addon, Init)