src: add async context frame to AsyncResource

Add member to hold the async context frame to AsyncResource to avoid
the need for the async_resource_context_frames_ map in env.

Semver major because it changes ABI.

PR-URL: https://github.com/nodejs/node/pull/56082
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
pull/56096/head
Gerhard Stöbich 2024-12-01 19:28:25 +01:00 committed by GitHub
parent de5f91db8b
commit 58982d712b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 9 additions and 48 deletions

View File

@ -17,28 +17,23 @@ AsyncResource::AsyncResource(Isolate* isolate,
const char* name,
async_id trigger_async_id)
: env_(Environment::GetCurrent(isolate)),
resource_(isolate, resource) {
resource_(isolate, resource),
context_frame_(isolate, async_context_frame::current(isolate)) {
CHECK_NOT_NULL(env_);
env_->SetAsyncResourceContextFrame(
reinterpret_cast<std::uintptr_t>(this),
{isolate, async_context_frame::current(isolate)});
async_context_ = EmitAsyncInit(isolate, resource, name, trigger_async_id);
}
AsyncResource::~AsyncResource() {
CHECK_NOT_NULL(env_);
EmitAsyncDestroy(env_, async_context_);
env_->RemoveAsyncResourceContextFrame(reinterpret_cast<std::uintptr_t>(this));
}
MaybeLocal<Value> AsyncResource::MakeCallback(Local<Function> callback,
int argc,
Local<Value>* argv) {
auto isolate = env_->isolate();
auto context_frame =
env_->GetAsyncResourceContextFrame(reinterpret_cast<std::uintptr_t>(this))
.Get(isolate);
async_context_frame::Scope async_context_frame_scope(isolate, context_frame);
async_context_frame::Scope async_context_frame_scope(
isolate, context_frame_.Get(isolate));
return node::MakeCallback(
isolate, get_resource(), callback, argc, argv, async_context_);
@ -48,10 +43,8 @@ MaybeLocal<Value> AsyncResource::MakeCallback(const char* method,
int argc,
Local<Value>* argv) {
auto isolate = env_->isolate();
auto context_frame =
env_->GetAsyncResourceContextFrame(reinterpret_cast<std::uintptr_t>(this))
.Get(isolate);
async_context_frame::Scope async_context_frame_scope(isolate, context_frame);
async_context_frame::Scope async_context_frame_scope(
isolate, context_frame_.Get(isolate));
return node::MakeCallback(
isolate, get_resource(), method, argc, argv, async_context_);
@ -61,10 +54,8 @@ MaybeLocal<Value> AsyncResource::MakeCallback(Local<String> symbol,
int argc,
Local<Value>* argv) {
auto isolate = env_->isolate();
auto context_frame =
env_->GetAsyncResourceContextFrame(reinterpret_cast<std::uintptr_t>(this))
.Get(isolate);
async_context_frame::Scope async_context_frame_scope(isolate, context_frame);
async_context_frame::Scope async_context_frame_scope(
isolate, context_frame_.Get(isolate));
return node::MakeCallback(
isolate, get_resource(), symbol, argc, argv, async_context_);

View File

@ -908,26 +908,6 @@ inline void Environment::RemoveHeapSnapshotNearHeapLimitCallback(
heap_limit);
}
inline void Environment::SetAsyncResourceContextFrame(
std::uintptr_t async_resource_handle,
v8::Global<v8::Value>&& context_frame) {
async_resource_context_frames_.emplace(
std::make_pair(async_resource_handle, std::move(context_frame)));
}
inline const v8::Global<v8::Value>& Environment::GetAsyncResourceContextFrame(
std::uintptr_t async_resource_handle) {
auto&& async_resource_context_frame =
async_resource_context_frames_.find(async_resource_handle);
CHECK_NE(async_resource_context_frame, async_resource_context_frames_.end());
return async_resource_context_frame->second;
}
inline void Environment::RemoveAsyncResourceContextFrame(
std::uintptr_t async_resource_handle) {
async_resource_context_frames_.erase(async_resource_handle);
}
} // namespace node
// These two files depend on each other. Including base_object-inl.h after this

View File

@ -1070,14 +1070,6 @@ class Environment final : public MemoryRetainer {
v8::Global<v8::Module> temporary_required_module_facade_original;
void SetAsyncResourceContextFrame(std::uintptr_t async_resource_handle,
v8::Global<v8::Value>&&);
const v8::Global<v8::Value>& GetAsyncResourceContextFrame(
std::uintptr_t async_resource_handle);
void RemoveAsyncResourceContextFrame(std::uintptr_t async_resource_handle);
private:
inline void ThrowError(v8::Local<v8::Value> (*fun)(v8::Local<v8::String>,
v8::Local<v8::Value>),
@ -1252,9 +1244,6 @@ class Environment final : public MemoryRetainer {
// track of the BackingStore for a given pointer.
std::unordered_map<char*, std::unique_ptr<v8::BackingStore>>
released_allocated_buffers_;
std::unordered_map<std::uintptr_t, v8::Global<v8::Value>>
async_resource_context_frames_;
};
} // namespace node

View File

@ -1524,6 +1524,7 @@ class NODE_EXTERN AsyncResource {
private:
Environment* env_;
v8::Global<v8::Object> resource_;
v8::Global<v8::Value> context_frame_;
async_context async_context_;
};