| 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, intptr_t length) | 15 ApiMessageReader::ApiMessageReader(const uint8_t* buffer, intptr_t length) |
| 16 : BaseReader(buffer, length), | 16 : BaseReader(buffer, length), |
| 17 zone_(NULL), | 17 zone_(NULL), |
| 18 backward_references_(kNumInitialReferences), | 18 backward_references_(kNumInitialReferences), |
| 19 vm_isolate_references_(kNumInitialReferences), | 19 vm_isolate_references_(kNumInitialReferences), |
| 20 vm_symbol_references_(NULL) {} | 20 vm_symbol_references_(NULL) {} |
| 21 | 21 |
| 22 | |
| 23 ApiMessageReader::ApiMessageReader(Message* msg) | 22 ApiMessageReader::ApiMessageReader(Message* msg) |
| 24 : BaseReader(msg->IsRaw() ? reinterpret_cast<uint8_t*>(msg->raw_obj()) | 23 : BaseReader(msg->IsRaw() ? reinterpret_cast<uint8_t*>(msg->raw_obj()) |
| 25 : msg->data(), | 24 : msg->data(), |
| 26 msg->len()), | 25 msg->len()), |
| 27 zone_(NULL), | 26 zone_(NULL), |
| 28 backward_references_(kNumInitialReferences), | 27 backward_references_(kNumInitialReferences), |
| 29 vm_isolate_references_(kNumInitialReferences), | 28 vm_isolate_references_(kNumInitialReferences), |
| 30 vm_symbol_references_(NULL) {} | 29 vm_symbol_references_(NULL) {} |
| 31 | 30 |
| 32 | |
| 33 void ApiMessageReader::Init() { | 31 void ApiMessageReader::Init() { |
| 34 // We need to have an enclosing ApiNativeScope. | 32 // We need to have an enclosing ApiNativeScope. |
| 35 ASSERT(ApiNativeScope::Current() != NULL); | 33 ASSERT(ApiNativeScope::Current() != NULL); |
| 36 zone_ = ApiNativeScope::Current()->zone(); | 34 zone_ = ApiNativeScope::Current()->zone(); |
| 37 ASSERT(zone_ != NULL); | 35 ASSERT(zone_ != NULL); |
| 38 | 36 |
| 39 // Initialize marker objects used to handle Lists. | 37 // Initialize marker objects used to handle Lists. |
| 40 // TODO(sjesse): Remove this when message serialization format is | 38 // TODO(sjesse): Remove this when message serialization format is |
| 41 // updated. | 39 // updated. |
| 42 memset(&type_arguments_marker, 0, sizeof(type_arguments_marker)); | 40 memset(&type_arguments_marker, 0, sizeof(type_arguments_marker)); |
| 43 memset(&dynamic_type_marker, 0, sizeof(dynamic_type_marker)); | 41 memset(&dynamic_type_marker, 0, sizeof(dynamic_type_marker)); |
| 44 type_arguments_marker.type = | 42 type_arguments_marker.type = |
| 45 static_cast<Dart_CObject_Type>(Dart_CObject_Internal::kTypeArguments); | 43 static_cast<Dart_CObject_Type>(Dart_CObject_Internal::kTypeArguments); |
| 46 dynamic_type_marker.type = | 44 dynamic_type_marker.type = |
| 47 static_cast<Dart_CObject_Type>(Dart_CObject_Internal::kDynamicType); | 45 static_cast<Dart_CObject_Type>(Dart_CObject_Internal::kDynamicType); |
| 48 } | 46 } |
| 49 | 47 |
| 50 | |
| 51 Dart_CObject* ApiMessageReader::ReadMessage() { | 48 Dart_CObject* ApiMessageReader::ReadMessage() { |
| 52 Init(); | 49 Init(); |
| 53 if (PendingBytes() > 0) { | 50 if (PendingBytes() > 0) { |
| 54 // Read the object out of the message. | 51 // Read the object out of the message. |
| 55 return ReadObject(); | 52 return ReadObject(); |
| 56 } else { | 53 } else { |
| 57 const RawObject* raw_obj = | 54 const RawObject* raw_obj = |
| 58 reinterpret_cast<const RawObject*>(CurrentBufferAddress()); | 55 reinterpret_cast<const RawObject*>(CurrentBufferAddress()); |
| 59 ASSERT(ApiObjectConverter::CanConvert(raw_obj)); | 56 ASSERT(ApiObjectConverter::CanConvert(raw_obj)); |
| 60 Dart_CObject* cobj = | 57 Dart_CObject* cobj = |
| 61 reinterpret_cast<Dart_CObject*>(allocator(sizeof(Dart_CObject))); | 58 reinterpret_cast<Dart_CObject*>(allocator(sizeof(Dart_CObject))); |
| 62 ApiObjectConverter::Convert(raw_obj, cobj); | 59 ApiObjectConverter::Convert(raw_obj, cobj); |
| 63 return cobj; | 60 return cobj; |
| 64 } | 61 } |
| 65 } | 62 } |
| 66 | 63 |
| 67 | |
| 68 intptr_t ApiMessageReader::LookupInternalClass(intptr_t class_header) { | 64 intptr_t ApiMessageReader::LookupInternalClass(intptr_t class_header) { |
| 69 if (IsVMIsolateObject(class_header)) { | 65 if (IsVMIsolateObject(class_header)) { |
| 70 return GetVMIsolateObjectId(class_header); | 66 return GetVMIsolateObjectId(class_header); |
| 71 } | 67 } |
| 72 ASSERT(SerializedHeaderTag::decode(class_header) == kObjectId); | 68 ASSERT(SerializedHeaderTag::decode(class_header) == kObjectId); |
| 73 return SerializedHeaderData::decode(class_header); | 69 return SerializedHeaderData::decode(class_header); |
| 74 } | 70 } |
| 75 | 71 |
| 76 | |
| 77 Dart_CObject* ApiMessageReader::AllocateDartCObject(Dart_CObject_Type type) { | 72 Dart_CObject* ApiMessageReader::AllocateDartCObject(Dart_CObject_Type type) { |
| 78 Dart_CObject* value = | 73 Dart_CObject* value = |
| 79 reinterpret_cast<Dart_CObject*>(allocator(sizeof(Dart_CObject))); | 74 reinterpret_cast<Dart_CObject*>(allocator(sizeof(Dart_CObject))); |
| 80 ASSERT(value != NULL); | 75 ASSERT(value != NULL); |
| 81 value->type = type; | 76 value->type = type; |
| 82 return value; | 77 return value; |
| 83 } | 78 } |
| 84 | 79 |
| 85 | |
| 86 Dart_CObject* ApiMessageReader::AllocateDartCObjectUnsupported() { | 80 Dart_CObject* ApiMessageReader::AllocateDartCObjectUnsupported() { |
| 87 return AllocateDartCObject(Dart_CObject_kUnsupported); | 81 return AllocateDartCObject(Dart_CObject_kUnsupported); |
| 88 } | 82 } |
| 89 | 83 |
| 90 | |
| 91 Dart_CObject* ApiMessageReader::AllocateDartCObjectNull() { | 84 Dart_CObject* ApiMessageReader::AllocateDartCObjectNull() { |
| 92 return AllocateDartCObject(Dart_CObject_kNull); | 85 return AllocateDartCObject(Dart_CObject_kNull); |
| 93 } | 86 } |
| 94 | 87 |
| 95 | |
| 96 Dart_CObject* ApiMessageReader::AllocateDartCObjectBool(bool val) { | 88 Dart_CObject* ApiMessageReader::AllocateDartCObjectBool(bool val) { |
| 97 Dart_CObject* value = AllocateDartCObject(Dart_CObject_kBool); | 89 Dart_CObject* value = AllocateDartCObject(Dart_CObject_kBool); |
| 98 value->value.as_bool = val; | 90 value->value.as_bool = val; |
| 99 return value; | 91 return value; |
| 100 } | 92 } |
| 101 | 93 |
| 102 | |
| 103 Dart_CObject* ApiMessageReader::AllocateDartCObjectInt32(int32_t val) { | 94 Dart_CObject* ApiMessageReader::AllocateDartCObjectInt32(int32_t val) { |
| 104 Dart_CObject* value = AllocateDartCObject(Dart_CObject_kInt32); | 95 Dart_CObject* value = AllocateDartCObject(Dart_CObject_kInt32); |
| 105 value->value.as_int32 = val; | 96 value->value.as_int32 = val; |
| 106 return value; | 97 return value; |
| 107 } | 98 } |
| 108 | 99 |
| 109 | |
| 110 Dart_CObject* ApiMessageReader::AllocateDartCObjectInt64(int64_t val) { | 100 Dart_CObject* ApiMessageReader::AllocateDartCObjectInt64(int64_t val) { |
| 111 Dart_CObject* value = AllocateDartCObject(Dart_CObject_kInt64); | 101 Dart_CObject* value = AllocateDartCObject(Dart_CObject_kInt64); |
| 112 value->value.as_int64 = val; | 102 value->value.as_int64 = val; |
| 113 return value; | 103 return value; |
| 114 } | 104 } |
| 115 | 105 |
| 116 | |
| 117 _Dart_CObject* ApiMessageReader::singleton_uint32_typed_data_ = NULL; | 106 _Dart_CObject* ApiMessageReader::singleton_uint32_typed_data_ = NULL; |
| 118 | 107 |
| 119 Dart_CObject* ApiMessageReader::AllocateDartCObjectBigint() { | 108 Dart_CObject* ApiMessageReader::AllocateDartCObjectBigint() { |
| 120 Dart_CObject* value = AllocateDartCObject(Dart_CObject_kBigint); | 109 Dart_CObject* value = AllocateDartCObject(Dart_CObject_kBigint); |
| 121 value->value.as_bigint.neg = false; | 110 value->value.as_bigint.neg = false; |
| 122 value->value.as_bigint.used = 0; | 111 value->value.as_bigint.used = 0; |
| 123 if (singleton_uint32_typed_data_ == NULL) { | 112 if (singleton_uint32_typed_data_ == NULL) { |
| 124 singleton_uint32_typed_data_ = | 113 singleton_uint32_typed_data_ = |
| 125 AllocateDartCObjectTypedData(Dart_TypedData_kUint32, 0); | 114 AllocateDartCObjectTypedData(Dart_TypedData_kUint32, 0); |
| 126 } | 115 } |
| 127 value->value.as_bigint.digits = singleton_uint32_typed_data_; | 116 value->value.as_bigint.digits = singleton_uint32_typed_data_; |
| 128 value->type = Dart_CObject_kBigint; | 117 value->type = Dart_CObject_kBigint; |
| 129 return value; | 118 return value; |
| 130 } | 119 } |
| 131 | 120 |
| 132 | |
| 133 Dart_CObject* ApiMessageReader::AllocateDartCObjectDouble(double val) { | 121 Dart_CObject* ApiMessageReader::AllocateDartCObjectDouble(double val) { |
| 134 Dart_CObject* value = AllocateDartCObject(Dart_CObject_kDouble); | 122 Dart_CObject* value = AllocateDartCObject(Dart_CObject_kDouble); |
| 135 value->value.as_double = val; | 123 value->value.as_double = val; |
| 136 return value; | 124 return value; |
| 137 } | 125 } |
| 138 | 126 |
| 139 | |
| 140 Dart_CObject* ApiMessageReader::AllocateDartCObjectString(intptr_t length) { | 127 Dart_CObject* ApiMessageReader::AllocateDartCObjectString(intptr_t length) { |
| 141 // Allocate a Dart_CObject structure followed by an array of chars | 128 // Allocate a Dart_CObject structure followed by an array of chars |
| 142 // for the string content. The pointer to the string content is set | 129 // for the string content. The pointer to the string content is set |
| 143 // up to this area. | 130 // up to this area. |
| 144 Dart_CObject* value = reinterpret_cast<Dart_CObject*>( | 131 Dart_CObject* value = reinterpret_cast<Dart_CObject*>( |
| 145 allocator(sizeof(Dart_CObject) + length + 1)); | 132 allocator(sizeof(Dart_CObject) + length + 1)); |
| 146 ASSERT(value != NULL); | 133 ASSERT(value != NULL); |
| 147 value->value.as_string = reinterpret_cast<char*>(value) + sizeof(*value); | 134 value->value.as_string = reinterpret_cast<char*>(value) + sizeof(*value); |
| 148 value->type = Dart_CObject_kString; | 135 value->type = Dart_CObject_kString; |
| 149 return value; | 136 return value; |
| 150 } | 137 } |
| 151 | 138 |
| 152 | |
| 153 static int GetTypedDataSizeInBytes(Dart_TypedData_Type type) { | 139 static int GetTypedDataSizeInBytes(Dart_TypedData_Type type) { |
| 154 switch (type) { | 140 switch (type) { |
| 155 case Dart_TypedData_kInt8: | 141 case Dart_TypedData_kInt8: |
| 156 case Dart_TypedData_kUint8: | 142 case Dart_TypedData_kUint8: |
| 157 case Dart_TypedData_kUint8Clamped: | 143 case Dart_TypedData_kUint8Clamped: |
| 158 return 1; | 144 return 1; |
| 159 case Dart_TypedData_kInt16: | 145 case Dart_TypedData_kInt16: |
| 160 case Dart_TypedData_kUint16: | 146 case Dart_TypedData_kUint16: |
| 161 return 2; | 147 return 2; |
| 162 case Dart_TypedData_kInt32: | 148 case Dart_TypedData_kInt32: |
| 163 case Dart_TypedData_kUint32: | 149 case Dart_TypedData_kUint32: |
| 164 case Dart_TypedData_kFloat32: | 150 case Dart_TypedData_kFloat32: |
| 165 return 4; | 151 return 4; |
| 166 case Dart_TypedData_kInt64: | 152 case Dart_TypedData_kInt64: |
| 167 case Dart_TypedData_kUint64: | 153 case Dart_TypedData_kUint64: |
| 168 case Dart_TypedData_kFloat64: | 154 case Dart_TypedData_kFloat64: |
| 169 return 8; | 155 return 8; |
| 170 default: | 156 default: |
| 171 break; | 157 break; |
| 172 } | 158 } |
| 173 UNREACHABLE(); | 159 UNREACHABLE(); |
| 174 return -1; | 160 return -1; |
| 175 } | 161 } |
| 176 | 162 |
| 177 | |
| 178 Dart_CObject* ApiMessageReader::AllocateDartCObjectTypedData( | 163 Dart_CObject* ApiMessageReader::AllocateDartCObjectTypedData( |
| 179 Dart_TypedData_Type type, | 164 Dart_TypedData_Type type, |
| 180 intptr_t length) { | 165 intptr_t length) { |
| 181 // Allocate a Dart_CObject structure followed by an array of bytes | 166 // Allocate a Dart_CObject structure followed by an array of bytes |
| 182 // for the byte array content. The pointer to the byte array content | 167 // for the byte array content. The pointer to the byte array content |
| 183 // is set up to this area. | 168 // is set up to this area. |
| 184 intptr_t length_in_bytes = GetTypedDataSizeInBytes(type) * length; | 169 intptr_t length_in_bytes = GetTypedDataSizeInBytes(type) * length; |
| 185 Dart_CObject* value = reinterpret_cast<Dart_CObject*>( | 170 Dart_CObject* value = reinterpret_cast<Dart_CObject*>( |
| 186 allocator(sizeof(Dart_CObject) + length_in_bytes)); | 171 allocator(sizeof(Dart_CObject) + length_in_bytes)); |
| 187 ASSERT(value != NULL); | 172 ASSERT(value != NULL); |
| 188 value->type = Dart_CObject_kTypedData; | 173 value->type = Dart_CObject_kTypedData; |
| 189 value->value.as_typed_data.type = type; | 174 value->value.as_typed_data.type = type; |
| 190 value->value.as_typed_data.length = length_in_bytes; | 175 value->value.as_typed_data.length = length_in_bytes; |
| 191 if (length > 0) { | 176 if (length > 0) { |
| 192 value->value.as_typed_data.values = | 177 value->value.as_typed_data.values = |
| 193 reinterpret_cast<uint8_t*>(value) + sizeof(*value); | 178 reinterpret_cast<uint8_t*>(value) + sizeof(*value); |
| 194 } else { | 179 } else { |
| 195 value->value.as_typed_data.values = NULL; | 180 value->value.as_typed_data.values = NULL; |
| 196 } | 181 } |
| 197 return value; | 182 return value; |
| 198 } | 183 } |
| 199 | 184 |
| 200 | |
| 201 Dart_CObject* ApiMessageReader::AllocateDartCObjectArray(intptr_t length) { | 185 Dart_CObject* ApiMessageReader::AllocateDartCObjectArray(intptr_t length) { |
| 202 // Allocate a Dart_CObject structure followed by an array of | 186 // Allocate a Dart_CObject structure followed by an array of |
| 203 // pointers to Dart_CObject structures. The pointer to the array | 187 // pointers to Dart_CObject structures. The pointer to the array |
| 204 // content is set up to this area. | 188 // content is set up to this area. |
| 205 Dart_CObject* value = reinterpret_cast<Dart_CObject*>( | 189 Dart_CObject* value = reinterpret_cast<Dart_CObject*>( |
| 206 allocator(sizeof(Dart_CObject) + length * sizeof(value))); | 190 allocator(sizeof(Dart_CObject) + length * sizeof(value))); |
| 207 ASSERT(value != NULL); | 191 ASSERT(value != NULL); |
| 208 value->type = Dart_CObject_kArray; | 192 value->type = Dart_CObject_kArray; |
| 209 value->value.as_array.length = length; | 193 value->value.as_array.length = length; |
| 210 if (length > 0) { | 194 if (length > 0) { |
| 211 value->value.as_array.values = reinterpret_cast<Dart_CObject**>(value + 1); | 195 value->value.as_array.values = reinterpret_cast<Dart_CObject**>(value + 1); |
| 212 } else { | 196 } else { |
| 213 value->value.as_array.values = NULL; | 197 value->value.as_array.values = NULL; |
| 214 } | 198 } |
| 215 return value; | 199 return value; |
| 216 } | 200 } |
| 217 | 201 |
| 218 | |
| 219 Dart_CObject* ApiMessageReader::AllocateDartCObjectVmIsolateObj(intptr_t id) { | 202 Dart_CObject* ApiMessageReader::AllocateDartCObjectVmIsolateObj(intptr_t id) { |
| 220 RawObject* raw = VmIsolateSnapshotObject(id); | 203 RawObject* raw = VmIsolateSnapshotObject(id); |
| 221 intptr_t cid = raw->GetClassId(); | 204 intptr_t cid = raw->GetClassId(); |
| 222 switch (cid) { | 205 switch (cid) { |
| 223 case kOneByteStringCid: { | 206 case kOneByteStringCid: { |
| 224 RawOneByteString* raw_str = reinterpret_cast<RawOneByteString*>(raw); | 207 RawOneByteString* raw_str = reinterpret_cast<RawOneByteString*>(raw); |
| 225 const char* str = reinterpret_cast<const char*>(raw_str->ptr()->data()); | 208 const char* str = reinterpret_cast<const char*>(raw_str->ptr()->data()); |
| 226 ASSERT(str != NULL); | 209 ASSERT(str != NULL); |
| 227 Dart_CObject* object = NULL; | 210 Dart_CObject* object = NULL; |
| 228 for (intptr_t i = 0; i < vm_isolate_references_.length(); i++) { | 211 for (intptr_t i = 0; i < vm_isolate_references_.length(); i++) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 247 return GetCanonicalMintObject(Dart_CObject_kInt64, value64); | 230 return GetCanonicalMintObject(Dart_CObject_kInt64, value64); |
| 248 } | 231 } |
| 249 } | 232 } |
| 250 | 233 |
| 251 default: | 234 default: |
| 252 UNREACHABLE(); | 235 UNREACHABLE(); |
| 253 return NULL; | 236 return NULL; |
| 254 } | 237 } |
| 255 } | 238 } |
| 256 | 239 |
| 257 | |
| 258 Dart_CObject_Internal* ApiMessageReader::AllocateDartCObjectInternal( | 240 Dart_CObject_Internal* ApiMessageReader::AllocateDartCObjectInternal( |
| 259 Dart_CObject_Internal::Type type) { | 241 Dart_CObject_Internal::Type type) { |
| 260 Dart_CObject_Internal* value = reinterpret_cast<Dart_CObject_Internal*>( | 242 Dart_CObject_Internal* value = reinterpret_cast<Dart_CObject_Internal*>( |
| 261 allocator(sizeof(Dart_CObject_Internal))); | 243 allocator(sizeof(Dart_CObject_Internal))); |
| 262 ASSERT(value != NULL); | 244 ASSERT(value != NULL); |
| 263 value->type = static_cast<Dart_CObject_Type>(type); | 245 value->type = static_cast<Dart_CObject_Type>(type); |
| 264 return value; | 246 return value; |
| 265 } | 247 } |
| 266 | 248 |
| 267 | |
| 268 Dart_CObject_Internal* ApiMessageReader::AllocateDartCObjectClass() { | 249 Dart_CObject_Internal* ApiMessageReader::AllocateDartCObjectClass() { |
| 269 return AllocateDartCObjectInternal(Dart_CObject_Internal::kClass); | 250 return AllocateDartCObjectInternal(Dart_CObject_Internal::kClass); |
| 270 } | 251 } |
| 271 | 252 |
| 272 | |
| 273 ApiMessageReader::BackRefNode* ApiMessageReader::AllocateBackRefNode( | 253 ApiMessageReader::BackRefNode* ApiMessageReader::AllocateBackRefNode( |
| 274 Dart_CObject* reference, | 254 Dart_CObject* reference, |
| 275 DeserializeState state) { | 255 DeserializeState state) { |
| 276 BackRefNode* value = | 256 BackRefNode* value = |
| 277 reinterpret_cast<BackRefNode*>(allocator(sizeof(BackRefNode))); | 257 reinterpret_cast<BackRefNode*>(allocator(sizeof(BackRefNode))); |
| 278 value->set_reference(reference); | 258 value->set_reference(reference); |
| 279 value->set_state(state); | 259 value->set_state(state); |
| 280 return value; | 260 return value; |
| 281 } | 261 } |
| 282 | 262 |
| 283 | |
| 284 static Dart_TypedData_Type GetTypedDataTypeFromView( | 263 static Dart_TypedData_Type GetTypedDataTypeFromView( |
| 285 Dart_CObject_Internal* object, | 264 Dart_CObject_Internal* object, |
| 286 char* class_name) { | 265 char* class_name) { |
| 287 struct { | 266 struct { |
| 288 const char* name; | 267 const char* name; |
| 289 Dart_TypedData_Type type; | 268 Dart_TypedData_Type type; |
| 290 } view_class_names[] = { | 269 } view_class_names[] = { |
| 291 {"_Int8ArrayView", Dart_TypedData_kInt8}, | 270 {"_Int8ArrayView", Dart_TypedData_kInt8}, |
| 292 {"_Uint8ArrayView", Dart_TypedData_kUint8}, | 271 {"_Uint8ArrayView", Dart_TypedData_kUint8}, |
| 293 {"_Uint8ClampedArrayView", Dart_TypedData_kUint8Clamped}, | 272 {"_Uint8ClampedArrayView", Dart_TypedData_kUint8Clamped}, |
| (...skipping 13 matching lines...) Expand all Loading... |
| 307 while (view_class_names[i].name != NULL) { | 286 while (view_class_names[i].name != NULL) { |
| 308 if (strncmp(view_class_names[i].name, class_name, | 287 if (strncmp(view_class_names[i].name, class_name, |
| 309 strlen(view_class_names[i].name)) == 0) { | 288 strlen(view_class_names[i].name)) == 0) { |
| 310 return view_class_names[i].type; | 289 return view_class_names[i].type; |
| 311 } | 290 } |
| 312 i++; | 291 i++; |
| 313 } | 292 } |
| 314 return Dart_TypedData_kInvalid; | 293 return Dart_TypedData_kInvalid; |
| 315 } | 294 } |
| 316 | 295 |
| 317 | |
| 318 Dart_CObject* ApiMessageReader::ReadInlinedObject(intptr_t object_id) { | 296 Dart_CObject* ApiMessageReader::ReadInlinedObject(intptr_t object_id) { |
| 319 // Read the class header information and lookup the class. | 297 // Read the class header information and lookup the class. |
| 320 intptr_t class_header = Read<int32_t>(); | 298 intptr_t class_header = Read<int32_t>(); |
| 321 intptr_t tags = ReadTags(); | 299 intptr_t tags = ReadTags(); |
| 322 USE(tags); | 300 USE(tags); |
| 323 intptr_t class_id; | 301 intptr_t class_id; |
| 324 | 302 |
| 325 // There is limited support for reading regular dart instances. Only | 303 // There is limited support for reading regular dart instances. Only |
| 326 // typed data views are currently handled. | 304 // typed data views are currently handled. |
| 327 if (SerializedHeaderData::decode(class_header) == kInstanceObjectId) { | 305 if (SerializedHeaderData::decode(class_header) == kInstanceObjectId) { |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 400 } | 378 } |
| 401 for (int i = 0; i < len; i++) { | 379 for (int i = 0; i < len; i++) { |
| 402 value->value.as_array.values[i] = ReadObjectRef(); | 380 value->value.as_array.values[i] = ReadObjectRef(); |
| 403 } | 381 } |
| 404 return value; | 382 return value; |
| 405 } | 383 } |
| 406 | 384 |
| 407 return ReadInternalVMObject(class_id, object_id); | 385 return ReadInternalVMObject(class_id, object_id); |
| 408 } | 386 } |
| 409 | 387 |
| 410 | |
| 411 Dart_CObject* ApiMessageReader::ReadPredefinedSymbol(intptr_t object_id) { | 388 Dart_CObject* ApiMessageReader::ReadPredefinedSymbol(intptr_t object_id) { |
| 412 ASSERT(Symbols::IsPredefinedSymbolId(object_id)); | 389 ASSERT(Symbols::IsPredefinedSymbolId(object_id)); |
| 413 intptr_t symbol_id = object_id - kMaxPredefinedObjectIds; | 390 intptr_t symbol_id = object_id - kMaxPredefinedObjectIds; |
| 414 Dart_CObject* object; | 391 Dart_CObject* object; |
| 415 if (vm_symbol_references_ != NULL && | 392 if (vm_symbol_references_ != NULL && |
| 416 (object = vm_symbol_references_[symbol_id]) != NULL) { | 393 (object = vm_symbol_references_[symbol_id]) != NULL) { |
| 417 return object; | 394 return object; |
| 418 } | 395 } |
| 419 | 396 |
| 420 if (vm_symbol_references_ == NULL) { | 397 if (vm_symbol_references_ == NULL) { |
| 421 intptr_t size = | 398 intptr_t size = |
| 422 (sizeof(*vm_symbol_references_) * Symbols::kMaxPredefinedId); | 399 (sizeof(*vm_symbol_references_) * Symbols::kMaxPredefinedId); |
| 423 vm_symbol_references_ = reinterpret_cast<Dart_CObject**>(allocator(size)); | 400 vm_symbol_references_ = reinterpret_cast<Dart_CObject**>(allocator(size)); |
| 424 memset(vm_symbol_references_, 0, size); | 401 memset(vm_symbol_references_, 0, size); |
| 425 } | 402 } |
| 426 | 403 |
| 427 object = CreateDartCObjectString(Symbols::GetPredefinedSymbol(object_id)); | 404 object = CreateDartCObjectString(Symbols::GetPredefinedSymbol(object_id)); |
| 428 ASSERT(vm_symbol_references_[symbol_id] == NULL); | 405 ASSERT(vm_symbol_references_[symbol_id] == NULL); |
| 429 vm_symbol_references_[symbol_id] = object; | 406 vm_symbol_references_[symbol_id] = object; |
| 430 return object; | 407 return object; |
| 431 } | 408 } |
| 432 | 409 |
| 433 | |
| 434 intptr_t ApiMessageReader::NextAvailableObjectId() const { | 410 intptr_t ApiMessageReader::NextAvailableObjectId() const { |
| 435 return backward_references_.length() + kMaxPredefinedObjectIds; | 411 return backward_references_.length() + kMaxPredefinedObjectIds; |
| 436 } | 412 } |
| 437 | 413 |
| 438 | |
| 439 Dart_CObject* ApiMessageReader::CreateDartCObjectString(RawObject* raw) { | 414 Dart_CObject* ApiMessageReader::CreateDartCObjectString(RawObject* raw) { |
| 440 ASSERT(RawObject::IsOneByteStringClassId(raw->GetClassId())); | 415 ASSERT(RawObject::IsOneByteStringClassId(raw->GetClassId())); |
| 441 RawOneByteString* raw_str = reinterpret_cast<RawOneByteString*>(raw); | 416 RawOneByteString* raw_str = reinterpret_cast<RawOneByteString*>(raw); |
| 442 intptr_t len = Smi::Value(raw_str->ptr()->length_); | 417 intptr_t len = Smi::Value(raw_str->ptr()->length_); |
| 443 Dart_CObject* object = AllocateDartCObjectString(len); | 418 Dart_CObject* object = AllocateDartCObjectString(len); |
| 444 char* p = object->value.as_string; | 419 char* p = object->value.as_string; |
| 445 memmove(p, raw_str->ptr()->data(), len); | 420 memmove(p, raw_str->ptr()->data(), len); |
| 446 p[len] = '\0'; | 421 p[len] = '\0'; |
| 447 return object; | 422 return object; |
| 448 } | 423 } |
| 449 | 424 |
| 450 | |
| 451 Dart_CObject* ApiMessageReader::GetCanonicalMintObject(Dart_CObject_Type type, | 425 Dart_CObject* ApiMessageReader::GetCanonicalMintObject(Dart_CObject_Type type, |
| 452 int64_t value64) { | 426 int64_t value64) { |
| 453 Dart_CObject* object = NULL; | 427 Dart_CObject* object = NULL; |
| 454 for (intptr_t i = 0; i < vm_isolate_references_.length(); i++) { | 428 for (intptr_t i = 0; i < vm_isolate_references_.length(); i++) { |
| 455 object = vm_isolate_references_.At(i); | 429 object = vm_isolate_references_.At(i); |
| 456 if (object->type == type) { | 430 if (object->type == type) { |
| 457 if (value64 == object->value.as_int64) { | 431 if (value64 == object->value.as_int64) { |
| 458 return object; | 432 return object; |
| 459 } | 433 } |
| 460 } | 434 } |
| 461 } | 435 } |
| 462 if (type == Dart_CObject_kInt32) { | 436 if (type == Dart_CObject_kInt32) { |
| 463 object = AllocateDartCObjectInt32(static_cast<int32_t>(value64)); | 437 object = AllocateDartCObjectInt32(static_cast<int32_t>(value64)); |
| 464 } else { | 438 } else { |
| 465 object = AllocateDartCObjectInt64(value64); | 439 object = AllocateDartCObjectInt64(value64); |
| 466 } | 440 } |
| 467 vm_isolate_references_.Add(object); | 441 vm_isolate_references_.Add(object); |
| 468 return object; | 442 return object; |
| 469 } | 443 } |
| 470 | 444 |
| 471 | |
| 472 Dart_CObject* ApiMessageReader::ReadObjectRef() { | 445 Dart_CObject* ApiMessageReader::ReadObjectRef() { |
| 473 int64_t value64 = Read<int64_t>(); | 446 int64_t value64 = Read<int64_t>(); |
| 474 if ((value64 & kSmiTagMask) == 0) { | 447 if ((value64 & kSmiTagMask) == 0) { |
| 475 int64_t untagged_value = value64 >> kSmiTagShift; | 448 int64_t untagged_value = value64 >> kSmiTagShift; |
| 476 if ((kMinInt32 <= untagged_value) && (untagged_value <= kMaxInt32)) { | 449 if ((kMinInt32 <= untagged_value) && (untagged_value <= kMaxInt32)) { |
| 477 return AllocateDartCObjectInt32(static_cast<int32_t>(untagged_value)); | 450 return AllocateDartCObjectInt32(static_cast<int32_t>(untagged_value)); |
| 478 } else { | 451 } else { |
| 479 return AllocateDartCObjectInt64(untagged_value); | 452 return AllocateDartCObjectInt64(untagged_value); |
| 480 } | 453 } |
| 481 } | 454 } |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 516 if ((class_id == kArrayCid) || (class_id == kImmutableArrayCid)) { | 489 if ((class_id == kArrayCid) || (class_id == kImmutableArrayCid)) { |
| 517 ASSERT(GetBackRef(object_id) == NULL); | 490 ASSERT(GetBackRef(object_id) == NULL); |
| 518 intptr_t len = ReadSmiValue(); | 491 intptr_t len = ReadSmiValue(); |
| 519 Dart_CObject* value = AllocateDartCObjectArray(len); | 492 Dart_CObject* value = AllocateDartCObjectArray(len); |
| 520 AddBackRef(object_id, value, kIsNotDeserialized); | 493 AddBackRef(object_id, value, kIsNotDeserialized); |
| 521 return value; | 494 return value; |
| 522 } | 495 } |
| 523 return ReadInternalVMObject(class_id, object_id); | 496 return ReadInternalVMObject(class_id, object_id); |
| 524 } | 497 } |
| 525 | 498 |
| 526 | |
| 527 Dart_CObject* ApiMessageReader::ReadVMIsolateObject(intptr_t value) { | 499 Dart_CObject* ApiMessageReader::ReadVMIsolateObject(intptr_t value) { |
| 528 intptr_t object_id = GetVMIsolateObjectId(value); | 500 intptr_t object_id = GetVMIsolateObjectId(value); |
| 529 if (object_id == kNullObject) { | 501 if (object_id == kNullObject) { |
| 530 return AllocateDartCObjectNull(); | 502 return AllocateDartCObjectNull(); |
| 531 } | 503 } |
| 532 if (object_id == kTrueValue) { | 504 if (object_id == kTrueValue) { |
| 533 return AllocateDartCObjectBool(true); | 505 return AllocateDartCObjectBool(true); |
| 534 } | 506 } |
| 535 if (object_id == kFalseValue) { | 507 if (object_id == kFalseValue) { |
| 536 return AllocateDartCObjectBool(false); | 508 return AllocateDartCObjectBool(false); |
| 537 } | 509 } |
| 538 if (object_id == kDoubleObject) { | 510 if (object_id == kDoubleObject) { |
| 539 return AllocateDartCObjectDouble(ReadDouble()); | 511 return AllocateDartCObjectDouble(ReadDouble()); |
| 540 } | 512 } |
| 541 if (Symbols::IsPredefinedSymbolId(object_id)) { | 513 if (Symbols::IsPredefinedSymbolId(object_id)) { |
| 542 return ReadPredefinedSymbol(object_id); | 514 return ReadPredefinedSymbol(object_id); |
| 543 } | 515 } |
| 544 // No other VM isolate objects are supported. | 516 // No other VM isolate objects are supported. |
| 545 return AllocateDartCObjectNull(); | 517 return AllocateDartCObjectNull(); |
| 546 } | 518 } |
| 547 | 519 |
| 548 | |
| 549 Dart_CObject* ApiMessageReader::ReadInternalVMObject(intptr_t class_id, | 520 Dart_CObject* ApiMessageReader::ReadInternalVMObject(intptr_t class_id, |
| 550 intptr_t object_id) { | 521 intptr_t object_id) { |
| 551 switch (class_id) { | 522 switch (class_id) { |
| 552 case kClassCid: { | 523 case kClassCid: { |
| 553 Read<bool>(); // Consume the is_in_fullsnapshot indicator. | 524 Read<bool>(); // Consume the is_in_fullsnapshot indicator. |
| 554 Dart_CObject_Internal* object = AllocateDartCObjectClass(); | 525 Dart_CObject_Internal* object = AllocateDartCObjectClass(); |
| 555 AddBackRef(object_id, object, kIsDeserialized); | 526 AddBackRef(object_id, object, kIsDeserialized); |
| 556 object->internal.as_class.library_url = ReadObjectImpl(); | 527 object->internal.as_class.library_url = ReadObjectImpl(); |
| 557 ASSERT(object->internal.as_class.library_url->type == | 528 ASSERT(object->internal.as_class.library_url->type == |
| 558 Dart_CObject_kString); | 529 Dart_CObject_kString); |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 698 AddBackRef(object_id, object, kIsDeserialized); | 669 AddBackRef(object_id, object, kIsDeserialized); |
| 699 return object; | 670 return object; |
| 700 } | 671 } |
| 701 | 672 |
| 702 #define READ_TYPED_DATA_HEADER(type) \ | 673 #define READ_TYPED_DATA_HEADER(type) \ |
| 703 intptr_t len = ReadSmiValue(); \ | 674 intptr_t len = ReadSmiValue(); \ |
| 704 Dart_CObject* object = \ | 675 Dart_CObject* object = \ |
| 705 AllocateDartCObjectTypedData(Dart_TypedData_k##type, len); \ | 676 AllocateDartCObjectTypedData(Dart_TypedData_k##type, len); \ |
| 706 AddBackRef(object_id, object, kIsDeserialized); | 677 AddBackRef(object_id, object, kIsDeserialized); |
| 707 | 678 |
| 708 | |
| 709 #define READ_TYPED_DATA(type, ctype) \ | 679 #define READ_TYPED_DATA(type, ctype) \ |
| 710 { \ | 680 { \ |
| 711 READ_TYPED_DATA_HEADER(type); \ | 681 READ_TYPED_DATA_HEADER(type); \ |
| 712 if (len > 0) { \ | 682 if (len > 0) { \ |
| 713 ctype* p = reinterpret_cast<ctype*>(object->value.as_typed_data.values); \ | 683 ctype* p = reinterpret_cast<ctype*>(object->value.as_typed_data.values); \ |
| 714 for (intptr_t i = 0; i < len; i++) { \ | 684 for (intptr_t i = 0; i < len; i++) { \ |
| 715 p[i] = Read<ctype>(); \ | 685 p[i] = Read<ctype>(); \ |
| 716 } \ | 686 } \ |
| 717 } \ | 687 } \ |
| 718 return object; \ | 688 return object; \ |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 810 return value; | 780 return value; |
| 811 } | 781 } |
| 812 default: | 782 default: |
| 813 // Everything else not supported. | 783 // Everything else not supported. |
| 814 Dart_CObject* value = AllocateDartCObjectUnsupported(); | 784 Dart_CObject* value = AllocateDartCObjectUnsupported(); |
| 815 AddBackRef(object_id, value, kIsDeserialized); | 785 AddBackRef(object_id, value, kIsDeserialized); |
| 816 return value; | 786 return value; |
| 817 } | 787 } |
| 818 } | 788 } |
| 819 | 789 |
| 820 | |
| 821 Dart_CObject* ApiMessageReader::ReadIndexedObject(intptr_t object_id) { | 790 Dart_CObject* ApiMessageReader::ReadIndexedObject(intptr_t object_id) { |
| 822 if (object_id == kDynamicType || object_id == kDoubleType || | 791 if (object_id == kDynamicType || object_id == kDoubleType || |
| 823 object_id == kIntType || object_id == kBoolType || | 792 object_id == kIntType || object_id == kBoolType || |
| 824 object_id == kStringType) { | 793 object_id == kStringType) { |
| 825 // Always return dynamic type (this is only a marker). | 794 // Always return dynamic type (this is only a marker). |
| 826 return &dynamic_type_marker; | 795 return &dynamic_type_marker; |
| 827 } | 796 } |
| 828 intptr_t index = object_id - kMaxPredefinedObjectIds; | 797 intptr_t index = object_id - kMaxPredefinedObjectIds; |
| 829 ASSERT((0 <= index) && (index < backward_references_.length())); | 798 ASSERT((0 <= index) && (index < backward_references_.length())); |
| 830 ASSERT(backward_references_[index]->reference() != NULL); | 799 ASSERT(backward_references_[index]->reference() != NULL); |
| 831 return backward_references_[index]->reference(); | 800 return backward_references_[index]->reference(); |
| 832 } | 801 } |
| 833 | 802 |
| 834 | |
| 835 Dart_CObject* ApiMessageReader::ReadObject() { | 803 Dart_CObject* ApiMessageReader::ReadObject() { |
| 836 Dart_CObject* value = ReadObjectImpl(); | 804 Dart_CObject* value = ReadObjectImpl(); |
| 837 for (intptr_t i = 0; i < backward_references_.length(); i++) { | 805 for (intptr_t i = 0; i < backward_references_.length(); i++) { |
| 838 if (!backward_references_[i]->is_deserialized()) { | 806 if (!backward_references_[i]->is_deserialized()) { |
| 839 ReadObjectImpl(); | 807 ReadObjectImpl(); |
| 840 backward_references_[i]->set_state(kIsDeserialized); | 808 backward_references_[i]->set_state(kIsDeserialized); |
| 841 } | 809 } |
| 842 } | 810 } |
| 843 return value; | 811 return value; |
| 844 } | 812 } |
| 845 | 813 |
| 846 | |
| 847 Dart_CObject* ApiMessageReader::ReadObjectImpl() { | 814 Dart_CObject* ApiMessageReader::ReadObjectImpl() { |
| 848 int64_t value64 = Read<int64_t>(); | 815 int64_t value64 = Read<int64_t>(); |
| 849 if ((value64 & kSmiTagMask) == 0) { | 816 if ((value64 & kSmiTagMask) == 0) { |
| 850 int64_t untagged_value = value64 >> kSmiTagShift; | 817 int64_t untagged_value = value64 >> kSmiTagShift; |
| 851 if ((kMinInt32 <= untagged_value) && (untagged_value <= kMaxInt32)) { | 818 if ((kMinInt32 <= untagged_value) && (untagged_value <= kMaxInt32)) { |
| 852 return AllocateDartCObjectInt32(static_cast<int32_t>(untagged_value)); | 819 return AllocateDartCObjectInt32(static_cast<int32_t>(untagged_value)); |
| 853 } else { | 820 } else { |
| 854 return AllocateDartCObjectInt64(untagged_value); | 821 return AllocateDartCObjectInt64(untagged_value); |
| 855 } | 822 } |
| 856 } | 823 } |
| 857 ASSERT((value64 <= kIntptrMax) && (value64 >= kIntptrMin)); | 824 ASSERT((value64 <= kIntptrMax) && (value64 >= kIntptrMin)); |
| 858 intptr_t value = static_cast<intptr_t>(value64); | 825 intptr_t value = static_cast<intptr_t>(value64); |
| 859 if (IsVMIsolateObject(value)) { | 826 if (IsVMIsolateObject(value)) { |
| 860 return ReadVMIsolateObject(value); | 827 return ReadVMIsolateObject(value); |
| 861 } | 828 } |
| 862 if (SerializedHeaderTag::decode(value) == kObjectId) { | 829 if (SerializedHeaderTag::decode(value) == kObjectId) { |
| 863 return ReadIndexedObject(SerializedHeaderData::decode(value)); | 830 return ReadIndexedObject(SerializedHeaderData::decode(value)); |
| 864 } | 831 } |
| 865 ASSERT(SerializedHeaderTag::decode(value) == kInlined); | 832 ASSERT(SerializedHeaderTag::decode(value) == kInlined); |
| 866 | 833 |
| 867 intptr_t object_id = SerializedHeaderData::decode(value); | 834 intptr_t object_id = SerializedHeaderData::decode(value); |
| 868 if (object_id == kOmittedObjectId) { | 835 if (object_id == kOmittedObjectId) { |
| 869 object_id = NextAvailableObjectId(); | 836 object_id = NextAvailableObjectId(); |
| 870 } | 837 } |
| 871 return ReadInlinedObject(object_id); | 838 return ReadInlinedObject(object_id); |
| 872 } | 839 } |
| 873 | 840 |
| 874 | |
| 875 void ApiMessageReader::AddBackRef(intptr_t id, | 841 void ApiMessageReader::AddBackRef(intptr_t id, |
| 876 Dart_CObject* obj, | 842 Dart_CObject* obj, |
| 877 DeserializeState state) { | 843 DeserializeState state) { |
| 878 intptr_t index = (id - kMaxPredefinedObjectIds); | 844 intptr_t index = (id - kMaxPredefinedObjectIds); |
| 879 ASSERT(index == backward_references_.length()); | 845 ASSERT(index == backward_references_.length()); |
| 880 BackRefNode* node = AllocateBackRefNode(obj, state); | 846 BackRefNode* node = AllocateBackRefNode(obj, state); |
| 881 ASSERT(node != NULL); | 847 ASSERT(node != NULL); |
| 882 backward_references_.Add(node); | 848 backward_references_.Add(node); |
| 883 } | 849 } |
| 884 | 850 |
| 885 | |
| 886 Dart_CObject* ApiMessageReader::GetBackRef(intptr_t id) { | 851 Dart_CObject* ApiMessageReader::GetBackRef(intptr_t id) { |
| 887 ASSERT(id >= kMaxPredefinedObjectIds); | 852 ASSERT(id >= kMaxPredefinedObjectIds); |
| 888 intptr_t index = (id - kMaxPredefinedObjectIds); | 853 intptr_t index = (id - kMaxPredefinedObjectIds); |
| 889 if (index < backward_references_.length()) { | 854 if (index < backward_references_.length()) { |
| 890 return backward_references_[index]->reference(); | 855 return backward_references_[index]->reference(); |
| 891 } | 856 } |
| 892 return NULL; | 857 return NULL; |
| 893 } | 858 } |
| 894 | 859 |
| 895 | |
| 896 void ApiMessageWriter::WriteMessage(intptr_t field_count, intptr_t* data) { | 860 void ApiMessageWriter::WriteMessage(intptr_t field_count, intptr_t* data) { |
| 897 // Write out the serialization header value for this object. | 861 // Write out the serialization header value for this object. |
| 898 WriteInlinedObjectHeader(kMaxPredefinedObjectIds); | 862 WriteInlinedObjectHeader(kMaxPredefinedObjectIds); |
| 899 | 863 |
| 900 // Write out the class and tags information. | 864 // Write out the class and tags information. |
| 901 WriteIndexedObject(kArrayCid); | 865 WriteIndexedObject(kArrayCid); |
| 902 WriteTags(0); | 866 WriteTags(0); |
| 903 | 867 |
| 904 // Write out the length field. | 868 // Write out the length field. |
| 905 Write<RawObject*>(Smi::New(field_count)); | 869 Write<RawObject*>(Smi::New(field_count)); |
| 906 | 870 |
| 907 // Write out the type arguments. | 871 // Write out the type arguments. |
| 908 WriteNullObject(); | 872 WriteNullObject(); |
| 909 | 873 |
| 910 // Write out the individual Smis. | 874 // Write out the individual Smis. |
| 911 for (int i = 0; i < field_count; i++) { | 875 for (int i = 0; i < field_count; i++) { |
| 912 Write<RawObject*>(Integer::New(data[i])); | 876 Write<RawObject*>(Integer::New(data[i])); |
| 913 } | 877 } |
| 914 } | 878 } |
| 915 | 879 |
| 916 | |
| 917 void ApiMessageWriter::MarkCObject(Dart_CObject* object, intptr_t object_id) { | 880 void ApiMessageWriter::MarkCObject(Dart_CObject* object, intptr_t object_id) { |
| 918 // Mark the object as serialized by adding the object id to the | 881 // Mark the object as serialized by adding the object id to the |
| 919 // upper bits of the type field in the Dart_CObject structure. Add | 882 // upper bits of the type field in the Dart_CObject structure. Add |
| 920 // an offset for making marking of object id 0 possible. | 883 // an offset for making marking of object id 0 possible. |
| 921 ASSERT(!IsCObjectMarked(object)); | 884 ASSERT(!IsCObjectMarked(object)); |
| 922 intptr_t mark_value = object_id + kDartCObjectMarkOffset; | 885 intptr_t mark_value = object_id + kDartCObjectMarkOffset; |
| 923 object->type = static_cast<Dart_CObject_Type>( | 886 object->type = static_cast<Dart_CObject_Type>( |
| 924 ((mark_value) << kDartCObjectTypeBits) | object->type); | 887 ((mark_value) << kDartCObjectTypeBits) | object->type); |
| 925 } | 888 } |
| 926 | 889 |
| 927 | |
| 928 void ApiMessageWriter::UnmarkCObject(Dart_CObject* object) { | 890 void ApiMessageWriter::UnmarkCObject(Dart_CObject* object) { |
| 929 ASSERT(IsCObjectMarked(object)); | 891 ASSERT(IsCObjectMarked(object)); |
| 930 object->type = | 892 object->type = |
| 931 static_cast<Dart_CObject_Type>(object->type & kDartCObjectTypeMask); | 893 static_cast<Dart_CObject_Type>(object->type & kDartCObjectTypeMask); |
| 932 } | 894 } |
| 933 | 895 |
| 934 | |
| 935 bool ApiMessageWriter::IsCObjectMarked(Dart_CObject* object) { | 896 bool ApiMessageWriter::IsCObjectMarked(Dart_CObject* object) { |
| 936 return (object->type & kDartCObjectMarkMask) != 0; | 897 return (object->type & kDartCObjectMarkMask) != 0; |
| 937 } | 898 } |
| 938 | 899 |
| 939 | |
| 940 intptr_t ApiMessageWriter::GetMarkedCObjectMark(Dart_CObject* object) { | 900 intptr_t ApiMessageWriter::GetMarkedCObjectMark(Dart_CObject* object) { |
| 941 ASSERT(IsCObjectMarked(object)); | 901 ASSERT(IsCObjectMarked(object)); |
| 942 intptr_t mark_value = | 902 intptr_t mark_value = |
| 943 ((object->type & kDartCObjectMarkMask) >> kDartCObjectTypeBits); | 903 ((object->type & kDartCObjectMarkMask) >> kDartCObjectTypeBits); |
| 944 // An offset was added to object id for making marking object id 0 possible. | 904 // An offset was added to object id for making marking object id 0 possible. |
| 945 return mark_value - kDartCObjectMarkOffset; | 905 return mark_value - kDartCObjectMarkOffset; |
| 946 } | 906 } |
| 947 | 907 |
| 948 | |
| 949 void ApiMessageWriter::UnmarkAllCObjects(Dart_CObject* object) { | 908 void ApiMessageWriter::UnmarkAllCObjects(Dart_CObject* object) { |
| 950 if (!IsCObjectMarked(object)) return; | 909 if (!IsCObjectMarked(object)) return; |
| 951 UnmarkCObject(object); | 910 UnmarkCObject(object); |
| 952 if (object->type == Dart_CObject_kArray) { | 911 if (object->type == Dart_CObject_kArray) { |
| 953 for (int i = 0; i < object->value.as_array.length; i++) { | 912 for (int i = 0; i < object->value.as_array.length; i++) { |
| 954 Dart_CObject* element = object->value.as_array.values[i]; | 913 Dart_CObject* element = object->value.as_array.values[i]; |
| 955 UnmarkAllCObjects(element); | 914 UnmarkAllCObjects(element); |
| 956 } | 915 } |
| 957 } | 916 } |
| 958 } | 917 } |
| 959 | 918 |
| 960 | |
| 961 void ApiMessageWriter::AddToForwardList(Dart_CObject* object) { | 919 void ApiMessageWriter::AddToForwardList(Dart_CObject* object) { |
| 962 if (forward_id_ >= forward_list_length_) { | 920 if (forward_id_ >= forward_list_length_) { |
| 963 void* new_list = NULL; | 921 void* new_list = NULL; |
| 964 if (forward_list_length_ == 0) { | 922 if (forward_list_length_ == 0) { |
| 965 forward_list_length_ = 4; | 923 forward_list_length_ = 4; |
| 966 intptr_t new_size = forward_list_length_ * sizeof(object); | 924 intptr_t new_size = forward_list_length_ * sizeof(object); |
| 967 new_list = ::malloc(new_size); | 925 new_list = ::malloc(new_size); |
| 968 } else { | 926 } else { |
| 969 forward_list_length_ *= 2; | 927 forward_list_length_ *= 2; |
| 970 intptr_t new_size = (forward_list_length_ * sizeof(object)); | 928 intptr_t new_size = (forward_list_length_ * sizeof(object)); |
| 971 new_list = ::realloc(forward_list_, new_size); | 929 new_list = ::realloc(forward_list_, new_size); |
| 972 } | 930 } |
| 973 ASSERT(new_list != NULL); | 931 ASSERT(new_list != NULL); |
| 974 forward_list_ = reinterpret_cast<Dart_CObject**>(new_list); | 932 forward_list_ = reinterpret_cast<Dart_CObject**>(new_list); |
| 975 } | 933 } |
| 976 forward_list_[forward_id_] = object; | 934 forward_list_[forward_id_] = object; |
| 977 forward_id_ += 1; | 935 forward_id_ += 1; |
| 978 } | 936 } |
| 979 | 937 |
| 980 | |
| 981 void ApiMessageWriter::WriteSmi(int64_t value) { | 938 void ApiMessageWriter::WriteSmi(int64_t value) { |
| 982 ASSERT(Smi::IsValid(value)); | 939 ASSERT(Smi::IsValid(value)); |
| 983 Write<RawObject*>(Smi::New(static_cast<intptr_t>(value))); | 940 Write<RawObject*>(Smi::New(static_cast<intptr_t>(value))); |
| 984 } | 941 } |
| 985 | 942 |
| 986 | |
| 987 void ApiMessageWriter::WriteNullObject() { | 943 void ApiMessageWriter::WriteNullObject() { |
| 988 WriteVMIsolateObject(kNullObject); | 944 WriteVMIsolateObject(kNullObject); |
| 989 } | 945 } |
| 990 | 946 |
| 991 | |
| 992 void ApiMessageWriter::WriteMint(Dart_CObject* object, int64_t value) { | 947 void ApiMessageWriter::WriteMint(Dart_CObject* object, int64_t value) { |
| 993 ASSERT(!Smi::IsValid(value)); | 948 ASSERT(!Smi::IsValid(value)); |
| 994 // Write out the serialization header value for mint object. | 949 // Write out the serialization header value for mint object. |
| 995 WriteInlinedHeader(object); | 950 WriteInlinedHeader(object); |
| 996 // Write out the class and tags information. | 951 // Write out the class and tags information. |
| 997 WriteIndexedObject(kMintCid); | 952 WriteIndexedObject(kMintCid); |
| 998 WriteTags(0); | 953 WriteTags(0); |
| 999 // Write the 64-bit value. | 954 // Write the 64-bit value. |
| 1000 Write<int64_t>(value); | 955 Write<int64_t>(value); |
| 1001 } | 956 } |
| 1002 | 957 |
| 1003 | |
| 1004 void ApiMessageWriter::WriteInt32(Dart_CObject* object) { | 958 void ApiMessageWriter::WriteInt32(Dart_CObject* object) { |
| 1005 int64_t value = object->value.as_int32; | 959 int64_t value = object->value.as_int32; |
| 1006 if (Smi::IsValid(value)) { | 960 if (Smi::IsValid(value)) { |
| 1007 WriteSmi(value); | 961 WriteSmi(value); |
| 1008 } else { | 962 } else { |
| 1009 WriteMint(object, value); | 963 WriteMint(object, value); |
| 1010 } | 964 } |
| 1011 } | 965 } |
| 1012 | 966 |
| 1013 | |
| 1014 void ApiMessageWriter::WriteInt64(Dart_CObject* object) { | 967 void ApiMessageWriter::WriteInt64(Dart_CObject* object) { |
| 1015 int64_t value = object->value.as_int64; | 968 int64_t value = object->value.as_int64; |
| 1016 if (Smi::IsValid(value)) { | 969 if (Smi::IsValid(value)) { |
| 1017 WriteSmi(value); | 970 WriteSmi(value); |
| 1018 } else { | 971 } else { |
| 1019 WriteMint(object, value); | 972 WriteMint(object, value); |
| 1020 } | 973 } |
| 1021 } | 974 } |
| 1022 | 975 |
| 1023 | |
| 1024 void ApiMessageWriter::WriteInlinedHeader(Dart_CObject* object) { | 976 void ApiMessageWriter::WriteInlinedHeader(Dart_CObject* object) { |
| 1025 // Write out the serialization header value for this object. | 977 // Write out the serialization header value for this object. |
| 1026 WriteInlinedObjectHeader(kMaxPredefinedObjectIds + object_id_); | 978 WriteInlinedObjectHeader(kMaxPredefinedObjectIds + object_id_); |
| 1027 // Mark object with its object id. | 979 // Mark object with its object id. |
| 1028 MarkCObject(object, object_id_); | 980 MarkCObject(object, object_id_); |
| 1029 // Advance object id. | 981 // Advance object id. |
| 1030 object_id_++; | 982 object_id_++; |
| 1031 } | 983 } |
| 1032 | 984 |
| 1033 | |
| 1034 bool ApiMessageWriter::WriteCObject(Dart_CObject* object) { | 985 bool ApiMessageWriter::WriteCObject(Dart_CObject* object) { |
| 1035 if (IsCObjectMarked(object)) { | 986 if (IsCObjectMarked(object)) { |
| 1036 intptr_t object_id = GetMarkedCObjectMark(object); | 987 intptr_t object_id = GetMarkedCObjectMark(object); |
| 1037 WriteIndexedObject(kMaxPredefinedObjectIds + object_id); | 988 WriteIndexedObject(kMaxPredefinedObjectIds + object_id); |
| 1038 return true; | 989 return true; |
| 1039 } | 990 } |
| 1040 | 991 |
| 1041 Dart_CObject_Type type = object->type; | 992 Dart_CObject_Type type = object->type; |
| 1042 if (type == Dart_CObject_kArray) { | 993 if (type == Dart_CObject_kArray) { |
| 1043 const intptr_t array_length = object->value.as_array.length; | 994 const intptr_t array_length = object->value.as_array.length; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 1057 // Write out array elements. | 1008 // Write out array elements. |
| 1058 for (int i = 0; i < array_length; i++) { | 1009 for (int i = 0; i < array_length; i++) { |
| 1059 bool success = WriteCObjectRef(object->value.as_array.values[i]); | 1010 bool success = WriteCObjectRef(object->value.as_array.values[i]); |
| 1060 if (!success) return false; | 1011 if (!success) return false; |
| 1061 } | 1012 } |
| 1062 return true; | 1013 return true; |
| 1063 } | 1014 } |
| 1064 return WriteCObjectInlined(object, type); | 1015 return WriteCObjectInlined(object, type); |
| 1065 } | 1016 } |
| 1066 | 1017 |
| 1067 | |
| 1068 bool ApiMessageWriter::WriteCObjectRef(Dart_CObject* object) { | 1018 bool ApiMessageWriter::WriteCObjectRef(Dart_CObject* object) { |
| 1069 if (IsCObjectMarked(object)) { | 1019 if (IsCObjectMarked(object)) { |
| 1070 intptr_t object_id = GetMarkedCObjectMark(object); | 1020 intptr_t object_id = GetMarkedCObjectMark(object); |
| 1071 WriteIndexedObject(kMaxPredefinedObjectIds + object_id); | 1021 WriteIndexedObject(kMaxPredefinedObjectIds + object_id); |
| 1072 return true; | 1022 return true; |
| 1073 } | 1023 } |
| 1074 | 1024 |
| 1075 Dart_CObject_Type type = object->type; | 1025 Dart_CObject_Type type = object->type; |
| 1076 if (type == Dart_CObject_kArray) { | 1026 if (type == Dart_CObject_kArray) { |
| 1077 const intptr_t array_length = object->value.as_array.length; | 1027 const intptr_t array_length = object->value.as_array.length; |
| 1078 if (array_length < 0 || array_length > Array::kMaxElements) { | 1028 if (array_length < 0 || array_length > Array::kMaxElements) { |
| 1079 return false; | 1029 return false; |
| 1080 } | 1030 } |
| 1081 // Write out the serialization header value for this object. | 1031 // Write out the serialization header value for this object. |
| 1082 WriteInlinedHeader(object); | 1032 WriteInlinedHeader(object); |
| 1083 // Write out the class information. | 1033 // Write out the class information. |
| 1084 WriteIndexedObject(kArrayCid); | 1034 WriteIndexedObject(kArrayCid); |
| 1085 WriteTags(0); | 1035 WriteTags(0); |
| 1086 // Write out the length information. | 1036 // Write out the length information. |
| 1087 WriteSmi(array_length); | 1037 WriteSmi(array_length); |
| 1088 // Add object to forward list so that this object is serialized later. | 1038 // Add object to forward list so that this object is serialized later. |
| 1089 AddToForwardList(object); | 1039 AddToForwardList(object); |
| 1090 return true; | 1040 return true; |
| 1091 } | 1041 } |
| 1092 return WriteCObjectInlined(object, type); | 1042 return WriteCObjectInlined(object, type); |
| 1093 } | 1043 } |
| 1094 | 1044 |
| 1095 | |
| 1096 bool ApiMessageWriter::WriteForwardedCObject(Dart_CObject* object) { | 1045 bool ApiMessageWriter::WriteForwardedCObject(Dart_CObject* object) { |
| 1097 ASSERT(IsCObjectMarked(object)); | 1046 ASSERT(IsCObjectMarked(object)); |
| 1098 Dart_CObject_Type type = | 1047 Dart_CObject_Type type = |
| 1099 static_cast<Dart_CObject_Type>(object->type & kDartCObjectTypeMask); | 1048 static_cast<Dart_CObject_Type>(object->type & kDartCObjectTypeMask); |
| 1100 ASSERT(type == Dart_CObject_kArray); | 1049 ASSERT(type == Dart_CObject_kArray); |
| 1101 const intptr_t array_length = object->value.as_array.length; | 1050 const intptr_t array_length = object->value.as_array.length; |
| 1102 if (array_length < 0 || array_length > Array::kMaxElements) { | 1051 if (array_length < 0 || array_length > Array::kMaxElements) { |
| 1103 return false; | 1052 return false; |
| 1104 } | 1053 } |
| 1105 | 1054 |
| 1106 // Write out the serialization header value for this object. | 1055 // Write out the serialization header value for this object. |
| 1107 intptr_t object_id = GetMarkedCObjectMark(object); | 1056 intptr_t object_id = GetMarkedCObjectMark(object); |
| 1108 WriteInlinedObjectHeader(kMaxPredefinedObjectIds + object_id); | 1057 WriteInlinedObjectHeader(kMaxPredefinedObjectIds + object_id); |
| 1109 // Write out the class and tags information. | 1058 // Write out the class and tags information. |
| 1110 WriteIndexedObject(kArrayCid); | 1059 WriteIndexedObject(kArrayCid); |
| 1111 WriteTags(0); | 1060 WriteTags(0); |
| 1112 // Write out the length information. | 1061 // Write out the length information. |
| 1113 WriteSmi(array_length); | 1062 WriteSmi(array_length); |
| 1114 // Write out the type arguments. | 1063 // Write out the type arguments. |
| 1115 WriteNullObject(); | 1064 WriteNullObject(); |
| 1116 // Write out array elements. | 1065 // Write out array elements. |
| 1117 for (int i = 0; i < array_length; i++) { | 1066 for (int i = 0; i < array_length; i++) { |
| 1118 bool success = WriteCObjectRef(object->value.as_array.values[i]); | 1067 bool success = WriteCObjectRef(object->value.as_array.values[i]); |
| 1119 if (!success) return false; | 1068 if (!success) return false; |
| 1120 } | 1069 } |
| 1121 return true; | 1070 return true; |
| 1122 } | 1071 } |
| 1123 | 1072 |
| 1124 | |
| 1125 bool ApiMessageWriter::WriteCObjectInlined(Dart_CObject* object, | 1073 bool ApiMessageWriter::WriteCObjectInlined(Dart_CObject* object, |
| 1126 Dart_CObject_Type type) { | 1074 Dart_CObject_Type type) { |
| 1127 switch (type) { | 1075 switch (type) { |
| 1128 case Dart_CObject_kNull: | 1076 case Dart_CObject_kNull: |
| 1129 WriteNullObject(); | 1077 WriteNullObject(); |
| 1130 break; | 1078 break; |
| 1131 case Dart_CObject_kBool: | 1079 case Dart_CObject_kBool: |
| 1132 if (object->value.as_bool) { | 1080 if (object->value.as_bool) { |
| 1133 WriteVMIsolateObject(kTrueValue); | 1081 WriteVMIsolateObject(kTrueValue); |
| 1134 } else { | 1082 } else { |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1262 // order to post external arrays through ports. We need to make | 1210 // order to post external arrays through ports. We need to make |
| 1263 // sure that messages containing pointers can never be posted | 1211 // sure that messages containing pointers can never be posted |
| 1264 // to other processes. | 1212 // to other processes. |
| 1265 | 1213 |
| 1266 // Write out serialization header value for this object. | 1214 // Write out serialization header value for this object. |
| 1267 WriteInlinedHeader(object); | 1215 WriteInlinedHeader(object); |
| 1268 // Write out the class and tag information. | 1216 // Write out the class and tag information. |
| 1269 WriteIndexedObject(kExternalTypedDataUint8ArrayCid); | 1217 WriteIndexedObject(kExternalTypedDataUint8ArrayCid); |
| 1270 WriteTags(0); | 1218 WriteTags(0); |
| 1271 intptr_t length = object->value.as_external_typed_data.length; | 1219 intptr_t length = object->value.as_external_typed_data.length; |
| 1272 if (length < 0 || | 1220 if (length < 0 || length > ExternalTypedData::MaxElements( |
| 1273 length > | 1221 kExternalTypedDataUint8ArrayCid)) { |
| 1274 ExternalTypedData::MaxElements(kExternalTypedDataUint8ArrayCid)) { | |
| 1275 return false; | 1222 return false; |
| 1276 } | 1223 } |
| 1277 uint8_t* data = object->value.as_external_typed_data.data; | 1224 uint8_t* data = object->value.as_external_typed_data.data; |
| 1278 void* peer = object->value.as_external_typed_data.peer; | 1225 void* peer = object->value.as_external_typed_data.peer; |
| 1279 Dart_WeakPersistentHandleFinalizer callback = | 1226 Dart_WeakPersistentHandleFinalizer callback = |
| 1280 object->value.as_external_typed_data.callback; | 1227 object->value.as_external_typed_data.callback; |
| 1281 WriteSmi(length); | 1228 WriteSmi(length); |
| 1282 WriteRawPointerValue(reinterpret_cast<intptr_t>(data)); | 1229 WriteRawPointerValue(reinterpret_cast<intptr_t>(data)); |
| 1283 WriteRawPointerValue(reinterpret_cast<intptr_t>(peer)); | 1230 WriteRawPointerValue(reinterpret_cast<intptr_t>(peer)); |
| 1284 WriteRawPointerValue(reinterpret_cast<intptr_t>(callback)); | 1231 WriteRawPointerValue(reinterpret_cast<intptr_t>(callback)); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 1299 Write<uint64_t>(object->value.as_capability.id); | 1246 Write<uint64_t>(object->value.as_capability.id); |
| 1300 break; | 1247 break; |
| 1301 } | 1248 } |
| 1302 default: | 1249 default: |
| 1303 FATAL1("Unexpected Dart_CObject_Type %d\n", type); | 1250 FATAL1("Unexpected Dart_CObject_Type %d\n", type); |
| 1304 } | 1251 } |
| 1305 | 1252 |
| 1306 return true; | 1253 return true; |
| 1307 } | 1254 } |
| 1308 | 1255 |
| 1309 | |
| 1310 bool ApiMessageWriter::WriteCMessage(Dart_CObject* object) { | 1256 bool ApiMessageWriter::WriteCMessage(Dart_CObject* object) { |
| 1311 bool success = WriteCObject(object); | 1257 bool success = WriteCObject(object); |
| 1312 if (!success) { | 1258 if (!success) { |
| 1313 UnmarkAllCObjects(object); | 1259 UnmarkAllCObjects(object); |
| 1314 return false; | 1260 return false; |
| 1315 } | 1261 } |
| 1316 // Write out all objects that were added to the forward list and have | 1262 // Write out all objects that were added to the forward list and have |
| 1317 // not been serialized yet. These would typically be fields of arrays. | 1263 // not been serialized yet. These would typically be fields of arrays. |
| 1318 // NOTE: The forward list might grow as we process the list. | 1264 // NOTE: The forward list might grow as we process the list. |
| 1319 for (intptr_t i = 0; i < forward_id_; i++) { | 1265 for (intptr_t i = 0; i < forward_id_; i++) { |
| 1320 success = WriteForwardedCObject(forward_list_[i]); | 1266 success = WriteForwardedCObject(forward_list_[i]); |
| 1321 if (!success) { | 1267 if (!success) { |
| 1322 UnmarkAllCObjects(object); | 1268 UnmarkAllCObjects(object); |
| 1323 return false; | 1269 return false; |
| 1324 } | 1270 } |
| 1325 } | 1271 } |
| 1326 UnmarkAllCObjects(object); | 1272 UnmarkAllCObjects(object); |
| 1327 return true; | 1273 return true; |
| 1328 } | 1274 } |
| 1329 | 1275 |
| 1330 } // namespace dart | 1276 } // namespace dart |
| OLD | NEW |