Skip to content

Commit 58982d7

Browse files
authored
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: #56082 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]>
1 parent de5f91d commit 58982d7

File tree

4 files changed

+9
-48
lines changed

4 files changed

+9
-48
lines changed

src/api/async_resource.cc

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,23 @@ AsyncResource::AsyncResource(Isolate* isolate,
1717
const char* name,
1818
async_id trigger_async_id)
1919
: env_(Environment::GetCurrent(isolate)),
20-
resource_(isolate, resource) {
20+
resource_(isolate, resource),
21+
context_frame_(isolate, async_context_frame::current(isolate)) {
2122
CHECK_NOT_NULL(env_);
22-
env_->SetAsyncResourceContextFrame(
23-
reinterpret_cast<std::uintptr_t>(this),
24-
{isolate, async_context_frame::current(isolate)});
2523
async_context_ = EmitAsyncInit(isolate, resource, name, trigger_async_id);
2624
}
2725

2826
AsyncResource::~AsyncResource() {
2927
CHECK_NOT_NULL(env_);
3028
EmitAsyncDestroy(env_, async_context_);
31-
env_->RemoveAsyncResourceContextFrame(reinterpret_cast<std::uintptr_t>(this));
3229
}
3330

3431
MaybeLocal<Value> AsyncResource::MakeCallback(Local<Function> callback,
3532
int argc,
3633
Local<Value>* argv) {
3734
auto isolate = env_->isolate();
38-
auto context_frame =
39-
env_->GetAsyncResourceContextFrame(reinterpret_cast<std::uintptr_t>(this))
40-
.Get(isolate);
41-
async_context_frame::Scope async_context_frame_scope(isolate, context_frame);
35+
async_context_frame::Scope async_context_frame_scope(
36+
isolate, context_frame_.Get(isolate));
4237

4338
return node::MakeCallback(
4439
isolate, get_resource(), callback, argc, argv, async_context_);
@@ -48,10 +43,8 @@ MaybeLocal<Value> AsyncResource::MakeCallback(const char* method,
4843
int argc,
4944
Local<Value>* argv) {
5045
auto isolate = env_->isolate();
51-
auto context_frame =
52-
env_->GetAsyncResourceContextFrame(reinterpret_cast<std::uintptr_t>(this))
53-
.Get(isolate);
54-
async_context_frame::Scope async_context_frame_scope(isolate, context_frame);
46+
async_context_frame::Scope async_context_frame_scope(
47+
isolate, context_frame_.Get(isolate));
5548

5649
return node::MakeCallback(
5750
isolate, get_resource(), method, argc, argv, async_context_);
@@ -61,10 +54,8 @@ MaybeLocal<Value> AsyncResource::MakeCallback(Local<String> symbol,
6154
int argc,
6255
Local<Value>* argv) {
6356
auto isolate = env_->isolate();
64-
auto context_frame =
65-
env_->GetAsyncResourceContextFrame(reinterpret_cast<std::uintptr_t>(this))
66-
.Get(isolate);
67-
async_context_frame::Scope async_context_frame_scope(isolate, context_frame);
57+
async_context_frame::Scope async_context_frame_scope(
58+
isolate, context_frame_.Get(isolate));
6859

6960
return node::MakeCallback(
7061
isolate, get_resource(), symbol, argc, argv, async_context_);

src/env-inl.h

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -908,26 +908,6 @@ inline void Environment::RemoveHeapSnapshotNearHeapLimitCallback(
908908
heap_limit);
909909
}
910910

911-
inline void Environment::SetAsyncResourceContextFrame(
912-
std::uintptr_t async_resource_handle,
913-
v8::Global<v8::Value>&& context_frame) {
914-
async_resource_context_frames_.emplace(
915-
std::make_pair(async_resource_handle, std::move(context_frame)));
916-
}
917-
918-
inline const v8::Global<v8::Value>& Environment::GetAsyncResourceContextFrame(
919-
std::uintptr_t async_resource_handle) {
920-
auto&& async_resource_context_frame =
921-
async_resource_context_frames_.find(async_resource_handle);
922-
CHECK_NE(async_resource_context_frame, async_resource_context_frames_.end());
923-
924-
return async_resource_context_frame->second;
925-
}
926-
927-
inline void Environment::RemoveAsyncResourceContextFrame(
928-
std::uintptr_t async_resource_handle) {
929-
async_resource_context_frames_.erase(async_resource_handle);
930-
}
931911
} // namespace node
932912

933913
// These two files depend on each other. Including base_object-inl.h after this

src/env.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,14 +1070,6 @@ class Environment final : public MemoryRetainer {
10701070

10711071
v8::Global<v8::Module> temporary_required_module_facade_original;
10721072

1073-
void SetAsyncResourceContextFrame(std::uintptr_t async_resource_handle,
1074-
v8::Global<v8::Value>&&);
1075-
1076-
const v8::Global<v8::Value>& GetAsyncResourceContextFrame(
1077-
std::uintptr_t async_resource_handle);
1078-
1079-
void RemoveAsyncResourceContextFrame(std::uintptr_t async_resource_handle);
1080-
10811073
private:
10821074
inline void ThrowError(v8::Local<v8::Value> (*fun)(v8::Local<v8::String>,
10831075
v8::Local<v8::Value>),
@@ -1252,9 +1244,6 @@ class Environment final : public MemoryRetainer {
12521244
// track of the BackingStore for a given pointer.
12531245
std::unordered_map<char*, std::unique_ptr<v8::BackingStore>>
12541246
released_allocated_buffers_;
1255-
1256-
std::unordered_map<std::uintptr_t, v8::Global<v8::Value>>
1257-
async_resource_context_frames_;
12581247
};
12591248

12601249
} // namespace node

src/node.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,6 +1524,7 @@ class NODE_EXTERN AsyncResource {
15241524
private:
15251525
Environment* env_;
15261526
v8::Global<v8::Object> resource_;
1527+
v8::Global<v8::Value> context_frame_;
15271528
async_context async_context_;
15281529
};
15291530

0 commit comments

Comments
 (0)