@@ -474,6 +474,39 @@ void TriggerHeapSnapshot(const FunctionCallbackInfo<Value>& args) {
474474 return args.GetReturnValue ().Set (filename_v);
475475}
476476
477+ class PrototypeChainHas : public v8 ::QueryObjectPredicate {
478+ public:
479+ PrototypeChainHas (Local<Context> context, Local<Object> search)
480+ : context_(context), search_(search) {}
481+
482+ // What we can do in the filter can be quite limited, but looking up
483+ // the prototype chain is something that the inspector console API
484+ // queryObject() does so it is supported.
485+ bool Filter (Local<Object> object) override {
486+ for (Local<Value> proto = object->GetPrototype (); proto->IsObject ();
487+ proto = proto.As <Object>()->GetPrototype ()) {
488+ if (search_ == proto) return true ;
489+ }
490+ return false ;
491+ }
492+
493+ private:
494+ Local<Context> context_;
495+ Local<Object> search_;
496+ };
497+
498+ void CountObjectsWithPrototype (const FunctionCallbackInfo<Value>& args) {
499+ CHECK_EQ (args.Length (), 1 );
500+ CHECK (args[0 ]->IsObject ());
501+ Local<Object> proto = args[0 ].As <Object>();
502+ Isolate* isolate = args.GetIsolate ();
503+ Local<Context> context = isolate->GetCurrentContext ();
504+ PrototypeChainHas prototype_chain_has (context, proto);
505+ std::vector<Global<Object>> out;
506+ isolate->GetHeapProfiler ()->QueryObjects (context, &prototype_chain_has, &out);
507+ args.GetReturnValue ().Set (static_cast <uint32_t >(out.size ()));
508+ }
509+
477510void Initialize (Local<Object> target,
478511 Local<Value> unused,
479512 Local<Context> context,
@@ -482,12 +515,15 @@ void Initialize(Local<Object> target,
482515 SetMethod (context, target, " triggerHeapSnapshot" , TriggerHeapSnapshot);
483516 SetMethod (
484517 context, target, " createHeapSnapshotStream" , CreateHeapSnapshotStream);
518+ SetMethod (
519+ context, target, " countObjectsWithPrototype" , CountObjectsWithPrototype);
485520}
486521
487522void RegisterExternalReferences (ExternalReferenceRegistry* registry) {
488523 registry->Register (BuildEmbedderGraph);
489524 registry->Register (TriggerHeapSnapshot);
490525 registry->Register (CreateHeapSnapshotStream);
526+ registry->Register (CountObjectsWithPrototype);
491527}
492528
493529} // namespace heap
0 commit comments