diff --git a/src/heap/memory-measurement.cc b/src/heap/memory-measurement.cc index d0ade9be31..966644d4d6 100644 --- a/src/heap/memory-measurement.cc +++ b/src/heap/memory-measurement.cc @@ -333,8 +333,13 @@ bool NativeContextInferrer::InferForJSObject(Isolate* isolate, Map map, return true; } } - // TODO(ulan): Add a more precise inference for the case when - // the current context is the shared context. + // The maximum number of steps to perform when looking for the context. + const int kMaxSteps = 3; + Object maybe_constructor = map.TryGetConstructor(isolate, kMaxSteps); + if (maybe_constructor.IsJSFunction()) { + return InferForJSFunction(JSFunction::cast(maybe_constructor), + native_context); + } return false; } diff --git a/test/cctest/heap/test-memory-measurement.cc b/test/cctest/heap/test-memory-measurement.cc index 5295fd6b15..f517264788 100644 --- a/test/cctest/heap/test-memory-measurement.cc +++ b/test/cctest/heap/test-memory-measurement.cc @@ -51,6 +51,7 @@ TEST(NativeContextInferrerJSObject) { LocalContext env; Isolate* isolate = CcTest::i_isolate(); HandleScope scope(isolate); + Handle native_context = GetNativeContext(isolate, env.local()); v8::Local result = CompileRun("({a : 10})"); Handle object = Utils::OpenHandle(*result); Handle function = Handle::cast(object); @@ -58,8 +59,8 @@ TEST(NativeContextInferrerJSObject) { Address inferred_context = 0; // TODO(ulan): Enable this test once we have more precise native // context inference. - CHECK( - !inferrer.Infer(isolate, function->map(), *function, &inferred_context)); + CHECK(inferrer.Infer(isolate, function->map(), *function, &inferred_context)); + CHECK_EQ(native_context->ptr(), inferred_context); } TEST(NativeContextStatsMerge) {