From e26b89e8be2ae15b5ddebd1954ef59684862bbbf Mon Sep 17 00:00:00 2001 From: Chengzhong Wu Date: Fri, 1 Sep 2023 00:29:19 +0800 Subject: [PATCH] doc: fix node-api call example MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The `return` statement should not be enclosed in a nested conditional branch. PR-URL: https://github.com/nodejs/node/pull/49395 Reviewed-By: Tobias Nießen Reviewed-By: Daeyeon Jeong Reviewed-By: Harshitha K P Reviewed-By: Luigi Pinca Reviewed-By: Michael Dawson --- doc/api/n-api.md | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/doc/api/n-api.md b/doc/api/n-api.md index 8fd814c75ad66c..f1dd92b8bc66d6 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -346,7 +346,7 @@ napi_value create_addon(napi_env env); // addon.c #include "addon.h" -#define NAPI_CALL(env, call) \ +#define NODE_API_CALL(env, call) \ do { \ napi_status status = (call); \ if (status != napi_ok) { \ @@ -355,13 +355,14 @@ napi_value create_addon(napi_env env); const char* err_message = error_info->error_message; \ bool is_pending; \ napi_is_exception_pending((env), &is_pending); \ + /* If an exception is already pending, don't rethrow it */ \ if (!is_pending) { \ const char* message = (err_message == NULL) \ ? "empty error message" \ : err_message; \ napi_throw_error((env), NULL, message); \ - return NULL; \ } \ + return NULL; \ } \ } while(0) @@ -373,20 +374,20 @@ DoSomethingUseful(napi_env env, napi_callback_info info) { napi_value create_addon(napi_env env) { napi_value result; - NAPI_CALL(env, napi_create_object(env, &result)); + NODE_API_CALL(env, napi_create_object(env, &result)); napi_value exported_function; - NAPI_CALL(env, napi_create_function(env, - "doSomethingUseful", - NAPI_AUTO_LENGTH, - DoSomethingUseful, - NULL, - &exported_function)); - - NAPI_CALL(env, napi_set_named_property(env, - result, - "doSomethingUseful", - exported_function)); + NODE_API_CALL(env, napi_create_function(env, + "doSomethingUseful", + NAPI_AUTO_LENGTH, + DoSomethingUseful, + NULL, + &exported_function)); + + NODE_API_CALL(env, napi_set_named_property(env, + result, + "doSomethingUseful", + exported_function)); return result; }