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: https://github.com/nodejs/node/pull/22002 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Jon Moss <me@jonathanmoss.me> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Minwoo Jung <minwoo@nodesource.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: Ali Ijaz Sheikh <ofrobots@google.com>
This commit is contained in:
parent
af7164ebcc
commit
5be9b1d7f3
@ -734,6 +734,27 @@ std::string AsyncWrap::diagnostic_name() const {
|
|||||||
std::to_string(static_cast<int64_t>(async_id_)) + ")";
|
std::to_string(static_cast<int64_t>(async_id_)) + ")";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Local<Object> AsyncWrap::GetOwner() {
|
||||||
|
return GetOwner(env(), object());
|
||||||
|
}
|
||||||
|
|
||||||
|
Local<Object> AsyncWrap::GetOwner(Environment* env, Local<Object> obj) {
|
||||||
|
v8::EscapableHandleScope handle_scope(env->isolate());
|
||||||
|
CHECK(!obj.IsEmpty());
|
||||||
|
|
||||||
|
v8::TryCatch ignore_exceptions(env->isolate());
|
||||||
|
while (true) {
|
||||||
|
Local<Value> owner;
|
||||||
|
if (!obj->Get(env->context(),
|
||||||
|
env->owner_symbol()).ToLocal(&owner) ||
|
||||||
|
!owner->IsObject()) {
|
||||||
|
return handle_scope.Escape(obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
obj = owner.As<Object>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace node
|
} // namespace node
|
||||||
|
|
||||||
NODE_BUILTIN_MODULE_CONTEXT_AWARE(async_wrap, node::AsyncWrap::Initialize)
|
NODE_BUILTIN_MODULE_CONTEXT_AWARE(async_wrap, node::AsyncWrap::Initialize)
|
||||||
|
@ -178,6 +178,12 @@ class AsyncWrap : public BaseObject {
|
|||||||
|
|
||||||
static void WeakCallback(const v8::WeakCallbackInfo<DestroyParam> &info);
|
static void WeakCallback(const v8::WeakCallbackInfo<DestroyParam> &info);
|
||||||
|
|
||||||
|
// Returns the object that 'owns' an async wrap. For example, for a
|
||||||
|
// TCP connection handle, this is the corresponding net.Socket.
|
||||||
|
v8::Local<v8::Object> GetOwner();
|
||||||
|
static v8::Local<v8::Object> GetOwner(Environment* env,
|
||||||
|
v8::Local<v8::Object> obj);
|
||||||
|
|
||||||
// This is a simplified version of InternalCallbackScope that only runs
|
// This is a simplified version of InternalCallbackScope that only runs
|
||||||
// the `before` and `after` hooks. Only use it when not actually calling
|
// the `before` and `after` hooks. Only use it when not actually calling
|
||||||
// back into JS; otherwise, use InternalCallbackScope.
|
// back into JS; otherwise, use InternalCallbackScope.
|
||||||
|
12
src/node.cc
12
src/node.cc
@ -1138,7 +1138,7 @@ static void GetActiveRequests(const FunctionCallbackInfo<Value>& args) {
|
|||||||
for (auto w : *env->req_wrap_queue()) {
|
for (auto w : *env->req_wrap_queue()) {
|
||||||
if (w->persistent().IsEmpty())
|
if (w->persistent().IsEmpty())
|
||||||
continue;
|
continue;
|
||||||
argv[idx] = w->object();
|
argv[idx] = w->GetOwner();
|
||||||
if (++idx >= arraysize(argv)) {
|
if (++idx >= arraysize(argv)) {
|
||||||
fn->Call(ctx, ary, idx, argv).ToLocalChecked();
|
fn->Call(ctx, ary, idx, argv).ToLocalChecked();
|
||||||
idx = 0;
|
idx = 0;
|
||||||
@ -1164,16 +1164,10 @@ void GetActiveHandles(const FunctionCallbackInfo<Value>& args) {
|
|||||||
Local<Value> argv[NODE_PUSH_VAL_TO_ARRAY_MAX];
|
Local<Value> argv[NODE_PUSH_VAL_TO_ARRAY_MAX];
|
||||||
size_t idx = 0;
|
size_t idx = 0;
|
||||||
|
|
||||||
Local<String> owner_sym = env->owner_string();
|
|
||||||
|
|
||||||
for (auto w : *env->handle_wrap_queue()) {
|
for (auto w : *env->handle_wrap_queue()) {
|
||||||
if (w->persistent().IsEmpty() || !HandleWrap::HasRef(w))
|
if (!HandleWrap::HasRef(w))
|
||||||
continue;
|
continue;
|
||||||
Local<Object> object = w->object();
|
argv[idx] = w->GetOwner();
|
||||||
Local<Value> owner = object->Get(owner_sym);
|
|
||||||
if (owner->IsUndefined())
|
|
||||||
owner = object;
|
|
||||||
argv[idx] = owner;
|
|
||||||
if (++idx >= arraysize(argv)) {
|
if (++idx >= arraysize(argv)) {
|
||||||
fn->Call(ctx, ary, idx, argv).ToLocalChecked();
|
fn->Call(ctx, ary, idx, argv).ToLocalChecked();
|
||||||
idx = 0;
|
idx = 0;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user