Skip to content

Commit 4321cb2

Browse files
Gabriel Schulhoftargos
Gabriel Schulhof
authored andcommitted
doc: update js-native-api example
Update example that shows how to separate N-API code which is not Node.js-specific from code which defines a Node.js N-API addon. In its existing state the example uses the pattern ```C assert(napi_*() == napi_ok); ``` However, this would result in no N-API calls when building with `-DNDEBUG`. This change moves away from assert and uses a macro `NAPI_CALL()` which throws the string corresponding to the non-`napi_ok` status as a JS exception and short-circuits the binding by returning `NULL`. PR-URL: #28657 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 8ddf86b commit 4321cb2

File tree

1 file changed

+44
-14
lines changed

1 file changed

+44
-14
lines changed

doc/api/n-api.md

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -192,33 +192,63 @@ napi_value create_addon(napi_env env);
192192
```C
193193
// addon.c
194194
#include "addon.h"
195+
196+
#define NAPI_CALL(env, call) \
197+
do { \
198+
napi_status status = (call); \
199+
if (status != napi_ok) { \
200+
const napi_extended_error_info* error_info = NULL; \
201+
napi_get_last_error_info((env), &error_info); \
202+
bool is_pending; \
203+
napi_is_exception_pending((env), &is_pending); \
204+
if (!is_pending) { \
205+
const char* message = (error_info->error_message == NULL) \
206+
? "empty error message" \
207+
: error_info->error_message; \
208+
napi_throw_error((env), NULL, message); \
209+
return NULL; \
210+
} \
211+
} \
212+
} while(0)
213+
214+
static napi_value
215+
DoSomethingUseful(napi_env env, napi_callback_info info) {
216+
// Do something useful.
217+
return NULL;
218+
}
219+
195220
napi_value create_addon(napi_env env) {
196221
napi_value result;
197-
assert(napi_create_object(env, &result) == napi_ok);
222+
NAPI_CALL(env, napi_create_object(env, &result));
223+
198224
napi_value exported_function;
199-
assert(napi_create_function(env,
200-
"doSomethingUseful",
201-
NAPI_AUTO_LENGTH,
202-
DoSomethingUseful,
203-
NULL,
204-
&exported_function) == napi_ok);
205-
assert(napi_set_named_property(env,
206-
result,
207-
"doSomethingUseful",
208-
exported_function) == napi_ok);
225+
NAPI_CALL(env, napi_create_function(env,
226+
"doSomethingUseful",
227+
NAPI_AUTO_LENGTH,
228+
DoSomethingUseful,
229+
NULL,
230+
&exported_function));
231+
232+
NAPI_CALL(env, napi_set_named_property(env,
233+
result,
234+
"doSomethingUseful",
235+
exported_function));
236+
209237
return result;
210238
}
211239
```
212240

213241
```C
214242
// addon_node.c
215243
#include <node_api.h>
244+
#include "addon.h"
216245

217-
static napi_value Init(napi_env env, napi_value exports) {
246+
NAPI_MODULE_INIT() {
247+
// This function body is expected to return a `napi_value`.
248+
// The variables `napi_env env` and `napi_value exports` may be used within
249+
// the body, as they are provided by the definition of `NAPI_MODULE_INIT()`.
218250
return create_addon(env);
219251
}
220-
221-
NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)
222252
```
223253

224254
## Basic N-API Data Types

0 commit comments

Comments
 (0)