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/dart_api_message.h" | 5 #include "vm/dart_api_message.h" |
6 #include "vm/object.h" | 6 #include "vm/object.h" |
7 #include "vm/snapshot_ids.h" | 7 #include "vm/snapshot_ids.h" |
8 #include "vm/symbols.h" | 8 #include "vm/symbols.h" |
9 #include "vm/unicode.h" | 9 #include "vm/unicode.h" |
10 | 10 |
(...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
382 ::free(latin1); | 382 ::free(latin1); |
383 return object; | 383 return object; |
384 } | 384 } |
385 case kTwoByteStringCid: { | 385 case kTwoByteStringCid: { |
386 intptr_t len = ReadSmiValue(); | 386 intptr_t len = ReadSmiValue(); |
387 intptr_t hash = ReadSmiValue(); | 387 intptr_t hash = ReadSmiValue(); |
388 USE(hash); | 388 USE(hash); |
389 uint16_t *utf16 = | 389 uint16_t *utf16 = |
390 reinterpret_cast<uint16_t*>(::malloc(len * sizeof(uint16_t))); | 390 reinterpret_cast<uint16_t*>(::malloc(len * sizeof(uint16_t))); |
391 intptr_t utf8_len = 0; | 391 intptr_t utf8_len = 0; |
392 // Read all the UTF-16 code units. | |
392 for (intptr_t i = 0; i < len; i++) { | 393 for (intptr_t i = 0; i < len; i++) { |
393 utf16[i] = Read<uint16_t>(); | 394 utf16[i] = Read<uint16_t>(); |
394 // TODO(sgjesse): Check for surrogate pairs. | 395 } |
395 utf8_len += Utf8::Length(utf16[i]); | 396 // Calculate the UTF-8 length. |
397 for (intptr_t i = 0; i < len; i++) { | |
erikcorry
2012/11/23 14:25:47
There is a CodePointIterator in object.h that you
Søren Gjesse
2012/11/26 08:53:00
That is not possible as this is a raw de-serialize
| |
398 if (i < len - 1 && | |
399 Utf16::IsLeadSurrogate(utf16[i]) && | |
400 Utf16::IsTrailSurrogate(utf16[i + 1])) { | |
401 uint32_t ch = Utf16::Decode(utf16[i], utf16[i + 1]); | |
402 utf8_len += Utf8::Length(ch); | |
403 i++; | |
404 } else { | |
405 utf8_len += Utf8::Length(utf16[i]); | |
406 } | |
396 } | 407 } |
397 Dart_CObject* object = AllocateDartCObjectString(utf8_len); | 408 Dart_CObject* object = AllocateDartCObjectString(utf8_len); |
398 AddBackRef(object_id, object, kIsDeserialized); | 409 AddBackRef(object_id, object, kIsDeserialized); |
399 char* p = object->value.as_string; | 410 char* p = object->value.as_string; |
400 for (intptr_t i = 0; i < len; i++) { | 411 for (intptr_t i = 0; i < len; i++) { |
401 // TODO(sgjesse): Check for surrogate pairs. | 412 if (i < len - 1 && |
402 p += Utf8::Encode(utf16[i], p); | 413 Utf16::IsLeadSurrogate(utf16[i]) && |
414 Utf16::IsTrailSurrogate(utf16[i + 1])) { | |
415 uint32_t ch = Utf16::Decode(utf16[i], utf16[i + 1]); | |
416 p += Utf8::Encode(ch, p); | |
417 i++; | |
418 } else { | |
419 p += Utf8::Encode(utf16[i], p); | |
420 } | |
403 } | 421 } |
404 *p = '\0'; | 422 *p = '\0'; |
405 ASSERT(p == (object->value.as_string + utf8_len)); | 423 ASSERT(p == (object->value.as_string + utf8_len)); |
406 ::free(utf16); | 424 ::free(utf16); |
407 return object; | 425 return object; |
408 } | 426 } |
409 case kUint8ArrayCid: { | 427 case kUint8ArrayCid: { |
410 intptr_t len = ReadSmiValue(); | 428 intptr_t len = ReadSmiValue(); |
411 Dart_CObject* object = AllocateDartCObjectUint8Array(len); | 429 Dart_CObject* object = AllocateDartCObjectUint8Array(len); |
412 AddBackRef(object_id, object, kIsDeserialized); | 430 AddBackRef(object_id, object, kIsDeserialized); |
(...skipping 368 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
781 // Write out the class and tags information. | 799 // Write out the class and tags information. |
782 WriteIndexedObject(kDoubleCid); | 800 WriteIndexedObject(kDoubleCid); |
783 WriteIntptrValue(0); | 801 WriteIntptrValue(0); |
784 // Write double value. | 802 // Write double value. |
785 Write<double>(object->value.as_double); | 803 Write<double>(object->value.as_double); |
786 break; | 804 break; |
787 case Dart_CObject::kString: { | 805 case Dart_CObject::kString: { |
788 const uint8_t* utf8_str = | 806 const uint8_t* utf8_str = |
789 reinterpret_cast<const uint8_t*>(object->value.as_string); | 807 reinterpret_cast<const uint8_t*>(object->value.as_string); |
790 intptr_t utf8_len = strlen(object->value.as_string); | 808 intptr_t utf8_len = strlen(object->value.as_string); |
791 if (!Utf8::IsValid(utf8_str, utf8_len)) { | 809 if (!Utf8::IsValidAllowSurrogates(utf8_str, utf8_len)) { |
792 return false; | 810 return false; |
793 } | 811 } |
794 | 812 |
795 Utf8::Type type; | 813 Utf8::Type type; |
796 intptr_t len = Utf8::CodePointCount(utf8_str, utf8_len, &type); | 814 intptr_t len = Utf8::CodePointCount(utf8_str, utf8_len, &type); |
797 | 815 |
798 // Write out the serialization header value for this object. | 816 // Write out the serialization header value for this object. |
799 WriteInlinedHeader(object); | 817 WriteInlinedHeader(object); |
800 // Write out the class and tags information. | 818 // Write out the class and tags information. |
801 WriteIndexedObject(type == Utf8::kLatin1 ? kOneByteStringCid | 819 WriteIndexedObject(type == Utf8::kLatin1 ? kOneByteStringCid |
802 : kTwoByteStringCid); | 820 : kTwoByteStringCid); |
803 WriteIntptrValue(0); | 821 WriteIntptrValue(0); |
804 // Write string length, hash and content | 822 // Write string length, hash and content |
805 WriteSmi(len); | 823 WriteSmi(len); |
806 WriteSmi(0); // TODO(sgjesse): Hash - not written. | 824 WriteSmi(0); // TODO(sgjesse): Hash - not written. |
807 if (type == Utf8::kLatin1) { | 825 if (type == Utf8::kLatin1) { |
808 uint8_t* latin1_str = | 826 uint8_t* latin1_str = |
809 reinterpret_cast<uint8_t*>(::malloc(len * sizeof(uint8_t))); | 827 reinterpret_cast<uint8_t*>(::malloc(len * sizeof(uint8_t))); |
810 Utf8::DecodeToLatin1(utf8_str, utf8_len, latin1_str, len); | 828 bool success = Utf8::DecodeToLatin1(utf8_str, |
829 utf8_len, | |
830 latin1_str, | |
831 len); | |
832 ASSERT(success); | |
811 for (intptr_t i = 0; i < len; i++) { | 833 for (intptr_t i = 0; i < len; i++) { |
812 Write<uint8_t>(latin1_str[i]); | 834 Write<uint8_t>(latin1_str[i]); |
813 } | 835 } |
814 ::free(latin1_str); | 836 ::free(latin1_str); |
815 } else { | 837 } else { |
816 // TODO(sgjesse): Make sure surrogate pairs are handled. | 838 // TODO(sgjesse): Make sure surrogate pairs are handled. |
817 uint16_t* utf16_str = | 839 uint16_t* utf16_str = |
818 reinterpret_cast<uint16_t*>(::malloc(len * sizeof(uint16_t))); | 840 reinterpret_cast<uint16_t*>(::malloc(len * sizeof(uint16_t))); |
819 Utf8::DecodeToUTF16(utf8_str, utf8_len, utf16_str, len); | 841 bool success = Utf8::DecodeToUTF16AllowSurrogates(utf8_str, |
842 utf8_len, | |
843 utf16_str, | |
844 len); | |
845 ASSERT(success); | |
820 for (intptr_t i = 0; i < len; i++) { | 846 for (intptr_t i = 0; i < len; i++) { |
821 Write<uint16_t>(utf16_str[i]); | 847 Write<uint16_t>(utf16_str[i]); |
822 } | 848 } |
823 ::free(utf16_str); | 849 ::free(utf16_str); |
824 } | 850 } |
825 break; | 851 break; |
826 } | 852 } |
827 case Dart_CObject::kUint8Array: { | 853 case Dart_CObject::kUint8Array: { |
828 // Write out the serialization header value for this object. | 854 // Write out the serialization header value for this object. |
829 WriteInlinedHeader(object); | 855 WriteInlinedHeader(object); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
882 if (!success) { | 908 if (!success) { |
883 UnmarkAllCObjects(object); | 909 UnmarkAllCObjects(object); |
884 return false; | 910 return false; |
885 } | 911 } |
886 } | 912 } |
887 UnmarkAllCObjects(object); | 913 UnmarkAllCObjects(object); |
888 return true; | 914 return true; |
889 } | 915 } |
890 | 916 |
891 } // namespace dart | 917 } // namespace dart |
OLD | NEW |