| 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 "include/dart_debugger_api.h" | 5 #include "include/dart_debugger_api.h" |
| 6 #include "platform/assert.h" | 6 #include "platform/assert.h" |
| 7 #include "vm/bigint_operations.h" | 7 #include "vm/bigint_operations.h" |
| 8 #include "vm/class_finalizer.h" | 8 #include "vm/class_finalizer.h" |
| 9 #include "vm/dart_api_impl.h" | 9 #include "vm/dart_api_impl.h" |
| 10 #include "vm/dart_api_message.h" | 10 #include "vm/dart_api_message.h" |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 | 51 |
| 52 | 52 |
| 53 static uint8_t* malloc_allocator( | 53 static uint8_t* malloc_allocator( |
| 54 uint8_t* ptr, intptr_t old_size, intptr_t new_size) { | 54 uint8_t* ptr, intptr_t old_size, intptr_t new_size) { |
| 55 return reinterpret_cast<uint8_t*>(realloc(ptr, new_size)); | 55 return reinterpret_cast<uint8_t*>(realloc(ptr, new_size)); |
| 56 } | 56 } |
| 57 | 57 |
| 58 | 58 |
| 59 static uint8_t* zone_allocator( | 59 static uint8_t* zone_allocator( |
| 60 uint8_t* ptr, intptr_t old_size, intptr_t new_size) { | 60 uint8_t* ptr, intptr_t old_size, intptr_t new_size) { |
| 61 Zone* zone = Isolate::Current()->current_zone(); | 61 StackZone* zone = Isolate::Current()->current_zone(); |
| 62 return zone->Realloc<uint8_t>(ptr, old_size, new_size); | 62 return zone->Realloc<uint8_t>(ptr, old_size, new_size); |
| 63 } | 63 } |
| 64 | 64 |
| 65 | 65 |
| 66 // Compare two Dart_CObject object graphs rooted in first and | 66 // Compare two Dart_CObject object graphs rooted in first and |
| 67 // second. The second graph will be destroyed by this operation no matter | 67 // second. The second graph will be destroyed by this operation no matter |
| 68 // whether the graphs are equal or not. | 68 // whether the graphs are equal or not. |
| 69 static void CompareDartCObjects(Dart_CObject* first, Dart_CObject* second) { | 69 static void CompareDartCObjects(Dart_CObject* first, Dart_CObject* second) { |
| 70 // Return immediately if entering a cycle. | 70 // Return immediately if entering a cycle. |
| 71 if (second->type == Dart_CObject::kNumberOfTypes) return; | 71 if (second->type == Dart_CObject::kNumberOfTypes) return; |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 writer.WriteCMessage(root); | 125 writer.WriteCMessage(root); |
| 126 | 126 |
| 127 ApiMessageReader api_reader(buffer, writer.BytesWritten(), &zone_allocator); | 127 ApiMessageReader api_reader(buffer, writer.BytesWritten(), &zone_allocator); |
| 128 Dart_CObject* new_root = api_reader.ReadMessage(); | 128 Dart_CObject* new_root = api_reader.ReadMessage(); |
| 129 | 129 |
| 130 // Check that the two messages are the same. | 130 // Check that the two messages are the same. |
| 131 CompareDartCObjects(root, new_root); | 131 CompareDartCObjects(root, new_root); |
| 132 } | 132 } |
| 133 | 133 |
| 134 TEST_CASE(SerializeNull) { | 134 TEST_CASE(SerializeNull) { |
| 135 Zone zone(Isolate::Current()); | 135 StackZone zone(Isolate::Current()); |
| 136 | 136 |
| 137 // Write snapshot with object content. | 137 // Write snapshot with object content. |
| 138 uint8_t* buffer; | 138 uint8_t* buffer; |
| 139 MessageWriter writer(&buffer, &zone_allocator); | 139 MessageWriter writer(&buffer, &zone_allocator); |
| 140 const Object& null_object = Object::Handle(); | 140 const Object& null_object = Object::Handle(); |
| 141 writer.WriteMessage(null_object); | 141 writer.WriteMessage(null_object); |
| 142 intptr_t buffer_len = writer.BytesWritten(); | 142 intptr_t buffer_len = writer.BytesWritten(); |
| 143 | 143 |
| 144 // Read object back from the snapshot. | 144 // Read object back from the snapshot. |
| 145 SnapshotReader reader(buffer, buffer_len, | 145 SnapshotReader reader(buffer, buffer_len, |
| 146 Snapshot::kMessage, Isolate::Current()); | 146 Snapshot::kMessage, Isolate::Current()); |
| 147 const Object& serialized_object = Object::Handle(reader.ReadObject()); | 147 const Object& serialized_object = Object::Handle(reader.ReadObject()); |
| 148 EXPECT(Equals(null_object, serialized_object)); | 148 EXPECT(Equals(null_object, serialized_object)); |
| 149 | 149 |
| 150 // Read object back from the snapshot into a C structure. | 150 // Read object back from the snapshot into a C structure. |
| 151 ApiNativeScope scope; | 151 ApiNativeScope scope; |
| 152 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); | 152 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); |
| 153 Dart_CObject* root = api_reader.ReadMessage(); | 153 Dart_CObject* root = api_reader.ReadMessage(); |
| 154 EXPECT_NOTNULL(root); | 154 EXPECT_NOTNULL(root); |
| 155 EXPECT_EQ(Dart_CObject::kNull, root->type); | 155 EXPECT_EQ(Dart_CObject::kNull, root->type); |
| 156 CheckEncodeDecodeMessage(root); | 156 CheckEncodeDecodeMessage(root); |
| 157 } | 157 } |
| 158 | 158 |
| 159 | 159 |
| 160 TEST_CASE(SerializeSmi1) { | 160 TEST_CASE(SerializeSmi1) { |
| 161 Zone zone(Isolate::Current()); | 161 StackZone zone(Isolate::Current()); |
| 162 | 162 |
| 163 // Write snapshot with object content. | 163 // Write snapshot with object content. |
| 164 uint8_t* buffer; | 164 uint8_t* buffer; |
| 165 MessageWriter writer(&buffer, &zone_allocator); | 165 MessageWriter writer(&buffer, &zone_allocator); |
| 166 const Smi& smi = Smi::Handle(Smi::New(124)); | 166 const Smi& smi = Smi::Handle(Smi::New(124)); |
| 167 writer.WriteMessage(smi); | 167 writer.WriteMessage(smi); |
| 168 intptr_t buffer_len = writer.BytesWritten(); | 168 intptr_t buffer_len = writer.BytesWritten(); |
| 169 | 169 |
| 170 // Read object back from the snapshot. | 170 // Read object back from the snapshot. |
| 171 SnapshotReader reader(buffer, buffer_len, | 171 SnapshotReader reader(buffer, buffer_len, |
| 172 Snapshot::kMessage, Isolate::Current()); | 172 Snapshot::kMessage, Isolate::Current()); |
| 173 const Object& serialized_object = Object::Handle(reader.ReadObject()); | 173 const Object& serialized_object = Object::Handle(reader.ReadObject()); |
| 174 EXPECT(Equals(smi, serialized_object)); | 174 EXPECT(Equals(smi, serialized_object)); |
| 175 | 175 |
| 176 // Read object back from the snapshot into a C structure. | 176 // Read object back from the snapshot into a C structure. |
| 177 ApiNativeScope scope; | 177 ApiNativeScope scope; |
| 178 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); | 178 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); |
| 179 Dart_CObject* root = api_reader.ReadMessage(); | 179 Dart_CObject* root = api_reader.ReadMessage(); |
| 180 EXPECT_NOTNULL(root); | 180 EXPECT_NOTNULL(root); |
| 181 EXPECT_EQ(Dart_CObject::kInt32, root->type); | 181 EXPECT_EQ(Dart_CObject::kInt32, root->type); |
| 182 EXPECT_EQ(smi.Value(), root->value.as_int32); | 182 EXPECT_EQ(smi.Value(), root->value.as_int32); |
| 183 CheckEncodeDecodeMessage(root); | 183 CheckEncodeDecodeMessage(root); |
| 184 } | 184 } |
| 185 | 185 |
| 186 | 186 |
| 187 TEST_CASE(SerializeSmi2) { | 187 TEST_CASE(SerializeSmi2) { |
| 188 Zone zone(Isolate::Current()); | 188 StackZone zone(Isolate::Current()); |
| 189 | 189 |
| 190 // Write snapshot with object content. | 190 // Write snapshot with object content. |
| 191 uint8_t* buffer; | 191 uint8_t* buffer; |
| 192 MessageWriter writer(&buffer, &zone_allocator); | 192 MessageWriter writer(&buffer, &zone_allocator); |
| 193 const Smi& smi = Smi::Handle(Smi::New(-1)); | 193 const Smi& smi = Smi::Handle(Smi::New(-1)); |
| 194 writer.WriteMessage(smi); | 194 writer.WriteMessage(smi); |
| 195 intptr_t buffer_len = writer.BytesWritten(); | 195 intptr_t buffer_len = writer.BytesWritten(); |
| 196 | 196 |
| 197 // Read object back from the snapshot. | 197 // Read object back from the snapshot. |
| 198 SnapshotReader reader(buffer, buffer_len, | 198 SnapshotReader reader(buffer, buffer_len, |
| (...skipping 28 matching lines...) Expand all Loading... |
| 227 // Read object back from the snapshot into a C structure. | 227 // Read object back from the snapshot into a C structure. |
| 228 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); | 228 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); |
| 229 Dart_CObject* root = api_reader.ReadMessage(); | 229 Dart_CObject* root = api_reader.ReadMessage(); |
| 230 EXPECT_NOTNULL(root); | 230 EXPECT_NOTNULL(root); |
| 231 CheckEncodeDecodeMessage(root); | 231 CheckEncodeDecodeMessage(root); |
| 232 return root; | 232 return root; |
| 233 } | 233 } |
| 234 | 234 |
| 235 | 235 |
| 236 void CheckMint(int64_t value) { | 236 void CheckMint(int64_t value) { |
| 237 Zone zone(Isolate::Current()); | 237 StackZone zone(Isolate::Current()); |
| 238 | 238 |
| 239 const Mint& mint = Mint::Handle(Mint::New(value)); | 239 const Mint& mint = Mint::Handle(Mint::New(value)); |
| 240 ApiNativeScope scope; | 240 ApiNativeScope scope; |
| 241 Dart_CObject* mint_cobject = SerializeAndDeserializeMint(mint); | 241 Dart_CObject* mint_cobject = SerializeAndDeserializeMint(mint); |
| 242 // On 64-bit platforms mints always require 64-bits as the smi range | 242 // On 64-bit platforms mints always require 64-bits as the smi range |
| 243 // here covers most of the 64-bit range. On 32-bit platforms the smi | 243 // here covers most of the 64-bit range. On 32-bit platforms the smi |
| 244 // range covers most of the 32-bit range and values outside that | 244 // range covers most of the 32-bit range and values outside that |
| 245 // range are also represented as mints. | 245 // range are also represented as mints. |
| 246 #if defined(ARCH_IS_64_BIT) | 246 #if defined(ARCH_IS_64_BIT) |
| 247 EXPECT_EQ(Dart_CObject::kInt64, mint_cobject->type); | 247 EXPECT_EQ(Dart_CObject::kInt64, mint_cobject->type); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 272 // Max positive mint - 1. | 272 // Max positive mint - 1. |
| 273 CheckMint(kMaxInt64 - 1); | 273 CheckMint(kMaxInt64 - 1); |
| 274 // Min negative mint. | 274 // Min negative mint. |
| 275 CheckMint(kMinInt64); | 275 CheckMint(kMinInt64); |
| 276 // Min negative mint + 1. | 276 // Min negative mint + 1. |
| 277 CheckMint(kMinInt64 + 1); | 277 CheckMint(kMinInt64 + 1); |
| 278 } | 278 } |
| 279 | 279 |
| 280 | 280 |
| 281 TEST_CASE(SerializeDouble) { | 281 TEST_CASE(SerializeDouble) { |
| 282 Zone zone(Isolate::Current()); | 282 StackZone zone(Isolate::Current()); |
| 283 | 283 |
| 284 // Write snapshot with object content. | 284 // Write snapshot with object content. |
| 285 uint8_t* buffer; | 285 uint8_t* buffer; |
| 286 MessageWriter writer(&buffer, &zone_allocator); | 286 MessageWriter writer(&buffer, &zone_allocator); |
| 287 const Double& dbl = Double::Handle(Double::New(101.29)); | 287 const Double& dbl = Double::Handle(Double::New(101.29)); |
| 288 writer.WriteMessage(dbl); | 288 writer.WriteMessage(dbl); |
| 289 intptr_t buffer_len = writer.BytesWritten(); | 289 intptr_t buffer_len = writer.BytesWritten(); |
| 290 | 290 |
| 291 // Read object back from the snapshot. | 291 // Read object back from the snapshot. |
| 292 SnapshotReader reader(buffer, buffer_len, | 292 SnapshotReader reader(buffer, buffer_len, |
| 293 Snapshot::kMessage, Isolate::Current()); | 293 Snapshot::kMessage, Isolate::Current()); |
| 294 const Object& serialized_object = Object::Handle(reader.ReadObject()); | 294 const Object& serialized_object = Object::Handle(reader.ReadObject()); |
| 295 EXPECT(Equals(dbl, serialized_object)); | 295 EXPECT(Equals(dbl, serialized_object)); |
| 296 | 296 |
| 297 // Read object back from the snapshot into a C structure. | 297 // Read object back from the snapshot into a C structure. |
| 298 ApiNativeScope scope; | 298 ApiNativeScope scope; |
| 299 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); | 299 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); |
| 300 Dart_CObject* root = api_reader.ReadMessage(); | 300 Dart_CObject* root = api_reader.ReadMessage(); |
| 301 EXPECT_NOTNULL(root); | 301 EXPECT_NOTNULL(root); |
| 302 EXPECT_EQ(Dart_CObject::kDouble, root->type); | 302 EXPECT_EQ(Dart_CObject::kDouble, root->type); |
| 303 EXPECT_EQ(dbl.value(), root->value.as_double); | 303 EXPECT_EQ(dbl.value(), root->value.as_double); |
| 304 CheckEncodeDecodeMessage(root); | 304 CheckEncodeDecodeMessage(root); |
| 305 } | 305 } |
| 306 | 306 |
| 307 | 307 |
| 308 TEST_CASE(SerializeTrue) { | 308 TEST_CASE(SerializeTrue) { |
| 309 Zone zone(Isolate::Current()); | 309 StackZone zone(Isolate::Current()); |
| 310 | 310 |
| 311 // Write snapshot with true object. | 311 // Write snapshot with true object. |
| 312 uint8_t* buffer; | 312 uint8_t* buffer; |
| 313 MessageWriter writer(&buffer, &zone_allocator); | 313 MessageWriter writer(&buffer, &zone_allocator); |
| 314 const Bool& bl = Bool::Handle(Bool::True()); | 314 const Bool& bl = Bool::Handle(Bool::True()); |
| 315 writer.WriteMessage(bl); | 315 writer.WriteMessage(bl); |
| 316 intptr_t buffer_len = writer.BytesWritten(); | 316 intptr_t buffer_len = writer.BytesWritten(); |
| 317 | 317 |
| 318 // Read object back from the snapshot. | 318 // Read object back from the snapshot. |
| 319 SnapshotReader reader(buffer, buffer_len, | 319 SnapshotReader reader(buffer, buffer_len, |
| 320 Snapshot::kMessage, Isolate::Current()); | 320 Snapshot::kMessage, Isolate::Current()); |
| 321 const Object& serialized_object = Object::Handle(reader.ReadObject()); | 321 const Object& serialized_object = Object::Handle(reader.ReadObject()); |
| 322 fprintf(stderr, "%s / %s\n", bl.ToCString(), serialized_object.ToCString()); | 322 fprintf(stderr, "%s / %s\n", bl.ToCString(), serialized_object.ToCString()); |
| 323 | 323 |
| 324 EXPECT(Equals(bl, serialized_object)); | 324 EXPECT(Equals(bl, serialized_object)); |
| 325 | 325 |
| 326 // Read object back from the snapshot into a C structure. | 326 // Read object back from the snapshot into a C structure. |
| 327 ApiNativeScope scope; | 327 ApiNativeScope scope; |
| 328 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); | 328 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); |
| 329 Dart_CObject* root = api_reader.ReadMessage(); | 329 Dart_CObject* root = api_reader.ReadMessage(); |
| 330 EXPECT_NOTNULL(root); | 330 EXPECT_NOTNULL(root); |
| 331 EXPECT_EQ(Dart_CObject::kBool, root->type); | 331 EXPECT_EQ(Dart_CObject::kBool, root->type); |
| 332 EXPECT_EQ(true, root->value.as_bool); | 332 EXPECT_EQ(true, root->value.as_bool); |
| 333 CheckEncodeDecodeMessage(root); | 333 CheckEncodeDecodeMessage(root); |
| 334 } | 334 } |
| 335 | 335 |
| 336 | 336 |
| 337 TEST_CASE(SerializeFalse) { | 337 TEST_CASE(SerializeFalse) { |
| 338 Zone zone(Isolate::Current()); | 338 StackZone zone(Isolate::Current()); |
| 339 | 339 |
| 340 // Write snapshot with false object. | 340 // Write snapshot with false object. |
| 341 uint8_t* buffer; | 341 uint8_t* buffer; |
| 342 MessageWriter writer(&buffer, &zone_allocator); | 342 MessageWriter writer(&buffer, &zone_allocator); |
| 343 const Bool& bl = Bool::Handle(Bool::False()); | 343 const Bool& bl = Bool::Handle(Bool::False()); |
| 344 writer.WriteMessage(bl); | 344 writer.WriteMessage(bl); |
| 345 intptr_t buffer_len = writer.BytesWritten(); | 345 intptr_t buffer_len = writer.BytesWritten(); |
| 346 | 346 |
| 347 // Read object back from the snapshot. | 347 // Read object back from the snapshot. |
| 348 SnapshotReader reader(buffer, buffer_len, | 348 SnapshotReader reader(buffer, buffer_len, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 360 CheckEncodeDecodeMessage(root); | 360 CheckEncodeDecodeMessage(root); |
| 361 } | 361 } |
| 362 | 362 |
| 363 | 363 |
| 364 static uword allocator(intptr_t size) { | 364 static uword allocator(intptr_t size) { |
| 365 return reinterpret_cast<uword>(malloc(size)); | 365 return reinterpret_cast<uword>(malloc(size)); |
| 366 } | 366 } |
| 367 | 367 |
| 368 | 368 |
| 369 TEST_CASE(SerializeBigint) { | 369 TEST_CASE(SerializeBigint) { |
| 370 Zone zone(Isolate::Current()); | 370 StackZone zone(Isolate::Current()); |
| 371 | 371 |
| 372 // Write snapshot with object content. | 372 // Write snapshot with object content. |
| 373 uint8_t* buffer; | 373 uint8_t* buffer; |
| 374 MessageWriter writer(&buffer, &zone_allocator); | 374 MessageWriter writer(&buffer, &zone_allocator); |
| 375 const Bigint& bigint = Bigint::Handle(Bigint::New(DART_INT64_C(0xfffffffff))); | 375 const Bigint& bigint = Bigint::Handle(Bigint::New(DART_INT64_C(0xfffffffff))); |
| 376 writer.WriteMessage(bigint); | 376 writer.WriteMessage(bigint); |
| 377 intptr_t buffer_len = writer.BytesWritten(); | 377 intptr_t buffer_len = writer.BytesWritten(); |
| 378 | 378 |
| 379 // Read object back from the snapshot. | 379 // Read object back from the snapshot. |
| 380 SnapshotReader reader(buffer, buffer_len, | 380 SnapshotReader reader(buffer, buffer_len, |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 420 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); | 420 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); |
| 421 Dart_CObject* root = api_reader.ReadMessage(); | 421 Dart_CObject* root = api_reader.ReadMessage(); |
| 422 // Bigint not supported. | 422 // Bigint not supported. |
| 423 EXPECT_NOTNULL(root); | 423 EXPECT_NOTNULL(root); |
| 424 CheckEncodeDecodeMessage(root); | 424 CheckEncodeDecodeMessage(root); |
| 425 return root; | 425 return root; |
| 426 } | 426 } |
| 427 | 427 |
| 428 | 428 |
| 429 void CheckBigint(const char* bigint_value) { | 429 void CheckBigint(const char* bigint_value) { |
| 430 Zone zone(Isolate::Current()); | 430 StackZone zone(Isolate::Current()); |
| 431 | 431 |
| 432 Bigint& bigint = Bigint::Handle(); | 432 Bigint& bigint = Bigint::Handle(); |
| 433 bigint ^= BigintOperations::NewFromCString(bigint_value); | 433 bigint ^= BigintOperations::NewFromCString(bigint_value); |
| 434 Dart_CObject* bigint_cobject = SerializeAndDeserializeBigint(bigint); | 434 Dart_CObject* bigint_cobject = SerializeAndDeserializeBigint(bigint); |
| 435 EXPECT_EQ(Dart_CObject::kBigint, bigint_cobject->type); | 435 EXPECT_EQ(Dart_CObject::kBigint, bigint_cobject->type); |
| 436 if (bigint_value[0] == '0') { | 436 if (bigint_value[0] == '0') { |
| 437 EXPECT_STREQ(bigint_value + 2, bigint_cobject->value.as_bigint); | 437 EXPECT_STREQ(bigint_value + 2, bigint_cobject->value.as_bigint); |
| 438 } else { | 438 } else { |
| 439 EXPECT_EQ('-', bigint_value[0]); | 439 EXPECT_EQ('-', bigint_value[0]); |
| 440 EXPECT_EQ('-', bigint_cobject->value.as_bigint[0]); | 440 EXPECT_EQ('-', bigint_cobject->value.as_bigint[0]); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 491 EXPECT(Object::pc_descriptors_class() == reader.ReadObject()); | 491 EXPECT(Object::pc_descriptors_class() == reader.ReadObject()); |
| 492 EXPECT(Object::exception_handlers_class() == reader.ReadObject()); | 492 EXPECT(Object::exception_handlers_class() == reader.ReadObject()); |
| 493 EXPECT(Object::context_class() == reader.ReadObject()); | 493 EXPECT(Object::context_class() == reader.ReadObject()); |
| 494 EXPECT(Object::context_scope_class() == reader.ReadObject()); | 494 EXPECT(Object::context_scope_class() == reader.ReadObject()); |
| 495 | 495 |
| 496 free(buffer); | 496 free(buffer); |
| 497 } | 497 } |
| 498 | 498 |
| 499 | 499 |
| 500 TEST_CASE(SerializeString) { | 500 TEST_CASE(SerializeString) { |
| 501 Zone zone(Isolate::Current()); | 501 StackZone zone(Isolate::Current()); |
| 502 | 502 |
| 503 // Write snapshot with object content. | 503 // Write snapshot with object content. |
| 504 uint8_t* buffer; | 504 uint8_t* buffer; |
| 505 MessageWriter writer(&buffer, &zone_allocator); | 505 MessageWriter writer(&buffer, &zone_allocator); |
| 506 static const char* cstr = "This string shall be serialized"; | 506 static const char* cstr = "This string shall be serialized"; |
| 507 String& str = String::Handle(String::New(cstr)); | 507 String& str = String::Handle(String::New(cstr)); |
| 508 writer.WriteMessage(str); | 508 writer.WriteMessage(str); |
| 509 intptr_t buffer_len = writer.BytesWritten(); | 509 intptr_t buffer_len = writer.BytesWritten(); |
| 510 | 510 |
| 511 // Read object back from the snapshot. | 511 // Read object back from the snapshot. |
| 512 SnapshotReader reader(buffer, buffer_len, | 512 SnapshotReader reader(buffer, buffer_len, |
| 513 Snapshot::kMessage, Isolate::Current()); | 513 Snapshot::kMessage, Isolate::Current()); |
| 514 String& serialized_str = String::Handle(); | 514 String& serialized_str = String::Handle(); |
| 515 serialized_str ^= reader.ReadObject(); | 515 serialized_str ^= reader.ReadObject(); |
| 516 EXPECT(str.Equals(serialized_str)); | 516 EXPECT(str.Equals(serialized_str)); |
| 517 | 517 |
| 518 // Read object back from the snapshot into a C structure. | 518 // Read object back from the snapshot into a C structure. |
| 519 ApiNativeScope scope; | 519 ApiNativeScope scope; |
| 520 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); | 520 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); |
| 521 Dart_CObject* root = api_reader.ReadMessage(); | 521 Dart_CObject* root = api_reader.ReadMessage(); |
| 522 EXPECT_EQ(Dart_CObject::kString, root->type); | 522 EXPECT_EQ(Dart_CObject::kString, root->type); |
| 523 EXPECT_STREQ(cstr, root->value.as_string); | 523 EXPECT_STREQ(cstr, root->value.as_string); |
| 524 CheckEncodeDecodeMessage(root); | 524 CheckEncodeDecodeMessage(root); |
| 525 } | 525 } |
| 526 | 526 |
| 527 | 527 |
| 528 TEST_CASE(SerializeArray) { | 528 TEST_CASE(SerializeArray) { |
| 529 Zone zone(Isolate::Current()); | 529 StackZone zone(Isolate::Current()); |
| 530 | 530 |
| 531 // Write snapshot with object content. | 531 // Write snapshot with object content. |
| 532 uint8_t* buffer; | 532 uint8_t* buffer; |
| 533 MessageWriter writer(&buffer, &zone_allocator); | 533 MessageWriter writer(&buffer, &zone_allocator); |
| 534 const int kArrayLength = 10; | 534 const int kArrayLength = 10; |
| 535 Array& array = Array::Handle(Array::New(kArrayLength)); | 535 Array& array = Array::Handle(Array::New(kArrayLength)); |
| 536 Smi& smi = Smi::Handle(); | 536 Smi& smi = Smi::Handle(); |
| 537 for (int i = 0; i < kArrayLength; i++) { | 537 for (int i = 0; i < kArrayLength; i++) { |
| 538 smi ^= Smi::New(i); | 538 smi ^= Smi::New(i); |
| 539 array.SetAt(i, smi); | 539 array.SetAt(i, smi); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 557 for (int i = 0; i < kArrayLength; i++) { | 557 for (int i = 0; i < kArrayLength; i++) { |
| 558 Dart_CObject* element = root->value.as_array.values[i]; | 558 Dart_CObject* element = root->value.as_array.values[i]; |
| 559 EXPECT_EQ(Dart_CObject::kInt32, element->type); | 559 EXPECT_EQ(Dart_CObject::kInt32, element->type); |
| 560 EXPECT_EQ(i, element->value.as_int32); | 560 EXPECT_EQ(i, element->value.as_int32); |
| 561 } | 561 } |
| 562 CheckEncodeDecodeMessage(root); | 562 CheckEncodeDecodeMessage(root); |
| 563 } | 563 } |
| 564 | 564 |
| 565 | 565 |
| 566 TEST_CASE(SerializeEmptyArray) { | 566 TEST_CASE(SerializeEmptyArray) { |
| 567 Zone zone(Isolate::Current()); | 567 StackZone zone(Isolate::Current()); |
| 568 | 568 |
| 569 // Write snapshot with object content. | 569 // Write snapshot with object content. |
| 570 uint8_t* buffer; | 570 uint8_t* buffer; |
| 571 MessageWriter writer(&buffer, &zone_allocator); | 571 MessageWriter writer(&buffer, &zone_allocator); |
| 572 const int kArrayLength = 0; | 572 const int kArrayLength = 0; |
| 573 Array& array = Array::Handle(Array::New(kArrayLength)); | 573 Array& array = Array::Handle(Array::New(kArrayLength)); |
| 574 writer.WriteMessage(array); | 574 writer.WriteMessage(array); |
| 575 intptr_t buffer_len = writer.BytesWritten(); | 575 intptr_t buffer_len = writer.BytesWritten(); |
| 576 | 576 |
| 577 // Read object back from the snapshot. | 577 // Read object back from the snapshot. |
| 578 SnapshotReader reader(buffer, buffer_len, | 578 SnapshotReader reader(buffer, buffer_len, |
| 579 Snapshot::kMessage, Isolate::Current()); | 579 Snapshot::kMessage, Isolate::Current()); |
| 580 Array& serialized_array = Array::Handle(); | 580 Array& serialized_array = Array::Handle(); |
| 581 serialized_array ^= reader.ReadObject(); | 581 serialized_array ^= reader.ReadObject(); |
| 582 EXPECT(array.Equals(serialized_array)); | 582 EXPECT(array.Equals(serialized_array)); |
| 583 | 583 |
| 584 // Read object back from the snapshot into a C structure. | 584 // Read object back from the snapshot into a C structure. |
| 585 ApiNativeScope scope; | 585 ApiNativeScope scope; |
| 586 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); | 586 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); |
| 587 Dart_CObject* root = api_reader.ReadMessage(); | 587 Dart_CObject* root = api_reader.ReadMessage(); |
| 588 EXPECT_EQ(Dart_CObject::kArray, root->type); | 588 EXPECT_EQ(Dart_CObject::kArray, root->type); |
| 589 EXPECT_EQ(kArrayLength, root->value.as_array.length); | 589 EXPECT_EQ(kArrayLength, root->value.as_array.length); |
| 590 EXPECT(root->value.as_array.values == NULL); | 590 EXPECT(root->value.as_array.values == NULL); |
| 591 CheckEncodeDecodeMessage(root); | 591 CheckEncodeDecodeMessage(root); |
| 592 } | 592 } |
| 593 | 593 |
| 594 | 594 |
| 595 TEST_CASE(SerializeByteArray) { | 595 TEST_CASE(SerializeByteArray) { |
| 596 Zone zone(Isolate::Current()); | 596 StackZone zone(Isolate::Current()); |
| 597 | 597 |
| 598 // Write snapshot with object content. | 598 // Write snapshot with object content. |
| 599 uint8_t* buffer; | 599 uint8_t* buffer; |
| 600 MessageWriter writer(&buffer, &zone_allocator); | 600 MessageWriter writer(&buffer, &zone_allocator); |
| 601 const int kByteArrayLength = 256; | 601 const int kByteArrayLength = 256; |
| 602 Uint8Array& byte_array = | 602 Uint8Array& byte_array = |
| 603 Uint8Array::Handle(Uint8Array::New(kByteArrayLength)); | 603 Uint8Array::Handle(Uint8Array::New(kByteArrayLength)); |
| 604 for (int i = 0; i < kByteArrayLength; i++) { | 604 for (int i = 0; i < kByteArrayLength; i++) { |
| 605 byte_array.SetAt(i, i); | 605 byte_array.SetAt(i, i); |
| 606 } | 606 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 621 EXPECT_EQ(Dart_CObject::kUint8Array, root->type); | 621 EXPECT_EQ(Dart_CObject::kUint8Array, root->type); |
| 622 EXPECT_EQ(kByteArrayLength, root->value.as_byte_array.length); | 622 EXPECT_EQ(kByteArrayLength, root->value.as_byte_array.length); |
| 623 for (int i = 0; i < kByteArrayLength; i++) { | 623 for (int i = 0; i < kByteArrayLength; i++) { |
| 624 EXPECT(root->value.as_byte_array.values[i] == i); | 624 EXPECT(root->value.as_byte_array.values[i] == i); |
| 625 } | 625 } |
| 626 CheckEncodeDecodeMessage(root); | 626 CheckEncodeDecodeMessage(root); |
| 627 } | 627 } |
| 628 | 628 |
| 629 | 629 |
| 630 TEST_CASE(SerializeEmptyByteArray) { | 630 TEST_CASE(SerializeEmptyByteArray) { |
| 631 Zone zone(Isolate::Current()); | 631 StackZone zone(Isolate::Current()); |
| 632 | 632 |
| 633 // Write snapshot with object content. | 633 // Write snapshot with object content. |
| 634 uint8_t* buffer; | 634 uint8_t* buffer; |
| 635 MessageWriter writer(&buffer, &zone_allocator); | 635 MessageWriter writer(&buffer, &zone_allocator); |
| 636 const int kByteArrayLength = 0; | 636 const int kByteArrayLength = 0; |
| 637 Uint8Array& byte_array = | 637 Uint8Array& byte_array = |
| 638 Uint8Array::Handle(Uint8Array::New(kByteArrayLength)); | 638 Uint8Array::Handle(Uint8Array::New(kByteArrayLength)); |
| 639 writer.WriteMessage(byte_array); | 639 writer.WriteMessage(byte_array); |
| 640 intptr_t buffer_len = writer.BytesWritten(); | 640 intptr_t buffer_len = writer.BytesWritten(); |
| 641 | 641 |
| (...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 849 { | 849 { |
| 850 TestIsolateScope __test_isolate__; | 850 TestIsolateScope __test_isolate__; |
| 851 | 851 |
| 852 // Create a test library and Load up a test script in it. | 852 // Create a test library and Load up a test script in it. |
| 853 TestCase::LoadTestScript(kScriptChars, NULL); | 853 TestCase::LoadTestScript(kScriptChars, NULL); |
| 854 timer1.Stop(); | 854 timer1.Stop(); |
| 855 OS::PrintErr("Without Snapshot: %"Pd64"us\n", timer1.TotalElapsedTime()); | 855 OS::PrintErr("Without Snapshot: %"Pd64"us\n", timer1.TotalElapsedTime()); |
| 856 | 856 |
| 857 // Write snapshot with object content. | 857 // Write snapshot with object content. |
| 858 Isolate* isolate = Isolate::Current(); | 858 Isolate* isolate = Isolate::Current(); |
| 859 Zone zone(isolate); | 859 StackZone zone(isolate); |
| 860 HandleScope scope(isolate); | 860 HandleScope scope(isolate); |
| 861 FullSnapshotWriter writer(&buffer, &malloc_allocator); | 861 FullSnapshotWriter writer(&buffer, &malloc_allocator); |
| 862 writer.WriteFullSnapshot(); | 862 writer.WriteFullSnapshot(); |
| 863 } | 863 } |
| 864 | 864 |
| 865 // Now Create another isolate using the snapshot and execute a method | 865 // Now Create another isolate using the snapshot and execute a method |
| 866 // from the script. | 866 // from the script. |
| 867 Timer timer2(true, "Snapshot_test"); | 867 Timer timer2(true, "Snapshot_test"); |
| 868 timer2.Start(); | 868 timer2.Start(); |
| 869 TestCase::CreateTestIsolateFromSnapshot(buffer); | 869 TestCase::CreateTestIsolateFromSnapshot(buffer); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 895 | 895 |
| 896 uint8_t* buffer; | 896 uint8_t* buffer; |
| 897 | 897 |
| 898 // Start an Isolate, load a script and create a full snapshot. | 898 // Start an Isolate, load a script and create a full snapshot. |
| 899 Timer timer1(true, "Snapshot_test"); | 899 Timer timer1(true, "Snapshot_test"); |
| 900 timer1.Start(); | 900 timer1.Start(); |
| 901 { | 901 { |
| 902 TestIsolateScope __test_isolate__; | 902 TestIsolateScope __test_isolate__; |
| 903 | 903 |
| 904 Isolate* isolate = Isolate::Current(); | 904 Isolate* isolate = Isolate::Current(); |
| 905 Zone zone(isolate); | 905 StackZone zone(isolate); |
| 906 HandleScope scope(isolate); | 906 HandleScope scope(isolate); |
| 907 | 907 |
| 908 // Create a test library and Load up a test script in it. | 908 // Create a test library and Load up a test script in it. |
| 909 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); | 909 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); |
| 910 ClassFinalizer::FinalizePendingClasses(); | 910 ClassFinalizer::FinalizePendingClasses(); |
| 911 timer1.Stop(); | 911 timer1.Stop(); |
| 912 OS::PrintErr("Without Snapshot: %"Pd64"us\n", timer1.TotalElapsedTime()); | 912 OS::PrintErr("Without Snapshot: %"Pd64"us\n", timer1.TotalElapsedTime()); |
| 913 | 913 |
| 914 // Write snapshot with object content. | 914 // Write snapshot with object content. |
| 915 FullSnapshotWriter writer(&buffer, &malloc_allocator); | 915 FullSnapshotWriter writer(&buffer, &malloc_allocator); |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1063 EXPECT_VALID(result); | 1063 EXPECT_VALID(result); |
| 1064 Dart_ExitScope(); | 1064 Dart_ExitScope(); |
| 1065 } | 1065 } |
| 1066 Dart_ShutdownIsolate(); | 1066 Dart_ShutdownIsolate(); |
| 1067 free(full_snapshot); | 1067 free(full_snapshot); |
| 1068 free(script_snapshot); | 1068 free(script_snapshot); |
| 1069 } | 1069 } |
| 1070 | 1070 |
| 1071 | 1071 |
| 1072 TEST_CASE(IntArrayMessage) { | 1072 TEST_CASE(IntArrayMessage) { |
| 1073 Zone zone(Isolate::Current()); | 1073 StackZone zone(Isolate::Current()); |
| 1074 uint8_t* buffer = NULL; | 1074 uint8_t* buffer = NULL; |
| 1075 ApiMessageWriter writer(&buffer, &zone_allocator); | 1075 ApiMessageWriter writer(&buffer, &zone_allocator); |
| 1076 | 1076 |
| 1077 static const int kArrayLength = 2; | 1077 static const int kArrayLength = 2; |
| 1078 intptr_t data[kArrayLength] = {1, 2}; | 1078 intptr_t data[kArrayLength] = {1, 2}; |
| 1079 int len = kArrayLength; | 1079 int len = kArrayLength; |
| 1080 writer.WriteMessage(len, data); | 1080 writer.WriteMessage(len, data); |
| 1081 | 1081 |
| 1082 // Read object back from the snapshot into a C structure. | 1082 // Read object back from the snapshot into a C structure. |
| 1083 ApiNativeScope scope; | 1083 ApiNativeScope scope; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1146 EXPECT_VALID(bigint_result); | 1146 EXPECT_VALID(bigint_result); |
| 1147 Dart_Handle string_result; | 1147 Dart_Handle string_result; |
| 1148 string_result = Dart_Invoke(lib, Dart_NewString("getString"), 0, NULL); | 1148 string_result = Dart_Invoke(lib, Dart_NewString("getString"), 0, NULL); |
| 1149 EXPECT_VALID(string_result); | 1149 EXPECT_VALID(string_result); |
| 1150 EXPECT(Dart_IsString(string_result)); | 1150 EXPECT(Dart_IsString(string_result)); |
| 1151 | 1151 |
| 1152 { | 1152 { |
| 1153 DARTSCOPE_NOCHECKS(isolate); | 1153 DARTSCOPE_NOCHECKS(isolate); |
| 1154 | 1154 |
| 1155 { | 1155 { |
| 1156 Zone zone(Isolate::Current()); | 1156 StackZone zone(Isolate::Current()); |
| 1157 uint8_t* buffer; | 1157 uint8_t* buffer; |
| 1158 MessageWriter writer(&buffer, &zone_allocator); | 1158 MessageWriter writer(&buffer, &zone_allocator); |
| 1159 Smi& smi = Smi::Handle(); | 1159 Smi& smi = Smi::Handle(); |
| 1160 smi ^= Api::UnwrapHandle(smi_result); | 1160 smi ^= Api::UnwrapHandle(smi_result); |
| 1161 writer.WriteMessage(smi); | 1161 writer.WriteMessage(smi); |
| 1162 intptr_t buffer_len = writer.BytesWritten(); | 1162 intptr_t buffer_len = writer.BytesWritten(); |
| 1163 | 1163 |
| 1164 // Read object back from the snapshot into a C structure. | 1164 // Read object back from the snapshot into a C structure. |
| 1165 ApiNativeScope scope; | 1165 ApiNativeScope scope; |
| 1166 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); | 1166 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); |
| 1167 Dart_CObject* root = api_reader.ReadMessage(); | 1167 Dart_CObject* root = api_reader.ReadMessage(); |
| 1168 EXPECT_NOTNULL(root); | 1168 EXPECT_NOTNULL(root); |
| 1169 EXPECT_EQ(Dart_CObject::kInt32, root->type); | 1169 EXPECT_EQ(Dart_CObject::kInt32, root->type); |
| 1170 EXPECT_EQ(42, root->value.as_int32); | 1170 EXPECT_EQ(42, root->value.as_int32); |
| 1171 CheckEncodeDecodeMessage(root); | 1171 CheckEncodeDecodeMessage(root); |
| 1172 } | 1172 } |
| 1173 { | 1173 { |
| 1174 Zone zone(Isolate::Current()); | 1174 StackZone zone(Isolate::Current()); |
| 1175 uint8_t* buffer; | 1175 uint8_t* buffer; |
| 1176 MessageWriter writer(&buffer, &zone_allocator); | 1176 MessageWriter writer(&buffer, &zone_allocator); |
| 1177 Bigint& bigint = Bigint::Handle(); | 1177 Bigint& bigint = Bigint::Handle(); |
| 1178 bigint ^= Api::UnwrapHandle(bigint_result); | 1178 bigint ^= Api::UnwrapHandle(bigint_result); |
| 1179 writer.WriteMessage(bigint); | 1179 writer.WriteMessage(bigint); |
| 1180 intptr_t buffer_len = writer.BytesWritten(); | 1180 intptr_t buffer_len = writer.BytesWritten(); |
| 1181 | 1181 |
| 1182 // Read object back from the snapshot into a C structure. | 1182 // Read object back from the snapshot into a C structure. |
| 1183 ApiNativeScope scope; | 1183 ApiNativeScope scope; |
| 1184 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); | 1184 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); |
| 1185 Dart_CObject* root = api_reader.ReadMessage(); | 1185 Dart_CObject* root = api_reader.ReadMessage(); |
| 1186 EXPECT_NOTNULL(root); | 1186 EXPECT_NOTNULL(root); |
| 1187 EXPECT_EQ(Dart_CObject::kBigint, root->type); | 1187 EXPECT_EQ(Dart_CObject::kBigint, root->type); |
| 1188 EXPECT_STREQ("-424242424242424242424242424242424242", | 1188 EXPECT_STREQ("-424242424242424242424242424242424242", |
| 1189 root->value.as_bigint); | 1189 root->value.as_bigint); |
| 1190 CheckEncodeDecodeMessage(root); | 1190 CheckEncodeDecodeMessage(root); |
| 1191 } | 1191 } |
| 1192 { | 1192 { |
| 1193 Zone zone(Isolate::Current()); | 1193 StackZone zone(Isolate::Current()); |
| 1194 uint8_t* buffer; | 1194 uint8_t* buffer; |
| 1195 MessageWriter writer(&buffer, &zone_allocator); | 1195 MessageWriter writer(&buffer, &zone_allocator); |
| 1196 String& str = String::Handle(); | 1196 String& str = String::Handle(); |
| 1197 str ^= Api::UnwrapHandle(string_result); | 1197 str ^= Api::UnwrapHandle(string_result); |
| 1198 writer.WriteMessage(str); | 1198 writer.WriteMessage(str); |
| 1199 intptr_t buffer_len = writer.BytesWritten(); | 1199 intptr_t buffer_len = writer.BytesWritten(); |
| 1200 | 1200 |
| 1201 // Read object back from the snapshot into a C structure. | 1201 // Read object back from the snapshot into a C structure. |
| 1202 ApiNativeScope scope; | 1202 ApiNativeScope scope; |
| 1203 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); | 1203 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); |
| (...skipping 762 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1966 EXPECT(Dart_ErrorHasException(result)); | 1966 EXPECT(Dart_ErrorHasException(result)); |
| 1967 EXPECT_SUBSTRING("Exception: nulltruefalse1234563.14[]100123456789\n", | 1967 EXPECT_SUBSTRING("Exception: nulltruefalse1234563.14[]100123456789\n", |
| 1968 Dart_GetError(result)); | 1968 Dart_GetError(result)); |
| 1969 | 1969 |
| 1970 Dart_ExitScope(); | 1970 Dart_ExitScope(); |
| 1971 } | 1971 } |
| 1972 | 1972 |
| 1973 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). | 1973 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). |
| 1974 | 1974 |
| 1975 } // namespace dart | 1975 } // namespace dart |
| OLD | NEW |