Skip to content

Commit 619c5b6

Browse files
addaleaxtargos
authored andcommitted
src: do not require JS Context for ~AsyncResoure()
Allow the destructor to be called during GC, which is a common use case. PR-URL: #27255 Fixes: #27218 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
1 parent 8b5d738 commit 619c5b6

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

src/api/async_resource.cc

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "node.h"
2+
#include "env-inl.h"
23

34
namespace node {
45

@@ -15,43 +16,44 @@ AsyncResource::AsyncResource(Isolate* isolate,
1516
Local<Object> resource,
1617
const char* name,
1718
async_id trigger_async_id)
18-
: isolate_(isolate),
19+
: env_(Environment::GetCurrent(isolate)),
1920
resource_(isolate, resource) {
21+
CHECK_NOT_NULL(env_);
2022
async_context_ = EmitAsyncInit(isolate, resource, name,
2123
trigger_async_id);
2224
}
2325

2426
AsyncResource::~AsyncResource() {
25-
EmitAsyncDestroy(isolate_, async_context_);
27+
EmitAsyncDestroy(env_, async_context_);
2628
resource_.Reset();
2729
}
2830

2931
MaybeLocal<Value> AsyncResource::MakeCallback(Local<Function> callback,
3032
int argc,
3133
Local<Value>* argv) {
32-
return node::MakeCallback(isolate_, get_resource(),
34+
return node::MakeCallback(env_->isolate(), get_resource(),
3335
callback, argc, argv,
3436
async_context_);
3537
}
3638

3739
MaybeLocal<Value> AsyncResource::MakeCallback(const char* method,
3840
int argc,
3941
Local<Value>* argv) {
40-
return node::MakeCallback(isolate_, get_resource(),
42+
return node::MakeCallback(env_->isolate(), get_resource(),
4143
method, argc, argv,
4244
async_context_);
4345
}
4446

4547
MaybeLocal<Value> AsyncResource::MakeCallback(Local<String> symbol,
4648
int argc,
4749
Local<Value>* argv) {
48-
return node::MakeCallback(isolate_, get_resource(),
50+
return node::MakeCallback(env_->isolate(), get_resource(),
4951
symbol, argc, argv,
5052
async_context_);
5153
}
5254

5355
Local<Object> AsyncResource::get_resource() {
54-
return resource_.Get(isolate_);
56+
return resource_.Get(env_->isolate());
5557
}
5658

5759
async_id AsyncResource::get_async_id() const {
@@ -62,9 +64,11 @@ async_id AsyncResource::get_trigger_async_id() const {
6264
return async_context_.trigger_async_id;
6365
}
6466

67+
// TODO(addaleax): We shouldn’t need to use env_->isolate() if we’re just going
68+
// to end up using the Isolate* to figure out the Environment* again.
6569
AsyncResource::CallbackScope::CallbackScope(AsyncResource* res)
66-
: node::CallbackScope(res->isolate_,
67-
res->resource_.Get(res->isolate_),
70+
: node::CallbackScope(res->env_->isolate(),
71+
res->resource_.Get(res->env_->isolate()),
6872
res->async_context_) {}
6973

7074
} // namespace node

src/node.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ class NODE_EXTERN AsyncResource {
803803
};
804804

805805
private:
806-
v8::Isolate* isolate_;
806+
Environment* env_;
807807
v8::Persistent<v8::Object> resource_;
808808
async_context async_context_;
809809
};

0 commit comments

Comments
 (0)