Skip to content

Commit e5f3a69

Browse files
legendecasUlisesGascon
authored andcommitted
doc: fix node-api call example
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>
1 parent 5b70b68 commit e5f3a69

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

doc/api/n-api.md

+15-14
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ napi_value create_addon(napi_env env);
346346
// addon.c
347347
#include "addon.h"
348348

349-
#define NAPI_CALL(env, call) \
349+
#define NODE_API_CALL(env, call) \
350350
do { \
351351
napi_status status = (call); \
352352
if (status != napi_ok) { \
@@ -355,13 +355,14 @@ napi_value create_addon(napi_env env);
355355
const char* err_message = error_info->error_message; \
356356
bool is_pending; \
357357
napi_is_exception_pending((env), &is_pending); \
358+
/* If an exception is already pending, don't rethrow it */ \
358359
if (!is_pending) { \
359360
const char* message = (err_message == NULL) \
360361
? "empty error message" \
361362
: err_message; \
362363
napi_throw_error((env), NULL, message); \
363-
return NULL; \
364364
} \
365+
return NULL; \
365366
} \
366367
} while(0)
367368

@@ -373,20 +374,20 @@ DoSomethingUseful(napi_env env, napi_callback_info info) {
373374

374375
napi_value create_addon(napi_env env) {
375376
napi_value result;
376-
NAPI_CALL(env, napi_create_object(env, &result));
377+
NODE_API_CALL(env, napi_create_object(env, &result));
377378

378379
napi_value exported_function;
379-
NAPI_CALL(env, napi_create_function(env,
380-
"doSomethingUseful",
381-
NAPI_AUTO_LENGTH,
382-
DoSomethingUseful,
383-
NULL,
384-
&exported_function));
385-
386-
NAPI_CALL(env, napi_set_named_property(env,
387-
result,
388-
"doSomethingUseful",
389-
exported_function));
380+
NODE_API_CALL(env, napi_create_function(env,
381+
"doSomethingUseful",
382+
NAPI_AUTO_LENGTH,
383+
DoSomethingUseful,
384+
NULL,
385+
&exported_function));
386+
387+
NODE_API_CALL(env, napi_set_named_property(env,
388+
result,
389+
"doSomethingUseful",
390+
exported_function));
390391

391392
return result;
392393
}

0 commit comments

Comments
 (0)