Skip to content

Commit 947c6a4

Browse files
szegediRafaelGSS
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 <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Gerhard Stöbich <[email protected]>
1 parent 5edcb28 commit 947c6a4

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

src/api/hooks.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,12 @@ async_id AsyncHooksGetExecutionAsyncId(Isolate* isolate) {
196196
return env->execution_async_id();
197197
}
198198

199+
async_id AsyncHooksGetExecutionAsyncId(Local<Context> context) {
200+
Environment* env = Environment::GetCurrent(context);
201+
if (env == nullptr) return -1;
202+
return env->execution_async_id();
203+
}
204+
199205
async_id AsyncHooksGetTriggerAsyncId(Isolate* isolate) {
200206
Environment* env = Environment::GetCurrent(isolate);
201207
if (env == nullptr) return -1;

src/node.h

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

1407+
/* Returns the id of the current execution context. If the return value is
1408+
* zero then no execution has been set. This will happen if the user handles
1409+
* I/O from native code. */
1410+
NODE_EXTERN async_id
1411+
AsyncHooksGetExecutionAsyncId(v8::Local<v8::Context> context);
1412+
14071413
/* Return same value as async_hooks.triggerAsyncId(); */
14081414
NODE_EXTERN async_id AsyncHooksGetTriggerAsyncId(v8::Isolate* isolate);
14091415

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
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ assert.strictEqual(
99
binding.getExecutionAsyncId(),
1010
async_hooks.executionAsyncId(),
1111
);
12+
assert.strictEqual(
13+
binding.getExecutionAsyncIdWithContext(),
14+
async_hooks.executionAsyncId(),
15+
);
1216
assert.strictEqual(
1317
binding.getTriggerAsyncId(),
1418
async_hooks.triggerAsyncId(),
@@ -19,6 +23,10 @@ process.nextTick(common.mustCall(() => {
1923
binding.getExecutionAsyncId(),
2024
async_hooks.executionAsyncId(),
2125
);
26+
assert.strictEqual(
27+
binding.getExecutionAsyncIdWithContext(),
28+
async_hooks.executionAsyncId(),
29+
);
2230
assert.strictEqual(
2331
binding.getTriggerAsyncId(),
2432
async_hooks.triggerAsyncId(),

0 commit comments

Comments
 (0)