Skip to content

Commit

Permalink
doc: fix node-api call example
Browse files Browse the repository at this point in the history
The `return` statement should not be enclosed in a nested conditional
branch.

PR-URL: #49395
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Daeyeon Jeong <daeyeon.dev@gmail.com>
Reviewed-By: Harshitha K P <harshitha014@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Michael Dawson <midawson@redhat.com>
  • Loading branch information
legendecas authored and UlisesGascon committed Sep 10, 2023
1 parent 5b70b68 commit e5f3a69
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions doc/api/n-api.md
Expand Up @@ -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) { \
Expand All @@ -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)

Expand All @@ -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;
}
Expand Down

0 comments on commit e5f3a69

Please sign in to comment.