OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 | 5 |
6 #include "v8.h" | 6 #include "v8.h" |
7 | 7 |
8 #include "liveedit.h" | 8 #include "liveedit.h" |
9 | 9 |
10 #include "code-stubs.h" | 10 #include "code-stubs.h" |
(...skipping 921 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
932 | 932 |
933 // Finds all references to original and replaces them with substitution. | 933 // Finds all references to original and replaces them with substitution. |
934 static void ReplaceCodeObject(Handle<Code> original, | 934 static void ReplaceCodeObject(Handle<Code> original, |
935 Handle<Code> substitution) { | 935 Handle<Code> substitution) { |
936 // Perform a full GC in order to ensure that we are not in the middle of an | 936 // Perform a full GC in order to ensure that we are not in the middle of an |
937 // incremental marking phase when we are replacing the code object. | 937 // incremental marking phase when we are replacing the code object. |
938 // Since we are not in an incremental marking phase we can write pointers | 938 // Since we are not in an incremental marking phase we can write pointers |
939 // to code objects (that are never in new space) without worrying about | 939 // to code objects (that are never in new space) without worrying about |
940 // write barriers. | 940 // write barriers. |
941 Heap* heap = original->GetHeap(); | 941 Heap* heap = original->GetHeap(); |
942 heap->CollectAllGarbage(Heap::kMakeHeapIterableMask, | 942 HeapIterator iterator(heap); |
943 "liveedit.cc ReplaceCodeObject"); | |
944 | 943 |
945 ASSERT(!heap->InNewSpace(*substitution)); | 944 ASSERT(!heap->InNewSpace(*substitution)); |
946 | 945 |
947 DisallowHeapAllocation no_allocation; | |
948 | |
949 ReplacingVisitor visitor(*original, *substitution); | 946 ReplacingVisitor visitor(*original, *substitution); |
950 | 947 |
951 // Iterate over all roots. Stack frames may have pointer into original code, | 948 // Iterate over all roots. Stack frames may have pointer into original code, |
952 // so temporary replace the pointers with offset numbers | 949 // so temporary replace the pointers with offset numbers |
953 // in prologue/epilogue. | 950 // in prologue/epilogue. |
954 heap->IterateRoots(&visitor, VISIT_ALL); | 951 heap->IterateRoots(&visitor, VISIT_ALL); |
955 | 952 |
956 // Now iterate over all pointers of all objects, including code_target | 953 // Now iterate over all pointers of all objects, including code_target |
957 // implicit pointers. | 954 // implicit pointers. |
958 HeapIterator iterator(heap); | |
959 for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) { | 955 for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) { |
960 obj->Iterate(&visitor); | 956 obj->Iterate(&visitor); |
961 } | 957 } |
962 } | 958 } |
963 | 959 |
964 | 960 |
965 // Patch function literals. | 961 // Patch function literals. |
966 // Name 'literals' is a misnomer. Rather it's a cache for complex object | 962 // Name 'literals' is a misnomer. Rather it's a cache for complex object |
967 // boilerplates and for a native context. We must clean cached values. | 963 // boilerplates and for a native context. We must clean cached values. |
968 // Additionally we may need to allocate a new array if number of literals | 964 // Additionally we may need to allocate a new array if number of literals |
969 // changed. | 965 // changed. |
970 class LiteralFixer { | 966 class LiteralFixer { |
971 public: | 967 public: |
972 static void PatchLiterals(FunctionInfoWrapper* compile_info_wrapper, | 968 static void PatchLiterals(FunctionInfoWrapper* compile_info_wrapper, |
973 Handle<SharedFunctionInfo> shared_info, | 969 Handle<SharedFunctionInfo> shared_info, |
974 Isolate* isolate) { | 970 Isolate* isolate) { |
975 int new_literal_count = compile_info_wrapper->GetLiteralCount(); | 971 int new_literal_count = compile_info_wrapper->GetLiteralCount(); |
976 if (new_literal_count > 0) { | 972 if (new_literal_count > 0) { |
977 new_literal_count += JSFunction::kLiteralsPrefixSize; | 973 new_literal_count += JSFunction::kLiteralsPrefixSize; |
978 } | 974 } |
979 int old_literal_count = shared_info->num_literals(); | 975 int old_literal_count = shared_info->num_literals(); |
980 | 976 |
981 if (old_literal_count == new_literal_count) { | 977 if (old_literal_count == new_literal_count) { |
982 // If literal count didn't change, simply go over all functions | 978 // If literal count didn't change, simply go over all functions |
983 // and clear literal arrays. | 979 // and clear literal arrays. |
984 ClearValuesVisitor visitor; | 980 ClearValuesVisitor visitor; |
985 IterateJSFunctions(*shared_info, &visitor); | 981 IterateJSFunctions(shared_info, &visitor); |
986 } else { | 982 } else { |
987 // When literal count changes, we have to create new array instances. | 983 // When literal count changes, we have to create new array instances. |
988 // Since we cannot create instances when iterating heap, we should first | 984 // Since we cannot create instances when iterating heap, we should first |
989 // collect all functions and fix their literal arrays. | 985 // collect all functions and fix their literal arrays. |
990 Handle<FixedArray> function_instances = | 986 Handle<FixedArray> function_instances = |
991 CollectJSFunctions(shared_info, isolate); | 987 CollectJSFunctions(shared_info, isolate); |
992 for (int i = 0; i < function_instances->length(); i++) { | 988 for (int i = 0; i < function_instances->length(); i++) { |
993 Handle<JSFunction> fun(JSFunction::cast(function_instances->get(i))); | 989 Handle<JSFunction> fun(JSFunction::cast(function_instances->get(i))); |
994 Handle<FixedArray> old_literals(fun->literals()); | 990 Handle<FixedArray> old_literals(fun->literals()); |
995 Handle<FixedArray> new_literals = | 991 Handle<FixedArray> new_literals = |
(...skipping 14 matching lines...) Expand all Loading... |
1010 } | 1006 } |
1011 | 1007 |
1012 shared_info->set_num_literals(new_literal_count); | 1008 shared_info->set_num_literals(new_literal_count); |
1013 } | 1009 } |
1014 } | 1010 } |
1015 | 1011 |
1016 private: | 1012 private: |
1017 // Iterates all function instances in the HEAP that refers to the | 1013 // Iterates all function instances in the HEAP that refers to the |
1018 // provided shared_info. | 1014 // provided shared_info. |
1019 template<typename Visitor> | 1015 template<typename Visitor> |
1020 static void IterateJSFunctions(SharedFunctionInfo* shared_info, | 1016 static void IterateJSFunctions(Handle<SharedFunctionInfo> shared_info, |
1021 Visitor* visitor) { | 1017 Visitor* visitor) { |
1022 DisallowHeapAllocation no_allocation; | |
1023 | |
1024 HeapIterator iterator(shared_info->GetHeap()); | 1018 HeapIterator iterator(shared_info->GetHeap()); |
1025 for (HeapObject* obj = iterator.next(); obj != NULL; | 1019 for (HeapObject* obj = iterator.next(); obj != NULL; |
1026 obj = iterator.next()) { | 1020 obj = iterator.next()) { |
1027 if (obj->IsJSFunction()) { | 1021 if (obj->IsJSFunction()) { |
1028 JSFunction* function = JSFunction::cast(obj); | 1022 JSFunction* function = JSFunction::cast(obj); |
1029 if (function->shared() == shared_info) { | 1023 if (function->shared() == *shared_info) { |
1030 visitor->visit(function); | 1024 visitor->visit(function); |
1031 } | 1025 } |
1032 } | 1026 } |
1033 } | 1027 } |
1034 } | 1028 } |
1035 | 1029 |
1036 // Finds all instances of JSFunction that refers to the provided shared_info | 1030 // Finds all instances of JSFunction that refers to the provided shared_info |
1037 // and returns array with them. | 1031 // and returns array with them. |
1038 static Handle<FixedArray> CollectJSFunctions( | 1032 static Handle<FixedArray> CollectJSFunctions( |
1039 Handle<SharedFunctionInfo> shared_info, Isolate* isolate) { | 1033 Handle<SharedFunctionInfo> shared_info, Isolate* isolate) { |
1040 CountVisitor count_visitor; | 1034 CountVisitor count_visitor; |
1041 count_visitor.count = 0; | 1035 count_visitor.count = 0; |
1042 IterateJSFunctions(*shared_info, &count_visitor); | 1036 IterateJSFunctions(shared_info, &count_visitor); |
1043 int size = count_visitor.count; | 1037 int size = count_visitor.count; |
1044 | 1038 |
1045 Handle<FixedArray> result = isolate->factory()->NewFixedArray(size); | 1039 Handle<FixedArray> result = isolate->factory()->NewFixedArray(size); |
1046 if (size > 0) { | 1040 if (size > 0) { |
1047 CollectVisitor collect_visitor(result); | 1041 CollectVisitor collect_visitor(result); |
1048 IterateJSFunctions(*shared_info, &collect_visitor); | 1042 IterateJSFunctions(shared_info, &collect_visitor); |
1049 } | 1043 } |
1050 return result; | 1044 return result; |
1051 } | 1045 } |
1052 | 1046 |
1053 class ClearValuesVisitor { | 1047 class ClearValuesVisitor { |
1054 public: | 1048 public: |
1055 void visit(JSFunction* fun) { | 1049 void visit(JSFunction* fun) { |
1056 FixedArray* literals = fun->literals(); | 1050 FixedArray* literals = fun->literals(); |
1057 int len = literals->length(); | 1051 int len = literals->length(); |
1058 for (int j = JSFunction::kLiteralsPrefixSize; j < len; j++) { | 1052 for (int j = JSFunction::kLiteralsPrefixSize; j < len; j++) { |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1158 void LiveEdit::ReplaceFunctionCode( | 1152 void LiveEdit::ReplaceFunctionCode( |
1159 Handle<JSArray> new_compile_info_array, | 1153 Handle<JSArray> new_compile_info_array, |
1160 Handle<JSArray> shared_info_array) { | 1154 Handle<JSArray> shared_info_array) { |
1161 Isolate* isolate = new_compile_info_array->GetIsolate(); | 1155 Isolate* isolate = new_compile_info_array->GetIsolate(); |
1162 | 1156 |
1163 FunctionInfoWrapper compile_info_wrapper(new_compile_info_array); | 1157 FunctionInfoWrapper compile_info_wrapper(new_compile_info_array); |
1164 SharedInfoWrapper shared_info_wrapper(shared_info_array); | 1158 SharedInfoWrapper shared_info_wrapper(shared_info_array); |
1165 | 1159 |
1166 Handle<SharedFunctionInfo> shared_info = shared_info_wrapper.GetInfo(); | 1160 Handle<SharedFunctionInfo> shared_info = shared_info_wrapper.GetInfo(); |
1167 | 1161 |
1168 isolate->heap()->EnsureHeapIsIterable(); | 1162 isolate->heap()->MakeHeapIterable(); |
1169 | 1163 |
1170 if (IsJSFunctionCode(shared_info->code())) { | 1164 if (IsJSFunctionCode(shared_info->code())) { |
1171 Handle<Code> code = compile_info_wrapper.GetFunctionCode(); | 1165 Handle<Code> code = compile_info_wrapper.GetFunctionCode(); |
1172 ReplaceCodeObject(Handle<Code>(shared_info->code()), code); | 1166 ReplaceCodeObject(Handle<Code>(shared_info->code()), code); |
1173 Handle<Object> code_scope_info = compile_info_wrapper.GetCodeScopeInfo(); | 1167 Handle<Object> code_scope_info = compile_info_wrapper.GetCodeScopeInfo(); |
1174 if (code_scope_info->IsFixedArray()) { | 1168 if (code_scope_info->IsFixedArray()) { |
1175 shared_info->set_scope_info(ScopeInfo::cast(*code_scope_info)); | 1169 shared_info->set_scope_info(ScopeInfo::cast(*code_scope_info)); |
1176 } | 1170 } |
1177 shared_info->DisableOptimization(kLiveEdit); | 1171 shared_info->DisableOptimization(kLiveEdit); |
1178 // Update the type feedback vector | 1172 // Update the type feedback vector |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1395 position_change_array); | 1389 position_change_array); |
1396 int new_function_end = TranslatePosition(info->end_position(), | 1390 int new_function_end = TranslatePosition(info->end_position(), |
1397 position_change_array); | 1391 position_change_array); |
1398 int new_function_token_pos = | 1392 int new_function_token_pos = |
1399 TranslatePosition(info->function_token_position(), position_change_array); | 1393 TranslatePosition(info->function_token_position(), position_change_array); |
1400 | 1394 |
1401 info->set_start_position(new_function_start); | 1395 info->set_start_position(new_function_start); |
1402 info->set_end_position(new_function_end); | 1396 info->set_end_position(new_function_end); |
1403 info->set_function_token_position(new_function_token_pos); | 1397 info->set_function_token_position(new_function_token_pos); |
1404 | 1398 |
1405 info->GetIsolate()->heap()->EnsureHeapIsIterable(); | 1399 info->GetIsolate()->heap()->MakeHeapIterable(); |
1406 | 1400 |
1407 if (IsJSFunctionCode(info->code())) { | 1401 if (IsJSFunctionCode(info->code())) { |
1408 // Patch relocation info section of the code. | 1402 // Patch relocation info section of the code. |
1409 Handle<Code> patched_code = PatchPositionsInCode(Handle<Code>(info->code()), | 1403 Handle<Code> patched_code = PatchPositionsInCode(Handle<Code>(info->code()), |
1410 position_change_array); | 1404 position_change_array); |
1411 if (*patched_code != info->code()) { | 1405 if (*patched_code != info->code()) { |
1412 // Replace all references to the code across the heap. In particular, | 1406 // Replace all references to the code across the heap. In particular, |
1413 // some stubs may refer to this code and this code may be being executed | 1407 // some stubs may refer to this code and this code may be being executed |
1414 // on stack (it is safe to substitute the code object on stack, because | 1408 // on stack (it is safe to substitute the code object on stack, because |
1415 // we only change the structure of rinfo and leave instructions | 1409 // we only change the structure of rinfo and leave instructions |
(...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1833 } | 1827 } |
1834 } | 1828 } |
1835 return NULL; | 1829 return NULL; |
1836 } | 1830 } |
1837 | 1831 |
1838 | 1832 |
1839 bool LiveEdit::FindActiveGenerators(Handle<FixedArray> shared_info_array, | 1833 bool LiveEdit::FindActiveGenerators(Handle<FixedArray> shared_info_array, |
1840 Handle<FixedArray> result, | 1834 Handle<FixedArray> result, |
1841 int len) { | 1835 int len) { |
1842 Isolate* isolate = shared_info_array->GetIsolate(); | 1836 Isolate* isolate = shared_info_array->GetIsolate(); |
1843 Heap* heap = isolate->heap(); | |
1844 heap->EnsureHeapIsIterable(); | |
1845 bool found_suspended_activations = false; | 1837 bool found_suspended_activations = false; |
1846 | 1838 |
1847 ASSERT_LE(len, result->length()); | 1839 ASSERT_LE(len, result->length()); |
1848 | 1840 |
1849 DisallowHeapAllocation no_allocation; | |
1850 | |
1851 FunctionPatchabilityStatus active = FUNCTION_BLOCKED_ACTIVE_GENERATOR; | 1841 FunctionPatchabilityStatus active = FUNCTION_BLOCKED_ACTIVE_GENERATOR; |
1852 | 1842 |
| 1843 Heap* heap = isolate->heap(); |
1853 HeapIterator iterator(heap); | 1844 HeapIterator iterator(heap); |
1854 HeapObject* obj = NULL; | 1845 HeapObject* obj = NULL; |
1855 while ((obj = iterator.next()) != NULL) { | 1846 while ((obj = iterator.next()) != NULL) { |
1856 if (!obj->IsJSGeneratorObject()) continue; | 1847 if (!obj->IsJSGeneratorObject()) continue; |
1857 | 1848 |
1858 JSGeneratorObject* gen = JSGeneratorObject::cast(obj); | 1849 JSGeneratorObject* gen = JSGeneratorObject::cast(obj); |
1859 if (gen->is_closed()) continue; | 1850 if (gen->is_closed()) continue; |
1860 | 1851 |
1861 HandleScope scope(isolate); | 1852 HandleScope scope(isolate); |
1862 | 1853 |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2028 void LiveEditFunctionTracker::RecordRootFunctionInfo(Handle<Code> code) { | 2019 void LiveEditFunctionTracker::RecordRootFunctionInfo(Handle<Code> code) { |
2029 isolate_->active_function_info_listener()->FunctionCode(code); | 2020 isolate_->active_function_info_listener()->FunctionCode(code); |
2030 } | 2021 } |
2031 | 2022 |
2032 | 2023 |
2033 bool LiveEditFunctionTracker::IsActive(Isolate* isolate) { | 2024 bool LiveEditFunctionTracker::IsActive(Isolate* isolate) { |
2034 return isolate->active_function_info_listener() != NULL; | 2025 return isolate->active_function_info_listener() != NULL; |
2035 } | 2026 } |
2036 | 2027 |
2037 } } // namespace v8::internal | 2028 } } // namespace v8::internal |
OLD | NEW |