mirror of
https://github.com/nodejs/node.git
synced 2025-05-23 02:29:32 +00:00

Change `napi_callback` to return `napi_value` directly instead of requiring `napi_set_return_value`. When we invoke the callback, we will check the return value and call `SetReturnValue` ourselves. If the callback returns `NULL`, we don't set the return value in v8 which would have the same effect as previously if the callback didn't call `napi_set_return_value`. Seems to be a more natural way to handle return values from callbacks. As a consequence, remove `napi_set_return_value`. Add a `napi_value` to `napi_property_descriptor` to support string values which couldn't be passed in the `utf8name` parameter or symbols as property names. Class names, however, cannot be symbols so this `napi_value` must be a string type in that case. Remove all of the `napi_callback_info` helpers except for `napi_get_cb_info` and make all the parameters to `napi_get_cb_info` optional except for argc. Update all the test collateral according to these changes. Also add `test/addons-napi/common.h` to house some common macros for wrapping N-API calls and error handling. PR-URL: https://github.com/nodejs/node/pull/12248 Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
94 lines
2.5 KiB
C
94 lines
2.5 KiB
C
#include <node_api.h>
|
|
#include <string.h>
|
|
#include "../common.h"
|
|
|
|
napi_value Test(napi_env env, napi_callback_info info) {
|
|
size_t argc = 2;
|
|
napi_value args[2];
|
|
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
|
|
|
|
NAPI_ASSERT(env, argc >= 2, "Wrong number of arguments");
|
|
|
|
napi_valuetype valuetype0;
|
|
NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0));
|
|
|
|
NAPI_ASSERT(env, valuetype0 == napi_object,
|
|
"Wrong type of arguments. Expects an array as first argument.");
|
|
|
|
napi_valuetype valuetype1;
|
|
NAPI_CALL(env, napi_typeof(env, args[1], &valuetype1));
|
|
|
|
NAPI_ASSERT(env, valuetype1 == napi_number,
|
|
"Wrong type of arguments. Expects an integer as second argument.");
|
|
|
|
napi_value array = args[0];
|
|
int32_t index;
|
|
NAPI_CALL(env, napi_get_value_int32(env, args[1], &index));
|
|
|
|
NAPI_ASSERT(env, index >= 0, "Invalid index. Expects a positive integer.");
|
|
|
|
bool isarray;
|
|
NAPI_CALL(env, napi_is_array(env, array, &isarray));
|
|
|
|
if (!isarray) {
|
|
return NULL;
|
|
}
|
|
|
|
uint32_t length;
|
|
NAPI_CALL(env, napi_get_array_length(env, array, &length));
|
|
|
|
if ((uint32_t)index >= length) {
|
|
napi_value str;
|
|
const char* str_val = "Index out of bound!";
|
|
size_t str_len = strlen(str_val);
|
|
NAPI_CALL(env, napi_create_string_utf8(env, str_val, str_len, &str));
|
|
|
|
return str;
|
|
}
|
|
|
|
napi_value ret;
|
|
NAPI_CALL(env, napi_get_element(env, array, index, &ret));
|
|
|
|
return ret;
|
|
}
|
|
|
|
napi_value New(napi_env env, napi_callback_info info) {
|
|
size_t argc = 1;
|
|
napi_value args[1];
|
|
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
|
|
|
|
NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
|
|
|
|
napi_valuetype valuetype0;
|
|
NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0));
|
|
|
|
NAPI_ASSERT(env, valuetype0 == napi_object,
|
|
"Wrong type of arguments. Expects an array as first argument.");
|
|
|
|
napi_value ret;
|
|
NAPI_CALL(env, napi_create_array(env, &ret));
|
|
|
|
uint32_t i, length;
|
|
NAPI_CALL(env, napi_get_array_length(env, args[0], &length));
|
|
|
|
for (i = 0; i < length; i++) {
|
|
napi_value e;
|
|
NAPI_CALL(env, napi_get_element(env, args[0], i, &e));
|
|
NAPI_CALL(env, napi_set_element(env, ret, i, e));
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
void Init(napi_env env, napi_value exports, napi_value module, void* priv) {
|
|
napi_property_descriptor descriptors[] = {
|
|
DECLARE_NAPI_PROPERTY("Test", Test),
|
|
DECLARE_NAPI_PROPERTY("New", New),
|
|
};
|
|
|
|
NAPI_CALL_RETURN_VOID(env, napi_define_properties(
|
|
env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors));
|
|
}
|
|
|
|
NAPI_MODULE(addon, Init)
|