| 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 #include "src/api.h" | 5 #include "src/api.h" |
| 6 | 6 |
| 7 #include <string.h> // For memcpy, strlen. | 7 #include <string.h> // For memcpy, strlen. |
| 8 #ifdef V8_USE_ADDRESS_SANITIZER | 8 #ifdef V8_USE_ADDRESS_SANITIZER |
| 9 #include <sanitizer/asan_interface.h> | 9 #include <sanitizer/asan_interface.h> |
| 10 #endif // V8_USE_ADDRESS_SANITIZER | 10 #endif // V8_USE_ADDRESS_SANITIZER |
| (...skipping 5114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5125 static inline int WriteHelper(const String* string, | 5125 static inline int WriteHelper(const String* string, |
| 5126 CharType* buffer, | 5126 CharType* buffer, |
| 5127 int start, | 5127 int start, |
| 5128 int length, | 5128 int length, |
| 5129 int options) { | 5129 int options) { |
| 5130 i::Isolate* isolate = Utils::OpenHandle(string)->GetIsolate(); | 5130 i::Isolate* isolate = Utils::OpenHandle(string)->GetIsolate(); |
| 5131 LOG_API(isolate, "String::Write"); | 5131 LOG_API(isolate, "String::Write"); |
| 5132 ENTER_V8(isolate); | 5132 ENTER_V8(isolate); |
| 5133 DCHECK(start >= 0 && length >= -1); | 5133 DCHECK(start >= 0 && length >= -1); |
| 5134 i::Handle<i::String> str = Utils::OpenHandle(string); | 5134 i::Handle<i::String> str = Utils::OpenHandle(string); |
| 5135 isolate->string_tracker()->RecordWrite(str); | |
| 5136 if (options & String::HINT_MANY_WRITES_EXPECTED) { | 5135 if (options & String::HINT_MANY_WRITES_EXPECTED) { |
| 5137 // Flatten the string for efficiency. This applies whether we are | 5136 // Flatten the string for efficiency. This applies whether we are |
| 5138 // using StringCharacterStream or Get(i) to access the characters. | 5137 // using StringCharacterStream or Get(i) to access the characters. |
| 5139 str = i::String::Flatten(str); | 5138 str = i::String::Flatten(str); |
| 5140 } | 5139 } |
| 5141 int end = start + length; | 5140 int end = start + length; |
| 5142 if ((length == -1) || (length > str->length() - start) ) | 5141 if ((length == -1) || (length > str->length() - start) ) |
| 5143 end = str->length(); | 5142 end = str->length(); |
| 5144 if (end < 0) return 0; | 5143 if (end < 0) return 0; |
| 5145 i::String::WriteToFlat(*str, buffer, start, end); | 5144 i::String::WriteToFlat(*str, buffer, start, end); |
| (...skipping 685 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5831 } | 5830 } |
| 5832 | 5831 |
| 5833 | 5832 |
| 5834 bool v8::String::MakeExternal(v8::String::ExternalStringResource* resource) { | 5833 bool v8::String::MakeExternal(v8::String::ExternalStringResource* resource) { |
| 5835 i::Handle<i::String> obj = Utils::OpenHandle(this); | 5834 i::Handle<i::String> obj = Utils::OpenHandle(this); |
| 5836 i::Isolate* isolate = obj->GetIsolate(); | 5835 i::Isolate* isolate = obj->GetIsolate(); |
| 5837 if (i::StringShape(*obj).IsExternal()) { | 5836 if (i::StringShape(*obj).IsExternal()) { |
| 5838 return false; // Already an external string. | 5837 return false; // Already an external string. |
| 5839 } | 5838 } |
| 5840 ENTER_V8(isolate); | 5839 ENTER_V8(isolate); |
| 5841 if (isolate->string_tracker()->IsFreshUnusedString(obj)) { | |
| 5842 return false; | |
| 5843 } | |
| 5844 if (isolate->heap()->IsInGCPostProcessing()) { | 5840 if (isolate->heap()->IsInGCPostProcessing()) { |
| 5845 return false; | 5841 return false; |
| 5846 } | 5842 } |
| 5847 CHECK(resource && resource->data()); | 5843 CHECK(resource && resource->data()); |
| 5848 | 5844 |
| 5849 bool result = obj->MakeExternal(resource); | 5845 bool result = obj->MakeExternal(resource); |
| 5850 // Assert that if CanMakeExternal(), then externalizing actually succeeds. | 5846 // Assert that if CanMakeExternal(), then externalizing actually succeeds. |
| 5851 DCHECK(!CanMakeExternal() || result); | 5847 DCHECK(!CanMakeExternal() || result); |
| 5852 if (result) { | 5848 if (result) { |
| 5853 DCHECK(obj->IsExternalString()); | 5849 DCHECK(obj->IsExternalString()); |
| 5854 isolate->heap()->external_string_table()->AddString(*obj); | 5850 isolate->heap()->external_string_table()->AddString(*obj); |
| 5855 } | 5851 } |
| 5856 return result; | 5852 return result; |
| 5857 } | 5853 } |
| 5858 | 5854 |
| 5859 | 5855 |
| 5860 bool v8::String::MakeExternal( | 5856 bool v8::String::MakeExternal( |
| 5861 v8::String::ExternalOneByteStringResource* resource) { | 5857 v8::String::ExternalOneByteStringResource* resource) { |
| 5862 i::Handle<i::String> obj = Utils::OpenHandle(this); | 5858 i::Handle<i::String> obj = Utils::OpenHandle(this); |
| 5863 i::Isolate* isolate = obj->GetIsolate(); | 5859 i::Isolate* isolate = obj->GetIsolate(); |
| 5864 if (i::StringShape(*obj).IsExternal()) { | 5860 if (i::StringShape(*obj).IsExternal()) { |
| 5865 return false; // Already an external string. | 5861 return false; // Already an external string. |
| 5866 } | 5862 } |
| 5867 ENTER_V8(isolate); | 5863 ENTER_V8(isolate); |
| 5868 if (isolate->string_tracker()->IsFreshUnusedString(obj)) { | |
| 5869 return false; | |
| 5870 } | |
| 5871 if (isolate->heap()->IsInGCPostProcessing()) { | 5864 if (isolate->heap()->IsInGCPostProcessing()) { |
| 5872 return false; | 5865 return false; |
| 5873 } | 5866 } |
| 5874 CHECK(resource && resource->data()); | 5867 CHECK(resource && resource->data()); |
| 5875 | 5868 |
| 5876 bool result = obj->MakeExternal(resource); | 5869 bool result = obj->MakeExternal(resource); |
| 5877 // Assert that if CanMakeExternal(), then externalizing actually succeeds. | 5870 // Assert that if CanMakeExternal(), then externalizing actually succeeds. |
| 5878 DCHECK(!CanMakeExternal() || result); | 5871 DCHECK(!CanMakeExternal() || result); |
| 5879 if (result) { | 5872 if (result) { |
| 5880 DCHECK(obj->IsExternalString()); | 5873 DCHECK(obj->IsExternalString()); |
| 5881 isolate->heap()->external_string_table()->AddString(*obj); | 5874 isolate->heap()->external_string_table()->AddString(*obj); |
| 5882 } | 5875 } |
| 5883 return result; | 5876 return result; |
| 5884 } | 5877 } |
| 5885 | 5878 |
| 5886 | 5879 |
| 5887 bool v8::String::CanMakeExternal() { | 5880 bool v8::String::CanMakeExternal() { |
| 5888 i::Handle<i::String> obj = Utils::OpenHandle(this); | 5881 i::Handle<i::String> obj = Utils::OpenHandle(this); |
| 5889 i::Isolate* isolate = obj->GetIsolate(); | 5882 i::Isolate* isolate = obj->GetIsolate(); |
| 5890 | 5883 |
| 5891 if (isolate->string_tracker()->IsFreshUnusedString(obj)) return false; | 5884 // Old space strings should be externalized. |
| 5885 if (!isolate->heap()->new_space()->Contains(*obj)) return true; |
| 5892 int size = obj->Size(); // Byte size of the original string. | 5886 int size = obj->Size(); // Byte size of the original string. |
| 5893 if (size < i::ExternalString::kShortSize) return false; | 5887 if (size <= i::ExternalString::kShortSize) return false; |
| 5894 i::StringShape shape(*obj); | 5888 i::StringShape shape(*obj); |
| 5895 return !shape.IsExternal(); | 5889 return !shape.IsExternal(); |
| 5896 } | 5890 } |
| 5897 | 5891 |
| 5898 | 5892 |
| 5899 Isolate* v8::Object::GetIsolate() { | 5893 Isolate* v8::Object::GetIsolate() { |
| 5900 i::Isolate* i_isolate = Utils::OpenHandle(this)->GetIsolate(); | 5894 i::Isolate* i_isolate = Utils::OpenHandle(this)->GetIsolate(); |
| 5901 return reinterpret_cast<Isolate*>(i_isolate); | 5895 return reinterpret_cast<Isolate*>(i_isolate); |
| 5902 } | 5896 } |
| 5903 | 5897 |
| (...skipping 2499 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 8403 Address callback_address = | 8397 Address callback_address = |
| 8404 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback)); | 8398 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback)); |
| 8405 VMState<EXTERNAL> state(isolate); | 8399 VMState<EXTERNAL> state(isolate); |
| 8406 ExternalCallbackScope call_scope(isolate, callback_address); | 8400 ExternalCallbackScope call_scope(isolate, callback_address); |
| 8407 callback(info); | 8401 callback(info); |
| 8408 } | 8402 } |
| 8409 | 8403 |
| 8410 | 8404 |
| 8411 } // namespace internal | 8405 } // namespace internal |
| 8412 } // namespace v8 | 8406 } // namespace v8 |
| OLD | NEW |