node/test/addons/async-hooks-id/binding.cc
Attila Szegedi ffb9bfb206
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: https://github.com/nodejs/node/pull/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>
2025-04-17 18:00:05 +00:00

36 lines
1017 B
C++

#include "node.h"
namespace {
using v8::FunctionCallbackInfo;
using v8::Local;
using v8::Object;
using v8::Value;
void GetExecutionAsyncId(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(
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);
}
} // anonymous namespace
NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)