Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(169)

Side by Side Diff: runtime/vm/dart_api_message.cc

Issue 11410032: Add support for non ASCII strings when communicating with native ports (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 10
10 namespace dart { 11 namespace dart {
11 12
12 static const int kNumInitialReferences = 4; 13 static const int kNumInitialReferences = 4;
13 14
14 ApiMessageReader::ApiMessageReader(const uint8_t* buffer, 15 ApiMessageReader::ApiMessageReader(const uint8_t* buffer,
15 intptr_t length, 16 intptr_t length,
16 ReAlloc alloc) 17 ReAlloc alloc)
17 : BaseReader(buffer, length), 18 : BaseReader(buffer, length),
18 alloc_(alloc), 19 alloc_(alloc),
(...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after
348 USE(hash); 349 USE(hash);
349 Dart_CObject* object = AllocateDartCObjectString(len); 350 Dart_CObject* object = AllocateDartCObjectString(len);
350 AddBackRef(object_id, object, kIsDeserialized); 351 AddBackRef(object_id, object, kIsDeserialized);
351 char* p = object->value.as_string; 352 char* p = object->value.as_string;
352 for (intptr_t i = 0; i < len; i++) { 353 for (intptr_t i = 0; i < len; i++) {
353 p[i] = Read<uint8_t>(); 354 p[i] = Read<uint8_t>();
354 } 355 }
355 p[len] = '\0'; 356 p[len] = '\0';
356 return object; 357 return object;
357 } 358 }
358 case kTwoByteStringCid: 359 case kTwoByteStringCid: {
359 // Two byte strings not supported. 360 intptr_t len = ReadSmiValue();
360 return AllocateDartCObjectUnsupported(); 361 intptr_t hash = ReadSmiValue();
362 USE(hash);
363 uint16_t *utf16 =
364 reinterpret_cast<uint16_t*>(::malloc(len * sizeof(uint16_t)));
365 intptr_t utf8_len = 0;
366 for (intptr_t i = 0; i < len; i++) {
367 utf16[i] = Read<uint16_t>();
368 // TODO(sgjesse): Check for surrogate pairs.
369 utf8_len += Utf8::Length(utf16[i]);
370 }
371 Dart_CObject* object = AllocateDartCObjectString(utf8_len);
372 AddBackRef(object_id, object, kIsDeserialized);
373 char* p = object->value.as_string;
374 for (intptr_t i = 0; i < len; i++) {
375 // TODO(sgjesse): Check for surrogate pairs.
376 p += Utf8::Encode(utf16[i], p);
377 }
378 *p = '\0';
379 ASSERT(p == object->value.as_string + utf8_len);
380 ::free(utf16);
381 return object;
382 }
361 case kUint8ArrayCid: { 383 case kUint8ArrayCid: {
362 intptr_t len = ReadSmiValue(); 384 intptr_t len = ReadSmiValue();
363 Dart_CObject* object = AllocateDartCObjectUint8Array(len); 385 Dart_CObject* object = AllocateDartCObjectUint8Array(len);
364 AddBackRef(object_id, object, kIsDeserialized); 386 AddBackRef(object_id, object, kIsDeserialized);
365 if (len > 0) { 387 if (len > 0) {
366 uint8_t* p = object->value.as_byte_array.values; 388 uint8_t* p = object->value.as_byte_array.values;
367 for (intptr_t i = 0; i < len; i++) { 389 for (intptr_t i = 0; i < len; i++) {
368 p[i] = Read<uint8_t>(); 390 p[i] = Read<uint8_t>();
369 } 391 }
370 } 392 }
(...skipping 356 matching lines...) Expand 10 before | Expand all | Expand 10 after
727 case Dart_CObject::kDouble: 749 case Dart_CObject::kDouble:
728 // Write out the serialization header value for this object. 750 // Write out the serialization header value for this object.
729 WriteInlinedHeader(object); 751 WriteInlinedHeader(object);
730 // Write out the class and tags information. 752 // Write out the class and tags information.
731 WriteIndexedObject(kDoubleCid); 753 WriteIndexedObject(kDoubleCid);
732 WriteIntptrValue(0); 754 WriteIntptrValue(0);
733 // Write double value. 755 // Write double value.
734 Write<double>(object->value.as_double); 756 Write<double>(object->value.as_double);
735 break; 757 break;
736 case Dart_CObject::kString: { 758 case Dart_CObject::kString: {
759 const uint8_t* utf8_str =
760 reinterpret_cast<const uint8_t*>(object->value.as_string);
761 intptr_t utf8_len = strlen(object->value.as_string);
762 Utf8::Type type;
763 intptr_t len = Utf8::CodePointCount(utf8_str, utf8_len, &type);
764
737 // Write out the serialization header value for this object. 765 // Write out the serialization header value for this object.
738 WriteInlinedHeader(object); 766 WriteInlinedHeader(object);
739 // Write out the class and tags information. 767 // Write out the class and tags information.
740 WriteIndexedObject(kOneByteStringCid); 768 WriteIndexedObject(type == Utf8::kAscii ? kOneByteStringCid
769 : kTwoByteStringCid);
741 WriteIntptrValue(0); 770 WriteIntptrValue(0);
742 // Write string length, hash and content 771 // Write string length, hash and content
743 char* str = object->value.as_string;
744 intptr_t len = strlen(str);
745 WriteSmi(len); 772 WriteSmi(len);
746 WriteSmi(0); // TODO(sgjesse): Hash - not written. 773 WriteSmi(0); // TODO(sgjesse): Hash - not written.
747 for (intptr_t i = 0; i < len; i++) { 774 if (type == Utf8::kAscii) {
748 Write<uint8_t>(str[i]); 775 for (intptr_t i = 0; i < len; i++) {
776 Write<uint8_t>(utf8_str[i]);
777 }
778 } else {
779 // TODO(sgjesse): Make sure surrogate pairs are handled.
780 uint16_t* utf16_str =
781 reinterpret_cast<uint16_t*>(::malloc(len * sizeof(uint16_t)));
782 Utf8::DecodeToUTF16(utf8_str, utf8_len, utf16_str, len);
783 for (intptr_t i = 0; i < len; i++) {
784 Write<uint16_t>(utf16_str[i]);
785 }
786 ::free(utf16_str);
749 } 787 }
750 break; 788 break;
751 } 789 }
752 case Dart_CObject::kUint8Array: { 790 case Dart_CObject::kUint8Array: {
753 // Write out the serialization header value for this object. 791 // Write out the serialization header value for this object.
754 WriteInlinedHeader(object); 792 WriteInlinedHeader(object);
755 // Write out the class and tags information. 793 // Write out the class and tags information.
756 WriteIndexedObject(kUint8ArrayCid); 794 WriteIndexedObject(kUint8ArrayCid);
757 WriteIntptrValue(0); 795 WriteIntptrValue(0);
758 uint8_t* bytes = object->value.as_byte_array.values; 796 uint8_t* bytes = object->value.as_byte_array.values;
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
796 // Write out all objects that were added to the forward list and have 834 // Write out all objects that were added to the forward list and have
797 // not been serialized yet. These would typically be fields of arrays. 835 // not been serialized yet. These would typically be fields of arrays.
798 // NOTE: The forward list might grow as we process the list. 836 // NOTE: The forward list might grow as we process the list.
799 for (intptr_t i = 0; i < forward_id_; i++) { 837 for (intptr_t i = 0; i < forward_id_; i++) {
800 WriteForwardedCObject(forward_list_[i]); 838 WriteForwardedCObject(forward_list_[i]);
801 } 839 }
802 UnmarkAllCObjects(object); 840 UnmarkAllCObjects(object);
803 } 841 }
804 842
805 } // namespace dart 843 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698