| 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 "platform/globals.h" | 5 #include "platform/globals.h" |
| 6 | 6 |
| 7 #include "include/dart_tools_api.h" | 7 #include "include/dart_tools_api.h" |
| 8 #include "platform/assert.h" | 8 #include "platform/assert.h" |
| 9 #include "vm/class_finalizer.h" | 9 #include "vm/class_finalizer.h" |
| 10 #include "vm/dart_api_impl.h" | 10 #include "vm/dart_api_impl.h" |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 } | 128 } |
| 129 } | 129 } |
| 130 | 130 |
| 131 | 131 |
| 132 static void CheckEncodeDecodeMessage(Dart_CObject* root) { | 132 static void CheckEncodeDecodeMessage(Dart_CObject* root) { |
| 133 // Encode and decode the message. | 133 // Encode and decode the message. |
| 134 uint8_t* buffer = NULL; | 134 uint8_t* buffer = NULL; |
| 135 ApiMessageWriter writer(&buffer, &malloc_allocator); | 135 ApiMessageWriter writer(&buffer, &malloc_allocator); |
| 136 writer.WriteCMessage(root); | 136 writer.WriteCMessage(root); |
| 137 | 137 |
| 138 ApiMessageReader api_reader(buffer, writer.BytesWritten(), &zone_allocator); | 138 ApiMessageReader api_reader(buffer, writer.BytesWritten()); |
| 139 Dart_CObject* new_root = api_reader.ReadMessage(); | 139 Dart_CObject* new_root = api_reader.ReadMessage(); |
| 140 | 140 |
| 141 // Check that the two messages are the same. | 141 // Check that the two messages are the same. |
| 142 CompareDartCObjects(root, new_root); | 142 CompareDartCObjects(root, new_root); |
| 143 } | 143 } |
| 144 | 144 |
| 145 | 145 |
| 146 static void ExpectEncodeFail(Dart_CObject* root) { | 146 static void ExpectEncodeFail(Dart_CObject* root) { |
| 147 uint8_t* buffer = NULL; | 147 uint8_t* buffer = NULL; |
| 148 ApiMessageWriter writer(&buffer, &malloc_allocator); | 148 ApiMessageWriter writer(&buffer, &malloc_allocator); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 164 // Read object back from the snapshot. | 164 // Read object back from the snapshot. |
| 165 MessageSnapshotReader reader(buffer, | 165 MessageSnapshotReader reader(buffer, |
| 166 buffer_len, | 166 buffer_len, |
| 167 Isolate::Current(), | 167 Isolate::Current(), |
| 168 zone.GetZone()); | 168 zone.GetZone()); |
| 169 const Object& serialized_object = Object::Handle(reader.ReadObject()); | 169 const Object& serialized_object = Object::Handle(reader.ReadObject()); |
| 170 EXPECT(Equals(null_object, serialized_object)); | 170 EXPECT(Equals(null_object, serialized_object)); |
| 171 | 171 |
| 172 // Read object back from the snapshot into a C structure. | 172 // Read object back from the snapshot into a C structure. |
| 173 ApiNativeScope scope; | 173 ApiNativeScope scope; |
| 174 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); | 174 ApiMessageReader api_reader(buffer, buffer_len); |
| 175 Dart_CObject* root = api_reader.ReadMessage(); | 175 Dart_CObject* root = api_reader.ReadMessage(); |
| 176 EXPECT_NOTNULL(root); | 176 EXPECT_NOTNULL(root); |
| 177 EXPECT_EQ(Dart_CObject_kNull, root->type); | 177 EXPECT_EQ(Dart_CObject_kNull, root->type); |
| 178 CheckEncodeDecodeMessage(root); | 178 CheckEncodeDecodeMessage(root); |
| 179 } | 179 } |
| 180 | 180 |
| 181 | 181 |
| 182 TEST_CASE(SerializeSmi1) { | 182 TEST_CASE(SerializeSmi1) { |
| 183 StackZone zone(Thread::Current()); | 183 StackZone zone(Thread::Current()); |
| 184 | 184 |
| 185 // Write snapshot with object content. | 185 // Write snapshot with object content. |
| 186 const Smi& smi = Smi::Handle(Smi::New(124)); | 186 const Smi& smi = Smi::Handle(Smi::New(124)); |
| 187 uint8_t* buffer; | 187 uint8_t* buffer; |
| 188 MessageWriter writer(&buffer, &zone_allocator, true); | 188 MessageWriter writer(&buffer, &zone_allocator, true); |
| 189 writer.WriteMessage(smi); | 189 writer.WriteMessage(smi); |
| 190 intptr_t buffer_len = writer.BytesWritten(); | 190 intptr_t buffer_len = writer.BytesWritten(); |
| 191 | 191 |
| 192 // Read object back from the snapshot. | 192 // Read object back from the snapshot. |
| 193 MessageSnapshotReader reader(buffer, | 193 MessageSnapshotReader reader(buffer, |
| 194 buffer_len, | 194 buffer_len, |
| 195 Isolate::Current(), | 195 Isolate::Current(), |
| 196 zone.GetZone()); | 196 zone.GetZone()); |
| 197 const Object& serialized_object = Object::Handle(reader.ReadObject()); | 197 const Object& serialized_object = Object::Handle(reader.ReadObject()); |
| 198 EXPECT(Equals(smi, serialized_object)); | 198 EXPECT(Equals(smi, serialized_object)); |
| 199 | 199 |
| 200 // Read object back from the snapshot into a C structure. | 200 // Read object back from the snapshot into a C structure. |
| 201 ApiNativeScope scope; | 201 ApiNativeScope scope; |
| 202 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); | 202 ApiMessageReader api_reader(buffer, buffer_len); |
| 203 Dart_CObject* root = api_reader.ReadMessage(); | 203 Dart_CObject* root = api_reader.ReadMessage(); |
| 204 EXPECT_NOTNULL(root); | 204 EXPECT_NOTNULL(root); |
| 205 EXPECT_EQ(Dart_CObject_kInt32, root->type); | 205 EXPECT_EQ(Dart_CObject_kInt32, root->type); |
| 206 EXPECT_EQ(smi.Value(), root->value.as_int32); | 206 EXPECT_EQ(smi.Value(), root->value.as_int32); |
| 207 CheckEncodeDecodeMessage(root); | 207 CheckEncodeDecodeMessage(root); |
| 208 } | 208 } |
| 209 | 209 |
| 210 | 210 |
| 211 TEST_CASE(SerializeSmi2) { | 211 TEST_CASE(SerializeSmi2) { |
| 212 StackZone zone(Thread::Current()); | 212 StackZone zone(Thread::Current()); |
| 213 | 213 |
| 214 // Write snapshot with object content. | 214 // Write snapshot with object content. |
| 215 const Smi& smi = Smi::Handle(Smi::New(-1)); | 215 const Smi& smi = Smi::Handle(Smi::New(-1)); |
| 216 uint8_t* buffer; | 216 uint8_t* buffer; |
| 217 MessageWriter writer(&buffer, &zone_allocator, true); | 217 MessageWriter writer(&buffer, &zone_allocator, true); |
| 218 writer.WriteMessage(smi); | 218 writer.WriteMessage(smi); |
| 219 intptr_t buffer_len = writer.BytesWritten(); | 219 intptr_t buffer_len = writer.BytesWritten(); |
| 220 | 220 |
| 221 // Read object back from the snapshot. | 221 // Read object back from the snapshot. |
| 222 MessageSnapshotReader reader(buffer, | 222 MessageSnapshotReader reader(buffer, |
| 223 buffer_len, | 223 buffer_len, |
| 224 Isolate::Current(), | 224 Isolate::Current(), |
| 225 zone.GetZone()); | 225 zone.GetZone()); |
| 226 const Object& serialized_object = Object::Handle(reader.ReadObject()); | 226 const Object& serialized_object = Object::Handle(reader.ReadObject()); |
| 227 EXPECT(Equals(smi, serialized_object)); | 227 EXPECT(Equals(smi, serialized_object)); |
| 228 | 228 |
| 229 // Read object back from the snapshot into a C structure. | 229 // Read object back from the snapshot into a C structure. |
| 230 ApiNativeScope scope; | 230 ApiNativeScope scope; |
| 231 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); | 231 ApiMessageReader api_reader(buffer, buffer_len); |
| 232 Dart_CObject* root = api_reader.ReadMessage(); | 232 Dart_CObject* root = api_reader.ReadMessage(); |
| 233 EXPECT_NOTNULL(root); | 233 EXPECT_NOTNULL(root); |
| 234 EXPECT_EQ(Dart_CObject_kInt32, root->type); | 234 EXPECT_EQ(Dart_CObject_kInt32, root->type); |
| 235 EXPECT_EQ(smi.Value(), root->value.as_int32); | 235 EXPECT_EQ(smi.Value(), root->value.as_int32); |
| 236 CheckEncodeDecodeMessage(root); | 236 CheckEncodeDecodeMessage(root); |
| 237 } | 237 } |
| 238 | 238 |
| 239 | 239 |
| 240 Dart_CObject* SerializeAndDeserializeMint(const Mint& mint) { | 240 Dart_CObject* SerializeAndDeserializeMint(const Mint& mint) { |
| 241 // Write snapshot with object content. | 241 // Write snapshot with object content. |
| 242 uint8_t* buffer; | 242 uint8_t* buffer; |
| 243 MessageWriter writer(&buffer, &zone_allocator, true); | 243 MessageWriter writer(&buffer, &zone_allocator, true); |
| 244 writer.WriteMessage(mint); | 244 writer.WriteMessage(mint); |
| 245 intptr_t buffer_len = writer.BytesWritten(); | 245 intptr_t buffer_len = writer.BytesWritten(); |
| 246 | 246 |
| 247 { | 247 { |
| 248 // Switch to a regular zone, where VM handle allocation is allowed. | 248 // Switch to a regular zone, where VM handle allocation is allowed. |
| 249 StackZone zone(Thread::Current()); | 249 StackZone zone(Thread::Current()); |
| 250 // Read object back from the snapshot. | 250 // Read object back from the snapshot. |
| 251 MessageSnapshotReader reader(buffer, | 251 MessageSnapshotReader reader(buffer, |
| 252 buffer_len, | 252 buffer_len, |
| 253 Isolate::Current(), | 253 Isolate::Current(), |
| 254 Thread::Current()->zone()); | 254 Thread::Current()->zone()); |
| 255 const Object& serialized_object = Object::Handle(reader.ReadObject()); | 255 const Object& serialized_object = Object::Handle(reader.ReadObject()); |
| 256 EXPECT(serialized_object.IsMint()); | 256 EXPECT(serialized_object.IsMint()); |
| 257 } | 257 } |
| 258 | 258 |
| 259 // Read object back from the snapshot into a C structure. | 259 // Read object back from the snapshot into a C structure. |
| 260 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); | 260 ApiMessageReader api_reader(buffer, buffer_len); |
| 261 Dart_CObject* root = api_reader.ReadMessage(); | 261 Dart_CObject* root = api_reader.ReadMessage(); |
| 262 EXPECT_NOTNULL(root); | 262 EXPECT_NOTNULL(root); |
| 263 CheckEncodeDecodeMessage(root); | 263 CheckEncodeDecodeMessage(root); |
| 264 return root; | 264 return root; |
| 265 } | 265 } |
| 266 | 266 |
| 267 | 267 |
| 268 void CheckMint(int64_t value) { | 268 void CheckMint(int64_t value) { |
| 269 StackZone zone(Thread::Current()); | 269 StackZone zone(Thread::Current()); |
| 270 | 270 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 324 // Read object back from the snapshot. | 324 // Read object back from the snapshot. |
| 325 MessageSnapshotReader reader(buffer, | 325 MessageSnapshotReader reader(buffer, |
| 326 buffer_len, | 326 buffer_len, |
| 327 Isolate::Current(), | 327 Isolate::Current(), |
| 328 zone.GetZone()); | 328 zone.GetZone()); |
| 329 const Object& serialized_object = Object::Handle(reader.ReadObject()); | 329 const Object& serialized_object = Object::Handle(reader.ReadObject()); |
| 330 EXPECT(Equals(dbl, serialized_object)); | 330 EXPECT(Equals(dbl, serialized_object)); |
| 331 | 331 |
| 332 // Read object back from the snapshot into a C structure. | 332 // Read object back from the snapshot into a C structure. |
| 333 ApiNativeScope scope; | 333 ApiNativeScope scope; |
| 334 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); | 334 ApiMessageReader api_reader(buffer, buffer_len); |
| 335 Dart_CObject* root = api_reader.ReadMessage(); | 335 Dart_CObject* root = api_reader.ReadMessage(); |
| 336 EXPECT_NOTNULL(root); | 336 EXPECT_NOTNULL(root); |
| 337 EXPECT_EQ(Dart_CObject_kDouble, root->type); | 337 EXPECT_EQ(Dart_CObject_kDouble, root->type); |
| 338 EXPECT_EQ(dbl.value(), root->value.as_double); | 338 EXPECT_EQ(dbl.value(), root->value.as_double); |
| 339 CheckEncodeDecodeMessage(root); | 339 CheckEncodeDecodeMessage(root); |
| 340 } | 340 } |
| 341 | 341 |
| 342 | 342 |
| 343 TEST_CASE(SerializeTrue) { | 343 TEST_CASE(SerializeTrue) { |
| 344 StackZone zone(Thread::Current()); | 344 StackZone zone(Thread::Current()); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 355 buffer_len, | 355 buffer_len, |
| 356 Isolate::Current(), | 356 Isolate::Current(), |
| 357 zone.GetZone()); | 357 zone.GetZone()); |
| 358 const Object& serialized_object = Object::Handle(reader.ReadObject()); | 358 const Object& serialized_object = Object::Handle(reader.ReadObject()); |
| 359 fprintf(stderr, "%s / %s\n", bl.ToCString(), serialized_object.ToCString()); | 359 fprintf(stderr, "%s / %s\n", bl.ToCString(), serialized_object.ToCString()); |
| 360 | 360 |
| 361 EXPECT(Equals(bl, serialized_object)); | 361 EXPECT(Equals(bl, serialized_object)); |
| 362 | 362 |
| 363 // Read object back from the snapshot into a C structure. | 363 // Read object back from the snapshot into a C structure. |
| 364 ApiNativeScope scope; | 364 ApiNativeScope scope; |
| 365 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); | 365 ApiMessageReader api_reader(buffer, buffer_len); |
| 366 Dart_CObject* root = api_reader.ReadMessage(); | 366 Dart_CObject* root = api_reader.ReadMessage(); |
| 367 EXPECT_NOTNULL(root); | 367 EXPECT_NOTNULL(root); |
| 368 EXPECT_EQ(Dart_CObject_kBool, root->type); | 368 EXPECT_EQ(Dart_CObject_kBool, root->type); |
| 369 EXPECT_EQ(true, root->value.as_bool); | 369 EXPECT_EQ(true, root->value.as_bool); |
| 370 CheckEncodeDecodeMessage(root); | 370 CheckEncodeDecodeMessage(root); |
| 371 } | 371 } |
| 372 | 372 |
| 373 | 373 |
| 374 TEST_CASE(SerializeFalse) { | 374 TEST_CASE(SerializeFalse) { |
| 375 StackZone zone(Thread::Current()); | 375 StackZone zone(Thread::Current()); |
| 376 | 376 |
| 377 // Write snapshot with false object. | 377 // Write snapshot with false object. |
| 378 const Bool& bl = Bool::False(); | 378 const Bool& bl = Bool::False(); |
| 379 uint8_t* buffer; | 379 uint8_t* buffer; |
| 380 MessageWriter writer(&buffer, &zone_allocator, true); | 380 MessageWriter writer(&buffer, &zone_allocator, true); |
| 381 writer.WriteMessage(bl); | 381 writer.WriteMessage(bl); |
| 382 intptr_t buffer_len = writer.BytesWritten(); | 382 intptr_t buffer_len = writer.BytesWritten(); |
| 383 | 383 |
| 384 // Read object back from the snapshot. | 384 // Read object back from the snapshot. |
| 385 MessageSnapshotReader reader(buffer, | 385 MessageSnapshotReader reader(buffer, |
| 386 buffer_len, | 386 buffer_len, |
| 387 Isolate::Current(), | 387 Isolate::Current(), |
| 388 zone.GetZone()); | 388 zone.GetZone()); |
| 389 const Object& serialized_object = Object::Handle(reader.ReadObject()); | 389 const Object& serialized_object = Object::Handle(reader.ReadObject()); |
| 390 EXPECT(Equals(bl, serialized_object)); | 390 EXPECT(Equals(bl, serialized_object)); |
| 391 | 391 |
| 392 // Read object back from the snapshot into a C structure. | 392 // Read object back from the snapshot into a C structure. |
| 393 ApiNativeScope scope; | 393 ApiNativeScope scope; |
| 394 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); | 394 ApiMessageReader api_reader(buffer, buffer_len); |
| 395 Dart_CObject* root = api_reader.ReadMessage(); | 395 Dart_CObject* root = api_reader.ReadMessage(); |
| 396 EXPECT_NOTNULL(root); | 396 EXPECT_NOTNULL(root); |
| 397 EXPECT_EQ(Dart_CObject_kBool, root->type); | 397 EXPECT_EQ(Dart_CObject_kBool, root->type); |
| 398 EXPECT_EQ(false, root->value.as_bool); | 398 EXPECT_EQ(false, root->value.as_bool); |
| 399 CheckEncodeDecodeMessage(root); | 399 CheckEncodeDecodeMessage(root); |
| 400 } | 400 } |
| 401 | 401 |
| 402 | 402 |
| 403 static uword allocator(intptr_t size) { | 403 static uword allocator(intptr_t size) { |
| 404 return reinterpret_cast<uword>(malloc(size)); | 404 return reinterpret_cast<uword>(malloc(size)); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 420 buffer_len, | 420 buffer_len, |
| 421 Isolate::Current(), | 421 Isolate::Current(), |
| 422 zone.GetZone()); | 422 zone.GetZone()); |
| 423 Capability& obj = Capability::Handle(); | 423 Capability& obj = Capability::Handle(); |
| 424 obj ^= reader.ReadObject(); | 424 obj ^= reader.ReadObject(); |
| 425 | 425 |
| 426 EXPECT_STREQ(12345, obj.Id()); | 426 EXPECT_STREQ(12345, obj.Id()); |
| 427 | 427 |
| 428 // Read object back from the snapshot into a C structure. | 428 // Read object back from the snapshot into a C structure. |
| 429 ApiNativeScope scope; | 429 ApiNativeScope scope; |
| 430 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); | 430 ApiMessageReader api_reader(buffer, buffer_len); |
| 431 Dart_CObject* root = api_reader.ReadMessage(); | 431 Dart_CObject* root = api_reader.ReadMessage(); |
| 432 EXPECT_NOTNULL(root); | 432 EXPECT_NOTNULL(root); |
| 433 EXPECT_EQ(Dart_CObject_kCapability, root->type); | 433 EXPECT_EQ(Dart_CObject_kCapability, root->type); |
| 434 int64_t id = root->value.as_capability.id; | 434 int64_t id = root->value.as_capability.id; |
| 435 EXPECT_EQ(12345, id); | 435 EXPECT_EQ(12345, id); |
| 436 CheckEncodeDecodeMessage(root); | 436 CheckEncodeDecodeMessage(root); |
| 437 } | 437 } |
| 438 | 438 |
| 439 | 439 |
| 440 TEST_CASE(SerializeBigint) { | 440 TEST_CASE(SerializeBigint) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 455 buffer_len, | 455 buffer_len, |
| 456 Isolate::Current(), | 456 Isolate::Current(), |
| 457 zone.GetZone()); | 457 zone.GetZone()); |
| 458 Bigint& obj = Bigint::Handle(); | 458 Bigint& obj = Bigint::Handle(); |
| 459 obj ^= reader.ReadObject(); | 459 obj ^= reader.ReadObject(); |
| 460 | 460 |
| 461 EXPECT_STREQ(bigint.ToHexCString(allocator), obj.ToHexCString(allocator)); | 461 EXPECT_STREQ(bigint.ToHexCString(allocator), obj.ToHexCString(allocator)); |
| 462 | 462 |
| 463 // Read object back from the snapshot into a C structure. | 463 // Read object back from the snapshot into a C structure. |
| 464 ApiNativeScope scope; | 464 ApiNativeScope scope; |
| 465 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); | 465 ApiMessageReader api_reader(buffer, buffer_len); |
| 466 Dart_CObject* root = api_reader.ReadMessage(); | 466 Dart_CObject* root = api_reader.ReadMessage(); |
| 467 EXPECT_NOTNULL(root); | 467 EXPECT_NOTNULL(root); |
| 468 EXPECT_EQ(Dart_CObject_kBigint, root->type); | 468 EXPECT_EQ(Dart_CObject_kBigint, root->type); |
| 469 char* hex_value = TestCase::BigintToHexValue(root); | 469 char* hex_value = TestCase::BigintToHexValue(root); |
| 470 EXPECT_STREQ(cstr, hex_value); | 470 EXPECT_STREQ(cstr, hex_value); |
| 471 free(hex_value); | 471 free(hex_value); |
| 472 CheckEncodeDecodeMessage(root); | 472 CheckEncodeDecodeMessage(root); |
| 473 } | 473 } |
| 474 | 474 |
| 475 | 475 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 491 Bigint& serialized_bigint = Bigint::Handle(); | 491 Bigint& serialized_bigint = Bigint::Handle(); |
| 492 serialized_bigint ^= reader.ReadObject(); | 492 serialized_bigint ^= reader.ReadObject(); |
| 493 const char* str1 = bigint.ToHexCString(allocator); | 493 const char* str1 = bigint.ToHexCString(allocator); |
| 494 const char* str2 = serialized_bigint.ToHexCString(allocator); | 494 const char* str2 = serialized_bigint.ToHexCString(allocator); |
| 495 EXPECT_STREQ(str1, str2); | 495 EXPECT_STREQ(str1, str2); |
| 496 free(const_cast<char*>(str1)); | 496 free(const_cast<char*>(str1)); |
| 497 free(const_cast<char*>(str2)); | 497 free(const_cast<char*>(str2)); |
| 498 } | 498 } |
| 499 | 499 |
| 500 // Read object back from the snapshot into a C structure. | 500 // Read object back from the snapshot into a C structure. |
| 501 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); | 501 ApiMessageReader api_reader(buffer, buffer_len); |
| 502 Dart_CObject* root = api_reader.ReadMessage(); | 502 Dart_CObject* root = api_reader.ReadMessage(); |
| 503 // Bigint not supported. | 503 // Bigint not supported. |
| 504 EXPECT_NOTNULL(root); | 504 EXPECT_NOTNULL(root); |
| 505 CheckEncodeDecodeMessage(root); | 505 CheckEncodeDecodeMessage(root); |
| 506 return root; | 506 return root; |
| 507 } | 507 } |
| 508 | 508 |
| 509 | 509 |
| 510 void CheckBigint(const char* bigint_value) { | 510 void CheckBigint(const char* bigint_value) { |
| 511 StackZone zone(Thread::Current()); | 511 StackZone zone(Thread::Current()); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 586 MessageSnapshotReader reader(buffer, | 586 MessageSnapshotReader reader(buffer, |
| 587 buffer_len, | 587 buffer_len, |
| 588 Isolate::Current(), | 588 Isolate::Current(), |
| 589 zone.GetZone()); | 589 zone.GetZone()); |
| 590 String& serialized_str = String::Handle(); | 590 String& serialized_str = String::Handle(); |
| 591 serialized_str ^= reader.ReadObject(); | 591 serialized_str ^= reader.ReadObject(); |
| 592 EXPECT(str.Equals(serialized_str)); | 592 EXPECT(str.Equals(serialized_str)); |
| 593 | 593 |
| 594 // Read object back from the snapshot into a C structure. | 594 // Read object back from the snapshot into a C structure. |
| 595 ApiNativeScope scope; | 595 ApiNativeScope scope; |
| 596 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); | 596 ApiMessageReader api_reader(buffer, buffer_len); |
| 597 Dart_CObject* root = api_reader.ReadMessage(); | 597 Dart_CObject* root = api_reader.ReadMessage(); |
| 598 EXPECT_EQ(Dart_CObject_kString, root->type); | 598 EXPECT_EQ(Dart_CObject_kString, root->type); |
| 599 EXPECT_STREQ(cstr, root->value.as_string); | 599 EXPECT_STREQ(cstr, root->value.as_string); |
| 600 CheckEncodeDecodeMessage(root); | 600 CheckEncodeDecodeMessage(root); |
| 601 } | 601 } |
| 602 | 602 |
| 603 | 603 |
| 604 TEST_CASE(SerializeString) { | 604 TEST_CASE(SerializeString) { |
| 605 TestString("This string shall be serialized"); | 605 TestString("This string shall be serialized"); |
| 606 TestString("æøå"); // This file is UTF-8 encoded. | 606 TestString("æøå"); // This file is UTF-8 encoded. |
| (...skipping 29 matching lines...) Expand all Loading... |
| 636 MessageSnapshotReader reader(buffer, | 636 MessageSnapshotReader reader(buffer, |
| 637 buffer_len, | 637 buffer_len, |
| 638 Isolate::Current(), | 638 Isolate::Current(), |
| 639 zone.GetZone()); | 639 zone.GetZone()); |
| 640 Array& serialized_array = Array::Handle(); | 640 Array& serialized_array = Array::Handle(); |
| 641 serialized_array ^= reader.ReadObject(); | 641 serialized_array ^= reader.ReadObject(); |
| 642 EXPECT(array.CanonicalizeEquals(serialized_array)); | 642 EXPECT(array.CanonicalizeEquals(serialized_array)); |
| 643 | 643 |
| 644 // Read object back from the snapshot into a C structure. | 644 // Read object back from the snapshot into a C structure. |
| 645 ApiNativeScope scope; | 645 ApiNativeScope scope; |
| 646 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); | 646 ApiMessageReader api_reader(buffer, buffer_len); |
| 647 Dart_CObject* root = api_reader.ReadMessage(); | 647 Dart_CObject* root = api_reader.ReadMessage(); |
| 648 EXPECT_EQ(Dart_CObject_kArray, root->type); | 648 EXPECT_EQ(Dart_CObject_kArray, root->type); |
| 649 EXPECT_EQ(kArrayLength, root->value.as_array.length); | 649 EXPECT_EQ(kArrayLength, root->value.as_array.length); |
| 650 for (int i = 0; i < kArrayLength; i++) { | 650 for (int i = 0; i < kArrayLength; i++) { |
| 651 Dart_CObject* element = root->value.as_array.values[i]; | 651 Dart_CObject* element = root->value.as_array.values[i]; |
| 652 EXPECT_EQ(Dart_CObject_kInt32, element->type); | 652 EXPECT_EQ(Dart_CObject_kInt32, element->type); |
| 653 EXPECT_EQ(i, element->value.as_int32); | 653 EXPECT_EQ(i, element->value.as_int32); |
| 654 } | 654 } |
| 655 CheckEncodeDecodeMessage(root); | 655 CheckEncodeDecodeMessage(root); |
| 656 } | 656 } |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 723 MessageSnapshotReader reader(buffer, | 723 MessageSnapshotReader reader(buffer, |
| 724 buffer_len, | 724 buffer_len, |
| 725 Isolate::Current(), | 725 Isolate::Current(), |
| 726 zone.GetZone()); | 726 zone.GetZone()); |
| 727 Array& serialized_array = Array::Handle(); | 727 Array& serialized_array = Array::Handle(); |
| 728 serialized_array ^= reader.ReadObject(); | 728 serialized_array ^= reader.ReadObject(); |
| 729 EXPECT(array.CanonicalizeEquals(serialized_array)); | 729 EXPECT(array.CanonicalizeEquals(serialized_array)); |
| 730 | 730 |
| 731 // Read object back from the snapshot into a C structure. | 731 // Read object back from the snapshot into a C structure. |
| 732 ApiNativeScope scope; | 732 ApiNativeScope scope; |
| 733 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); | 733 ApiMessageReader api_reader(buffer, buffer_len); |
| 734 Dart_CObject* root = api_reader.ReadMessage(); | 734 Dart_CObject* root = api_reader.ReadMessage(); |
| 735 EXPECT_EQ(Dart_CObject_kArray, root->type); | 735 EXPECT_EQ(Dart_CObject_kArray, root->type); |
| 736 EXPECT_EQ(kArrayLength, root->value.as_array.length); | 736 EXPECT_EQ(kArrayLength, root->value.as_array.length); |
| 737 EXPECT(root->value.as_array.values == NULL); | 737 EXPECT(root->value.as_array.values == NULL); |
| 738 CheckEncodeDecodeMessage(root); | 738 CheckEncodeDecodeMessage(root); |
| 739 } | 739 } |
| 740 | 740 |
| 741 | 741 |
| 742 TEST_CASE(SerializeByteArray) { | 742 TEST_CASE(SerializeByteArray) { |
| 743 StackZone zone(Thread::Current()); | 743 StackZone zone(Thread::Current()); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 758 MessageSnapshotReader reader(buffer, | 758 MessageSnapshotReader reader(buffer, |
| 759 buffer_len, | 759 buffer_len, |
| 760 Isolate::Current(), | 760 Isolate::Current(), |
| 761 zone.GetZone()); | 761 zone.GetZone()); |
| 762 TypedData& serialized_typed_data = TypedData::Handle(); | 762 TypedData& serialized_typed_data = TypedData::Handle(); |
| 763 serialized_typed_data ^= reader.ReadObject(); | 763 serialized_typed_data ^= reader.ReadObject(); |
| 764 EXPECT(serialized_typed_data.IsTypedData()); | 764 EXPECT(serialized_typed_data.IsTypedData()); |
| 765 | 765 |
| 766 // Read object back from the snapshot into a C structure. | 766 // Read object back from the snapshot into a C structure. |
| 767 ApiNativeScope scope; | 767 ApiNativeScope scope; |
| 768 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); | 768 ApiMessageReader api_reader(buffer, buffer_len); |
| 769 Dart_CObject* root = api_reader.ReadMessage(); | 769 Dart_CObject* root = api_reader.ReadMessage(); |
| 770 EXPECT_EQ(Dart_CObject_kTypedData, root->type); | 770 EXPECT_EQ(Dart_CObject_kTypedData, root->type); |
| 771 EXPECT_EQ(kTypedDataLength, root->value.as_typed_data.length); | 771 EXPECT_EQ(kTypedDataLength, root->value.as_typed_data.length); |
| 772 for (int i = 0; i < kTypedDataLength; i++) { | 772 for (int i = 0; i < kTypedDataLength; i++) { |
| 773 EXPECT(root->value.as_typed_data.values[i] == i); | 773 EXPECT(root->value.as_typed_data.values[i] == i); |
| 774 } | 774 } |
| 775 CheckEncodeDecodeMessage(root); | 775 CheckEncodeDecodeMessage(root); |
| 776 } | 776 } |
| 777 | 777 |
| 778 | 778 |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 871 MessageSnapshotReader reader(buffer, | 871 MessageSnapshotReader reader(buffer, |
| 872 buffer_len, | 872 buffer_len, |
| 873 Isolate::Current(), | 873 Isolate::Current(), |
| 874 zone.GetZone()); | 874 zone.GetZone()); |
| 875 TypedData& serialized_typed_data = TypedData::Handle(); | 875 TypedData& serialized_typed_data = TypedData::Handle(); |
| 876 serialized_typed_data ^= reader.ReadObject(); | 876 serialized_typed_data ^= reader.ReadObject(); |
| 877 EXPECT(serialized_typed_data.IsTypedData()); | 877 EXPECT(serialized_typed_data.IsTypedData()); |
| 878 | 878 |
| 879 // Read object back from the snapshot into a C structure. | 879 // Read object back from the snapshot into a C structure. |
| 880 ApiNativeScope scope; | 880 ApiNativeScope scope; |
| 881 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); | 881 ApiMessageReader api_reader(buffer, buffer_len); |
| 882 Dart_CObject* root = api_reader.ReadMessage(); | 882 Dart_CObject* root = api_reader.ReadMessage(); |
| 883 EXPECT_EQ(Dart_CObject_kTypedData, root->type); | 883 EXPECT_EQ(Dart_CObject_kTypedData, root->type); |
| 884 EXPECT_EQ(Dart_TypedData_kUint8, root->value.as_typed_data.type); | 884 EXPECT_EQ(Dart_TypedData_kUint8, root->value.as_typed_data.type); |
| 885 EXPECT_EQ(kTypedDataLength, root->value.as_typed_data.length); | 885 EXPECT_EQ(kTypedDataLength, root->value.as_typed_data.length); |
| 886 EXPECT(root->value.as_typed_data.values == NULL); | 886 EXPECT(root->value.as_typed_data.values == NULL); |
| 887 CheckEncodeDecodeMessage(root); | 887 CheckEncodeDecodeMessage(root); |
| 888 } | 888 } |
| 889 | 889 |
| 890 | 890 |
| 891 class TestSnapshotWriter : public SnapshotWriter { | 891 class TestSnapshotWriter : public SnapshotWriter { |
| (...skipping 831 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1723 uint8_t* buffer = NULL; | 1723 uint8_t* buffer = NULL; |
| 1724 ApiMessageWriter writer(&buffer, &zone_allocator); | 1724 ApiMessageWriter writer(&buffer, &zone_allocator); |
| 1725 | 1725 |
| 1726 static const int kArrayLength = 2; | 1726 static const int kArrayLength = 2; |
| 1727 intptr_t data[kArrayLength] = {1, 2}; | 1727 intptr_t data[kArrayLength] = {1, 2}; |
| 1728 int len = kArrayLength; | 1728 int len = kArrayLength; |
| 1729 writer.WriteMessage(len, data); | 1729 writer.WriteMessage(len, data); |
| 1730 | 1730 |
| 1731 // Read object back from the snapshot into a C structure. | 1731 // Read object back from the snapshot into a C structure. |
| 1732 ApiNativeScope scope; | 1732 ApiNativeScope scope; |
| 1733 ApiMessageReader api_reader(buffer, | 1733 ApiMessageReader api_reader(buffer, writer.BytesWritten()); |
| 1734 writer.BytesWritten(), | |
| 1735 &zone_allocator); | |
| 1736 Dart_CObject* root = api_reader.ReadMessage(); | 1734 Dart_CObject* root = api_reader.ReadMessage(); |
| 1737 EXPECT_EQ(Dart_CObject_kArray, root->type); | 1735 EXPECT_EQ(Dart_CObject_kArray, root->type); |
| 1738 EXPECT_EQ(kArrayLength, root->value.as_array.length); | 1736 EXPECT_EQ(kArrayLength, root->value.as_array.length); |
| 1739 for (int i = 0; i < kArrayLength; i++) { | 1737 for (int i = 0; i < kArrayLength; i++) { |
| 1740 Dart_CObject* element = root->value.as_array.values[i]; | 1738 Dart_CObject* element = root->value.as_array.values[i]; |
| 1741 EXPECT_EQ(Dart_CObject_kInt32, element->type); | 1739 EXPECT_EQ(Dart_CObject_kInt32, element->type); |
| 1742 EXPECT_EQ(i + 1, element->value.as_int32); | 1740 EXPECT_EQ(i + 1, element->value.as_int32); |
| 1743 } | 1741 } |
| 1744 CheckEncodeDecodeMessage(root); | 1742 CheckEncodeDecodeMessage(root); |
| 1745 } | 1743 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 1759 MessageWriter writer(&buffer, &zone_allocator, false); | 1757 MessageWriter writer(&buffer, &zone_allocator, false); |
| 1760 writer.WriteMessage(obj); | 1758 writer.WriteMessage(obj); |
| 1761 *buffer_len = writer.BytesWritten(); | 1759 *buffer_len = writer.BytesWritten(); |
| 1762 return buffer; | 1760 return buffer; |
| 1763 } | 1761 } |
| 1764 | 1762 |
| 1765 | 1763 |
| 1766 // Helper function to deserialize the result into a Dart_CObject structure. | 1764 // Helper function to deserialize the result into a Dart_CObject structure. |
| 1767 static Dart_CObject* GetDeserialized(uint8_t* buffer, intptr_t buffer_len) { | 1765 static Dart_CObject* GetDeserialized(uint8_t* buffer, intptr_t buffer_len) { |
| 1768 // Read object back from the snapshot into a C structure. | 1766 // Read object back from the snapshot into a C structure. |
| 1769 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); | 1767 ApiMessageReader api_reader(buffer, buffer_len); |
| 1770 return api_reader.ReadMessage(); | 1768 return api_reader.ReadMessage(); |
| 1771 } | 1769 } |
| 1772 | 1770 |
| 1773 | 1771 |
| 1774 static void CheckString(Dart_Handle dart_string, const char* expected) { | 1772 static void CheckString(Dart_Handle dart_string, const char* expected) { |
| 1775 StackZone zone(Thread::Current()); | 1773 StackZone zone(Thread::Current()); |
| 1776 String& str = String::Handle(); | 1774 String& str = String::Handle(); |
| 1777 str ^= Api::UnwrapHandle(dart_string); | 1775 str ^= Api::UnwrapHandle(dart_string); |
| 1778 uint8_t* buffer; | 1776 uint8_t* buffer; |
| 1779 MessageWriter writer(&buffer, &zone_allocator, false); | 1777 MessageWriter writer(&buffer, &zone_allocator, false); |
| 1780 writer.WriteMessage(str); | 1778 writer.WriteMessage(str); |
| 1781 intptr_t buffer_len = writer.BytesWritten(); | 1779 intptr_t buffer_len = writer.BytesWritten(); |
| 1782 | 1780 |
| 1783 // Read object back from the snapshot into a C structure. | 1781 // Read object back from the snapshot into a C structure. |
| 1784 ApiNativeScope scope; | 1782 ApiNativeScope scope; |
| 1785 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); | 1783 ApiMessageReader api_reader(buffer, buffer_len); |
| 1786 Dart_CObject* root = api_reader.ReadMessage(); | 1784 Dart_CObject* root = api_reader.ReadMessage(); |
| 1787 EXPECT_NOTNULL(root); | 1785 EXPECT_NOTNULL(root); |
| 1788 EXPECT_EQ(Dart_CObject_kString, root->type); | 1786 EXPECT_EQ(Dart_CObject_kString, root->type); |
| 1789 EXPECT_STREQ(expected, root->value.as_string); | 1787 EXPECT_STREQ(expected, root->value.as_string); |
| 1790 CheckEncodeDecodeMessage(root); | 1788 CheckEncodeDecodeMessage(root); |
| 1791 } | 1789 } |
| 1792 | 1790 |
| 1793 | 1791 |
| 1794 static void CheckStringInvalid(Dart_Handle dart_string) { | 1792 static void CheckStringInvalid(Dart_Handle dart_string) { |
| 1795 StackZone zone(Thread::Current()); | 1793 StackZone zone(Thread::Current()); |
| 1796 String& str = String::Handle(); | 1794 String& str = String::Handle(); |
| 1797 str ^= Api::UnwrapHandle(dart_string); | 1795 str ^= Api::UnwrapHandle(dart_string); |
| 1798 uint8_t* buffer; | 1796 uint8_t* buffer; |
| 1799 MessageWriter writer(&buffer, &zone_allocator, false); | 1797 MessageWriter writer(&buffer, &zone_allocator, false); |
| 1800 writer.WriteMessage(str); | 1798 writer.WriteMessage(str); |
| 1801 intptr_t buffer_len = writer.BytesWritten(); | 1799 intptr_t buffer_len = writer.BytesWritten(); |
| 1802 | 1800 |
| 1803 // Read object back from the snapshot into a C structure. | 1801 // Read object back from the snapshot into a C structure. |
| 1804 ApiNativeScope scope; | 1802 ApiNativeScope scope; |
| 1805 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); | 1803 ApiMessageReader api_reader(buffer, buffer_len); |
| 1806 Dart_CObject* root = api_reader.ReadMessage(); | 1804 Dart_CObject* root = api_reader.ReadMessage(); |
| 1807 EXPECT_NOTNULL(root); | 1805 EXPECT_NOTNULL(root); |
| 1808 EXPECT_EQ(Dart_CObject_kUnsupported, root->type); | 1806 EXPECT_EQ(Dart_CObject_kUnsupported, root->type); |
| 1809 } | 1807 } |
| 1810 | 1808 |
| 1811 | 1809 |
| 1812 UNIT_TEST_CASE(DartGeneratedMessages) { | 1810 UNIT_TEST_CASE(DartGeneratedMessages) { |
| 1813 static const char* kCustomIsolateScriptChars = | 1811 static const char* kCustomIsolateScriptChars = |
| 1814 "getSmi() {\n" | 1812 "getSmi() {\n" |
| 1815 " return 42;\n" | 1813 " return 42;\n" |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1905 StackZone zone(Thread::Current()); | 1903 StackZone zone(Thread::Current()); |
| 1906 Smi& smi = Smi::Handle(); | 1904 Smi& smi = Smi::Handle(); |
| 1907 smi ^= Api::UnwrapHandle(smi_result); | 1905 smi ^= Api::UnwrapHandle(smi_result); |
| 1908 uint8_t* buffer; | 1906 uint8_t* buffer; |
| 1909 MessageWriter writer(&buffer, &zone_allocator, false); | 1907 MessageWriter writer(&buffer, &zone_allocator, false); |
| 1910 writer.WriteMessage(smi); | 1908 writer.WriteMessage(smi); |
| 1911 intptr_t buffer_len = writer.BytesWritten(); | 1909 intptr_t buffer_len = writer.BytesWritten(); |
| 1912 | 1910 |
| 1913 // Read object back from the snapshot into a C structure. | 1911 // Read object back from the snapshot into a C structure. |
| 1914 ApiNativeScope scope; | 1912 ApiNativeScope scope; |
| 1915 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); | 1913 ApiMessageReader api_reader(buffer, buffer_len); |
| 1916 Dart_CObject* root = api_reader.ReadMessage(); | 1914 Dart_CObject* root = api_reader.ReadMessage(); |
| 1917 EXPECT_NOTNULL(root); | 1915 EXPECT_NOTNULL(root); |
| 1918 EXPECT_EQ(Dart_CObject_kInt32, root->type); | 1916 EXPECT_EQ(Dart_CObject_kInt32, root->type); |
| 1919 EXPECT_EQ(42, root->value.as_int32); | 1917 EXPECT_EQ(42, root->value.as_int32); |
| 1920 CheckEncodeDecodeMessage(root); | 1918 CheckEncodeDecodeMessage(root); |
| 1921 } | 1919 } |
| 1922 { | 1920 { |
| 1923 StackZone zone(Thread::Current()); | 1921 StackZone zone(Thread::Current()); |
| 1924 Bigint& bigint = Bigint::Handle(); | 1922 Bigint& bigint = Bigint::Handle(); |
| 1925 bigint ^= Api::UnwrapHandle(bigint_result); | 1923 bigint ^= Api::UnwrapHandle(bigint_result); |
| 1926 uint8_t* buffer; | 1924 uint8_t* buffer; |
| 1927 MessageWriter writer(&buffer, &zone_allocator, false); | 1925 MessageWriter writer(&buffer, &zone_allocator, false); |
| 1928 writer.WriteMessage(bigint); | 1926 writer.WriteMessage(bigint); |
| 1929 intptr_t buffer_len = writer.BytesWritten(); | 1927 intptr_t buffer_len = writer.BytesWritten(); |
| 1930 | 1928 |
| 1931 // Read object back from the snapshot into a C structure. | 1929 // Read object back from the snapshot into a C structure. |
| 1932 ApiNativeScope scope; | 1930 ApiNativeScope scope; |
| 1933 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); | 1931 ApiMessageReader api_reader(buffer, buffer_len); |
| 1934 Dart_CObject* root = api_reader.ReadMessage(); | 1932 Dart_CObject* root = api_reader.ReadMessage(); |
| 1935 EXPECT_NOTNULL(root); | 1933 EXPECT_NOTNULL(root); |
| 1936 EXPECT_EQ(Dart_CObject_kBigint, root->type); | 1934 EXPECT_EQ(Dart_CObject_kBigint, root->type); |
| 1937 char* hex_value = TestCase::BigintToHexValue(root); | 1935 char* hex_value = TestCase::BigintToHexValue(root); |
| 1938 EXPECT_STREQ("-0x424242424242424242424242424242424242", hex_value); | 1936 EXPECT_STREQ("-0x424242424242424242424242424242424242", hex_value); |
| 1939 free(hex_value); | 1937 free(hex_value); |
| 1940 CheckEncodeDecodeMessage(root); | 1938 CheckEncodeDecodeMessage(root); |
| 1941 } | 1939 } |
| 1942 CheckString(ascii_string_result, "Hello, world!"); | 1940 CheckString(ascii_string_result, "Hello, world!"); |
| 1943 CheckString(non_ascii_string_result, "Blåbærgrød"); | 1941 CheckString(non_ascii_string_result, "Blåbærgrød"); |
| (...skipping 1112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3056 StackZone zone(Thread::Current()); | 3054 StackZone zone(Thread::Current()); |
| 3057 uint8_t* buffer; | 3055 uint8_t* buffer; |
| 3058 MessageWriter writer(&buffer, &zone_allocator, true); | 3056 MessageWriter writer(&buffer, &zone_allocator, true); |
| 3059 writer.WriteInlinedObjectHeader(kOmittedObjectId); | 3057 writer.WriteInlinedObjectHeader(kOmittedObjectId); |
| 3060 // For performance, we'd like single-byte headers when ids are omitted. | 3058 // For performance, we'd like single-byte headers when ids are omitted. |
| 3061 // If this starts failing, consider renumbering the snapshot ids. | 3059 // If this starts failing, consider renumbering the snapshot ids. |
| 3062 EXPECT_EQ(1, writer.BytesWritten()); | 3060 EXPECT_EQ(1, writer.BytesWritten()); |
| 3063 } | 3061 } |
| 3064 | 3062 |
| 3065 } // namespace dart | 3063 } // namespace dart |
| OLD | NEW |