@@ -1651,20 +1651,27 @@ inline Value Function::Call(napi_value recv, size_t argc, const napi_value* args
1651
1651
}
1652
1652
1653
1653
inline Value Function::MakeCallback (
1654
- napi_value recv, const std::initializer_list<napi_value>& args) const {
1655
- return MakeCallback (recv, args.size (), args.begin ());
1654
+ napi_value recv,
1655
+ const std::initializer_list<napi_value>& args,
1656
+ napi_async_context context) const {
1657
+ return MakeCallback (recv, args.size (), args.begin (), context);
1656
1658
}
1657
1659
1658
1660
inline Value Function::MakeCallback (
1659
- napi_value recv, const std::vector<napi_value>& args) const {
1660
- return MakeCallback (recv, args.size (), args.data ());
1661
+ napi_value recv,
1662
+ const std::vector<napi_value>& args,
1663
+ napi_async_context context) const {
1664
+ return MakeCallback (recv, args.size (), args.data (), context);
1661
1665
}
1662
1666
1663
1667
inline Value Function::MakeCallback (
1664
- napi_value recv, size_t argc, const napi_value* args) const {
1668
+ napi_value recv,
1669
+ size_t argc,
1670
+ const napi_value* args,
1671
+ napi_async_context context) const {
1665
1672
napi_value result;
1666
1673
napi_status status = napi_make_callback (
1667
- _env, nullptr , recv, _value, argc, args, &result);
1674
+ _env, context , recv, _value, argc, args, &result);
1668
1675
NAPI_THROW_IF_FAILED (_env, status, Value ());
1669
1676
return Value (_env, result);
1670
1677
}
@@ -2416,29 +2423,36 @@ inline Napi::Value FunctionReference::Call(
2416
2423
}
2417
2424
2418
2425
inline Napi::Value FunctionReference::MakeCallback (
2419
- napi_value recv, const std::initializer_list<napi_value>& args) const {
2426
+ napi_value recv,
2427
+ const std::initializer_list<napi_value>& args,
2428
+ napi_async_context context) const {
2420
2429
EscapableHandleScope scope (_env);
2421
- Napi::Value result = Value ().MakeCallback (recv, args);
2430
+ Napi::Value result = Value ().MakeCallback (recv, args, context );
2422
2431
if (scope.Env ().IsExceptionPending ()) {
2423
2432
return Value ();
2424
2433
}
2425
2434
return scope.Escape (result);
2426
2435
}
2427
2436
2428
2437
inline Napi::Value FunctionReference::MakeCallback (
2429
- napi_value recv, const std::vector<napi_value>& args) const {
2438
+ napi_value recv,
2439
+ const std::vector<napi_value>& args,
2440
+ napi_async_context context) const {
2430
2441
EscapableHandleScope scope (_env);
2431
- Napi::Value result = Value ().MakeCallback (recv, args);
2442
+ Napi::Value result = Value ().MakeCallback (recv, args, context );
2432
2443
if (scope.Env ().IsExceptionPending ()) {
2433
2444
return Value ();
2434
2445
}
2435
2446
return scope.Escape (result);
2436
2447
}
2437
2448
2438
2449
inline Napi::Value FunctionReference::MakeCallback (
2439
- napi_value recv, size_t argc, const napi_value* args) const {
2450
+ napi_value recv,
2451
+ size_t argc,
2452
+ const napi_value* args,
2453
+ napi_async_context context) const {
2440
2454
EscapableHandleScope scope (_env);
2441
- Napi::Value result = Value ().MakeCallback (recv, argc, args);
2455
+ Napi::Value result = Value ().MakeCallback (recv, argc, args, context );
2442
2456
if (scope.Env ().IsExceptionPending ()) {
2443
2457
return Value ();
2444
2458
}
@@ -3274,6 +3288,54 @@ inline Value EscapableHandleScope::Escape(napi_value escapee) {
3274
3288
return Value (_env, result);
3275
3289
}
3276
3290
3291
+ // //////////////////////////////////////////////////////////////////////////////
3292
+ // AsyncContext class
3293
+ // //////////////////////////////////////////////////////////////////////////////
3294
+
3295
+ inline AsyncContext::AsyncContext (napi_env env, const char * resource_name)
3296
+ : AsyncContext(env, resource_name, Object::New(env)) {
3297
+ }
3298
+
3299
+ inline AsyncContext::AsyncContext (napi_env env,
3300
+ const char * resource_name,
3301
+ const Object& resource)
3302
+ : _env(env),
3303
+ _context(nullptr ) {
3304
+ napi_value resource_id;
3305
+ napi_status status = napi_create_string_utf8 (
3306
+ _env, resource_name, NAPI_AUTO_LENGTH, &resource_id);
3307
+ NAPI_THROW_IF_FAILED_VOID (_env, status);
3308
+
3309
+ status = napi_async_init (_env, resource, resource_id, &_context);
3310
+ NAPI_THROW_IF_FAILED_VOID (_env, status);
3311
+ }
3312
+
3313
+ inline AsyncContext::~AsyncContext () {
3314
+ if (_context != nullptr ) {
3315
+ napi_async_destroy (_env, _context);
3316
+ _context = nullptr ;
3317
+ }
3318
+ }
3319
+
3320
+ inline AsyncContext::AsyncContext (AsyncContext&& other) {
3321
+ _env = other._env ;
3322
+ other._env = nullptr ;
3323
+ _context = other._context ;
3324
+ other._context = nullptr ;
3325
+ }
3326
+
3327
+ inline AsyncContext& AsyncContext::operator =(AsyncContext&& other) {
3328
+ _env = other._env ;
3329
+ other._env = nullptr ;
3330
+ _context = other._context ;
3331
+ other._context = nullptr ;
3332
+ return *this ;
3333
+ }
3334
+
3335
+ inline AsyncContext::operator napi_async_context () const {
3336
+ return _context;
3337
+ }
3338
+
3277
3339
// //////////////////////////////////////////////////////////////////////////////
3278
3340
// AsyncWorker class
3279
3341
// //////////////////////////////////////////////////////////////////////////////
0 commit comments