| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "vm/snapshot.h" | 5 #include "vm/snapshot.h" |
| 6 | 6 |
| 7 #include "vm/assert.h" | 7 #include "vm/assert.h" |
| 8 #include "vm/bigint_operations.h" | 8 #include "vm/bigint_operations.h" |
| 9 #include "vm/class_finalizer.h" | 9 #include "vm/class_finalizer.h" |
| 10 #include "vm/unit_test.h" | 10 #include "vm/unit_test.h" |
| (...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 String& url = String::Handle(String::New("dart-test:SerializeScript")); | 268 String& url = String::Handle(String::New("dart-test:SerializeScript")); |
| 269 String& source = String::Handle(String::New(kScriptChars)); | 269 String& source = String::Handle(String::New(kScriptChars)); |
| 270 Script& script = Script::Handle(Script::New(url, source, RawScript::kSource)); | 270 Script& script = Script::Handle(Script::New(url, source, RawScript::kSource)); |
| 271 const String& lib_url = String::Handle(String::NewSymbol("TestLib")); | 271 const String& lib_url = String::Handle(String::NewSymbol("TestLib")); |
| 272 Library& lib = Library::Handle(Library::New(lib_url)); | 272 Library& lib = Library::Handle(Library::New(lib_url)); |
| 273 lib.Register(); | 273 lib.Register(); |
| 274 EXPECT(CompilerTest::TestCompileScript(lib, script)); | 274 EXPECT(CompilerTest::TestCompileScript(lib, script)); |
| 275 | 275 |
| 276 // Write snapshot with object content. | 276 // Write snapshot with object content. |
| 277 uint8_t* buffer; | 277 uint8_t* buffer; |
| 278 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator); | 278 SnapshotWriter writer(Snapshot::kScript, &buffer, &allocator); |
| 279 writer.WriteObject(script.raw()); | 279 writer.WriteObject(script.raw()); |
| 280 writer.FinalizeBuffer(); | 280 writer.FinalizeBuffer(); |
| 281 | 281 |
| 282 // Create a snapshot object using the buffer. | 282 // Create a snapshot object using the buffer. |
| 283 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | 283 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); |
| 284 | 284 |
| 285 // Read object back from the snapshot. | 285 // Read object back from the snapshot. |
| 286 Isolate* isolate= Isolate::Current(); | 286 Isolate* isolate= Isolate::Current(); |
| 287 SnapshotReader reader(snapshot, isolate->heap(), isolate->object_store()); | 287 SnapshotReader reader(snapshot, isolate->heap(), isolate->object_store()); |
| 288 Script& serialized_script = Script::Handle(); | 288 Script& serialized_script = Script::Handle(); |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 435 EXPECT_VALID(result); | 435 EXPECT_VALID(result); |
| 436 Dart_ExitScope(); | 436 Dart_ExitScope(); |
| 437 } | 437 } |
| 438 Dart_ShutdownIsolate(); | 438 Dart_ShutdownIsolate(); |
| 439 free(buffer); | 439 free(buffer); |
| 440 } | 440 } |
| 441 | 441 |
| 442 | 442 |
| 443 UNIT_TEST_CASE(ScriptSnapshot) { | 443 UNIT_TEST_CASE(ScriptSnapshot) { |
| 444 const char* kScriptChars = | 444 const char* kScriptChars = |
| 445 "class Fields {\n" | 445 "class Fields {" |
| 446 " Fields(int i, int j) : fld1 = i, fld2 = j {}\n" | 446 " Fields(int i, int j) : fld1 = i, fld2 = j {}" |
| 447 " int fld1;\n" | 447 " int fld1;" |
| 448 " final int fld2;\n" | 448 " final int fld2;" |
| 449 " static int fld3;\n" | 449 " static int fld3;" |
| 450 " static final int fld4 = 10;\n" | 450 " static final int fld4 = 10;" |
| 451 "}\n" | 451 "}" |
| 452 "class FieldsTest {\n" | 452 "class FieldsTest {" |
| 453 " static Fields testMain() {\n" | 453 " static Fields testMain() {" |
| 454 " Fields obj = new Fields(10, 20);\n" | 454 " Fields obj = new Fields(10, 20);" |
| 455 " return obj;\n" | 455 " Fields.fld3 = 100;" |
| 456 " }\n" | 456 " if (obj === null) {" |
| 457 "}\n"; | 457 " throw new Exception('Allocation failure');" |
| 458 " }" |
| 459 " if (obj.fld1 != 10) {" |
| 460 " throw new Exception('fld1 needs to be 10');" |
| 461 " }" |
| 462 " if (obj.fld2 != 20) {" |
| 463 " throw new Exception('fld2 needs to be 20');" |
| 464 " }" |
| 465 " if (Fields.fld3 != 100) {" |
| 466 " throw new Exception('Fields.fld3 needs to be 100');" |
| 467 " }" |
| 468 " if (Fields.fld4 != 10) {" |
| 469 " throw new Exception('Fields.fld4 needs to be 10');" |
| 470 " }" |
| 471 " return obj;" |
| 472 " }" |
| 473 "}"; |
| 458 Dart_Handle result; | 474 Dart_Handle result; |
| 459 | 475 |
| 460 uint8_t* buffer; | 476 uint8_t* buffer; |
| 461 intptr_t size; | 477 intptr_t size; |
| 478 uint8_t* full_snapshot = NULL; |
| 479 uint8_t* script_snapshot = NULL; |
| 462 | 480 |
| 463 // Start an Isolate, load a script and create a script snapshot. | |
| 464 { | 481 { |
| 482 // Start an Isolate, and create a full snapshot of it. |
| 465 TestIsolateScope __test_isolate__; | 483 TestIsolateScope __test_isolate__; |
| 466 Dart_EnterScope(); // Start a Dart API scope for invoking API functions. | 484 Dart_EnterScope(); // Start a Dart API scope for invoking API functions. |
| 467 | 485 |
| 486 // Write out the script snapshot. |
| 487 result = Dart_CreateSnapshot(&buffer, &size); |
| 488 EXPECT_VALID(result); |
| 489 full_snapshot = reinterpret_cast<uint8_t*>(malloc(size)); |
| 490 memmove(full_snapshot, buffer, size); |
| 491 Dart_ExitScope(); |
| 492 } |
| 493 |
| 494 { |
| 495 // Create an Isolate using the full snapshot, load a script and create |
| 496 // a script snapshot of the script. |
| 497 TestCase::CreateTestIsolateFromSnapshot(full_snapshot); |
| 498 Dart_EnterScope(); // Start a Dart API scope for invoking API functions. |
| 499 |
| 468 // Create a test library and Load up a test script in it. | 500 // Create a test library and Load up a test script in it. |
| 469 TestCase::LoadTestScript(kScriptChars, NULL); | 501 TestCase::LoadTestScript(kScriptChars, NULL); |
| 470 | 502 |
| 471 // Write out the script snapshot. | 503 // Write out the script snapshot. |
| 472 result = Dart_CreateScriptSnapshot(TestCase::lib(), &buffer, &size); | 504 result = Dart_CreateScriptSnapshot(TestCase::lib(), &buffer, &size); |
| 473 EXPECT_VALID(result); | 505 EXPECT_VALID(result); |
| 506 script_snapshot = reinterpret_cast<uint8_t*>(malloc(size)); |
| 507 memmove(script_snapshot, buffer, size); |
| 474 Dart_ExitScope(); | 508 Dart_ExitScope(); |
| 509 Dart_ShutdownIsolate(); |
| 475 } | 510 } |
| 476 | 511 |
| 477 // Now Create another isolate and load the application snapshot and | |
| 478 // execute it. | |
| 479 { | 512 { |
| 480 TestIsolateScope __test_isolate__; | 513 // Now Create an Isolate using the full snapshot and load the |
| 514 // script snapshot created above and execute it. |
| 515 TestCase::CreateTestIsolateFromSnapshot(full_snapshot); |
| 481 Dart_EnterScope(); // Start a Dart API scope for invoking API functions. | 516 Dart_EnterScope(); // Start a Dart API scope for invoking API functions. |
| 482 | 517 |
| 483 // Load the test library from the snapshot. | 518 // Load the test library from the snapshot. |
| 484 result = Dart_LoadScriptFromSnapshot(buffer); | 519 EXPECT(script_snapshot != NULL); |
| 520 result = Dart_LoadScriptFromSnapshot(script_snapshot); |
| 485 EXPECT_VALID(result); | 521 EXPECT_VALID(result); |
| 486 | 522 |
| 487 // Invoke a function which returns an object. | 523 // Invoke a function which returns an object. |
| 488 result = Dart_InvokeStatic(result, | 524 result = Dart_InvokeStatic(result, |
| 489 Dart_NewString("FieldsTest"), | 525 Dart_NewString("FieldsTest"), |
| 490 Dart_NewString("testMain"), | 526 Dart_NewString("testMain"), |
| 491 0, | 527 0, |
| 492 NULL); | 528 NULL); |
| 493 EXPECT_VALID(result); | 529 EXPECT_VALID(result); |
| 494 Dart_ExitScope(); | 530 Dart_ExitScope(); |
| 495 } | 531 } |
| 496 Dart_ShutdownIsolate(); | 532 Dart_ShutdownIsolate(); |
| 497 free(buffer); | 533 free(full_snapshot); |
| 534 free(script_snapshot); |
| 498 } | 535 } |
| 499 | 536 |
| 500 #endif // TARGET_ARCH_IA32. | 537 #endif // TARGET_ARCH_IA32. |
| 501 | 538 |
| 502 } // namespace dart | 539 } // namespace dart |
| OLD | NEW |