@@ -474,6 +474,39 @@ void TriggerHeapSnapshot(const FunctionCallbackInfo<Value>& args) { |
474 | 474 | return args.GetReturnValue().Set(filename_v); |
475 | 475 | } |
476 | 476 |
|
| 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 | + |
477 | 510 | void Initialize(Local<Object> target, |
478 | 511 | Local<Value> unused, |
479 | 512 | Local<Context> context, |
@@ -482,12 +515,15 @@ void Initialize(Local<Object> target, |
482 | 515 | SetMethod(context, target, "triggerHeapSnapshot", TriggerHeapSnapshot); |
483 | 516 | SetMethod( |
484 | 517 | context, target, "createHeapSnapshotStream", CreateHeapSnapshotStream); |
| 518 | + SetMethod( |
| 519 | + context, target, "countObjectsWithPrototype", CountObjectsWithPrototype); |
485 | 520 | } |
486 | 521 |
|
487 | 522 | void RegisterExternalReferences(ExternalReferenceRegistry* registry) { |
488 | 523 | registry->Register(BuildEmbedderGraph); |
489 | 524 | registry->Register(TriggerHeapSnapshot); |
490 | 525 | registry->Register(CreateHeapSnapshotStream); |
| 526 | + registry->Register(CountObjectsWithPrototype); |
491 | 527 | } |
492 | 528 |
|
493 | 529 | } // namespace heap |
|