Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(279)

Side by Side Diff: runtime/vm/snapshot_test.cc

Issue 1285293004: Migrate most uses of StackZone(Isolate*) to Thread*. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Final cleanup. Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « runtime/vm/scavenger.cc ('k') | runtime/vm/unit_test.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
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);
149 const bool result = writer.WriteCMessage(root); 149 const bool result = writer.WriteCMessage(root);
150 EXPECT_EQ(false, result); 150 EXPECT_EQ(false, result);
151 } 151 }
152 152
153 153
154 TEST_CASE(SerializeNull) { 154 TEST_CASE(SerializeNull) {
155 StackZone zone(Isolate::Current()); 155 StackZone zone(Thread::Current());
156 156
157 // Write snapshot with object content. 157 // Write snapshot with object content.
158 const Object& null_object = Object::Handle(); 158 const Object& null_object = Object::Handle();
159 uint8_t* buffer; 159 uint8_t* buffer;
160 MessageWriter writer(&buffer, &zone_allocator, true); 160 MessageWriter writer(&buffer, &zone_allocator, true);
161 writer.WriteMessage(null_object); 161 writer.WriteMessage(null_object);
162 intptr_t buffer_len = writer.BytesWritten(); 162 intptr_t buffer_len = writer.BytesWritten();
163 163
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, &zone_allocator);
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(Isolate::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, &zone_allocator);
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(Isolate::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,
(...skipping 16 matching lines...) Expand all
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(Isolate::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, &zone_allocator);
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(Isolate::Current()); 269 StackZone zone(Thread::Current());
270 270
271 Mint& mint = Mint::Handle(); 271 Mint& mint = Mint::Handle();
272 mint ^= Integer::New(value); 272 mint ^= Integer::New(value);
273 ApiNativeScope scope; 273 ApiNativeScope scope;
274 Dart_CObject* mint_cobject = SerializeAndDeserializeMint(mint); 274 Dart_CObject* mint_cobject = SerializeAndDeserializeMint(mint);
275 // On 64-bit platforms mints always require 64-bits as the smi range 275 // On 64-bit platforms mints always require 64-bits as the smi range
276 // here covers most of the 64-bit range. On 32-bit platforms the smi 276 // here covers most of the 64-bit range. On 32-bit platforms the smi
277 // range covers most of the 32-bit range and values outside that 277 // range covers most of the 32-bit range and values outside that
278 // range are also represented as mints. 278 // range are also represented as mints.
279 #if defined(ARCH_IS_64_BIT) 279 #if defined(ARCH_IS_64_BIT)
(...skipping 25 matching lines...) Expand all
305 // Max positive mint - 1. 305 // Max positive mint - 1.
306 CheckMint(kMaxInt64 - 1); 306 CheckMint(kMaxInt64 - 1);
307 // Min negative mint. 307 // Min negative mint.
308 CheckMint(kMinInt64); 308 CheckMint(kMinInt64);
309 // Min negative mint + 1. 309 // Min negative mint + 1.
310 CheckMint(kMinInt64 + 1); 310 CheckMint(kMinInt64 + 1);
311 } 311 }
312 312
313 313
314 TEST_CASE(SerializeDouble) { 314 TEST_CASE(SerializeDouble) {
315 StackZone zone(Isolate::Current()); 315 StackZone zone(Thread::Current());
316 316
317 // Write snapshot with object content. 317 // Write snapshot with object content.
318 const Double& dbl = Double::Handle(Double::New(101.29)); 318 const Double& dbl = Double::Handle(Double::New(101.29));
319 uint8_t* buffer; 319 uint8_t* buffer;
320 MessageWriter writer(&buffer, &zone_allocator, true); 320 MessageWriter writer(&buffer, &zone_allocator, true);
321 writer.WriteMessage(dbl); 321 writer.WriteMessage(dbl);
322 intptr_t buffer_len = writer.BytesWritten(); 322 intptr_t buffer_len = writer.BytesWritten();
323 323
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, &zone_allocator);
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(Isolate::Current()); 344 StackZone zone(Thread::Current());
345 345
346 // Write snapshot with true object. 346 // Write snapshot with true object.
347 const Bool& bl = Bool::True(); 347 const Bool& bl = Bool::True();
348 uint8_t* buffer; 348 uint8_t* buffer;
349 MessageWriter writer(&buffer, &zone_allocator, true); 349 MessageWriter writer(&buffer, &zone_allocator, true);
350 writer.WriteMessage(bl); 350 writer.WriteMessage(bl);
351 intptr_t buffer_len = writer.BytesWritten(); 351 intptr_t buffer_len = writer.BytesWritten();
352 352
353 // Read object back from the snapshot. 353 // Read object back from the snapshot.
354 MessageSnapshotReader reader(buffer, 354 MessageSnapshotReader reader(buffer,
(...skipping 10 matching lines...) Expand all
365 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); 365 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator);
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(Isolate::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,
(...skipping 13 matching lines...) Expand all
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));
405 } 405 }
406 406
407 407
408 TEST_CASE(SerializeCapability) { 408 TEST_CASE(SerializeCapability) {
409 StackZone zone(Isolate::Current()); 409 StackZone zone(Thread::Current());
410 410
411 // Write snapshot with object content. 411 // Write snapshot with object content.
412 const Capability& capability = Capability::Handle(Capability::New(12345)); 412 const Capability& capability = Capability::Handle(Capability::New(12345));
413 uint8_t* buffer; 413 uint8_t* buffer;
414 MessageWriter writer(&buffer, &zone_allocator, true); 414 MessageWriter writer(&buffer, &zone_allocator, true);
415 writer.WriteMessage(capability); 415 writer.WriteMessage(capability);
416 intptr_t buffer_len = writer.BytesWritten(); 416 intptr_t buffer_len = writer.BytesWritten();
417 417
418 // Read object back from the snapshot. 418 // Read object back from the snapshot.
419 MessageSnapshotReader reader(buffer, 419 MessageSnapshotReader reader(buffer,
(...skipping 11 matching lines...) Expand all
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) {
441 StackZone zone(Isolate::Current()); 441 StackZone zone(Thread::Current());
442 442
443 // Write snapshot with object content. 443 // Write snapshot with object content.
444 const char* cstr = "0x270FFFFFFFFFFFFFD8F0"; 444 const char* cstr = "0x270FFFFFFFFFFFFFD8F0";
445 const String& str = String::Handle(String::New(cstr)); 445 const String& str = String::Handle(String::New(cstr));
446 Bigint& bigint = Bigint::Handle(); 446 Bigint& bigint = Bigint::Handle();
447 bigint ^= Integer::NewCanonical(str); 447 bigint ^= Integer::NewCanonical(str);
448 uint8_t* buffer; 448 uint8_t* buffer;
449 MessageWriter writer(&buffer, &zone_allocator, true); 449 MessageWriter writer(&buffer, &zone_allocator, true);
450 writer.WriteMessage(bigint); 450 writer.WriteMessage(bigint);
451 intptr_t buffer_len = writer.BytesWritten(); 451 intptr_t buffer_len = writer.BytesWritten();
(...skipping 23 matching lines...) Expand all
475 475
476 Dart_CObject* SerializeAndDeserializeBigint(const Bigint& bigint) { 476 Dart_CObject* SerializeAndDeserializeBigint(const Bigint& bigint) {
477 // Write snapshot with object content. 477 // Write snapshot with object content.
478 uint8_t* buffer; 478 uint8_t* buffer;
479 MessageWriter writer(&buffer, &zone_allocator, true); 479 MessageWriter writer(&buffer, &zone_allocator, true);
480 writer.WriteMessage(bigint); 480 writer.WriteMessage(bigint);
481 intptr_t buffer_len = writer.BytesWritten(); 481 intptr_t buffer_len = writer.BytesWritten();
482 482
483 { 483 {
484 // Switch to a regular zone, where VM handle allocation is allowed. 484 // Switch to a regular zone, where VM handle allocation is allowed.
485 StackZone zone(Isolate::Current()); 485 StackZone zone(Thread::Current());
486 // Read object back from the snapshot. 486 // Read object back from the snapshot.
487 MessageSnapshotReader reader(buffer, 487 MessageSnapshotReader reader(buffer,
488 buffer_len, 488 buffer_len,
489 Isolate::Current(), 489 Isolate::Current(),
490 Thread::Current()->zone()); 490 Thread::Current()->zone());
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, &zone_allocator);
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(Isolate::Current()); 511 StackZone zone(Thread::Current());
512 Bigint& bigint = Bigint::Handle(); 512 Bigint& bigint = Bigint::Handle();
513 bigint ^= Bigint::NewFromCString(bigint_value); 513 bigint ^= Bigint::NewFromCString(bigint_value);
514 ApiNativeScope scope; 514 ApiNativeScope scope;
515 Dart_CObject* bigint_cobject = SerializeAndDeserializeBigint(bigint); 515 Dart_CObject* bigint_cobject = SerializeAndDeserializeBigint(bigint);
516 EXPECT_EQ(Dart_CObject_kBigint, bigint_cobject->type); 516 EXPECT_EQ(Dart_CObject_kBigint, bigint_cobject->type);
517 char* hex_value = TestCase::BigintToHexValue(bigint_cobject); 517 char* hex_value = TestCase::BigintToHexValue(bigint_cobject);
518 EXPECT_STREQ(bigint_value, hex_value); 518 EXPECT_STREQ(bigint_value, hex_value);
519 free(hex_value); 519 free(hex_value);
520 } 520 }
521 521
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
566 EXPECT(Object::pc_descriptors_class() == reader.ReadObject()); 566 EXPECT(Object::pc_descriptors_class() == reader.ReadObject());
567 EXPECT(Object::exception_handlers_class() == reader.ReadObject()); 567 EXPECT(Object::exception_handlers_class() == reader.ReadObject());
568 EXPECT(Object::context_class() == reader.ReadObject()); 568 EXPECT(Object::context_class() == reader.ReadObject());
569 EXPECT(Object::context_scope_class() == reader.ReadObject()); 569 EXPECT(Object::context_scope_class() == reader.ReadObject());
570 570
571 free(buffer); 571 free(buffer);
572 } 572 }
573 573
574 574
575 static void TestString(const char* cstr) { 575 static void TestString(const char* cstr) {
576 StackZone zone(Isolate::Current()); 576 StackZone zone(Thread::Current());
577 EXPECT(Utf8::IsValid(reinterpret_cast<const uint8_t*>(cstr), strlen(cstr))); 577 EXPECT(Utf8::IsValid(reinterpret_cast<const uint8_t*>(cstr), strlen(cstr)));
578 // Write snapshot with object content. 578 // Write snapshot with object content.
579 String& str = String::Handle(String::New(cstr)); 579 String& str = String::Handle(String::New(cstr));
580 uint8_t* buffer; 580 uint8_t* buffer;
581 MessageWriter writer(&buffer, &zone_allocator, true); 581 MessageWriter writer(&buffer, &zone_allocator, true);
582 writer.WriteMessage(str); 582 writer.WriteMessage(str);
583 intptr_t buffer_len = writer.BytesWritten(); 583 intptr_t buffer_len = writer.BytesWritten();
584 584
585 // Read object back from the snapshot. 585 // Read object back from the snapshot.
586 MessageSnapshotReader reader(buffer, 586 MessageSnapshotReader reader(buffer,
(...skipping 23 matching lines...) Expand all
610 "\xDF\xBF" // U+07FF 610 "\xDF\xBF" // U+07FF
611 "\xE0\xA0\x80" // U+0800 611 "\xE0\xA0\x80" // U+0800
612 "\xEF\xBF\xBF"; // U+FFFF 612 "\xEF\xBF\xBF"; // U+FFFF
613 613
614 TestString(data); 614 TestString(data);
615 // TODO(sgjesse): Add tests with non-BMP characters. 615 // TODO(sgjesse): Add tests with non-BMP characters.
616 } 616 }
617 617
618 618
619 TEST_CASE(SerializeArray) { 619 TEST_CASE(SerializeArray) {
620 StackZone zone(Isolate::Current()); 620 StackZone zone(Thread::Current());
621 621
622 // Write snapshot with object content. 622 // Write snapshot with object content.
623 const int kArrayLength = 10; 623 const int kArrayLength = 10;
624 Array& array = Array::Handle(Array::New(kArrayLength)); 624 Array& array = Array::Handle(Array::New(kArrayLength));
625 Smi& smi = Smi::Handle(); 625 Smi& smi = Smi::Handle();
626 for (int i = 0; i < kArrayLength; i++) { 626 for (int i = 0; i < kArrayLength; i++) {
627 smi ^= Smi::New(i); 627 smi ^= Smi::New(i);
628 array.SetAt(i, smi); 628 array.SetAt(i, smi);
629 } 629 }
630 uint8_t* buffer; 630 uint8_t* buffer;
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
702 TEST_CASE(FailSerializeLargeExternalTypedData) { 702 TEST_CASE(FailSerializeLargeExternalTypedData) {
703 Dart_CObject root; 703 Dart_CObject root;
704 root.type = Dart_CObject_kExternalTypedData; 704 root.type = Dart_CObject_kExternalTypedData;
705 root.value.as_typed_data.length = 705 root.value.as_typed_data.length =
706 ExternalTypedData::MaxElements(kExternalTypedDataUint8ArrayCid) + 1; 706 ExternalTypedData::MaxElements(kExternalTypedDataUint8ArrayCid) + 1;
707 ExpectEncodeFail(&root); 707 ExpectEncodeFail(&root);
708 } 708 }
709 709
710 710
711 TEST_CASE(SerializeEmptyArray) { 711 TEST_CASE(SerializeEmptyArray) {
712 StackZone zone(Isolate::Current()); 712 StackZone zone(Thread::Current());
713 713
714 // Write snapshot with object content. 714 // Write snapshot with object content.
715 const int kArrayLength = 0; 715 const int kArrayLength = 0;
716 Array& array = Array::Handle(Array::New(kArrayLength)); 716 Array& array = Array::Handle(Array::New(kArrayLength));
717 uint8_t* buffer; 717 uint8_t* buffer;
718 MessageWriter writer(&buffer, &zone_allocator, true); 718 MessageWriter writer(&buffer, &zone_allocator, true);
719 writer.WriteMessage(array); 719 writer.WriteMessage(array);
720 intptr_t buffer_len = writer.BytesWritten(); 720 intptr_t buffer_len = writer.BytesWritten();
721 721
722 // Read object back from the snapshot. 722 // Read object back from the snapshot.
(...skipping 10 matching lines...) Expand all
733 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); 733 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator);
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(Isolate::Current()); 743 StackZone zone(Thread::Current());
744 744
745 // Write snapshot with object content. 745 // Write snapshot with object content.
746 const int kTypedDataLength = 256; 746 const int kTypedDataLength = 256;
747 TypedData& typed_data = TypedData::Handle( 747 TypedData& typed_data = TypedData::Handle(
748 TypedData::New(kTypedDataUint8ArrayCid, kTypedDataLength)); 748 TypedData::New(kTypedDataUint8ArrayCid, kTypedDataLength));
749 for (int i = 0; i < kTypedDataLength; i++) { 749 for (int i = 0; i < kTypedDataLength; i++) {
750 typed_data.SetUint8(i, i); 750 typed_data.SetUint8(i, i);
751 } 751 }
752 uint8_t* buffer; 752 uint8_t* buffer;
753 MessageWriter writer(&buffer, &zone_allocator, true); 753 MessageWriter writer(&buffer, &zone_allocator, true);
(...skipping 17 matching lines...) Expand all
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
779 #define TEST_TYPED_ARRAY(darttype, ctype) \ 779 #define TEST_TYPED_ARRAY(darttype, ctype) \
780 { \ 780 { \
781 StackZone zone(Isolate::Current()); \ 781 StackZone zone(Thread::Current()); \
782 const int kArrayLength = 127; \ 782 const int kArrayLength = 127; \
783 TypedData& array = TypedData::Handle( \ 783 TypedData& array = TypedData::Handle( \
784 TypedData::New(kTypedData##darttype##ArrayCid, kArrayLength)); \ 784 TypedData::New(kTypedData##darttype##ArrayCid, kArrayLength)); \
785 intptr_t scale = array.ElementSizeInBytes(); \ 785 intptr_t scale = array.ElementSizeInBytes(); \
786 for (int i = 0; i < kArrayLength; i++) { \ 786 for (int i = 0; i < kArrayLength; i++) { \
787 array.Set##darttype((i * scale), i); \ 787 array.Set##darttype((i * scale), i); \
788 } \ 788 } \
789 uint8_t* buffer; \ 789 uint8_t* buffer; \
790 MessageWriter writer(&buffer, &zone_allocator, true); \ 790 MessageWriter writer(&buffer, &zone_allocator, true); \
791 writer.WriteMessage(array); \ 791 writer.WriteMessage(array); \
792 intptr_t buffer_len = writer.BytesWritten(); \ 792 intptr_t buffer_len = writer.BytesWritten(); \
793 MessageSnapshotReader reader(buffer, buffer_len, \ 793 MessageSnapshotReader reader(buffer, buffer_len, \
794 Isolate::Current(), \ 794 Isolate::Current(), \
795 zone.GetZone()); \ 795 zone.GetZone()); \
796 TypedData& serialized_array = TypedData::Handle(); \ 796 TypedData& serialized_array = TypedData::Handle(); \
797 serialized_array ^= reader.ReadObject(); \ 797 serialized_array ^= reader.ReadObject(); \
798 for (int i = 0; i < kArrayLength; i++) { \ 798 for (int i = 0; i < kArrayLength; i++) { \
799 EXPECT_EQ(static_cast<ctype>(i), \ 799 EXPECT_EQ(static_cast<ctype>(i), \
800 serialized_array.Get##darttype(i*scale)); \ 800 serialized_array.Get##darttype(i*scale)); \
801 } \ 801 } \
802 } 802 }
803 803
804 804
805 #define TEST_EXTERNAL_TYPED_ARRAY(darttype, ctype) \ 805 #define TEST_EXTERNAL_TYPED_ARRAY(darttype, ctype) \
806 { \ 806 { \
807 StackZone zone(Isolate::Current()); \ 807 StackZone zone(Thread::Current()); \
808 ctype data[] = { 0, 11, 22, 33, 44, 55, 66, 77 }; \ 808 ctype data[] = { 0, 11, 22, 33, 44, 55, 66, 77 }; \
809 intptr_t length = ARRAY_SIZE(data); \ 809 intptr_t length = ARRAY_SIZE(data); \
810 ExternalTypedData& array = ExternalTypedData::Handle( \ 810 ExternalTypedData& array = ExternalTypedData::Handle( \
811 ExternalTypedData::New(kExternalTypedData##darttype##ArrayCid, \ 811 ExternalTypedData::New(kExternalTypedData##darttype##ArrayCid, \
812 reinterpret_cast<uint8_t*>(data), length)); \ 812 reinterpret_cast<uint8_t*>(data), length)); \
813 intptr_t scale = array.ElementSizeInBytes(); \ 813 intptr_t scale = array.ElementSizeInBytes(); \
814 uint8_t* buffer; \ 814 uint8_t* buffer; \
815 MessageWriter writer(&buffer, &zone_allocator, true); \ 815 MessageWriter writer(&buffer, &zone_allocator, true); \
816 writer.WriteMessage(array); \ 816 writer.WriteMessage(array); \
817 intptr_t buffer_len = writer.BytesWritten(); \ 817 intptr_t buffer_len = writer.BytesWritten(); \
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
849 TEST_EXTERNAL_TYPED_ARRAY(Int32, int32_t); 849 TEST_EXTERNAL_TYPED_ARRAY(Int32, int32_t);
850 TEST_EXTERNAL_TYPED_ARRAY(Uint32, uint32_t); 850 TEST_EXTERNAL_TYPED_ARRAY(Uint32, uint32_t);
851 TEST_EXTERNAL_TYPED_ARRAY(Int64, int64_t); 851 TEST_EXTERNAL_TYPED_ARRAY(Int64, int64_t);
852 TEST_EXTERNAL_TYPED_ARRAY(Uint64, uint64_t); 852 TEST_EXTERNAL_TYPED_ARRAY(Uint64, uint64_t);
853 TEST_EXTERNAL_TYPED_ARRAY(Float32, float); 853 TEST_EXTERNAL_TYPED_ARRAY(Float32, float);
854 TEST_EXTERNAL_TYPED_ARRAY(Float64, double); 854 TEST_EXTERNAL_TYPED_ARRAY(Float64, double);
855 } 855 }
856 856
857 857
858 TEST_CASE(SerializeEmptyByteArray) { 858 TEST_CASE(SerializeEmptyByteArray) {
859 StackZone zone(Isolate::Current()); 859 StackZone zone(Thread::Current());
860 860
861 // Write snapshot with object content. 861 // Write snapshot with object content.
862 const int kTypedDataLength = 0; 862 const int kTypedDataLength = 0;
863 TypedData& typed_data = TypedData::Handle( 863 TypedData& typed_data = TypedData::Handle(
864 TypedData::New(kTypedDataUint8ArrayCid, kTypedDataLength)); 864 TypedData::New(kTypedDataUint8ArrayCid, kTypedDataLength));
865 uint8_t* buffer; 865 uint8_t* buffer;
866 MessageWriter writer(&buffer, &zone_allocator, true); 866 MessageWriter writer(&buffer, &zone_allocator, true);
867 writer.WriteMessage(typed_data); 867 writer.WriteMessage(typed_data);
868 intptr_t buffer_len = writer.BytesWritten(); 868 intptr_t buffer_len = writer.BytesWritten();
869 869
(...skipping 839 matching lines...) Expand 10 before | Expand all | Expand 10 after
1709 result = Dart_Invoke(lib, NewString("test_b"), 0, NULL); 1709 result = Dart_Invoke(lib, NewString("test_b"), 0, NULL);
1710 EXPECT(Dart_IsError(result) == saved_enable_type_checks_mode); 1710 EXPECT(Dart_IsError(result) == saved_enable_type_checks_mode);
1711 } 1711 }
1712 Dart_ShutdownIsolate(); 1712 Dart_ShutdownIsolate();
1713 free(full_snapshot); 1713 free(full_snapshot);
1714 free(script_snapshot); 1714 free(script_snapshot);
1715 } 1715 }
1716 1716
1717 1717
1718 TEST_CASE(IntArrayMessage) { 1718 TEST_CASE(IntArrayMessage) {
1719 StackZone zone(Isolate::Current()); 1719 StackZone zone(Thread::Current());
1720 uint8_t* buffer = NULL; 1720 uint8_t* buffer = NULL;
1721 ApiMessageWriter writer(&buffer, &zone_allocator); 1721 ApiMessageWriter writer(&buffer, &zone_allocator);
1722 1722
1723 static const int kArrayLength = 2; 1723 static const int kArrayLength = 2;
1724 intptr_t data[kArrayLength] = {1, 2}; 1724 intptr_t data[kArrayLength] = {1, 2};
1725 int len = kArrayLength; 1725 int len = kArrayLength;
1726 writer.WriteMessage(len, data); 1726 writer.WriteMessage(len, data);
1727 1727
1728 // Read object back from the snapshot into a C structure. 1728 // Read object back from the snapshot into a C structure.
1729 ApiNativeScope scope; 1729 ApiNativeScope scope;
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
1762 1762
1763 // Helper function to deserialize the result into a Dart_CObject structure. 1763 // Helper function to deserialize the result into a Dart_CObject structure.
1764 static Dart_CObject* GetDeserialized(uint8_t* buffer, intptr_t buffer_len) { 1764 static Dart_CObject* GetDeserialized(uint8_t* buffer, intptr_t buffer_len) {
1765 // Read object back from the snapshot into a C structure. 1765 // Read object back from the snapshot into a C structure.
1766 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); 1766 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator);
1767 return api_reader.ReadMessage(); 1767 return api_reader.ReadMessage();
1768 } 1768 }
1769 1769
1770 1770
1771 static void CheckString(Dart_Handle dart_string, const char* expected) { 1771 static void CheckString(Dart_Handle dart_string, const char* expected) {
1772 StackZone zone(Isolate::Current()); 1772 StackZone zone(Thread::Current());
1773 String& str = String::Handle(); 1773 String& str = String::Handle();
1774 str ^= Api::UnwrapHandle(dart_string); 1774 str ^= Api::UnwrapHandle(dart_string);
1775 uint8_t* buffer; 1775 uint8_t* buffer;
1776 MessageWriter writer(&buffer, &zone_allocator, false); 1776 MessageWriter writer(&buffer, &zone_allocator, false);
1777 writer.WriteMessage(str); 1777 writer.WriteMessage(str);
1778 intptr_t buffer_len = writer.BytesWritten(); 1778 intptr_t buffer_len = writer.BytesWritten();
1779 1779
1780 // Read object back from the snapshot into a C structure. 1780 // Read object back from the snapshot into a C structure.
1781 ApiNativeScope scope; 1781 ApiNativeScope scope;
1782 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); 1782 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator);
1783 Dart_CObject* root = api_reader.ReadMessage(); 1783 Dart_CObject* root = api_reader.ReadMessage();
1784 EXPECT_NOTNULL(root); 1784 EXPECT_NOTNULL(root);
1785 EXPECT_EQ(Dart_CObject_kString, root->type); 1785 EXPECT_EQ(Dart_CObject_kString, root->type);
1786 EXPECT_STREQ(expected, root->value.as_string); 1786 EXPECT_STREQ(expected, root->value.as_string);
1787 CheckEncodeDecodeMessage(root); 1787 CheckEncodeDecodeMessage(root);
1788 } 1788 }
1789 1789
1790 1790
1791 static void CheckStringInvalid(Dart_Handle dart_string) { 1791 static void CheckStringInvalid(Dart_Handle dart_string) {
1792 StackZone zone(Isolate::Current()); 1792 StackZone zone(Thread::Current());
1793 String& str = String::Handle(); 1793 String& str = String::Handle();
1794 str ^= Api::UnwrapHandle(dart_string); 1794 str ^= Api::UnwrapHandle(dart_string);
1795 uint8_t* buffer; 1795 uint8_t* buffer;
1796 MessageWriter writer(&buffer, &zone_allocator, false); 1796 MessageWriter writer(&buffer, &zone_allocator, false);
1797 writer.WriteMessage(str); 1797 writer.WriteMessage(str);
1798 intptr_t buffer_len = writer.BytesWritten(); 1798 intptr_t buffer_len = writer.BytesWritten();
1799 1799
1800 // Read object back from the snapshot into a C structure. 1800 // Read object back from the snapshot into a C structure.
1801 ApiNativeScope scope; 1801 ApiNativeScope scope;
1802 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); 1802 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator);
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
1892 Dart_Handle crappy_string_result; 1892 Dart_Handle crappy_string_result;
1893 crappy_string_result = 1893 crappy_string_result =
1894 Dart_Invoke(lib, NewString("getCrappyString"), 0, NULL); 1894 Dart_Invoke(lib, NewString("getCrappyString"), 0, NULL);
1895 EXPECT_VALID(crappy_string_result); 1895 EXPECT_VALID(crappy_string_result);
1896 EXPECT(Dart_IsString(crappy_string_result)); 1896 EXPECT(Dart_IsString(crappy_string_result));
1897 1897
1898 { 1898 {
1899 DARTSCOPE(isolate); 1899 DARTSCOPE(isolate);
1900 1900
1901 { 1901 {
1902 StackZone zone(Isolate::Current()); 1902 StackZone zone(Thread::Current());
1903 Smi& smi = Smi::Handle(); 1903 Smi& smi = Smi::Handle();
1904 smi ^= Api::UnwrapHandle(smi_result); 1904 smi ^= Api::UnwrapHandle(smi_result);
1905 uint8_t* buffer; 1905 uint8_t* buffer;
1906 MessageWriter writer(&buffer, &zone_allocator, false); 1906 MessageWriter writer(&buffer, &zone_allocator, false);
1907 writer.WriteMessage(smi); 1907 writer.WriteMessage(smi);
1908 intptr_t buffer_len = writer.BytesWritten(); 1908 intptr_t buffer_len = writer.BytesWritten();
1909 1909
1910 // Read object back from the snapshot into a C structure. 1910 // Read object back from the snapshot into a C structure.
1911 ApiNativeScope scope; 1911 ApiNativeScope scope;
1912 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); 1912 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator);
1913 Dart_CObject* root = api_reader.ReadMessage(); 1913 Dart_CObject* root = api_reader.ReadMessage();
1914 EXPECT_NOTNULL(root); 1914 EXPECT_NOTNULL(root);
1915 EXPECT_EQ(Dart_CObject_kInt32, root->type); 1915 EXPECT_EQ(Dart_CObject_kInt32, root->type);
1916 EXPECT_EQ(42, root->value.as_int32); 1916 EXPECT_EQ(42, root->value.as_int32);
1917 CheckEncodeDecodeMessage(root); 1917 CheckEncodeDecodeMessage(root);
1918 } 1918 }
1919 { 1919 {
1920 StackZone zone(Isolate::Current()); 1920 StackZone zone(Thread::Current());
1921 Bigint& bigint = Bigint::Handle(); 1921 Bigint& bigint = Bigint::Handle();
1922 bigint ^= Api::UnwrapHandle(bigint_result); 1922 bigint ^= Api::UnwrapHandle(bigint_result);
1923 uint8_t* buffer; 1923 uint8_t* buffer;
1924 MessageWriter writer(&buffer, &zone_allocator, false); 1924 MessageWriter writer(&buffer, &zone_allocator, false);
1925 writer.WriteMessage(bigint); 1925 writer.WriteMessage(bigint);
1926 intptr_t buffer_len = writer.BytesWritten(); 1926 intptr_t buffer_len = writer.BytesWritten();
1927 1927
1928 // Read object back from the snapshot into a C structure. 1928 // Read object back from the snapshot into a C structure.
1929 ApiNativeScope scope; 1929 ApiNativeScope scope;
1930 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); 1930 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator);
(...skipping 1112 matching lines...) Expand 10 before | Expand all | Expand 10 after
3043 EXPECT(Dart_IsError(result)); 3043 EXPECT(Dart_IsError(result));
3044 EXPECT(Dart_ErrorHasException(result)); 3044 EXPECT(Dart_ErrorHasException(result));
3045 EXPECT_SUBSTRING("Exception: nulltruefalse123456æøå3.14[]100123456789\n", 3045 EXPECT_SUBSTRING("Exception: nulltruefalse123456æøå3.14[]100123456789\n",
3046 Dart_GetError(result)); 3046 Dart_GetError(result));
3047 3047
3048 Dart_ExitScope(); 3048 Dart_ExitScope();
3049 } 3049 }
3050 3050
3051 3051
3052 TEST_CASE(OmittedObjectEncodingLength) { 3052 TEST_CASE(OmittedObjectEncodingLength) {
3053 StackZone zone(Isolate::Current()); 3053 StackZone zone(Thread::Current());
3054 uint8_t* buffer; 3054 uint8_t* buffer;
3055 MessageWriter writer(&buffer, &zone_allocator, true); 3055 MessageWriter writer(&buffer, &zone_allocator, true);
3056 writer.WriteInlinedObjectHeader(kOmittedObjectId); 3056 writer.WriteInlinedObjectHeader(kOmittedObjectId);
3057 // For performance, we'd like single-byte headers when ids are omitted. 3057 // For performance, we'd like single-byte headers when ids are omitted.
3058 // If this starts failing, consider renumbering the snapshot ids. 3058 // If this starts failing, consider renumbering the snapshot ids.
3059 EXPECT_EQ(1, writer.BytesWritten()); 3059 EXPECT_EQ(1, writer.BytesWritten());
3060 } 3060 }
3061 3061
3062 } // namespace dart 3062 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/scavenger.cc ('k') | runtime/vm/unit_test.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698