src: add ExecutionAsyncId getter for any Context · nodejs/node@e1d3a9e · GitHub
Skip to content

Commit e1d3a9e

Browse files
szegediaduh95
authored andcommitted
src: add ExecutionAsyncId getter for any Context
Adds a variant of AsyncHooksGetExecutionAsyncId that takes a V8 Context and returns the async ID belonging to the Environment (if any) of that Context. Sometimes we want to use Isolate::GetEnteredOrMicrotaskContext insteads of Isolate::GetCurrentContext (e.g. recording the async ID in a V8 GC prologue callback) when current context is not set. PR-URL: #57820 Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
1 parent d4fc282 commit e1d3a9e

4 files changed

Lines changed: 28 additions & 0 deletions

File tree

src/api/hooks.cc

Lines changed: 6 additions & 0 deletions

src/node.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,6 +1387,12 @@ NODE_EXTERN void RequestInterrupt(Environment* env,
13871387
* I/O from native code. */
13881388
NODE_EXTERN async_id AsyncHooksGetExecutionAsyncId(v8::Isolate* isolate);
13891389

1390+
/* Returns the id of the current execution context. If the return value is
1391+
* zero then no execution has been set. This will happen if the user handles
1392+
* I/O from native code. */
1393+
NODE_EXTERN async_id
1394+
AsyncHooksGetExecutionAsyncId(v8::Local<v8::Context> context);
1395+
13901396
/* Return same value as async_hooks.triggerAsyncId(); */
13911397
NODE_EXTERN async_id AsyncHooksGetTriggerAsyncId(v8::Isolate* isolate);
13921398

test/addons/async-hooks-id/binding.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,21 @@ void GetExecutionAsyncId(const FunctionCallbackInfo<Value>& args) {
1212
node::AsyncHooksGetExecutionAsyncId(args.GetIsolate()));
1313
}
1414

15+
void GetExecutionAsyncIdWithContext(const FunctionCallbackInfo<Value>& args) {
16+
args.GetReturnValue().Set(node::AsyncHooksGetExecutionAsyncId(
17+
args.GetIsolate()->GetCurrentContext()));
18+
}
19+
1520
void GetTriggerAsyncId(const FunctionCallbackInfo<Value>& args) {
1621
args.GetReturnValue().Set(
1722
node::AsyncHooksGetTriggerAsyncId(args.GetIsolate()));
1823
}
1924

2025
void Initialize(Local<Object> exports) {
2126
NODE_SET_METHOD(exports, "getExecutionAsyncId", GetExecutionAsyncId);
27+
NODE_SET_METHOD(exports,
28+
"getExecutionAsyncIdWithContext",
29+
GetExecutionAsyncIdWithContext);
2230
NODE_SET_METHOD(exports, "getTriggerAsyncId", GetTriggerAsyncId);
2331
}
2432

test/addons/async-hooks-id/test.js

Lines changed: 8 additions & 0 deletions

0 commit comments

Comments
 (0)