Skip to content

doc: fix node-api call example #49395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions doc/api/n-api.md
Original file line number Diff line number Diff line change
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