Skip to content

Commit 5af6a89

Browse files
addaleaxtargos
authored andcommitted
process: use owner_symbol for _getActive*
This makes it easier to provide public APIs in the return types of `process._getActiveHandles()` and `process._getActiveRequests()`. PR-URL: #22002 Backport-PR-URL: #22507 Reviewed-By: Michaël Zasso <[email protected]>
1 parent 46fbc23 commit 5af6a89

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

src/async_wrap.cc

+21
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,27 @@ std::string AsyncWrap::diagnostic_name() const {
736736
std::to_string(static_cast<int64_t>(async_id_)) + ")";
737737
}
738738

739+
Local<Object> AsyncWrap::GetOwner() {
740+
return GetOwner(env(), object());
741+
}
742+
743+
Local<Object> AsyncWrap::GetOwner(Environment* env, Local<Object> obj) {
744+
v8::EscapableHandleScope handle_scope(env->isolate());
745+
CHECK(!obj.IsEmpty());
746+
747+
v8::TryCatch ignore_exceptions(env->isolate());
748+
while (true) {
749+
Local<Value> owner;
750+
if (!obj->Get(env->context(),
751+
env->owner_symbol()).ToLocal(&owner) ||
752+
!owner->IsObject()) {
753+
return handle_scope.Escape(obj);
754+
}
755+
756+
obj = owner.As<Object>();
757+
}
758+
}
759+
739760
} // namespace node
740761

741762
NODE_BUILTIN_MODULE_CONTEXT_AWARE(async_wrap, node::AsyncWrap::Initialize)

src/async_wrap.h

+6
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,12 @@ class AsyncWrap : public BaseObject {
179179

180180
static void WeakCallback(const v8::WeakCallbackInfo<DestroyParam> &info);
181181

182+
// Returns the object that 'owns' an async wrap. For example, for a
183+
// TCP connection handle, this is the corresponding net.Socket.
184+
v8::Local<v8::Object> GetOwner();
185+
static v8::Local<v8::Object> GetOwner(Environment* env,
186+
v8::Local<v8::Object> obj);
187+
182188
// This is a simplified version of InternalCallbackScope that only runs
183189
// the `before` and `after` hooks. Only use it when not actually calling
184190
// back into JS; otherwise, use InternalCallbackScope.

src/node.cc

+3-9
Original file line numberDiff line numberDiff line change
@@ -1143,7 +1143,7 @@ static void GetActiveRequests(const FunctionCallbackInfo<Value>& args) {
11431143
for (auto w : *env->req_wrap_queue()) {
11441144
if (w->persistent().IsEmpty())
11451145
continue;
1146-
argv[idx] = w->object();
1146+
argv[idx] = w->GetOwner();
11471147
if (++idx >= arraysize(argv)) {
11481148
fn->Call(ctx, ary, idx, argv).ToLocalChecked();
11491149
idx = 0;
@@ -1169,16 +1169,10 @@ void GetActiveHandles(const FunctionCallbackInfo<Value>& args) {
11691169
Local<Value> argv[NODE_PUSH_VAL_TO_ARRAY_MAX];
11701170
size_t idx = 0;
11711171

1172-
Local<String> owner_sym = env->owner_string();
1173-
11741172
for (auto w : *env->handle_wrap_queue()) {
1175-
if (w->persistent().IsEmpty() || !HandleWrap::HasRef(w))
1173+
if (!HandleWrap::HasRef(w))
11761174
continue;
1177-
Local<Object> object = w->object();
1178-
Local<Value> owner = object->Get(owner_sym);
1179-
if (owner->IsUndefined())
1180-
owner = object;
1181-
argv[idx] = owner;
1175+
argv[idx] = w->GetOwner();
11821176
if (++idx >= arraysize(argv)) {
11831177
fn->Call(ctx, ary, idx, argv).ToLocalChecked();
11841178
idx = 0;

0 commit comments

Comments
 (0)