Add a UseCounter for Object.observe

It triggers once per context that calls observe (or attempts to access
any observation metadata, e.g. through Object.getNotifier).

Review URL: https://codereview.chromium.org/1048213002

Cr-Commit-Position: refs/heads/master@{#27557}
This commit is contained in:
adamk 2015-03-31 16:03:08 -07:00 committed by Commit bot
parent eb982a1bb1
commit 729b85ae86
3 changed files with 54 additions and 1 deletions

View File

@ -5146,6 +5146,7 @@ class V8_EXPORT Isolate {
kMarkDequeOverflow = 3,
kStoreBufferOverflow = 4,
kSlotsBufferOverflow = 5,
kObjectObserve = 6,
kUseCounterFeatureCount // This enum value must be last.
};

View File

@ -88,8 +88,9 @@ RUNTIME_FUNCTION(Runtime_DeliverObservationChangeRecords) {
RUNTIME_FUNCTION(Runtime_GetObservationState) {
SealHandleScope shs(isolate);
HandleScope scope(isolate);
DCHECK(args.length() == 0);
isolate->CountUsage(v8::Isolate::kObjectObserve);
return isolate->heap()->observation_state();
}

View File

@ -834,3 +834,54 @@ TEST(APIAccessorsShouldNotNotify) {
CompileRun("Object.defineProperty(obj, 'accessor', { value: 44 });");
CHECK(CompileRun("records")->IsNull());
}
namespace {
int* global_use_counts = NULL;
void MockUseCounterCallback(v8::Isolate* isolate,
v8::Isolate::UseCounterFeature feature) {
++global_use_counts[feature];
}
}
TEST(UseCountObjectObserve) {
i::Isolate* isolate = CcTest::i_isolate();
i::HandleScope scope(isolate);
LocalContext env;
int use_counts[v8::Isolate::kUseCounterFeatureCount] = {};
global_use_counts = use_counts;
CcTest::isolate()->SetUseCounterCallback(MockUseCounterCallback);
CompileRun(
"var obj = {};"
"Object.observe(obj, function(){})");
CHECK_EQ(1, use_counts[v8::Isolate::kObjectObserve]);
CompileRun(
"var obj2 = {};"
"Object.observe(obj2, function(){})");
// Only counts the first use of observe in a given context.
CHECK_EQ(1, use_counts[v8::Isolate::kObjectObserve]);
{
LocalContext env2;
CompileRun(
"var obj = {};"
"Object.observe(obj, function(){})");
}
// Counts different contexts separately.
CHECK_EQ(2, use_counts[v8::Isolate::kObjectObserve]);
}
TEST(UseCountObjectGetNotifier) {
i::Isolate* isolate = CcTest::i_isolate();
i::HandleScope scope(isolate);
LocalContext env;
int use_counts[v8::Isolate::kUseCounterFeatureCount] = {};
global_use_counts = use_counts;
CcTest::isolate()->SetUseCounterCallback(MockUseCounterCallback);
CompileRun("var obj = {}");
CompileRun("Object.getNotifier(obj)");
CHECK_EQ(1, use_counts[v8::Isolate::kObjectObserve]);
}