@@ -41,7 +41,8 @@ The documentation for N-API is structured as follows:
41
41
* [Working with JavaScript Properties][]
42
42
* [Working with JavaScript Functions][]
43
43
* [Object Wrap][]
44
- * [ Asynchronous Operations] [ ]
44
+ * [Simple Asynchronous Operations][]
45
+ * [Custom Asynchronous Operations][]
45
46
* [Promises][]
46
47
47
48
The N-API is a C API that ensures ABI stability across Node.js versions
@@ -263,7 +264,7 @@ It is intended only for logging purposes.
263
264
added: v8.0.0
264
265
-->
265
266
```C
266
- NAPI_EXTERN napi_status
267
+ napi_status
267
268
napi_get_last_error_info(napi_env env,
268
269
const napi_extended_error_info** result);
269
270
```
@@ -514,8 +515,8 @@ This API returns a JavaScript RangeError with the text provided.
514
515
added: v8.0.0
515
516
-->
516
517
```C
517
- NAPI_EXTERN napi_status napi_get_and_clear_last_exception (napi_env env,
518
- napi_value* result);
518
+ napi_status napi_get_and_clear_last_exception(napi_env env,
519
+ napi_value* result);
519
520
```
520
521
521
522
- `[in] env`: The environment that the API is invoked under.
@@ -530,7 +531,7 @@ This API returns true if an exception is pending.
530
531
added: v8.0.0
531
532
-->
532
533
```C
533
- NAPI_EXTERN napi_status napi_is_exception_pending(napi_env env, bool* result);
534
+ napi_status napi_is_exception_pending(napi_env env, bool* result);
534
535
```
535
536
536
537
- `[in] env`: The environment that the API is invoked under.
@@ -550,7 +551,7 @@ thrown to immediately terminate the process.
550
551
added: v8.2.0
551
552
-->
552
553
```C
553
- NAPI_EXTERN NAPI_NO_RETURN void napi_fatal_error (const char* location, const char* message);
554
+ NAPI_NO_RETURN void napi_fatal_error(const char* location, const char* message);
554
555
```
555
556
556
557
- `[in] location`: Optional location at which the error occurred.
@@ -717,10 +718,10 @@ reverse order from which they were created.
717
718
added: v8.0.0
718
719
-->
719
720
```C
720
- NAPI_EXTERN napi_status napi_escape_handle (napi_env env,
721
- napi_escapable_handle_scope scope,
722
- napi_value escapee,
723
- napi_value* result);
721
+ napi_status napi_escape_handle(napi_env env,
722
+ napi_escapable_handle_scope scope,
723
+ napi_value escapee,
724
+ napi_value* result);
724
725
```
725
726
726
727
- `[in] env`: The environment that the API is invoked under.
@@ -1468,10 +1469,10 @@ of the ECMAScript Language Specification.
1468
1469
added: v8.0.0
1469
1470
-->
1470
1471
```C
1471
- NAPI_EXTERN napi_status napi_create_string_latin1(napi_env env,
1472
- const char* str,
1473
- size_t length,
1474
- napi_value* result);
1472
+ napi_status napi_create_string_latin1(napi_env env,
1473
+ const char* str,
1474
+ size_t length,
1475
+ napi_value* result);
1475
1476
```
1476
1477
1477
1478
- `[in] env`: The environment that the API is invoked under.
@@ -1801,11 +1802,11 @@ JavaScript Number
1801
1802
added: v8.0.0
1802
1803
-->
1803
1804
```C
1804
- NAPI_EXTERN napi_status napi_get_value_string_latin1(napi_env env,
1805
- napi_value value,
1806
- char* buf,
1807
- size_t bufsize,
1808
- size_t* result)
1805
+ napi_status napi_get_value_string_latin1(napi_env env,
1806
+ napi_value value,
1807
+ char* buf,
1808
+ size_t bufsize,
1809
+ size_t* result)
1809
1810
```
1810
1811
1811
1812
- `[in] env`: The environment that the API is invoked under.
@@ -2780,8 +2781,8 @@ in as arguments to the function.
2780
2781
Returns `napi_ok` if the API succeeded.
2781
2782
2782
2783
This method allows a JavaScript function object to be called from a native
2783
- add-on. This is an primary mechanism of calling back *from* the add-on's
2784
- native code *into* JavaScript. For special cases like calling into JavaScript
2784
+ add-on. This is the primary mechanism of calling back *from* the add-on's
2785
+ native code *into* JavaScript. For the special case of calling into JavaScript
2785
2786
after an async operation, see [`napi_make_callback`][].
2786
2787
2787
2788
A sample use case might look as follows. Consider the following JavaScript
@@ -2993,39 +2994,6 @@ status = napi_new_instance(env, constructor, argc, argv, &value);
2993
2994
2994
2995
Returns `napi_ok` if the API succeeded.
2995
2996
2996
- ### * napi_make_callback*
2997
- <!-- YAML
2998
- added: v8.0.0
2999
- -->
3000
- ``` C
3001
- napi_status napi_make_callback (napi_env env,
3002
- napi_value recv,
3003
- napi_value func,
3004
- int argc,
3005
- const napi_value* argv,
3006
- napi_value* result)
3007
- ```
3008
-
3009
- - `[in] env`: The environment that the API is invoked under.
3010
- - `[in] recv`: The `this` object passed to the called function.
3011
- - `[in] func`: `napi_value` representing the JavaScript function
3012
- to be invoked.
3013
- - `[in] argc`: The count of elements in the `argv` array.
3014
- - `[in] argv`: Array of JavaScript values as `napi_value`
3015
- representing the arguments to the function.
3016
- - `[out] result`: `napi_value` representing the JavaScript object returned.
3017
-
3018
- Returns `napi_ok` if the API succeeded.
3019
-
3020
- This method allows a JavaScript function object to be called from a native
3021
- add-on. This API is similar to `napi_call_function`. However, it is used to call
3022
- *from* native code back *into* JavaScript *after* returning from an async
3023
- operation (when there is no other script on the stack). It is a fairly simple
3024
- wrapper around `node::MakeCallback`.
3025
-
3026
- For an example on how to use `napi_make_callback`, see the section on
3027
- [Asynchronous Operations][].
3028
-
3029
2997
## Object Wrap
3030
2998
3031
2999
N-API offers a way to "wrap" C++ classes and instances so that the class
@@ -3204,7 +3172,7 @@ restoring the JavaScript object's prototype chain. If a finalize callback was
3204
3172
associated with the wrapping, it will no longer be called when the JavaScript
3205
3173
object becomes garbage-collected.
3206
3174
3207
- ## Asynchronous Operations
3175
+ ## Simple Asynchronous Operations
3208
3176
3209
3177
Addon modules often need to leverage async helpers from libuv as part of their
3210
3178
implementation. This allows them to schedule work to be executed asynchronously
@@ -3240,8 +3208,8 @@ Once created the async worker can be queued
3240
3208
for execution using the [`napi_queue_async_work`][] function:
3241
3209
3242
3210
```C
3243
- NAPI_EXTERN napi_status napi_queue_async_work (napi_env env,
3244
- napi_async_work work);
3211
+ napi_status napi_queue_async_work(napi_env env,
3212
+ napi_async_work work);
3245
3213
```
3246
3214
3247
3215
[`napi_cancel_async_work`][] can be used if the work needs
@@ -3257,7 +3225,6 @@ callback invocation, even when it was cancelled.
3257
3225
added: v8.0.0
3258
3226
-->
3259
3227
```C
3260
- NAPI_EXTERN
3261
3228
napi_status napi_create_async_work(napi_env env,
3262
3229
napi_async_execute_callback execute,
3263
3230
napi_async_complete_callback complete,
@@ -3286,8 +3253,8 @@ required.
3286
3253
added: v8.0.0
3287
3254
-->
3288
3255
```C
3289
- NAPI_EXTERN napi_status napi_delete_async_work (napi_env env,
3290
- napi_async_work work);
3256
+ napi_status napi_delete_async_work(napi_env env,
3257
+ napi_async_work work);
3291
3258
```
3292
3259
3293
3260
- `[in] env`: The environment that the API is invoked under.
@@ -3302,8 +3269,8 @@ This API frees a previously allocated work object.
3302
3269
added: v8.0.0
3303
3270
-->
3304
3271
```C
3305
- NAPI_EXTERN napi_status napi_queue_async_work(napi_env env,
3306
- napi_async_work work);
3272
+ napi_status napi_queue_async_work(napi_env env,
3273
+ napi_async_work work);
3307
3274
```
3308
3275
3309
3276
- `[in] env`: The environment that the API is invoked under.
@@ -3319,8 +3286,8 @@ for execution.
3319
3286
added: v8.0.0
3320
3287
-->
3321
3288
```C
3322
- NAPI_EXTERN napi_status napi_cancel_async_work (napi_env env,
3323
- napi_async_work work);
3289
+ napi_status napi_cancel_async_work(napi_env env,
3290
+ napi_async_work work);
3324
3291
```
3325
3292
3326
3293
- `[in] env`: The environment that the API is invoked under.
@@ -3335,6 +3302,92 @@ the `complete` callback will be invoked with a status value of
3335
3302
`napi_cancelled`. The work should not be deleted before the `complete`
3336
3303
callback invocation, even if it has been successfully cancelled.
3337
3304
3305
+ ## Custom Asynchronous Operations
3306
+ The simple asynchronous work APIs above may not be appropriate for every
3307
+ scenario, because with those the async execution still happens on the main
3308
+ event loop. When using any other async mechanism, the following APIs are
3309
+ necessary to ensure an async operation is properly tracked by the runtime.
3310
+
3311
+ ### *napi_async_init**
3312
+ <!-- YAML
3313
+ added: REPLACEME
3314
+ -->
3315
+ ```C
3316
+ napi_status napi_async_init(napi_env env,
3317
+ napi_value async_resource,
3318
+ const char* async_resource_name,
3319
+ napi_async_context* result)
3320
+ ```
3321
+
3322
+ - `[in] env`: The environment that the API is invoked under.
3323
+ - `[in] async_resource`: An optional object associated with the async work
3324
+ that will be passed to possible async_hooks [`init` hooks][].
3325
+ - `[in] async_resource_name`: An identifier for the kind of resource that is
3326
+ being provided for diagnostic information exposed by the `async_hooks` API.
3327
+ - `[out] result`: The initialized async context.
3328
+
3329
+ Returns `napi_ok` if the API succeeded.
3330
+
3331
+ ### *napi_async_destroy**
3332
+ <!-- YAML
3333
+ added: REPLACEME
3334
+ -->
3335
+ ```C
3336
+ napi_status napi_async_destroy(napi_env env,
3337
+ napi_async_context async_context);
3338
+ ```
3339
+
3340
+ - `[in] env`: The environment that the API is invoked under.
3341
+ - `[in] async_context`: The async context to be destroyed.
3342
+
3343
+ Returns `napi_ok` if the API succeeded.
3344
+
3345
+ ### *napi_make_callback*
3346
+ <!-- YAML
3347
+ added: v8.0.0
3348
+ changes:
3349
+ - version: REPLACEME
3350
+ description: Added `async_context` parameter.
3351
+ -->
3352
+ ```C
3353
+ napi_status napi_make_callback(napi_env env,
3354
+ napi_async_context async_context,
3355
+ napi_value recv,
3356
+ napi_value func,
3357
+ int argc,
3358
+ const napi_value* argv,
3359
+ napi_value* result)
3360
+ ```
3361
+
3362
+ - `[in] env`: The environment that the API is invoked under.
3363
+ - `[in] async_context`: Context for the async operation that is
3364
+ invoking the callback. This should normally be a value previously
3365
+ obtained from [`napi_async_init`][]. However `NULL` is also allowed,
3366
+ which indicates the current async context (if any) is to be used
3367
+ for the callback.
3368
+ - `[in] recv`: The `this` object passed to the called function.
3369
+ - `[in] func`: `napi_value` representing the JavaScript function
3370
+ to be invoked.
3371
+ - `[in] argc`: The count of elements in the `argv` array.
3372
+ - `[in] argv`: Array of JavaScript values as `napi_value`
3373
+ representing the arguments to the function.
3374
+ - `[out] result`: `napi_value` representing the JavaScript object returned.
3375
+
3376
+ Returns `napi_ok` if the API succeeded.
3377
+
3378
+ This method allows a JavaScript function object to be called from a native
3379
+ add-on. This API is similar to `napi_call_function`. However, it is used to call
3380
+ *from* native code back *into* JavaScript *after* returning from an async
3381
+ operation (when there is no other script on the stack). It is a fairly simple
3382
+ wrapper around `node::MakeCallback`.
3383
+
3384
+ Note it is NOT necessary to use `napi_make_callback` from within a
3385
+ `napi_async_complete_callback`; in that situation the callback's async
3386
+ context has already been set up, so a direct call to `napi_call_function`
3387
+ is sufficient and appropriate. Use of the `napi_make_callback` function
3388
+ may be required when implementing custom async behavior that does not use
3389
+ `napi_create_async_work`.
3390
+
3338
3391
## Version Management
3339
3392
3340
3393
### napi_get_node_version
@@ -3350,7 +3403,6 @@ typedef struct {
3350
3403
const char* release;
3351
3404
} napi_node_version;
3352
3405
3353
- NAPI_EXTERN
3354
3406
napi_status napi_get_node_version(napi_env env,
3355
3407
const napi_node_version** version);
3356
3408
```
@@ -3371,8 +3423,8 @@ The returned buffer is statically allocated and does not need to be freed.
3371
3423
added: v8.0.0
3372
3424
-->
3373
3425
```C
3374
- NAPI_EXTERN napi_status napi_get_version (napi_env env,
3375
- uint32_t* result);
3426
+ napi_status napi_get_version(napi_env env,
3427
+ uint32_t* result);
3376
3428
```
3377
3429
3378
3430
- `[in] env`: The environment that the API is invoked under.
@@ -3455,9 +3507,9 @@ deferred = NULL;
3455
3507
added: REPLACEME
3456
3508
-->
3457
3509
```C
3458
- NAPI_EXTERN napi_status napi_create_promise (napi_env env,
3459
- napi_deferred* deferred,
3460
- napi_value* promise);
3510
+ napi_status napi_create_promise(napi_env env,
3511
+ napi_deferred* deferred,
3512
+ napi_value* promise);
3461
3513
```
3462
3514
3463
3515
- `[in] env`: The environment that the API is invoked under.
@@ -3475,9 +3527,9 @@ This API creates a deferred object and a JavaScript promise.
3475
3527
added: REPLACEME
3476
3528
-->
3477
3529
```C
3478
- NAPI_EXTERN napi_status napi_resolve_deferred(napi_env env,
3479
- napi_deferred deferred,
3480
- napi_value resolution);
3530
+ napi_status napi_resolve_deferred(napi_env env,
3531
+ napi_deferred deferred,
3532
+ napi_value resolution);
3481
3533
```
3482
3534
3483
3535
- `[in] env`: The environment that the API is invoked under.
@@ -3498,9 +3550,9 @@ The deferred object is freed upon successful completion.
3498
3550
added: REPLACEME
3499
3551
-->
3500
3552
```C
3501
- NAPI_EXTERN napi_status napi_reject_deferred (napi_env env,
3502
- napi_deferred deferred,
3503
- napi_value rejection);
3553
+ napi_status napi_reject_deferred(napi_env env,
3554
+ napi_deferred deferred,
3555
+ napi_value rejection);
3504
3556
```
3505
3557
3506
3558
- `[in] env`: The environment that the API is invoked under.
@@ -3521,9 +3573,9 @@ The deferred object is freed upon successful completion.
3521
3573
added: REPLACEME
3522
3574
-->
3523
3575
```C
3524
- NAPI_EXTERN napi_status napi_is_promise(napi_env env,
3525
- napi_value promise,
3526
- bool* is_promise);
3576
+ napi_status napi_is_promise(napi_env env,
3577
+ napi_value promise,
3578
+ bool* is_promise);
3527
3579
```
3528
3580
3529
3581
- `[in] env`: The environment that the API is invoked under.
@@ -3532,7 +3584,8 @@ NAPI_EXTERN napi_status napi_is_promise(napi_env env,
3532
3584
object - that is, a promise object created by the underlying engine.
3533
3585
3534
3586
[Promises]: #n_api_promises
3535
- [ Asynchronous Operations ] : #n_api_asynchronous_operations
3587
+ [Simple Asynchronous Operations]: #n_api_asynchronous_operations
3588
+ [Custom Asynchronous Operations]: #n_api_custom_asynchronous_operations
3536
3589
[Basic N-API Data Types]: #n_api_basic_n_api_data_types
3537
3590
[ECMAScript Language Specification]: https://tc39.github.io/ecma262/
3538
3591
[Error Handling]: #n_api_error_handling
0 commit comments