| Index: test/cctest/test-api.cc | 
| diff --git a/test/cctest/test-api.cc b/test/cctest/test-api.cc | 
| index 26483e019c6a49071c465f217fec001dab26e2ee..2735af4b6cf070d36b4ac458137efd927365bf81 100644 | 
| --- a/test/cctest/test-api.cc | 
| +++ b/test/cctest/test-api.cc | 
| @@ -10942,6 +10942,8 @@ static void entry_hook(uintptr_t function, | 
| ++foo_count; | 
|  | 
| // TODO(siggi): Verify return_addr_location. | 
| +  //     This can be done by capturing JitCodeEvents, but requires an ordered | 
| +  //     collection. | 
| } | 
|  | 
|  | 
| @@ -11027,6 +11029,123 @@ TEST(SetFunctionEntryHook) { | 
| } | 
|  | 
|  | 
| +static i::HashMap* code_map = NULL; | 
| +static int saw_foo = 0; | 
| +static int saw_bar = 0; | 
| +static int move_events = 0; | 
| + | 
| + | 
| +static void event_handler(const v8::JitCodeEvent* event) { | 
| +  CHECK(event != NULL); | 
| +  CHECK(code_map != NULL); | 
| + | 
| +  uint32_t hash = reinterpret_cast<uint32_t>(event->code_start); | 
| +  switch(event->type) { | 
| +    case v8::JitCodeEvent::CODE_ADDED: { | 
| +        CHECK(event->code_start != NULL); | 
| +        CHECK_NE(0, event->code_len); | 
| +        CHECK(event->name != NULL); | 
| +        i::HashMap::Entry* entry = | 
| +            code_map->Lookup(event->code_start, hash, true); | 
| +        entry->value = reinterpret_cast<void*>(event->code_len); | 
| + | 
| +        if (strcmp(event->name, "foo") == 0) { | 
| +          ++saw_foo; | 
| +        } else if (strcmp(event->name, "bar") == 0) { | 
| +          ++saw_bar; | 
| +        } | 
| +        break; | 
| +      } | 
| + | 
| +    case v8::JitCodeEvent::CODE_MOVED: { | 
| +        ++move_events; | 
| + | 
| +        // We should never see code move that we haven't seen before. | 
| +        i::HashMap::Entry* entry = | 
| +            code_map->Lookup(event->code_start, hash, false); | 
| +        CHECK(entry != NULL); | 
| +        CHECK_EQ(reinterpret_cast<void*>(event->code_len), | 
| +                 code_map->Remove(event->code_start, hash)); | 
| +        entry = code_map->Lookup(event->code_start, hash, true); | 
| +        entry->value = reinterpret_cast<void*>(event->code_len); | 
| +        break; | 
| +      } | 
| + | 
| +      break; | 
| +    case v8::JitCodeEvent::CODE_REMOVED: | 
| +      // Object/code removal events are currently not dispatched from the GC. | 
| +      CHECK(false); | 
| +      break; | 
| +    default: | 
| +      // Impossible event. | 
| +      CHECK(false); | 
| +      break; | 
| +  } | 
| +} | 
| + | 
| + | 
| +// Implemented in the test-alloc.cc test suite. | 
| +void SimulateFullSpace(i::PagedSpace* space); | 
| + | 
| + | 
| +static bool MatchPointers(void* key1, void* key2) { | 
| +  return key1 == key2; | 
| +} | 
| + | 
| + | 
| +TEST(SetJitCodeEventHandler) { | 
| +  // Run this test in a new isolate to make sure we don't | 
| +  // have remnants of state from other code. | 
| +  v8::Isolate* isolate = v8::Isolate::New(); | 
| +  isolate->Enter(); | 
| + | 
| +  i::HashMap code(MatchPointers); | 
| +  code_map = &code; | 
| +  saw_foo = 0; | 
| +  saw_bar = 0; | 
| +  move_events = 0; | 
| + | 
| + | 
| +  i::FLAG_stress_compaction = true; | 
| +  v8::HandleScope scope; | 
| + | 
| +  const char* script = | 
| +    "function bar() {" | 
| +    "  var sum = 0;" | 
| +    "  for (i = 0; i < 100; ++i)" | 
| +    "    sum = foo(i);" | 
| +    "  return sum;" | 
| +    "}" | 
| +    "function foo(i) { return i * i; };" | 
| +    "foo(100);"; | 
| + | 
| +  V8::SetJitCodeEventHandler(event_handler); | 
| + | 
| +  // Generate new code objects sparsely distributed across several | 
| +  // different fragmented code-space pages. | 
| +  const int kIterations = 10; | 
| +  for (int i = 0; i < kIterations; ++i) { | 
| +    LocalContext env; | 
| + | 
| +    i::AlwaysAllocateScope always_allocate; | 
| +    v8_compile(script); | 
| +    SimulateFullSpace(HEAP->code_space()); | 
| + | 
| +    ISOLATE->compilation_cache()->Clear(); | 
| +  } | 
| + | 
| +  CHECK_EQ(kIterations, saw_bar); | 
| +  CHECK_EQ(kIterations, saw_foo); | 
| +  CHECK_NE(0, move_events); | 
| + | 
| +  code_map = NULL; | 
| +  V8::SetJitCodeEventHandler(NULL); | 
| + | 
| +  isolate->Exit(); | 
| +  isolate->Dispose(); | 
| +} | 
| + | 
| + | 
| static int64_t cast(intptr_t x) { return static_cast<int64_t>(x); } | 
|  | 
|  | 
|  |