| 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 8425 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 8436 return Library::null(); | 8436 return Library::null(); |
| 8437 } | 8437 } |
| 8438 | 8438 |
| 8439 | 8439 |
| 8440 // See also Dart_ScriptGetTokenInfo. | 8440 // See also Dart_ScriptGetTokenInfo. |
| 8441 void Script::PrintJSONImpl(JSONStream* stream, bool ref) const { | 8441 void Script::PrintJSONImpl(JSONStream* stream, bool ref) const { |
| 8442 JSONObject jsobj(stream); | 8442 JSONObject jsobj(stream); |
| 8443 jsobj.AddProperty("type", JSONType(ref)); | 8443 jsobj.AddProperty("type", JSONType(ref)); |
| 8444 const String& name = String::Handle(url()); | 8444 const String& name = String::Handle(url()); |
| 8445 ASSERT(!name.IsNull()); | 8445 ASSERT(!name.IsNull()); |
| 8446 const String& encoded_url = String::Handle(String::EncodeURI(name)); | 8446 const String& encoded_url = String::Handle(String::EncodeIRI(name)); |
| 8447 ASSERT(!encoded_url.IsNull()); | 8447 ASSERT(!encoded_url.IsNull()); |
| 8448 const Library& lib = Library::Handle(FindLibrary()); | 8448 const Library& lib = Library::Handle(FindLibrary()); |
| 8449 intptr_t lib_index = (lib.IsNull()) ? -1 : lib.index(); | 8449 intptr_t lib_index = (lib.IsNull()) ? -1 : lib.index(); |
| 8450 jsobj.AddPropertyF("id", "libraries/%" Pd "/scripts/%s", | 8450 jsobj.AddPropertyF("id", "libraries/%" Pd "/scripts/%s", |
| 8451 lib_index, encoded_url.ToCString()); | 8451 lib_index, encoded_url.ToCString()); |
| 8452 jsobj.AddProperty("name", name.ToCString()); | 8452 jsobj.AddProperty("name", name.ToCString()); |
| 8453 jsobj.AddProperty("user_name", name.ToCString()); | 8453 jsobj.AddProperty("user_name", name.ToCString()); |
| 8454 jsobj.AddProperty("kind", GetKindAsCString()); | 8454 jsobj.AddProperty("kind", GetKindAsCString()); |
| 8455 if (ref) { | 8455 if (ref) { |
| 8456 return; | 8456 return; |
| (...skipping 8240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 16697 UNREACHABLE(); | 16697 UNREACHABLE(); |
| 16698 return 0; | 16698 return 0; |
| 16699 } | 16699 } |
| 16700 | 16700 |
| 16701 | 16701 |
| 16702 static int32_t MergeHexCharacters(int32_t c1, int32_t c2) { | 16702 static int32_t MergeHexCharacters(int32_t c1, int32_t c2) { |
| 16703 return GetHexValue(c1) << 4 | GetHexValue(c2); | 16703 return GetHexValue(c1) << 4 | GetHexValue(c2); |
| 16704 } | 16704 } |
| 16705 | 16705 |
| 16706 | 16706 |
| 16707 RawString* String::EncodeURI(const String& str) { | 16707 RawString* String::EncodeIRI(const String& str) { |
| 16708 // URI encoding is only specified for one byte strings. | 16708 const intptr_t len = Utf8::Length(str); |
| 16709 ASSERT(str.IsOneByteString() || str.IsExternalOneByteString()); | 16709 Zone* zone = Isolate::Current()->current_zone(); |
| 16710 uint8_t* utf8 = zone->Alloc<uint8_t>(len); |
| 16711 str.ToUTF8(utf8, len); |
| 16710 intptr_t num_escapes = 0; | 16712 intptr_t num_escapes = 0; |
| 16711 intptr_t len = str.Length(); | 16713 for (int i = 0; i < len; ++i) { |
| 16712 { | 16714 uint8_t byte = utf8[i]; |
| 16713 CodePointIterator cpi(str); | 16715 if (!IsURISafeCharacter(byte)) { |
| 16714 while (cpi.Next()) { | 16716 num_escapes += 2; |
| 16715 int32_t code_point = cpi.Current(); | |
| 16716 if (!IsURISafeCharacter(code_point)) { | |
| 16717 num_escapes += 2; | |
| 16718 } | |
| 16719 } | 16717 } |
| 16720 } | 16718 } |
| 16721 const String& dststr = String::Handle( | 16719 const String& dststr = String::Handle( |
| 16722 OneByteString::New(len + num_escapes, Heap::kNew)); | 16720 OneByteString::New(len + num_escapes, Heap::kNew)); |
| 16723 { | 16721 { |
| 16724 intptr_t index = 0; | 16722 intptr_t index = 0; |
| 16725 CodePointIterator cpi(str); | 16723 for (int i = 0; i < len; ++i) { |
| 16726 while (cpi.Next()) { | 16724 uint8_t byte = utf8[i]; |
| 16727 int32_t code_point = cpi.Current(); | 16725 if (!IsURISafeCharacter(byte)) { |
| 16728 if (!IsURISafeCharacter(code_point)) { | |
| 16729 OneByteString::SetCharAt(dststr, index, '%'); | 16726 OneByteString::SetCharAt(dststr, index, '%'); |
| 16730 OneByteString::SetCharAt(dststr, index + 1, | 16727 OneByteString::SetCharAt(dststr, index + 1, |
| 16731 GetHexCharacter(code_point >> 4)); | 16728 GetHexCharacter(byte >> 4)); |
| 16732 OneByteString::SetCharAt(dststr, index + 2, | 16729 OneByteString::SetCharAt(dststr, index + 2, |
| 16733 GetHexCharacter(code_point & 0xF)); | 16730 GetHexCharacter(byte & 0xF)); |
| 16734 index += 3; | 16731 index += 3; |
| 16735 } else { | 16732 } else { |
| 16736 OneByteString::SetCharAt(dststr, index, code_point); | 16733 ASSERT(byte <= 127); |
| 16734 OneByteString::SetCharAt(dststr, index, byte); |
| 16737 index += 1; | 16735 index += 1; |
| 16738 } | 16736 } |
| 16739 } | 16737 } |
| 16740 } | 16738 } |
| 16741 return dststr.raw(); | 16739 return dststr.raw(); |
| 16742 } | 16740 } |
| 16743 | 16741 |
| 16744 | 16742 |
| 16745 RawString* String::DecodeURI(const String& str) { | 16743 RawString* String::DecodeIRI(const String& str) { |
| 16746 // URI encoding is only specified for one byte strings. | |
| 16747 ASSERT(str.IsOneByteString() || str.IsExternalOneByteString()); | |
| 16748 CodePointIterator cpi(str); | 16744 CodePointIterator cpi(str); |
| 16749 intptr_t num_escapes = 0; | 16745 intptr_t num_escapes = 0; |
| 16750 intptr_t len = str.Length(); | 16746 intptr_t len = str.Length(); |
| 16751 { | 16747 { |
| 16752 CodePointIterator cpi(str); | 16748 CodePointIterator cpi(str); |
| 16753 while (cpi.Next()) { | 16749 while (cpi.Next()) { |
| 16754 int32_t code_point = cpi.Current(); | 16750 int32_t code_point = cpi.Current(); |
| 16755 if (IsPercent(code_point)) { | 16751 if (IsPercent(code_point)) { |
| 16756 // Verify that the two characters following the % are hex digits. | 16752 // Verify that the two characters following the % are hex digits. |
| 16757 if (!cpi.Next()) { | 16753 if (!cpi.Next()) { |
| 16758 return str.raw(); | 16754 return String::null(); |
| 16759 } | 16755 } |
| 16760 int32_t code_point = cpi.Current(); | 16756 int32_t code_point = cpi.Current(); |
| 16761 if (!IsHexCharacter(code_point)) { | 16757 if (!IsHexCharacter(code_point)) { |
| 16762 return str.raw(); | 16758 return String::null(); |
| 16763 } | 16759 } |
| 16764 if (!cpi.Next()) { | 16760 if (!cpi.Next()) { |
| 16765 return str.raw(); | 16761 return String::null(); |
| 16766 } | 16762 } |
| 16767 code_point = cpi.Current(); | 16763 code_point = cpi.Current(); |
| 16768 if (!IsHexCharacter(code_point)) { | 16764 if (!IsHexCharacter(code_point)) { |
| 16769 return str.raw(); | 16765 return str.raw(); |
| 16770 } | 16766 } |
| 16771 num_escapes += 2; | 16767 num_escapes += 2; |
| 16772 } | 16768 } |
| 16773 } | 16769 } |
| 16774 } | 16770 } |
| 16775 ASSERT(len - num_escapes > 0); | 16771 intptr_t utf8_len = len - num_escapes; |
| 16776 const String& dststr = String::Handle( | 16772 ASSERT(utf8_len >= 0); |
| 16777 OneByteString::New(len - num_escapes, Heap::kNew)); | 16773 Zone* zone = Isolate::Current()->current_zone(); |
| 16774 uint8_t* utf8 = zone->Alloc<uint8_t>(utf8_len); |
| 16778 { | 16775 { |
| 16779 intptr_t index = 0; | 16776 intptr_t index = 0; |
| 16780 CodePointIterator cpi(str); | 16777 CodePointIterator cpi(str); |
| 16781 while (cpi.Next()) { | 16778 while (cpi.Next()) { |
| 16779 ASSERT(index < utf8_len); |
| 16782 int32_t code_point = cpi.Current(); | 16780 int32_t code_point = cpi.Current(); |
| 16783 if (IsPercent(code_point)) { | 16781 if (IsPercent(code_point)) { |
| 16784 cpi.Next(); | 16782 cpi.Next(); |
| 16785 int32_t ch1 = cpi.Current(); | 16783 int32_t ch1 = cpi.Current(); |
| 16786 cpi.Next(); | 16784 cpi.Next(); |
| 16787 int32_t ch2 = cpi.Current(); | 16785 int32_t ch2 = cpi.Current(); |
| 16788 int32_t merged = MergeHexCharacters(ch1, ch2); | 16786 int32_t merged = MergeHexCharacters(ch1, ch2); |
| 16789 OneByteString::SetCharAt(dststr, index, merged); | 16787 ASSERT(merged >= 0 && merged < 256); |
| 16788 utf8[index] = static_cast<uint8_t>(merged); |
| 16790 } else { | 16789 } else { |
| 16791 OneByteString::SetCharAt(dststr, index, code_point); | 16790 ASSERT(code_point >= 0 && code_point < 256); |
| 16791 utf8[index] = static_cast<uint8_t>(code_point); |
| 16792 } | 16792 } |
| 16793 index++; | 16793 index++; |
| 16794 } | 16794 } |
| 16795 } | 16795 } |
| 16796 return dststr.raw(); | 16796 return FromUTF8(utf8, utf8_len); |
| 16797 } | 16797 } |
| 16798 | 16798 |
| 16799 | 16799 |
| 16800 RawString* String::NewFormatted(const char* format, ...) { | 16800 RawString* String::NewFormatted(const char* format, ...) { |
| 16801 va_list args; | 16801 va_list args; |
| 16802 va_start(args, format); | 16802 va_start(args, format); |
| 16803 RawString* result = NewFormattedV(format, args); | 16803 RawString* result = NewFormattedV(format, args); |
| 16804 NoGCScope no_gc; | 16804 NoGCScope no_gc; |
| 16805 va_end(args); | 16805 va_end(args); |
| 16806 return result; | 16806 return result; |
| (...skipping 2658 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 19465 return tag_label.ToCString(); | 19465 return tag_label.ToCString(); |
| 19466 } | 19466 } |
| 19467 | 19467 |
| 19468 | 19468 |
| 19469 void UserTag::PrintJSONImpl(JSONStream* stream, bool ref) const { | 19469 void UserTag::PrintJSONImpl(JSONStream* stream, bool ref) const { |
| 19470 Instance::PrintJSONImpl(stream, ref); | 19470 Instance::PrintJSONImpl(stream, ref); |
| 19471 } | 19471 } |
| 19472 | 19472 |
| 19473 | 19473 |
| 19474 } // namespace dart | 19474 } // namespace dart |
| OLD | NEW |