Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/api/hooks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ async_id AsyncHooksGetExecutionAsyncId(Isolate* isolate) {
return env->execution_async_id();
}

async_id AsyncHooksGetExecutionAsyncId(Local<Context> context) {
Environment* env = Environment::GetCurrent(context);
if (env == nullptr) return -1;
return env->execution_async_id();
}

async_id AsyncHooksGetTriggerAsyncId(Isolate* isolate) {
Environment* env = Environment::GetCurrent(isolate);
if (env == nullptr) return -1;
Expand Down
5 changes: 5 additions & 0 deletions src/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -1404,6 +1404,11 @@ NODE_EXTERN void RequestInterrupt(Environment* env,
* I/O from native code. */
NODE_EXTERN async_id AsyncHooksGetExecutionAsyncId(v8::Isolate* isolate);

/* Returns the id of the specified execution context. If the return value is
* zero then no execution has been set. This will happen if the user handles
* I/O from native code. */
NODE_EXTERN async_id AsyncHooksGetExecutionAsyncId(v8::Local<v8::Context> context);

/* Return same value as async_hooks.triggerAsyncId(); */
NODE_EXTERN async_id AsyncHooksGetTriggerAsyncId(v8::Isolate* isolate);

Expand Down
7 changes: 7 additions & 0 deletions test/addons/async-hooks-id/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,20 @@ void GetExecutionAsyncId(const FunctionCallbackInfo<Value>& args) {
node::AsyncHooksGetExecutionAsyncId(args.GetIsolate()));
}

void GetExecutionAsyncIdWithContext(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(
node::AsyncHooksGetExecutionAsyncId(args.GetIsolate()->GetCurrentContext()));
}


void GetTriggerAsyncId(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(
node::AsyncHooksGetTriggerAsyncId(args.GetIsolate()));
}

void Initialize(Local<Object> exports) {
NODE_SET_METHOD(exports, "getExecutionAsyncId", GetExecutionAsyncId);
NODE_SET_METHOD(exports, "getExecutionAsyncIdWithContext", GetExecutionAsyncIdWithContext);
NODE_SET_METHOD(exports, "getTriggerAsyncId", GetTriggerAsyncId);
}

Expand Down
8 changes: 8 additions & 0 deletions test/addons/async-hooks-id/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ assert.strictEqual(
binding.getExecutionAsyncId(),
async_hooks.executionAsyncId(),
);
assert.strictEqual(
binding.getExecutionAsyncIdWithContext(),
async_hooks.executionAsyncId(),
);
assert.strictEqual(
binding.getTriggerAsyncId(),
async_hooks.triggerAsyncId(),
Expand All @@ -19,6 +23,10 @@ process.nextTick(common.mustCall(() => {
binding.getExecutionAsyncId(),
async_hooks.executionAsyncId(),
);
assert.strictEqual(
binding.getExecutionAsyncIdWithContext(),
async_hooks.executionAsyncId(),
);
assert.strictEqual(
binding.getTriggerAsyncId(),
async_hooks.triggerAsyncId(),
Expand Down
Loading