Skip to content

Commit 5be9b1d

Browse files
committed
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 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Jon Moss <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Minwoo Jung <[email protected]> Reviewed-By: Tiancheng "Timothy" Gu <[email protected]> Reviewed-By: Ali Ijaz Sheikh <[email protected]>
1 parent af7164e commit 5be9b1d

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

src/async_wrap.cc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,27 @@ std::string AsyncWrap::diagnostic_name() const {
734734
std::to_string(static_cast<int64_t>(async_id_)) + ")";
735735
}
736736

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

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

src/async_wrap.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,12 @@ class AsyncWrap : public BaseObject {
178178

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

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

src/node.cc

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,7 +1138,7 @@ static void GetActiveRequests(const FunctionCallbackInfo<Value>& args) {
11381138
for (auto w : *env->req_wrap_queue()) {
11391139
if (w->persistent().IsEmpty())
11401140
continue;
1141-
argv[idx] = w->object();
1141+
argv[idx] = w->GetOwner();
11421142
if (++idx >= arraysize(argv)) {
11431143
fn->Call(ctx, ary, idx, argv).ToLocalChecked();
11441144
idx = 0;
@@ -1164,16 +1164,10 @@ void GetActiveHandles(const FunctionCallbackInfo<Value>& args) {
11641164
Local<Value> argv[NODE_PUSH_VAL_TO_ARRAY_MAX];
11651165
size_t idx = 0;
11661166

1167-
Local<String> owner_sym = env->owner_string();
1168-
11691167
for (auto w : *env->handle_wrap_queue()) {
1170-
if (w->persistent().IsEmpty() || !HandleWrap::HasRef(w))
1168+
if (!HandleWrap::HasRef(w))
11711169
continue;
1172-
Local<Object> object = w->object();
1173-
Local<Value> owner = object->Get(owner_sym);
1174-
if (owner->IsUndefined())
1175-
owner = object;
1176-
argv[idx] = owner;
1170+
argv[idx] = w->GetOwner();
11771171
if (++idx >= arraysize(argv)) {
11781172
fn->Call(ctx, ary, idx, argv).ToLocalChecked();
11791173
idx = 0;

0 commit comments

Comments
 (0)