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 |
11 namespace dart { | 11 namespace dart { |
12 | 12 |
13 static const int kNumInitialReferences = 4; | 13 static const int kNumInitialReferences = 4; |
14 | 14 |
15 ApiMessageReader::ApiMessageReader(const uint8_t* buffer, | 15 ApiMessageReader::ApiMessageReader(const uint8_t* buffer, |
16 intptr_t length, | 16 intptr_t length, |
17 ReAlloc alloc, | 17 ReAlloc alloc) |
18 bool use_vm_isolate_snapshot) | |
19 : BaseReader(buffer, length), | 18 : BaseReader(buffer, length), |
20 alloc_(alloc), | 19 alloc_(alloc), |
21 backward_references_(kNumInitialReferences), | 20 backward_references_(kNumInitialReferences), |
22 vm_isolate_references_(kNumInitialReferences), | 21 vm_isolate_references_(kNumInitialReferences), |
23 vm_symbol_references_(NULL), | 22 vm_symbol_references_(NULL) { |
24 max_vm_isolate_object_id_( | |
25 use_vm_isolate_snapshot ? | |
26 Object::vm_isolate_snapshot_object_table().Length() : 0) { | |
27 Init(); | 23 Init(); |
28 } | 24 } |
29 | 25 |
30 | 26 |
31 void ApiMessageReader::Init() { | 27 void ApiMessageReader::Init() { |
32 // Initialize marker objects used to handle Lists. | 28 // Initialize marker objects used to handle Lists. |
33 // TODO(sjesse): Remove this when message serialization format is | 29 // TODO(sjesse): Remove this when message serialization format is |
34 // updated. | 30 // updated. |
35 memset(&type_arguments_marker, 0, sizeof(type_arguments_marker)); | 31 memset(&type_arguments_marker, 0, sizeof(type_arguments_marker)); |
36 memset(&dynamic_type_marker, 0, sizeof(dynamic_type_marker)); | 32 memset(&dynamic_type_marker, 0, sizeof(dynamic_type_marker)); |
(...skipping 377 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
414 } | 410 } |
415 | 411 |
416 object = CreateDartCObjectString(Symbols::GetVMSymbol(object_id)); | 412 object = CreateDartCObjectString(Symbols::GetVMSymbol(object_id)); |
417 ASSERT(vm_symbol_references_[symbol_id] == NULL); | 413 ASSERT(vm_symbol_references_[symbol_id] == NULL); |
418 vm_symbol_references_[symbol_id] = object; | 414 vm_symbol_references_[symbol_id] = object; |
419 return object; | 415 return object; |
420 } | 416 } |
421 | 417 |
422 | 418 |
423 intptr_t ApiMessageReader::NextAvailableObjectId() const { | 419 intptr_t ApiMessageReader::NextAvailableObjectId() const { |
424 return backward_references_.length() + | 420 return backward_references_.length() + kMaxPredefinedObjectIds; |
425 kMaxPredefinedObjectIds + max_vm_isolate_object_id_; | |
426 } | 421 } |
427 | 422 |
428 | 423 |
429 Dart_CObject* ApiMessageReader::CreateDartCObjectString(RawObject* raw) { | 424 Dart_CObject* ApiMessageReader::CreateDartCObjectString(RawObject* raw) { |
430 ASSERT(RawObject::IsOneByteStringClassId(raw->GetClassId())); | 425 ASSERT(RawObject::IsOneByteStringClassId(raw->GetClassId())); |
431 RawOneByteString* raw_str = reinterpret_cast<RawOneByteString*>(raw); | 426 RawOneByteString* raw_str = reinterpret_cast<RawOneByteString*>(raw); |
432 intptr_t len = Smi::Value(raw_str->ptr()->length_); | 427 intptr_t len = Smi::Value(raw_str->ptr()->length_); |
433 Dart_CObject* object = AllocateDartCObjectString(len); | 428 Dart_CObject* object = AllocateDartCObjectString(len); |
434 char* p = object->value.as_string; | 429 char* p = object->value.as_string; |
435 memmove(p, raw_str->ptr()->data(), len); | 430 memmove(p, raw_str->ptr()->data(), len); |
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
800 Dart_CObject* ApiMessageReader::ReadIndexedObject(intptr_t object_id) { | 795 Dart_CObject* ApiMessageReader::ReadIndexedObject(intptr_t object_id) { |
801 if (object_id == kDynamicType || | 796 if (object_id == kDynamicType || |
802 object_id == kDoubleType || | 797 object_id == kDoubleType || |
803 object_id == kIntType || | 798 object_id == kIntType || |
804 object_id == kBoolType || | 799 object_id == kBoolType || |
805 object_id == kStringType) { | 800 object_id == kStringType) { |
806 // Always return dynamic type (this is only a marker). | 801 // Always return dynamic type (this is only a marker). |
807 return &dynamic_type_marker; | 802 return &dynamic_type_marker; |
808 } | 803 } |
809 intptr_t index = object_id - kMaxPredefinedObjectIds; | 804 intptr_t index = object_id - kMaxPredefinedObjectIds; |
810 if (index < max_vm_isolate_object_id_) { | |
811 return AllocateDartCObjectVmIsolateObj(index); | |
812 } | |
813 index -= max_vm_isolate_object_id_; | |
814 ASSERT((0 <= index) && (index < backward_references_.length())); | 805 ASSERT((0 <= index) && (index < backward_references_.length())); |
815 ASSERT(backward_references_[index]->reference() != NULL); | 806 ASSERT(backward_references_[index]->reference() != NULL); |
816 return backward_references_[index]->reference(); | 807 return backward_references_[index]->reference(); |
817 } | 808 } |
818 | 809 |
819 | 810 |
820 Dart_CObject* ApiMessageReader::ReadObject() { | 811 Dart_CObject* ApiMessageReader::ReadObject() { |
821 Dart_CObject* value = ReadObjectImpl(); | 812 Dart_CObject* value = ReadObjectImpl(); |
822 for (intptr_t i = 0; i < backward_references_.length(); i++) { | 813 for (intptr_t i = 0; i < backward_references_.length(); i++) { |
823 if (!backward_references_[i]->is_deserialized()) { | 814 if (!backward_references_[i]->is_deserialized()) { |
(...skipping 30 matching lines...) Expand all Loading... |
854 object_id = NextAvailableObjectId(); | 845 object_id = NextAvailableObjectId(); |
855 } | 846 } |
856 return ReadInlinedObject(object_id); | 847 return ReadInlinedObject(object_id); |
857 } | 848 } |
858 | 849 |
859 | 850 |
860 void ApiMessageReader::AddBackRef(intptr_t id, | 851 void ApiMessageReader::AddBackRef(intptr_t id, |
861 Dart_CObject* obj, | 852 Dart_CObject* obj, |
862 DeserializeState state) { | 853 DeserializeState state) { |
863 intptr_t index = (id - kMaxPredefinedObjectIds); | 854 intptr_t index = (id - kMaxPredefinedObjectIds); |
864 ASSERT(index >= max_vm_isolate_object_id_); | |
865 index -= max_vm_isolate_object_id_; | |
866 ASSERT(index == backward_references_.length()); | 855 ASSERT(index == backward_references_.length()); |
867 BackRefNode* node = AllocateBackRefNode(obj, state); | 856 BackRefNode* node = AllocateBackRefNode(obj, state); |
868 ASSERT(node != NULL); | 857 ASSERT(node != NULL); |
869 backward_references_.Add(node); | 858 backward_references_.Add(node); |
870 } | 859 } |
871 | 860 |
872 | 861 |
873 Dart_CObject* ApiMessageReader::GetBackRef(intptr_t id) { | 862 Dart_CObject* ApiMessageReader::GetBackRef(intptr_t id) { |
874 ASSERT(id >= kMaxPredefinedObjectIds); | 863 ASSERT(id >= kMaxPredefinedObjectIds); |
875 intptr_t index = (id - kMaxPredefinedObjectIds); | 864 intptr_t index = (id - kMaxPredefinedObjectIds); |
876 ASSERT(index >= max_vm_isolate_object_id_); | |
877 index -= max_vm_isolate_object_id_; | |
878 if (index < backward_references_.length()) { | 865 if (index < backward_references_.length()) { |
879 return backward_references_[index]->reference(); | 866 return backward_references_[index]->reference(); |
880 } | 867 } |
881 return NULL; | 868 return NULL; |
882 } | 869 } |
883 | 870 |
884 | 871 |
885 void ApiMessageWriter::WriteMessage(intptr_t field_count, intptr_t *data) { | 872 void ApiMessageWriter::WriteMessage(intptr_t field_count, intptr_t *data) { |
886 // Write out the serialization header value for this object. | 873 // Write out the serialization header value for this object. |
887 WriteInlinedObjectHeader(kMaxPredefinedObjectIds); | 874 WriteInlinedObjectHeader(kMaxPredefinedObjectIds); |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1005 if (Smi::IsValid(value)) { | 992 if (Smi::IsValid(value)) { |
1006 WriteSmi(value); | 993 WriteSmi(value); |
1007 } else { | 994 } else { |
1008 WriteMint(object, value); | 995 WriteMint(object, value); |
1009 } | 996 } |
1010 } | 997 } |
1011 | 998 |
1012 | 999 |
1013 void ApiMessageWriter::WriteInlinedHeader(Dart_CObject* object) { | 1000 void ApiMessageWriter::WriteInlinedHeader(Dart_CObject* object) { |
1014 // Write out the serialization header value for this object. | 1001 // Write out the serialization header value for this object. |
1015 WriteInlinedObjectHeader(SnapshotWriter::FirstObjectId() + object_id_); | 1002 WriteInlinedObjectHeader(kMaxPredefinedObjectIds + object_id_); |
1016 // Mark object with its object id. | 1003 // Mark object with its object id. |
1017 MarkCObject(object, object_id_); | 1004 MarkCObject(object, object_id_); |
1018 // Advance object id. | 1005 // Advance object id. |
1019 object_id_++; | 1006 object_id_++; |
1020 } | 1007 } |
1021 | 1008 |
1022 | 1009 |
1023 bool ApiMessageWriter::WriteCObject(Dart_CObject* object) { | 1010 bool ApiMessageWriter::WriteCObject(Dart_CObject* object) { |
1024 if (IsCObjectMarked(object)) { | 1011 if (IsCObjectMarked(object)) { |
1025 intptr_t object_id = GetMarkedCObjectMark(object); | 1012 intptr_t object_id = GetMarkedCObjectMark(object); |
1026 WriteIndexedObject(SnapshotWriter::FirstObjectId() + object_id); | 1013 WriteIndexedObject(kMaxPredefinedObjectIds + object_id); |
1027 return true; | 1014 return true; |
1028 } | 1015 } |
1029 | 1016 |
1030 Dart_CObject_Type type = object->type; | 1017 Dart_CObject_Type type = object->type; |
1031 if (type == Dart_CObject_kArray) { | 1018 if (type == Dart_CObject_kArray) { |
1032 const intptr_t array_length = object->value.as_array.length; | 1019 const intptr_t array_length = object->value.as_array.length; |
1033 if (array_length < 0 || | 1020 if (array_length < 0 || |
1034 array_length > Array::kMaxElements) { | 1021 array_length > Array::kMaxElements) { |
1035 return false; | 1022 return false; |
1036 } | 1023 } |
(...skipping 14 matching lines...) Expand all Loading... |
1051 } | 1038 } |
1052 return true; | 1039 return true; |
1053 } | 1040 } |
1054 return WriteCObjectInlined(object, type); | 1041 return WriteCObjectInlined(object, type); |
1055 } | 1042 } |
1056 | 1043 |
1057 | 1044 |
1058 bool ApiMessageWriter::WriteCObjectRef(Dart_CObject* object) { | 1045 bool ApiMessageWriter::WriteCObjectRef(Dart_CObject* object) { |
1059 if (IsCObjectMarked(object)) { | 1046 if (IsCObjectMarked(object)) { |
1060 intptr_t object_id = GetMarkedCObjectMark(object); | 1047 intptr_t object_id = GetMarkedCObjectMark(object); |
1061 WriteIndexedObject(SnapshotWriter::FirstObjectId() + object_id); | 1048 WriteIndexedObject(kMaxPredefinedObjectIds + object_id); |
1062 return true; | 1049 return true; |
1063 } | 1050 } |
1064 | 1051 |
1065 Dart_CObject_Type type = object->type; | 1052 Dart_CObject_Type type = object->type; |
1066 if (type == Dart_CObject_kArray) { | 1053 if (type == Dart_CObject_kArray) { |
1067 const intptr_t array_length = object->value.as_array.length; | 1054 const intptr_t array_length = object->value.as_array.length; |
1068 if (array_length < 0 || | 1055 if (array_length < 0 || |
1069 array_length > Array::kMaxElements) { | 1056 array_length > Array::kMaxElements) { |
1070 return false; | 1057 return false; |
1071 } | 1058 } |
(...skipping 17 matching lines...) Expand all Loading... |
1089 static_cast<Dart_CObject_Type>(object->type & kDartCObjectTypeMask); | 1076 static_cast<Dart_CObject_Type>(object->type & kDartCObjectTypeMask); |
1090 ASSERT(type == Dart_CObject_kArray); | 1077 ASSERT(type == Dart_CObject_kArray); |
1091 const intptr_t array_length = object->value.as_array.length; | 1078 const intptr_t array_length = object->value.as_array.length; |
1092 if (array_length < 0 || | 1079 if (array_length < 0 || |
1093 array_length > Array::kMaxElements) { | 1080 array_length > Array::kMaxElements) { |
1094 return false; | 1081 return false; |
1095 } | 1082 } |
1096 | 1083 |
1097 // Write out the serialization header value for this object. | 1084 // Write out the serialization header value for this object. |
1098 intptr_t object_id = GetMarkedCObjectMark(object); | 1085 intptr_t object_id = GetMarkedCObjectMark(object); |
1099 WriteInlinedObjectHeader(SnapshotWriter::FirstObjectId() + object_id); | 1086 WriteInlinedObjectHeader(kMaxPredefinedObjectIds + object_id); |
1100 // Write out the class and tags information. | 1087 // Write out the class and tags information. |
1101 WriteIndexedObject(kArrayCid); | 1088 WriteIndexedObject(kArrayCid); |
1102 WriteTags(0); | 1089 WriteTags(0); |
1103 // Write out the length information. | 1090 // Write out the length information. |
1104 WriteSmi(array_length); | 1091 WriteSmi(array_length); |
1105 // Write out the type arguments. | 1092 // Write out the type arguments. |
1106 WriteNullObject(); | 1093 WriteNullObject(); |
1107 // Write out array elements. | 1094 // Write out array elements. |
1108 for (int i = 0; i < array_length; i++) { | 1095 for (int i = 0; i < array_length; i++) { |
1109 bool success = WriteCObjectRef(object->value.as_array.values[i]); | 1096 bool success = WriteCObjectRef(object->value.as_array.values[i]); |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1303 if (!success) { | 1290 if (!success) { |
1304 UnmarkAllCObjects(object); | 1291 UnmarkAllCObjects(object); |
1305 return false; | 1292 return false; |
1306 } | 1293 } |
1307 } | 1294 } |
1308 UnmarkAllCObjects(object); | 1295 UnmarkAllCObjects(object); |
1309 return true; | 1296 return true; |
1310 } | 1297 } |
1311 | 1298 |
1312 } // namespace dart | 1299 } // namespace dart |
OLD | NEW |