Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "vm/object.h" | 5 #include "vm/object.h" |
| 6 | 6 |
| 7 #include "include/dart_api.h" | 7 #include "include/dart_api.h" |
| 8 #include "platform/assert.h" | 8 #include "platform/assert.h" |
| 9 #include "vm/assembler.h" | 9 #include "vm/assembler.h" |
| 10 #include "vm/cpu.h" | 10 #include "vm/cpu.h" |
| (...skipping 8507 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 8518 JSONObject jsobj(stream); | 8518 JSONObject jsobj(stream); |
| 8519 AddTypeProperties(&jsobj, "Script", JSONType(), ref); | 8519 AddTypeProperties(&jsobj, "Script", JSONType(), ref); |
| 8520 const String& name = String::Handle(url()); | 8520 const String& name = String::Handle(url()); |
| 8521 ASSERT(!name.IsNull()); | 8521 ASSERT(!name.IsNull()); |
| 8522 const String& encoded_url = String::Handle(String::EncodeIRI(name)); | 8522 const String& encoded_url = String::Handle(String::EncodeIRI(name)); |
| 8523 ASSERT(!encoded_url.IsNull()); | 8523 ASSERT(!encoded_url.IsNull()); |
| 8524 const Library& lib = Library::Handle(FindLibrary()); | 8524 const Library& lib = Library::Handle(FindLibrary()); |
| 8525 intptr_t lib_index = (lib.IsNull()) ? -1 : lib.index(); | 8525 intptr_t lib_index = (lib.IsNull()) ? -1 : lib.index(); |
| 8526 jsobj.AddPropertyF("id", "libraries/%" Pd "/scripts/%s", | 8526 jsobj.AddPropertyF("id", "libraries/%" Pd "/scripts/%s", |
| 8527 lib_index, encoded_url.ToCString()); | 8527 lib_index, encoded_url.ToCString()); |
| 8528 jsobj.AddProperty("name", name.ToCString()); | 8528 jsobj.AddPropertyS("name", name, name.Length()); |
|
turnidge
2014/09/10 19:55:28
I'm thinking it would be nicer to have the length
rmacnak
2014/09/10 21:38:49
Done.
| |
| 8529 jsobj.AddProperty("kind", GetKindAsCString()); | 8529 jsobj.AddProperty("kind", GetKindAsCString()); |
| 8530 if (ref) { | 8530 if (ref) { |
| 8531 return; | 8531 return; |
| 8532 } | 8532 } |
| 8533 jsobj.AddProperty("owningLibrary", lib); | 8533 jsobj.AddProperty("owningLibrary", lib); |
| 8534 const String& source = String::Handle(Source()); | 8534 const String& source = String::Handle(Source()); |
| 8535 jsobj.AddProperty("source", source.ToCString()); | 8535 jsobj.AddPropertyS("source", source, source.Length()); |
| 8536 | 8536 |
| 8537 // Print the line number table | 8537 // Print the line number table |
| 8538 { | 8538 { |
| 8539 JSONArray tokenPosTable(&jsobj, "tokenPosTable"); | 8539 JSONArray tokenPosTable(&jsobj, "tokenPosTable"); |
| 8540 | 8540 |
| 8541 const GrowableObjectArray& lineNumberArray = | 8541 const GrowableObjectArray& lineNumberArray = |
| 8542 GrowableObjectArray::Handle(GenerateLineNumberArray()); | 8542 GrowableObjectArray::Handle(GenerateLineNumberArray()); |
| 8543 Object& value = Object::Handle(); | 8543 Object& value = Object::Handle(); |
| 8544 intptr_t pos = 0; | 8544 intptr_t pos = 0; |
| 8545 | 8545 |
| (...skipping 1259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 9805 | 9805 |
| 9806 | 9806 |
| 9807 void Library::PrintJSONImpl(JSONStream* stream, bool ref) const { | 9807 void Library::PrintJSONImpl(JSONStream* stream, bool ref) const { |
| 9808 const char* library_name = String::Handle(name()).ToCString(); | 9808 const char* library_name = String::Handle(name()).ToCString(); |
| 9809 intptr_t id = index(); | 9809 intptr_t id = index(); |
| 9810 ASSERT(id >= 0); | 9810 ASSERT(id >= 0); |
| 9811 JSONObject jsobj(stream); | 9811 JSONObject jsobj(stream); |
| 9812 AddTypeProperties(&jsobj, "Library", JSONType(), ref); | 9812 AddTypeProperties(&jsobj, "Library", JSONType(), ref); |
| 9813 jsobj.AddPropertyF("id", "libraries/%" Pd "", id); | 9813 jsobj.AddPropertyF("id", "libraries/%" Pd "", id); |
| 9814 jsobj.AddProperty("name", library_name); | 9814 jsobj.AddProperty("name", library_name); |
| 9815 const char* library_url = String::Handle(url()).ToCString(); | 9815 const String& library_url = String::Handle(url()); |
| 9816 jsobj.AddProperty("url", library_url); | 9816 jsobj.AddPropertyS("url", library_url, library_url.Length()); |
| 9817 if (ref) { | 9817 if (ref) { |
| 9818 return; | 9818 return; |
| 9819 } | 9819 } |
| 9820 { | 9820 { |
| 9821 JSONArray jsarr(&jsobj, "classes"); | 9821 JSONArray jsarr(&jsobj, "classes"); |
| 9822 ClassDictionaryIterator class_iter(*this); | 9822 ClassDictionaryIterator class_iter(*this); |
| 9823 Class& klass = Class::Handle(); | 9823 Class& klass = Class::Handle(); |
| 9824 while (class_iter.HasNext()) { | 9824 while (class_iter.HasNext()) { |
| 9825 klass = class_iter.GetNextClass(); | 9825 klass = class_iter.GetNextClass(); |
| 9826 if (!klass.IsCanonicalSignatureClass() && | 9826 if (!klass.IsCanonicalSignatureClass() && |
| (...skipping 7893 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 17720 result = OneByteString::New(length, space); | 17720 result = OneByteString::New(length, space); |
| 17721 } else { | 17721 } else { |
| 17722 result = TwoByteString::New(length, space); | 17722 result = TwoByteString::New(length, space); |
| 17723 } | 17723 } |
| 17724 String::Copy(result, 0, str, begin_index, length); | 17724 String::Copy(result, 0, str, begin_index, length); |
| 17725 return result.raw(); | 17725 return result.raw(); |
| 17726 } | 17726 } |
| 17727 | 17727 |
| 17728 | 17728 |
| 17729 const char* String::ToCString() const { | 17729 const char* String::ToCString() const { |
| 17730 intptr_t length; | |
| 17731 return ToCString(&length); | |
| 17732 } | |
| 17733 | |
| 17734 | |
| 17735 const char* String::ToCString(intptr_t* length) const { | |
| 17736 if (IsOneByteString()) { | 17730 if (IsOneByteString()) { |
| 17737 // Quick conversion if OneByteString contains only ASCII characters. | 17731 // Quick conversion if OneByteString contains only ASCII characters. |
| 17738 intptr_t len = Length(); | 17732 intptr_t len = Length(); |
| 17739 if (len == 0) { | 17733 if (len == 0) { |
| 17740 *length = 0; | |
| 17741 return ""; | 17734 return ""; |
| 17742 } | 17735 } |
| 17743 Zone* zone = Isolate::Current()->current_zone(); | 17736 Zone* zone = Isolate::Current()->current_zone(); |
| 17744 uint8_t* result = zone->Alloc<uint8_t>(len + 1); | 17737 uint8_t* result = zone->Alloc<uint8_t>(len + 1); |
| 17745 NoGCScope no_gc; | 17738 NoGCScope no_gc; |
| 17746 const uint8_t* original_str = OneByteString::CharAddr(*this, 0); | 17739 const uint8_t* original_str = OneByteString::CharAddr(*this, 0); |
| 17747 for (intptr_t i = 0; i < len; i++) { | 17740 for (intptr_t i = 0; i < len; i++) { |
| 17748 if (original_str[i] <= Utf8::kMaxOneByteChar) { | 17741 if (original_str[i] <= Utf8::kMaxOneByteChar) { |
| 17749 result[i] = original_str[i]; | 17742 result[i] = original_str[i]; |
| 17750 } else { | 17743 } else { |
| 17751 len = -1; | 17744 len = -1; |
| 17752 break; | 17745 break; |
| 17753 } | 17746 } |
| 17754 } | 17747 } |
| 17755 if (len > 0) { | 17748 if (len > 0) { |
| 17756 result[len] = 0; | 17749 result[len] = 0; |
| 17757 *length = len; | |
| 17758 return reinterpret_cast<const char*>(result); | 17750 return reinterpret_cast<const char*>(result); |
| 17759 } | 17751 } |
| 17760 } | 17752 } |
| 17761 const intptr_t len = Utf8::Length(*this); | 17753 const intptr_t len = Utf8::Length(*this); |
| 17762 Zone* zone = Isolate::Current()->current_zone(); | 17754 Zone* zone = Isolate::Current()->current_zone(); |
| 17763 uint8_t* result = zone->Alloc<uint8_t>(len + 1); | 17755 uint8_t* result = zone->Alloc<uint8_t>(len + 1); |
| 17764 ToUTF8(result, len); | 17756 ToUTF8(result, len); |
| 17765 result[len] = 0; | 17757 result[len] = 0; |
| 17766 *length = len; | |
| 17767 return reinterpret_cast<const char*>(result); | 17758 return reinterpret_cast<const char*>(result); |
| 17768 } | 17759 } |
| 17769 | 17760 |
| 17770 | 17761 |
| 17771 const char* String::ToCStringTruncated(intptr_t max_len, | |
| 17772 bool* did_truncate, | |
| 17773 intptr_t* length) const { | |
| 17774 if (Length() <= max_len) { | |
| 17775 *did_truncate = false; | |
| 17776 return ToCString(length); | |
| 17777 } | |
| 17778 | |
| 17779 intptr_t aligned_limit = max_len; | |
| 17780 if (Utf16::IsLeadSurrogate(CharAt(max_len - 1))) { | |
| 17781 // Don't let truncation split a surrogate pair. | |
| 17782 aligned_limit--; | |
| 17783 } | |
| 17784 ASSERT(!Utf16::IsLeadSurrogate(CharAt(aligned_limit - 1))); | |
| 17785 | |
| 17786 *did_truncate = true; | |
| 17787 const String& truncated = | |
| 17788 String::Handle(String::SubString(*this, 0, aligned_limit)); | |
| 17789 return truncated.ToCString(length); | |
| 17790 } | |
| 17791 | |
| 17792 | |
| 17793 void String::PrintJSONImpl(JSONStream* stream, bool ref) const { | 17762 void String::PrintJSONImpl(JSONStream* stream, bool ref) const { |
| 17794 JSONObject jsobj(stream); | 17763 JSONObject jsobj(stream); |
| 17795 if (raw() == Symbols::OptimizedOut().raw()) { | 17764 if (raw() == Symbols::OptimizedOut().raw()) { |
| 17796 // TODO(turnidge): This is a hack. The user could have this | 17765 // TODO(turnidge): This is a hack. The user could have this |
| 17797 // special string in their program. Fixing this involves updating | 17766 // special string in their program. Fixing this involves updating |
| 17798 // the debugging api a bit. | 17767 // the debugging api a bit. |
| 17799 jsobj.AddProperty("type", "Sentinel"); | 17768 jsobj.AddProperty("type", "Sentinel"); |
| 17800 jsobj.AddProperty("id", "objects/optimized-out"); | 17769 jsobj.AddProperty("id", "objects/optimized-out"); |
| 17801 jsobj.AddProperty("valueAsString", "<optimized out>"); | 17770 jsobj.AddProperty("valueAsString", "<optimized out>"); |
| 17802 return; | 17771 return; |
| 17803 } | 17772 } |
| 17804 AddTypeProperties(&jsobj, "String", JSONType(), ref); | 17773 AddTypeProperties(&jsobj, "String", JSONType(), ref); |
| 17805 PrintSharedInstanceJSON(&jsobj, ref); | 17774 PrintSharedInstanceJSON(&jsobj, ref); |
| 17806 ObjectIdRing* ring = Isolate::Current()->object_id_ring(); | 17775 ObjectIdRing* ring = Isolate::Current()->object_id_ring(); |
| 17807 const intptr_t id = ring->GetIdForObject(raw()); | 17776 const intptr_t id = ring->GetIdForObject(raw()); |
| 17808 jsobj.AddPropertyF("id", "objects/%" Pd "", id); | 17777 jsobj.AddPropertyF("id", "objects/%" Pd "", id); |
| 17809 if (ref) { | 17778 if (ref) { |
| 17810 bool did_truncate = false; | 17779 bool did_truncate = jsobj.AddPropertyS("valueAsString", *this, 128); |
| 17811 intptr_t length = 0; | |
| 17812 const char* cstr = ToCStringTruncated(128, &did_truncate, &length); | |
| 17813 jsobj.AddProperty("valueAsString", cstr, length); | |
| 17814 if (did_truncate) { | 17780 if (did_truncate) { |
| 17815 jsobj.AddProperty("valueAsStringIsTruncated", did_truncate); | 17781 jsobj.AddProperty("valueAsStringIsTruncated", did_truncate); |
| 17816 } | 17782 } |
| 17817 } else { | 17783 } else { |
| 17818 intptr_t length = 0; | 17784 bool did_truncate = jsobj.AddPropertyS("valueAsString", *this, Length()); |
| 17819 const char* cstr = ToCString(&length); | 17785 ASSERT(!did_truncate); |
| 17820 jsobj.AddProperty("valueAsString", cstr, length); | |
| 17821 } | 17786 } |
| 17822 } | 17787 } |
| 17823 | 17788 |
| 17824 | 17789 |
| 17825 void String::ToUTF8(uint8_t* utf8_array, intptr_t array_len) const { | 17790 void String::ToUTF8(uint8_t* utf8_array, intptr_t array_len) const { |
| 17826 ASSERT(array_len >= Utf8::Length(*this)); | 17791 ASSERT(array_len >= Utf8::Length(*this)); |
| 17827 Utf8::Encode(*this, reinterpret_cast<char*>(utf8_array), array_len); | 17792 Utf8::Encode(*this, reinterpret_cast<char*>(utf8_array), array_len); |
| 17828 } | 17793 } |
| 17829 | 17794 |
| 17830 | 17795 |
| (...skipping 2523 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 20354 return tag_label.ToCString(); | 20319 return tag_label.ToCString(); |
| 20355 } | 20320 } |
| 20356 | 20321 |
| 20357 | 20322 |
| 20358 void UserTag::PrintJSONImpl(JSONStream* stream, bool ref) const { | 20323 void UserTag::PrintJSONImpl(JSONStream* stream, bool ref) const { |
| 20359 Instance::PrintJSONImpl(stream, ref); | 20324 Instance::PrintJSONImpl(stream, ref); |
| 20360 } | 20325 } |
| 20361 | 20326 |
| 20362 | 20327 |
| 20363 } // namespace dart | 20328 } // namespace dart |
| OLD | NEW |