|
25 | 25 | #include "env-inl.h" |
26 | 26 | #include "node_errors.h" |
27 | 27 | #include "node_external_reference.h" |
| 28 | +#include "node_internals.h" |
28 | 29 | #include "tracing/traced_value.h" |
29 | 30 | #include "util-inl.h" |
30 | 31 |
|
@@ -290,6 +291,17 @@ void AsyncWrap::PopAsyncContext(const FunctionCallbackInfo<Value>& args) { |
290 | 291 | args.GetReturnValue().Set(env->async_hooks()->pop_async_context(async_id)); |
291 | 292 | } |
292 | 293 |
|
| 294 | +// The resource of the innermost scope that skipped the id stack, if nothing |
| 295 | +// was pushed on the stack after it (depth is the stack length JS sees). |
| 296 | +static void LazyExecutionAsyncResource( |
| 297 | + const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 298 | + Environment* env = Environment::GetCurrent(args); |
| 299 | + uint32_t depth = args[0].As<v8::Uint32>()->Value(); |
| 300 | + InternalCallbackScope* scope = env->async_hooks()->lazy_top_; |
| 301 | + if (scope != nullptr && scope->lazy_depth() == depth) { |
| 302 | + args.GetReturnValue().Set(scope->lazy_resource(env->isolate())); |
| 303 | + } |
| 304 | +} |
293 | 305 |
|
294 | 306 | void AsyncWrap::ExecutionAsyncResource( |
295 | 307 | const FunctionCallbackInfo<Value>& args) { |
@@ -396,6 +408,10 @@ void AsyncWrap::CreatePerIsolateProperties(IsolateData* isolate_data, |
396 | 408 | SetMethod(isolate, target, "pushAsyncContext", PushAsyncContext); |
397 | 409 | SetMethod(isolate, target, "popAsyncContext", PopAsyncContext); |
398 | 410 | SetMethod(isolate, target, "executionAsyncResource", ExecutionAsyncResource); |
| 411 | + SetMethod(isolate, |
| 412 | + target, |
| 413 | + "lazyExecutionAsyncResource", |
| 414 | + LazyExecutionAsyncResource); |
399 | 415 | SetMethod(isolate, target, "clearAsyncIdStack", ClearAsyncIdStack); |
400 | 416 | SetMethod(isolate, target, "queueDestroyAsyncId", QueueDestroyAsyncId); |
401 | 417 | SetMethod(isolate, target, "setPromiseHooks", SetPromiseHooks); |
@@ -470,6 +486,7 @@ void AsyncWrap::CreatePerContextProperties(Local<Object> target, |
470 | 486 | SET_HOOKS_CONSTANT(kDefaultTriggerAsyncId); |
471 | 487 | SET_HOOKS_CONSTANT(kUsesExecutionAsyncResource); |
472 | 488 | SET_HOOKS_CONSTANT(kStackLength); |
| 489 | + SET_HOOKS_CONSTANT(kLazyScopes); |
473 | 490 | #undef SET_HOOKS_CONSTANT |
474 | 491 | FORCE_SET_TARGET_FIELD(target, "constants", constants); |
475 | 492 |
|
@@ -502,6 +519,7 @@ void AsyncWrap::RegisterExternalReferences( |
502 | 519 | registry->Register(PushAsyncContext); |
503 | 520 | registry->Register(PopAsyncContext); |
504 | 521 | registry->Register(ExecutionAsyncResource); |
| 522 | + registry->Register(LazyExecutionAsyncResource); |
505 | 523 | registry->Register(ClearAsyncIdStack); |
506 | 524 | registry->Register(QueueDestroyAsyncId); |
507 | 525 | registry->Register(SetPromiseHooks); |
|
0 commit comments