From ca786c3734f6e23e34cd60f13e6bdaab033c5739 Mon Sep 17 00:00:00 2001 From: Taylor Woll Date: Fri, 24 Mar 2017 01:26:09 -0700 Subject: n-api: change napi_callback to return napi_value 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 Reviewed-By: Anna Henningsen --- test/addons-napi/1_hello_world/binding.c | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) (limited to 'test/addons-napi/1_hello_world') diff --git a/test/addons-napi/1_hello_world/binding.c b/test/addons-napi/1_hello_world/binding.c index 882508b9548..57bac37fa0b 100644 --- a/test/addons-napi/1_hello_world/binding.c +++ b/test/addons-napi/1_hello_world/binding.c @@ -1,22 +1,18 @@ #include +#include "../common.h" +#include -void Method(napi_env env, napi_callback_info info) { - napi_status status; +napi_value Method(napi_env env, napi_callback_info info) { napi_value world; - status = napi_create_string_utf8(env, "world", -1, &world); - if (status != napi_ok) return; - status = napi_set_return_value(env, info, world); - if (status != napi_ok) return; + const char* str = "world"; + size_t str_len = strlen(str); + NAPI_CALL(env, napi_create_string_utf8(env, str, str_len, &world)); + return world; } -#define DECLARE_NAPI_METHOD(name, func) \ - { name, func, 0, 0, 0, napi_default, 0 } - void Init(napi_env env, napi_value exports, napi_value module, void* priv) { - napi_status status; - napi_property_descriptor desc = DECLARE_NAPI_METHOD("hello", Method); - status = napi_define_properties(env, exports, 1, &desc); - if (status != napi_ok) return; + napi_property_descriptor desc = DECLARE_NAPI_PROPERTY("hello", Method); + NAPI_CALL_RETURN_VOID(env, napi_define_properties(env, exports, 1, &desc)); } NAPI_MODULE(addon, Init) -- cgit v1.2.3