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/object_store.h" | 7 #include "vm/object_store.h" |
8 | 8 |
9 namespace dart { | 9 namespace dart { |
10 | 10 |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 value->value.as_array.length = length; | 159 value->value.as_array.length = length; |
160 if (length > 0) { | 160 if (length > 0) { |
161 value->value.as_array.values = reinterpret_cast<Dart_CObject**>(value + 1); | 161 value->value.as_array.values = reinterpret_cast<Dart_CObject**>(value + 1); |
162 } else { | 162 } else { |
163 value->value.as_array.values = NULL; | 163 value->value.as_array.values = NULL; |
164 } | 164 } |
165 return value; | 165 return value; |
166 } | 166 } |
167 | 167 |
168 | 168 |
| 169 ApiMessageReader::BackRefNode* ApiMessageReader::AllocateBackRefNode( |
| 170 Dart_CObject* reference, |
| 171 DeserializeState state) { |
| 172 BackRefNode* value = |
| 173 reinterpret_cast<BackRefNode*>(alloc_(NULL, 0, sizeof(BackRefNode))); |
| 174 value->set_reference(reference); |
| 175 value->set_state(state); |
| 176 return value; |
| 177 } |
| 178 |
| 179 |
169 Dart_CObject* ApiMessageReader::ReadInlinedObject(intptr_t object_id) { | 180 Dart_CObject* ApiMessageReader::ReadInlinedObject(intptr_t object_id) { |
170 // Read the class header information and lookup the class. | 181 // Read the class header information and lookup the class. |
171 intptr_t class_header = ReadIntptrValue(); | 182 intptr_t class_header = ReadIntptrValue(); |
172 intptr_t tags = ReadIntptrValue(); | 183 intptr_t tags = ReadIntptrValue(); |
173 USE(tags); | 184 USE(tags); |
174 intptr_t class_id; | 185 intptr_t class_id; |
175 | 186 |
176 // Reading of regular dart instances is not supported. | 187 // Reading of regular dart instances is not supported. |
177 if (SerializedHeaderData::decode(class_header) == kInstanceId) { | 188 if (SerializedHeaderData::decode(class_header) == kInstanceId) { |
178 return AllocateDartCObjectUnsupported(); | 189 return AllocateDartCObjectUnsupported(); |
179 } | 190 } |
180 | 191 |
181 ASSERT((class_header & kSmiTagMask) != 0); | 192 ASSERT((class_header & kSmiTagMask) != 0); |
182 class_id = LookupInternalClass(class_header); | 193 class_id = LookupInternalClass(class_header); |
| 194 if (class_id == ObjectStore::kArrayClass || |
| 195 class_id == ObjectStore::kImmutableArrayClass) { |
| 196 intptr_t len = ReadSmiValue(); |
| 197 Dart_CObject* value = GetBackRef(object_id); |
| 198 if (value == NULL) { |
| 199 value = AllocateDartCObjectArray(len); |
| 200 AddBackRef(object_id, value, kIsDeserialized); |
| 201 } |
| 202 // Skip type arguments. |
| 203 // TODO(sjesse): Remove this when message serialization format is |
| 204 // updated (currently type_arguments is leaked). |
| 205 Dart_CObject* type_arguments = ReadObjectImpl(); |
| 206 if (type_arguments != &type_arguments_marker && |
| 207 type_arguments->type != Dart_CObject::kNull) { |
| 208 return AllocateDartCObjectUnsupported(); |
| 209 } |
| 210 for (int i = 0; i < len; i++) { |
| 211 value->value.as_array.values[i] = ReadObjectRef(); |
| 212 } |
| 213 return value; |
| 214 } |
| 215 |
| 216 return ReadInternalVMObject(class_id, object_id); |
| 217 } |
| 218 |
| 219 |
| 220 Dart_CObject* ApiMessageReader::ReadObjectRef() { |
| 221 int64_t value = Read<int64_t>(); |
| 222 if ((value & kSmiTagMask) == 0) { |
| 223 int64_t untagged_value = value >> kSmiTagShift; |
| 224 if (kMinInt32 <= untagged_value && untagged_value <= kMaxInt32) { |
| 225 return AllocateDartCObjectInt32(untagged_value); |
| 226 } else { |
| 227 return AllocateDartCObjectInt64(untagged_value); |
| 228 } |
| 229 } |
| 230 ASSERT((value <= kIntptrMax) && (value >= kIntptrMin)); |
| 231 SerializedHeaderType header_type = SerializedHeaderTag::decode(value); |
| 232 intptr_t header_value = SerializedHeaderData::decode(value); |
| 233 |
| 234 if (header_type == kObjectId) { |
| 235 return ReadIndexedObject(header_value); |
| 236 } |
| 237 ASSERT(header_type == kInlined); |
| 238 // Read the class header information and lookup the class. |
| 239 intptr_t class_header = ReadIntptrValue(); |
| 240 |
| 241 // Reading of regular dart instances is not supported. |
| 242 if (SerializedHeaderData::decode(class_header) == kInstanceId) { |
| 243 return AllocateDartCObjectUnsupported(); |
| 244 } |
| 245 ASSERT((class_header & kSmiTagMask) != 0); |
| 246 intptr_t object_id = header_value; |
| 247 intptr_t class_id = LookupInternalClass(class_header); |
| 248 if (class_id == ObjectStore::kArrayClass || |
| 249 class_id == ObjectStore::kImmutableArrayClass) { |
| 250 ASSERT(GetBackRef(object_id) == NULL); |
| 251 intptr_t len = ReadSmiValue(); |
| 252 Dart_CObject* value = AllocateDartCObjectArray(len); |
| 253 AddBackRef(object_id, value, kIsNotDeserialized); |
| 254 return value; |
| 255 } |
| 256 intptr_t tags = ReadIntptrValue(); |
| 257 USE(tags); |
| 258 |
| 259 return ReadInternalVMObject(class_id, object_id); |
| 260 } |
| 261 |
| 262 |
| 263 Dart_CObject* ApiMessageReader::ReadInternalVMObject(intptr_t class_id, |
| 264 intptr_t object_id) { |
183 switch (class_id) { | 265 switch (class_id) { |
184 case Object::kClassClass: { | 266 case Object::kClassClass: { |
185 return AllocateDartCObjectUnsupported(); | 267 return AllocateDartCObjectUnsupported(); |
186 } | 268 } |
187 case Object::kTypeArgumentsClass: { | 269 case Object::kTypeArgumentsClass: { |
188 // TODO(sjesse): Remove this when message serialization format is | 270 // TODO(sjesse): Remove this when message serialization format is |
189 // updated (currently length is leaked). | 271 // updated (currently length is leaked). |
190 Dart_CObject* value = &type_arguments_marker; | 272 Dart_CObject* value = &type_arguments_marker; |
191 AddBackwardReference(object_id, value); | 273 AddBackRef(object_id, value, kIsDeserialized); |
192 Dart_CObject* length = ReadObject(); | 274 Dart_CObject* length = ReadObjectImpl(); |
193 ASSERT(length->type == Dart_CObject::kInt32); | 275 ASSERT(length->type == Dart_CObject::kInt32); |
194 for (int i = 0; i < length->value.as_int32; i++) { | 276 for (int i = 0; i < length->value.as_int32; i++) { |
195 Dart_CObject* type = ReadObject(); | 277 Dart_CObject* type = ReadObjectImpl(); |
196 if (type != &dynamic_type_marker) { | 278 if (type != &dynamic_type_marker) { |
197 return AllocateDartCObjectUnsupported(); | 279 return AllocateDartCObjectUnsupported(); |
198 } | 280 } |
199 } | 281 } |
200 return value; | 282 return value; |
201 } | 283 } |
202 case Object::kTypeParameterClass: { | 284 case Object::kTypeParameterClass: { |
203 // TODO(sgjesse): Fix this workaround ignoring the type parameter. | 285 // TODO(sgjesse): Fix this workaround ignoring the type parameter. |
204 Dart_CObject* value = &dynamic_type_marker; | 286 Dart_CObject* value = &dynamic_type_marker; |
205 AddBackwardReference(object_id, value); | 287 AddBackRef(object_id, value, kIsDeserialized); |
206 intptr_t index = ReadIntptrValue(); | 288 intptr_t index = ReadIntptrValue(); |
207 USE(index); | 289 USE(index); |
208 intptr_t token_index = ReadIntptrValue(); | 290 intptr_t token_index = ReadIntptrValue(); |
209 USE(token_index); | 291 USE(token_index); |
210 int8_t type_state = Read<int8_t>(); | 292 int8_t type_state = Read<int8_t>(); |
211 USE(type_state); | 293 USE(type_state); |
212 Dart_CObject* parameterized_class = ReadObject(); | 294 Dart_CObject* parameterized_class = ReadObjectImpl(); |
213 // The type parameter is finalized, therefore parameterized_class is null. | 295 // The type parameter is finalized, therefore parameterized_class is null. |
214 ASSERT(parameterized_class->type == Dart_CObject::kNull); | 296 ASSERT(parameterized_class->type == Dart_CObject::kNull); |
215 Dart_CObject* name = ReadObject(); | 297 Dart_CObject* name = ReadObjectImpl(); |
216 ASSERT(name->type == Dart_CObject::kString); | 298 ASSERT(name->type == Dart_CObject::kString); |
217 return value; | 299 return value; |
218 } | 300 } |
219 case ObjectStore::kArrayClass: { | |
220 intptr_t len = ReadSmiValue(); | |
221 Dart_CObject* value = AllocateDartCObjectArray(len); | |
222 AddBackwardReference(object_id, value); | |
223 // Skip type arguments. | |
224 // TODO(sjesse): Remove this when message serialization format is | |
225 // updated (currently type_arguments is leaked). | |
226 Dart_CObject* type_arguments = ReadObject(); | |
227 if (type_arguments != &type_arguments_marker && | |
228 type_arguments->type != Dart_CObject::kNull) { | |
229 return AllocateDartCObjectUnsupported(); | |
230 } | |
231 for (int i = 0; i < len; i++) { | |
232 value->value.as_array.values[i] = ReadObject(); | |
233 } | |
234 return value; | |
235 } | |
236 case ObjectStore::kMintClass: { | 301 case ObjectStore::kMintClass: { |
237 int64_t value = Read<int64_t>(); | 302 int64_t value = Read<int64_t>(); |
238 Dart_CObject* object; | 303 Dart_CObject* object; |
239 if (kMinInt32 <= value && value <= kMaxInt32) { | 304 if (kMinInt32 <= value && value <= kMaxInt32) { |
240 object = AllocateDartCObjectInt32(value); | 305 object = AllocateDartCObjectInt32(value); |
241 } else { | 306 } else { |
242 object = AllocateDartCObjectInt64(value); | 307 object = AllocateDartCObjectInt64(value); |
243 } | 308 } |
244 AddBackwardReference(object_id, object); | 309 AddBackRef(object_id, object, kIsDeserialized); |
245 return object; | 310 return object; |
246 } | 311 } |
247 case ObjectStore::kBigintClass: { | 312 case ObjectStore::kBigintClass: { |
248 // Read in the hex string representation of the bigint. | 313 // Read in the hex string representation of the bigint. |
249 intptr_t len = ReadIntptrValue(); | 314 intptr_t len = ReadIntptrValue(); |
250 Dart_CObject* object = AllocateDartCObjectBigint(len); | 315 Dart_CObject* object = AllocateDartCObjectBigint(len); |
251 AddBackwardReference(object_id, object); | 316 AddBackRef(object_id, object, kIsDeserialized); |
252 char* p = object->value.as_bigint; | 317 char* p = object->value.as_bigint; |
253 for (intptr_t i = 0; i < len; i++) { | 318 for (intptr_t i = 0; i < len; i++) { |
254 p[i] = Read<uint8_t>(); | 319 p[i] = Read<uint8_t>(); |
255 } | 320 } |
256 p[len] = '\0'; | 321 p[len] = '\0'; |
257 return object; | 322 return object; |
258 } | 323 } |
259 case ObjectStore::kDoubleClass: { | 324 case ObjectStore::kDoubleClass: { |
260 // Read the double value for the object. | 325 // Read the double value for the object. |
261 Dart_CObject* object = AllocateDartCObjectDouble(Read<double>()); | 326 Dart_CObject* object = AllocateDartCObjectDouble(Read<double>()); |
262 AddBackwardReference(object_id, object); | 327 AddBackRef(object_id, object, kIsDeserialized); |
263 return object; | 328 return object; |
264 } | 329 } |
265 case ObjectStore::kOneByteStringClass: { | 330 case ObjectStore::kOneByteStringClass: { |
266 intptr_t len = ReadSmiValue(); | 331 intptr_t len = ReadSmiValue(); |
267 intptr_t hash = ReadSmiValue(); | 332 intptr_t hash = ReadSmiValue(); |
268 USE(hash); | 333 USE(hash); |
269 Dart_CObject* object = AllocateDartCObjectString(len); | 334 Dart_CObject* object = AllocateDartCObjectString(len); |
270 AddBackwardReference(object_id, object); | 335 AddBackRef(object_id, object, kIsDeserialized); |
271 char* p = object->value.as_string; | 336 char* p = object->value.as_string; |
272 for (intptr_t i = 0; i < len; i++) { | 337 for (intptr_t i = 0; i < len; i++) { |
273 p[i] = Read<uint8_t>(); | 338 p[i] = Read<uint8_t>(); |
274 } | 339 } |
275 p[len] = '\0'; | 340 p[len] = '\0'; |
276 return object; | 341 return object; |
277 } | 342 } |
278 case ObjectStore::kTwoByteStringClass: | 343 case ObjectStore::kTwoByteStringClass: |
279 // Two byte strings not supported. | 344 // Two byte strings not supported. |
280 return AllocateDartCObjectUnsupported(); | 345 return AllocateDartCObjectUnsupported(); |
281 case ObjectStore::kFourByteStringClass: | 346 case ObjectStore::kFourByteStringClass: |
282 // Four byte strings not supported. | 347 // Four byte strings not supported. |
283 return AllocateDartCObjectUnsupported(); | 348 return AllocateDartCObjectUnsupported(); |
284 case ObjectStore::kUint8ArrayClass: { | 349 case ObjectStore::kUint8ArrayClass: { |
285 intptr_t len = ReadSmiValue(); | 350 intptr_t len = ReadSmiValue(); |
286 Dart_CObject* object = AllocateDartCObjectUint8Array(len); | 351 Dart_CObject* object = AllocateDartCObjectUint8Array(len); |
287 AddBackwardReference(object_id, object); | 352 AddBackRef(object_id, object, kIsDeserialized); |
288 if (len > 0) { | 353 if (len > 0) { |
289 uint8_t* p = object->value.as_byte_array.values; | 354 uint8_t* p = object->value.as_byte_array.values; |
290 for (intptr_t i = 0; i < len; i++) { | 355 for (intptr_t i = 0; i < len; i++) { |
291 p[i] = Read<uint8_t>(); | 356 p[i] = Read<uint8_t>(); |
292 } | 357 } |
293 } | 358 } |
294 return object; | 359 return object; |
295 } | 360 } |
296 default: | 361 default: |
297 // Everything else not supported. | 362 // Everything else not supported. |
(...skipping 15 matching lines...) Expand all Loading... |
313 if (object_id == ObjectStore::kDynamicType || | 378 if (object_id == ObjectStore::kDynamicType || |
314 object_id == ObjectStore::kDoubleInterface || | 379 object_id == ObjectStore::kDoubleInterface || |
315 object_id == ObjectStore::kIntInterface || | 380 object_id == ObjectStore::kIntInterface || |
316 object_id == ObjectStore::kBoolInterface || | 381 object_id == ObjectStore::kBoolInterface || |
317 object_id == ObjectStore::kStringInterface) { | 382 object_id == ObjectStore::kStringInterface) { |
318 // Always return dynamic type (this is only a marker). | 383 // Always return dynamic type (this is only a marker). |
319 return &dynamic_type_marker; | 384 return &dynamic_type_marker; |
320 } | 385 } |
321 intptr_t index = object_id - kMaxPredefinedObjectIds; | 386 intptr_t index = object_id - kMaxPredefinedObjectIds; |
322 ASSERT((0 <= index) && (index < backward_references_.length())); | 387 ASSERT((0 <= index) && (index < backward_references_.length())); |
323 ASSERT(backward_references_[index] != NULL); | 388 ASSERT(backward_references_[index]->reference() != NULL); |
324 return backward_references_[index]; | 389 return backward_references_[index]->reference(); |
325 } | |
326 | |
327 | |
328 Dart_CObject* ApiMessageReader::ReadObjectImpl(intptr_t header) { | |
329 SerializedHeaderType header_type = SerializedHeaderTag::decode(header); | |
330 intptr_t header_value = SerializedHeaderData::decode(header); | |
331 | |
332 if (header_type == kObjectId) { | |
333 return ReadIndexedObject(header_value); | |
334 } | |
335 ASSERT(header_type == kInlined); | |
336 return ReadInlinedObject(header_value); | |
337 } | 390 } |
338 | 391 |
339 | 392 |
340 Dart_CObject* ApiMessageReader::ReadObject() { | 393 Dart_CObject* ApiMessageReader::ReadObject() { |
| 394 Dart_CObject* value = ReadObjectImpl(); |
| 395 for (intptr_t i = 0; i < backward_references_.length(); i++) { |
| 396 if (!backward_references_[i]->is_deserialized()) { |
| 397 ReadObjectImpl(); |
| 398 backward_references_[i]->set_state(kIsDeserialized); |
| 399 } |
| 400 } |
| 401 return value; |
| 402 } |
| 403 |
| 404 |
| 405 Dart_CObject* ApiMessageReader::ReadObjectImpl() { |
341 int64_t value = Read<int64_t>(); | 406 int64_t value = Read<int64_t>(); |
342 if ((value & kSmiTagMask) == 0) { | 407 if ((value & kSmiTagMask) == 0) { |
343 int64_t untagged_value = value >> kSmiTagShift; | 408 int64_t untagged_value = value >> kSmiTagShift; |
344 if (kMinInt32 <= untagged_value && untagged_value <= kMaxInt32) { | 409 if (kMinInt32 <= untagged_value && untagged_value <= kMaxInt32) { |
345 return AllocateDartCObjectInt32(untagged_value); | 410 return AllocateDartCObjectInt32(untagged_value); |
346 } else { | 411 } else { |
347 return AllocateDartCObjectInt64(untagged_value); | 412 return AllocateDartCObjectInt64(untagged_value); |
348 } | 413 } |
349 } | 414 } |
350 ASSERT((value <= kIntptrMax) && (value >= kIntptrMin)); | 415 ASSERT((value <= kIntptrMax) && (value >= kIntptrMin)); |
351 return ReadObjectImpl(value); | 416 SerializedHeaderType header_type = SerializedHeaderTag::decode(value); |
| 417 intptr_t header_value = SerializedHeaderData::decode(value); |
| 418 |
| 419 if (header_type == kObjectId) { |
| 420 return ReadIndexedObject(header_value); |
| 421 } |
| 422 ASSERT(header_type == kInlined); |
| 423 return ReadInlinedObject(header_value); |
352 } | 424 } |
353 | 425 |
354 | 426 |
355 void ApiMessageReader::AddBackwardReference(intptr_t id, Dart_CObject* obj) { | 427 void ApiMessageReader::AddBackRef(intptr_t id, |
356 ASSERT((id - kMaxPredefinedObjectIds) == backward_references_.length()); | 428 Dart_CObject* obj, |
357 backward_references_.Add(obj); | 429 DeserializeState state) { |
| 430 intptr_t index = (id - kMaxPredefinedObjectIds); |
| 431 ASSERT(index == backward_references_.length()); |
| 432 BackRefNode* node = AllocateBackRefNode(obj, state); |
| 433 ASSERT(node != NULL); |
| 434 backward_references_.Add(node); |
358 } | 435 } |
359 | 436 |
| 437 |
| 438 Dart_CObject* ApiMessageReader::GetBackRef(intptr_t id) { |
| 439 ASSERT(id >= kMaxPredefinedObjectIds); |
| 440 intptr_t index = (id - kMaxPredefinedObjectIds); |
| 441 if (index < backward_references_.length()) { |
| 442 return backward_references_[index]->reference(); |
| 443 } |
| 444 return NULL; |
| 445 } |
| 446 |
| 447 |
360 void ApiMessageWriter::WriteMessage(intptr_t field_count, intptr_t *data) { | 448 void ApiMessageWriter::WriteMessage(intptr_t field_count, intptr_t *data) { |
361 // Write out the serialization header value for this object. | 449 // Write out the serialization header value for this object. |
362 WriteSerializationMarker(kInlined, kMaxPredefinedObjectIds); | 450 WriteSerializationMarker(kInlined, kMaxPredefinedObjectIds); |
363 | 451 |
364 // Write out the class and tags information. | 452 // Write out the class and tags information. |
365 WriteObjectHeader(ObjectStore::kArrayClass, 0); | 453 WriteObjectHeader(ObjectStore::kArrayClass, 0); |
366 | 454 |
367 // Write out the length field. | 455 // Write out the length field. |
368 Write<RawObject*>(Smi::New(field_count)); | 456 Write<RawObject*>(Smi::New(field_count)); |
369 | 457 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
416 UnmarkCObject(object); | 504 UnmarkCObject(object); |
417 if (object->type == Dart_CObject::kArray) { | 505 if (object->type == Dart_CObject::kArray) { |
418 for (int i = 0; i < object->value.as_array.length; i++) { | 506 for (int i = 0; i < object->value.as_array.length; i++) { |
419 Dart_CObject* element = object->value.as_array.values[i]; | 507 Dart_CObject* element = object->value.as_array.values[i]; |
420 UnmarkAllCObjects(element); | 508 UnmarkAllCObjects(element); |
421 } | 509 } |
422 } | 510 } |
423 } | 511 } |
424 | 512 |
425 | 513 |
| 514 void ApiMessageWriter::AddToForwardList(Dart_CObject* object) { |
| 515 if (forward_id_ >= forward_list_length_) { |
| 516 intptr_t new_size = (forward_list_length_ * sizeof(object)) * 2; |
| 517 void* new_list = ::realloc(forward_list_, new_size); |
| 518 ASSERT(new_list != NULL); |
| 519 forward_list_ = reinterpret_cast<Dart_CObject**>(new_list); |
| 520 } |
| 521 forward_list_[forward_id_] = object; |
| 522 forward_id_ += 1; |
| 523 } |
| 524 |
| 525 |
426 void ApiMessageWriter::WriteSmi(int64_t value) { | 526 void ApiMessageWriter::WriteSmi(int64_t value) { |
427 ASSERT(Smi::IsValid64(value)); | 527 ASSERT(Smi::IsValid64(value)); |
428 Write<RawObject*>(Smi::New(value)); | 528 Write<RawObject*>(Smi::New(value)); |
429 } | 529 } |
430 | 530 |
431 | 531 |
432 void ApiMessageWriter::WriteMint(Dart_CObject* object, int64_t value) { | 532 void ApiMessageWriter::WriteMint(Dart_CObject* object, int64_t value) { |
433 ASSERT(!Smi::IsValid64(value)); | 533 ASSERT(!Smi::IsValid64(value)); |
434 // Write out the serialization header value for mint object. | 534 // Write out the serialization header value for mint object. |
435 WriteInlinedHeader(object); | 535 WriteInlinedHeader(object); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
470 } | 570 } |
471 | 571 |
472 | 572 |
473 void ApiMessageWriter::WriteCObject(Dart_CObject* object) { | 573 void ApiMessageWriter::WriteCObject(Dart_CObject* object) { |
474 if (IsCObjectMarked(object)) { | 574 if (IsCObjectMarked(object)) { |
475 intptr_t object_id = GetMarkedCObjectMark(object); | 575 intptr_t object_id = GetMarkedCObjectMark(object); |
476 WriteIndexedObject(kMaxPredefinedObjectIds + object_id); | 576 WriteIndexedObject(kMaxPredefinedObjectIds + object_id); |
477 return; | 577 return; |
478 } | 578 } |
479 | 579 |
480 switch (object->type) { | 580 Dart_CObject::Type type = object->type; |
| 581 if (type == Dart_CObject::kArray) { |
| 582 // Write out the serialization header value for this object. |
| 583 WriteInlinedHeader(object); |
| 584 // Write out the class and tags information. |
| 585 WriteObjectHeader(ObjectStore::kArrayClass, 0); |
| 586 WriteSmi(object->value.as_array.length); |
| 587 // Write out the type arguments. |
| 588 WriteIndexedObject(Object::kNullObject); |
| 589 // Write out array elements. |
| 590 for (int i = 0; i < object->value.as_array.length; i++) { |
| 591 WriteCObjectRef(object->value.as_array.values[i]); |
| 592 } |
| 593 return; |
| 594 } |
| 595 return WriteCObjectInlined(object, type); |
| 596 } |
| 597 |
| 598 |
| 599 void ApiMessageWriter::WriteCObjectRef(Dart_CObject* object) { |
| 600 if (IsCObjectMarked(object)) { |
| 601 intptr_t object_id = GetMarkedCObjectMark(object); |
| 602 WriteIndexedObject(kMaxPredefinedObjectIds + object_id); |
| 603 return; |
| 604 } |
| 605 |
| 606 Dart_CObject::Type type = object->type; |
| 607 if (type == Dart_CObject::kArray) { |
| 608 // Write out the serialization header value for this object. |
| 609 WriteInlinedHeader(object); |
| 610 // Write out the class information. |
| 611 WriteIndexedObject(ObjectStore::kArrayClass); |
| 612 // Write out the length information. |
| 613 WriteSmi(object->value.as_array.length); |
| 614 // Add object to forward list so that this object is serialized later. |
| 615 AddToForwardList(object); |
| 616 return; |
| 617 } |
| 618 return WriteCObjectInlined(object, type); |
| 619 } |
| 620 |
| 621 |
| 622 void ApiMessageWriter::WriteForwardedCObject(Dart_CObject* object) { |
| 623 ASSERT(IsCObjectMarked(object)); |
| 624 Dart_CObject::Type type = |
| 625 static_cast<Dart_CObject::Type>(object->type & kDartCObjectTypeMask); |
| 626 ASSERT(type == Dart_CObject::kArray); |
| 627 |
| 628 // Write out the serialization header value for this object. |
| 629 intptr_t object_id = GetMarkedCObjectMark(object); |
| 630 WriteSerializationMarker(kInlined, kMaxPredefinedObjectIds + object_id); |
| 631 // Write out the class and tags information. |
| 632 WriteObjectHeader(ObjectStore::kArrayClass, 0); |
| 633 WriteSmi(object->value.as_array.length); |
| 634 // Write out the type arguments. |
| 635 WriteIndexedObject(Object::kNullObject); |
| 636 // Write out array elements. |
| 637 for (int i = 0; i < object->value.as_array.length; i++) { |
| 638 WriteCObjectRef(object->value.as_array.values[i]); |
| 639 } |
| 640 } |
| 641 |
| 642 |
| 643 void ApiMessageWriter::WriteCObjectInlined(Dart_CObject* object, |
| 644 Dart_CObject::Type type) { |
| 645 switch (type) { |
481 case Dart_CObject::kNull: | 646 case Dart_CObject::kNull: |
482 WriteIndexedObject(Object::kNullObject); | 647 WriteIndexedObject(Object::kNullObject); |
483 break; | 648 break; |
484 case Dart_CObject::kBool: | 649 case Dart_CObject::kBool: |
485 if (object->value.as_bool) { | 650 if (object->value.as_bool) { |
486 WriteIndexedObject(ObjectStore::kTrueValue); | 651 WriteIndexedObject(ObjectStore::kTrueValue); |
487 } else { | 652 } else { |
488 WriteIndexedObject(ObjectStore::kFalseValue); | 653 WriteIndexedObject(ObjectStore::kFalseValue); |
489 } | 654 } |
490 break; | 655 break; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
524 // Write string length, hash and content | 689 // Write string length, hash and content |
525 char* str = object->value.as_string; | 690 char* str = object->value.as_string; |
526 intptr_t len = strlen(str); | 691 intptr_t len = strlen(str); |
527 WriteSmi(len); | 692 WriteSmi(len); |
528 WriteSmi(0); // TODO(sgjesse): Hash - not written. | 693 WriteSmi(0); // TODO(sgjesse): Hash - not written. |
529 for (intptr_t i = 0; i < len; i++) { | 694 for (intptr_t i = 0; i < len; i++) { |
530 Write<uint8_t>(str[i]); | 695 Write<uint8_t>(str[i]); |
531 } | 696 } |
532 break; | 697 break; |
533 } | 698 } |
534 case Dart_CObject::kArray: { | |
535 // Write out the serialization header value for this object. | |
536 WriteInlinedHeader(object); | |
537 // Write out the class and tags information. | |
538 WriteObjectHeader(ObjectStore::kArrayClass, 0); | |
539 WriteSmi(object->value.as_array.length); | |
540 // Write out the type arguments. | |
541 WriteIndexedObject(Object::kNullObject); | |
542 // Write out array elements. | |
543 for (int i = 0; i < object->value.as_array.length; i++) { | |
544 WriteCObject(object->value.as_array.values[i]); | |
545 } | |
546 break; | |
547 } | |
548 case Dart_CObject::kUint8Array: { | 699 case Dart_CObject::kUint8Array: { |
549 // Write out the serialization header value for this object. | 700 // Write out the serialization header value for this object. |
550 WriteInlinedHeader(object); | 701 WriteInlinedHeader(object); |
551 // Write out the class and tags information. | 702 // Write out the class and tags information. |
552 WriteObjectHeader(ObjectStore::kUint8ArrayClass, 0); | 703 WriteObjectHeader(ObjectStore::kUint8ArrayClass, 0); |
553 uint8_t* bytes = object->value.as_byte_array.values; | 704 uint8_t* bytes = object->value.as_byte_array.values; |
554 intptr_t len = object->value.as_byte_array.length; | 705 intptr_t len = object->value.as_byte_array.length; |
555 WriteSmi(len); | 706 WriteSmi(len); |
556 for (intptr_t i = 0; i < len; i++) { | 707 for (intptr_t i = 0; i < len; i++) { |
557 Write<uint8_t>(bytes[i]); | 708 Write<uint8_t>(bytes[i]); |
558 } | 709 } |
559 break; | 710 break; |
560 } | 711 } |
561 default: | 712 default: |
562 UNREACHABLE(); | 713 UNREACHABLE(); |
563 } | 714 } |
564 } | 715 } |
565 | 716 |
566 | 717 |
567 void ApiMessageWriter::WriteCMessage(Dart_CObject* object) { | 718 void ApiMessageWriter::WriteCMessage(Dart_CObject* object) { |
568 WriteCObject(object); | 719 WriteCObject(object); |
| 720 // Write out all objects that were added to the forward list and have |
| 721 // not been serialized yet. These would typically be fields of arrays. |
| 722 // NOTE: The forward list might grow as we process the list. |
| 723 for (intptr_t i = 0; i < forward_id_; i++) { |
| 724 WriteForwardedCObject(forward_list_[i]); |
| 725 } |
569 UnmarkAllCObjects(object); | 726 UnmarkAllCObjects(object); |
570 FinalizeBuffer(); | 727 FinalizeBuffer(); |
571 } | 728 } |
572 | 729 |
573 } // namespace dart | 730 } // namespace dart |
OLD | NEW |