@@ -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
* [Script Execution][]
47
48
@@ -264,7 +265,7 @@ It is intended only for logging purposes.
264
265
added: v8.0.0
265
266
-->
266
267
```C
267
- NAPI_EXTERN napi_status
268
+ napi_status
268
269
napi_get_last_error_info(napi_env env,
269
270
const napi_extended_error_info** result);
270
271
```
@@ -515,8 +516,8 @@ This API returns a JavaScript RangeError with the text provided.
515
516
added: v8.0.0
516
517
-->
517
518
```C
518
- NAPI_EXTERN napi_status napi_get_and_clear_last_exception(napi_env env,
519
- napi_value* result);
519
+ napi_status napi_get_and_clear_last_exception(napi_env env,
520
+ napi_value* result);
520
521
```
521
522
522
523
- `[in] env`: The environment that the API is invoked under.
@@ -531,7 +532,7 @@ This API returns true if an exception is pending.
531
532
added: v8.0.0
532
533
-->
533
534
```C
534
- NAPI_EXTERN napi_status napi_is_exception_pending(napi_env env, bool* result);
535
+ napi_status napi_is_exception_pending(napi_env env, bool* result);
535
536
```
536
537
537
538
- `[in] env`: The environment that the API is invoked under.
@@ -551,7 +552,7 @@ thrown to immediately terminate the process.
551
552
added: v8.2.0
552
553
-->
553
554
```C
554
- NAPI_EXTERN NAPI_NO_RETURN void napi_fatal_error(const char* location, const char* message);
555
+ NAPI_NO_RETURN void napi_fatal_error(const char* location, const char* message);
555
556
```
556
557
557
558
- `[in] location`: Optional location at which the error occurred.
@@ -718,10 +719,10 @@ reverse order from which they were created.
718
719
added: v8.0.0
719
720
-->
720
721
```C
721
- NAPI_EXTERN napi_status napi_escape_handle(napi_env env,
722
- napi_escapable_handle_scope scope,
723
- napi_value escapee,
724
- napi_value* result);
722
+ napi_status napi_escape_handle(napi_env env,
723
+ napi_escapable_handle_scope scope,
724
+ napi_value escapee,
725
+ napi_value* result);
725
726
```
726
727
727
728
- `[in] env`: The environment that the API is invoked under.
@@ -1478,10 +1479,10 @@ of the ECMAScript Language Specification.
1478
1479
added: v8.0.0
1479
1480
-->
1480
1481
```C
1481
- NAPI_EXTERN napi_status napi_create_string_latin1(napi_env env,
1482
- const char* str,
1483
- size_t length,
1484
- napi_value* result);
1482
+ napi_status napi_create_string_latin1(napi_env env,
1483
+ const char* str,
1484
+ size_t length,
1485
+ napi_value* result);
1485
1486
```
1486
1487
1487
1488
- `[in] env`: The environment that the API is invoked under.
@@ -1811,11 +1812,11 @@ JavaScript Number
1811
1812
added: v8.0.0
1812
1813
-->
1813
1814
```C
1814
- NAPI_EXTERN napi_status napi_get_value_string_latin1(napi_env env,
1815
- napi_value value,
1816
- char* buf,
1817
- size_t bufsize,
1818
- size_t* result)
1815
+ napi_status napi_get_value_string_latin1(napi_env env,
1816
+ napi_value value,
1817
+ char* buf,
1818
+ size_t bufsize,
1819
+ size_t* result)
1819
1820
```
1820
1821
1821
1822
- `[in] env`: The environment that the API is invoked under.
@@ -2790,8 +2791,8 @@ in as arguments to the function.
2790
2791
Returns `napi_ok` if the API succeeded.
2791
2792
2792
2793
This method allows a JavaScript function object to be called from a native
2793
- add-on. This is an primary mechanism of calling back *from* the add-on's
2794
- native code *into* JavaScript. For special cases like calling into JavaScript
2794
+ add-on. This is the primary mechanism of calling back *from* the add-on's
2795
+ native code *into* JavaScript. For the special case of calling into JavaScript
2795
2796
after an async operation, see [`napi_make_callback`][].
2796
2797
2797
2798
A sample use case might look as follows. Consider the following JavaScript
@@ -3003,39 +3004,6 @@ status = napi_new_instance(env, constructor, argc, argv, &value);
3003
3004
3004
3005
Returns `napi_ok` if the API succeeded.
3005
3006
3006
- ### *napi_make_callback*
3007
- <!-- YAML
3008
- added: v8.0.0
3009
- -->
3010
- ```C
3011
- napi_status napi_make_callback(napi_env env,
3012
- napi_value recv,
3013
- napi_value func,
3014
- int argc,
3015
- const napi_value* argv,
3016
- napi_value* result)
3017
- ```
3018
-
3019
- - `[in] env`: The environment that the API is invoked under.
3020
- - `[in] recv`: The `this` object passed to the called function.
3021
- - `[in] func`: `napi_value` representing the JavaScript function
3022
- to be invoked.
3023
- - `[in] argc`: The count of elements in the `argv` array.
3024
- - `[in] argv`: Array of JavaScript values as `napi_value`
3025
- representing the arguments to the function.
3026
- - `[out] result`: `napi_value` representing the JavaScript object returned.
3027
-
3028
- Returns `napi_ok` if the API succeeded.
3029
-
3030
- This method allows a JavaScript function object to be called from a native
3031
- add-on. This API is similar to `napi_call_function`. However, it is used to call
3032
- *from* native code back *into* JavaScript *after* returning from an async
3033
- operation (when there is no other script on the stack). It is a fairly simple
3034
- wrapper around `node::MakeCallback`.
3035
-
3036
- For an example on how to use `napi_make_callback`, see the section on
3037
- [Asynchronous Operations][].
3038
-
3039
3007
## Object Wrap
3040
3008
3041
3009
N-API offers a way to "wrap" C++ classes and instances so that the class
@@ -3214,7 +3182,7 @@ restoring the JavaScript object's prototype chain. If a finalize callback was
3214
3182
associated with the wrapping, it will no longer be called when the JavaScript
3215
3183
object becomes garbage-collected.
3216
3184
3217
- ## Asynchronous Operations
3185
+ ## Simple Asynchronous Operations
3218
3186
3219
3187
Addon modules often need to leverage async helpers from libuv as part of their
3220
3188
implementation. This allows them to schedule work to be executed asynchronously
@@ -3250,8 +3218,8 @@ Once created the async worker can be queued
3250
3218
for execution using the [`napi_queue_async_work`][] function:
3251
3219
3252
3220
```C
3253
- NAPI_EXTERN napi_status napi_queue_async_work(napi_env env,
3254
- napi_async_work work);
3221
+ napi_status napi_queue_async_work(napi_env env,
3222
+ napi_async_work work);
3255
3223
```
3256
3224
3257
3225
[`napi_cancel_async_work`][] can be used if the work needs
@@ -3271,7 +3239,6 @@ changes:
3271
3239
description: Added `async_resource` and `async_resource_name` parameters.
3272
3240
-->
3273
3241
```C
3274
- NAPI_EXTERN
3275
3242
napi_status napi_create_async_work(napi_env env,
3276
3243
napi_value async_resource,
3277
3244
napi_value async_resource_name,
@@ -3314,8 +3281,8 @@ for more information.
3314
3281
added: v8.0.0
3315
3282
-->
3316
3283
```C
3317
- NAPI_EXTERN napi_status napi_delete_async_work(napi_env env,
3318
- napi_async_work work);
3284
+ napi_status napi_delete_async_work(napi_env env,
3285
+ napi_async_work work);
3319
3286
```
3320
3287
3321
3288
- `[in] env`: The environment that the API is invoked under.
@@ -3330,8 +3297,8 @@ This API frees a previously allocated work object.
3330
3297
added: v8.0.0
3331
3298
-->
3332
3299
```C
3333
- NAPI_EXTERN napi_status napi_queue_async_work(napi_env env,
3334
- napi_async_work work);
3300
+ napi_status napi_queue_async_work(napi_env env,
3301
+ napi_async_work work);
3335
3302
```
3336
3303
3337
3304
- `[in] env`: The environment that the API is invoked under.
@@ -3347,8 +3314,8 @@ for execution.
3347
3314
added: v8.0.0
3348
3315
-->
3349
3316
```C
3350
- NAPI_EXTERN napi_status napi_cancel_async_work(napi_env env,
3351
- napi_async_work work);
3317
+ napi_status napi_cancel_async_work(napi_env env,
3318
+ napi_async_work work);
3352
3319
```
3353
3320
3354
3321
- `[in] env`: The environment that the API is invoked under.
@@ -3363,6 +3330,93 @@ the `complete` callback will be invoked with a status value of
3363
3330
`napi_cancelled`. The work should not be deleted before the `complete`
3364
3331
callback invocation, even if it has been successfully cancelled.
3365
3332
3333
+ ## Custom Asynchronous Operations
3334
+ The simple asynchronous work APIs above may not be appropriate for every
3335
+ scenario, because with those the async execution still happens on the main
3336
+ event loop. When using any other async mechanism, the following APIs are
3337
+ necessary to ensure an async operation is properly tracked by the runtime.
3338
+
3339
+ ### *napi_async_init**
3340
+ <!-- YAML
3341
+ added: REPLACEME
3342
+ -->
3343
+ ```C
3344
+ napi_status napi_async_init(napi_env env,
3345
+ napi_value async_resource,
3346
+ napi_value async_resource_name,
3347
+ napi_async_context* result)
3348
+ ```
3349
+
3350
+ - `[in] env`: The environment that the API is invoked under.
3351
+ - `[in] async_resource`: An optional object associated with the async work
3352
+ that will be passed to possible `async_hooks` [`init` hooks][].
3353
+ - `[in] async_resource_name`: Required identifier for the kind of resource
3354
+ that is being provided for diagnostic information exposed by the
3355
+ `async_hooks` API.
3356
+ - `[out] result`: The initialized async context.
3357
+
3358
+ Returns `napi_ok` if the API succeeded.
3359
+
3360
+ ### *napi_async_destroy**
3361
+ <!-- YAML
3362
+ added: REPLACEME
3363
+ -->
3364
+ ```C
3365
+ napi_status napi_async_destroy(napi_env env,
3366
+ napi_async_context async_context);
3367
+ ```
3368
+
3369
+ - `[in] env`: The environment that the API is invoked under.
3370
+ - `[in] async_context`: The async context to be destroyed.
3371
+
3372
+ Returns `napi_ok` if the API succeeded.
3373
+
3374
+ ### *napi_make_callback*
3375
+ <!-- YAML
3376
+ added: v8.0.0
3377
+ changes:
3378
+ - version: REPLACEME
3379
+ description: Added `async_context` parameter.
3380
+ -->
3381
+ ```C
3382
+ napi_status napi_make_callback(napi_env env,
3383
+ napi_async_context async_context,
3384
+ napi_value recv,
3385
+ napi_value func,
3386
+ int argc,
3387
+ const napi_value* argv,
3388
+ napi_value* result)
3389
+ ```
3390
+
3391
+ - `[in] env`: The environment that the API is invoked under.
3392
+ - `[in] async_context`: Context for the async operation that is
3393
+ invoking the callback. This should normally be a value previously
3394
+ obtained from [`napi_async_init`][]. However `NULL` is also allowed,
3395
+ which indicates the current async context (if any) is to be used
3396
+ for the callback.
3397
+ - `[in] recv`: The `this` object passed to the called function.
3398
+ - `[in] func`: `napi_value` representing the JavaScript function
3399
+ to be invoked.
3400
+ - `[in] argc`: The count of elements in the `argv` array.
3401
+ - `[in] argv`: Array of JavaScript values as `napi_value`
3402
+ representing the arguments to the function.
3403
+ - `[out] result`: `napi_value` representing the JavaScript object returned.
3404
+
3405
+ Returns `napi_ok` if the API succeeded.
3406
+
3407
+ This method allows a JavaScript function object to be called from a native
3408
+ add-on. This API is similar to `napi_call_function`. However, it is used to call
3409
+ *from* native code back *into* JavaScript *after* returning from an async
3410
+ operation (when there is no other script on the stack). It is a fairly simple
3411
+ wrapper around `node::MakeCallback`.
3412
+
3413
+ Note it is *not* necessary to use `napi_make_callback` from within a
3414
+ `napi_async_complete_callback`; in that situation the callback's async
3415
+ context has already been set up, so a direct call to `napi_call_function`
3416
+ is sufficient and appropriate. Use of the `napi_make_callback` function
3417
+ may be required when implementing custom async behavior that does not use
3418
+ `napi_create_async_work`.
3419
+
3366
3420
## Version Management
3367
3421
3368
3422
### napi_get_node_version
@@ -3378,7 +3432,6 @@ typedef struct {
3378
3432
const char* release;
3379
3433
} napi_node_version;
3380
3434
3381
- NAPI_EXTERN
3382
3435
napi_status napi_get_node_version(napi_env env,
3383
3436
const napi_node_version** version);
3384
3437
```
@@ -3399,8 +3452,8 @@ The returned buffer is statically allocated and does not need to be freed.
3399
3452
added: v8.0.0
3400
3453
-->
3401
3454
```C
3402
- NAPI_EXTERN napi_status napi_get_version(napi_env env,
3403
- uint32_t* result);
3455
+ napi_status napi_get_version(napi_env env,
3456
+ uint32_t* result);
3404
3457
```
3405
3458
3406
3459
- `[in] env`: The environment that the API is invoked under.
@@ -3508,9 +3561,9 @@ deferred = NULL;
3508
3561
added: v8.5.0
3509
3562
-->
3510
3563
```C
3511
- NAPI_EXTERN napi_status napi_create_promise(napi_env env,
3512
- napi_deferred* deferred,
3513
- napi_value* promise);
3564
+ napi_status napi_create_promise(napi_env env,
3565
+ napi_deferred* deferred,
3566
+ napi_value* promise);
3514
3567
```
3515
3568
3516
3569
- `[in] env`: The environment that the API is invoked under.
@@ -3528,9 +3581,9 @@ This API creates a deferred object and a JavaScript promise.
3528
3581
added: v8.5.0
3529
3582
-->
3530
3583
```C
3531
- NAPI_EXTERN napi_status napi_resolve_deferred(napi_env env,
3532
- napi_deferred deferred,
3533
- napi_value resolution);
3584
+ napi_status napi_resolve_deferred(napi_env env,
3585
+ napi_deferred deferred,
3586
+ napi_value resolution);
3534
3587
```
3535
3588
3536
3589
- `[in] env`: The environment that the API is invoked under.
@@ -3551,9 +3604,9 @@ The deferred object is freed upon successful completion.
3551
3604
added: v8.5.0
3552
3605
-->
3553
3606
```C
3554
- NAPI_EXTERN napi_status napi_reject_deferred(napi_env env,
3555
- napi_deferred deferred,
3556
- napi_value rejection);
3607
+ napi_status napi_reject_deferred(napi_env env,
3608
+ napi_deferred deferred,
3609
+ napi_value rejection);
3557
3610
```
3558
3611
3559
3612
- `[in] env`: The environment that the API is invoked under.
@@ -3574,9 +3627,9 @@ The deferred object is freed upon successful completion.
3574
3627
added: v8.5.0
3575
3628
-->
3576
3629
```C
3577
- NAPI_EXTERN napi_status napi_is_promise(napi_env env,
3578
- napi_value promise,
3579
- bool* is_promise);
3630
+ napi_status napi_is_promise(napi_env env,
3631
+ napi_value promise,
3632
+ bool* is_promise);
3580
3633
```
3581
3634
3582
3635
- `[in] env`: The environment that the API is invoked under.
@@ -3604,7 +3657,8 @@ NAPI_EXTERN napi_status napi_run_script(napi_env env,
3604
3657
- `[out] result`: The value resulting from having executed the script.
3605
3658
3606
3659
[Promises]: #n_api_promises
3607
- [Asynchronous Operations]: #n_api_asynchronous_operations
3660
+ [Simple Asynchronous Operations]: #n_api_asynchronous_operations
3661
+ [Custom Asynchronous Operations]: #n_api_custom_asynchronous_operations
3608
3662
[Basic N-API Data Types]: #n_api_basic_n_api_data_types
3609
3663
[ECMAScript Language Specification]: https://tc39.github.io/ecma262/
3610
3664
[Error Handling]: #n_api_error_handling
0 commit comments