OLD | NEW |
1 // Copyright 2007-2010 the V8 project authors. All rights reserved. | 1 // Copyright 2007-2010 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 v8::HandleScope scope(isolate); | 149 v8::HandleScope scope(isolate); |
150 v8::Context::New(isolate); | 150 v8::Context::New(isolate); |
151 } | 151 } |
152 | 152 |
153 Isolate* internal_isolate = reinterpret_cast<Isolate*>(isolate); | 153 Isolate* internal_isolate = reinterpret_cast<Isolate*>(isolate); |
154 internal_isolate->heap()->CollectAllAvailableGarbage("serialize"); | 154 internal_isolate->heap()->CollectAllAvailableGarbage("serialize"); |
155 WriteToFile(internal_isolate, FLAG_testing_serialization_file); | 155 WriteToFile(internal_isolate, FLAG_testing_serialization_file); |
156 } | 156 } |
157 | 157 |
158 | 158 |
| 159 Vector<const uint8_t> ConstructSource(Vector<const uint8_t> head, |
| 160 Vector<const uint8_t> body, |
| 161 Vector<const uint8_t> tail, int repeats) { |
| 162 int source_length = head.length() + body.length() * repeats + tail.length(); |
| 163 uint8_t* source = NewArray<uint8_t>(static_cast<size_t>(source_length)); |
| 164 CopyChars(source, head.start(), head.length()); |
| 165 for (int i = 0; i < repeats; i++) { |
| 166 CopyChars(source + head.length() + i * body.length(), body.start(), |
| 167 body.length()); |
| 168 } |
| 169 CopyChars(source + head.length() + repeats * body.length(), tail.start(), |
| 170 tail.length()); |
| 171 return Vector<const uint8_t>(const_cast<const uint8_t*>(source), |
| 172 source_length); |
| 173 } |
| 174 |
| 175 |
159 // Test that the whole heap can be serialized. | 176 // Test that the whole heap can be serialized. |
160 UNINITIALIZED_TEST(Serialize) { | 177 UNINITIALIZED_TEST(Serialize) { |
161 if (!Snapshot::HaveASnapshotToStartFrom()) { | 178 if (!Snapshot::HaveASnapshotToStartFrom()) { |
162 v8::Isolate::CreateParams params; | 179 v8::Isolate::CreateParams params; |
163 params.enable_serializer = true; | 180 params.enable_serializer = true; |
164 v8::Isolate* isolate = v8::Isolate::New(params); | 181 v8::Isolate* isolate = v8::Isolate::New(params); |
165 Serialize(isolate); | 182 Serialize(isolate); |
166 } | 183 } |
167 } | 184 } |
168 | 185 |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
293 const char* c_source = "\"1234\".length"; | 310 const char* c_source = "\"1234\".length"; |
294 v8::Local<v8::String> source = v8::String::NewFromUtf8(isolate, c_source); | 311 v8::Local<v8::String> source = v8::String::NewFromUtf8(isolate, c_source); |
295 v8::Local<v8::Script> script = v8::Script::Compile(source); | 312 v8::Local<v8::Script> script = v8::Script::Compile(source); |
296 CHECK_EQ(4, script->Run()->Int32Value()); | 313 CHECK_EQ(4, script->Run()->Int32Value()); |
297 } | 314 } |
298 isolate->Dispose(); | 315 isolate->Dispose(); |
299 } | 316 } |
300 } | 317 } |
301 | 318 |
302 | 319 |
| 320 // Test that the whole heap can be serialized. |
| 321 UNINITIALIZED_TEST(SerializeMultiplePages) { |
| 322 if (!Snapshot::HaveASnapshotToStartFrom()) { |
| 323 v8::Isolate::CreateParams params; |
| 324 params.enable_serializer = true; |
| 325 v8::Isolate* isolate = v8::Isolate::New(params); |
| 326 { |
| 327 v8::Isolate::Scope isolate_scope(isolate); |
| 328 v8::HandleScope handle_scope(isolate); |
| 329 v8::Local<v8::Context> context = v8::Context::New(isolate); |
| 330 v8::Context::Scope context_scope(context); |
| 331 Vector<const uint8_t> source = ConstructSource( |
| 332 STATIC_CHAR_VECTOR("var s='"), STATIC_CHAR_VECTOR("A"), |
| 333 STATIC_CHAR_VECTOR("';"), Page::kMaxRegularHeapObjectSize - 100); |
| 334 v8::Handle<v8::String> source_str = v8::String::NewFromOneByte( |
| 335 isolate, source.start(), v8::String::kNormalString, source.length()); |
| 336 CompileRun(source_str); |
| 337 } |
| 338 |
| 339 Isolate* internal_isolate = reinterpret_cast<Isolate*>(isolate); |
| 340 internal_isolate->heap()->CollectAllAvailableGarbage("serialize"); |
| 341 WriteToFile(internal_isolate, FLAG_testing_serialization_file); |
| 342 } |
| 343 } |
| 344 |
| 345 |
303 UNINITIALIZED_TEST(PartialSerialization) { | 346 UNINITIALIZED_TEST(PartialSerialization) { |
304 if (!Snapshot::HaveASnapshotToStartFrom()) { | 347 if (!Snapshot::HaveASnapshotToStartFrom()) { |
305 v8::Isolate::CreateParams params; | 348 v8::Isolate::CreateParams params; |
306 params.enable_serializer = true; | 349 params.enable_serializer = true; |
307 v8::Isolate* v8_isolate = v8::Isolate::New(params); | 350 v8::Isolate* v8_isolate = v8::Isolate::New(params); |
308 Isolate* isolate = reinterpret_cast<Isolate*>(v8_isolate); | 351 Isolate* isolate = reinterpret_cast<Isolate*>(v8_isolate); |
309 v8_isolate->Enter(); | 352 v8_isolate->Enter(); |
310 { | 353 { |
311 Heap* heap = isolate->heap(); | 354 Heap* heap = isolate->heap(); |
312 | 355 |
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
539 } | 582 } |
540 } | 583 } |
541 | 584 |
542 | 585 |
543 UNINITIALIZED_TEST(CustomContextSerialization) { | 586 UNINITIALIZED_TEST(CustomContextSerialization) { |
544 if (!Snapshot::HaveASnapshotToStartFrom()) { | 587 if (!Snapshot::HaveASnapshotToStartFrom()) { |
545 v8::Isolate::CreateParams params; | 588 v8::Isolate::CreateParams params; |
546 params.enable_serializer = true; | 589 params.enable_serializer = true; |
547 v8::Isolate* v8_isolate = v8::Isolate::New(params); | 590 v8::Isolate* v8_isolate = v8::Isolate::New(params); |
548 Isolate* isolate = reinterpret_cast<Isolate*>(v8_isolate); | 591 Isolate* isolate = reinterpret_cast<Isolate*>(v8_isolate); |
549 Heap* heap = isolate->heap(); | |
550 { | 592 { |
551 v8::Isolate::Scope isolate_scope(v8_isolate); | 593 v8::Isolate::Scope isolate_scope(v8_isolate); |
552 | 594 |
553 v8::Persistent<v8::Context> env; | 595 v8::Persistent<v8::Context> env; |
554 { | 596 { |
555 HandleScope scope(isolate); | 597 HandleScope scope(isolate); |
556 env.Reset(v8_isolate, v8::Context::New(v8_isolate)); | 598 env.Reset(v8_isolate, v8::Context::New(v8_isolate)); |
557 } | 599 } |
558 DCHECK(!env.IsEmpty()); | 600 DCHECK(!env.IsEmpty()); |
559 { | 601 { |
560 v8::HandleScope handle_scope(v8_isolate); | 602 v8::HandleScope handle_scope(v8_isolate); |
561 v8::Local<v8::Context>::New(v8_isolate, env)->Enter(); | 603 v8::Local<v8::Context>::New(v8_isolate, env)->Enter(); |
562 // After execution, e's function context refers to the global object. | 604 // After execution, e's function context refers to the global object. |
563 CompileRun( | 605 CompileRun( |
564 "var e;" | 606 "var e;" |
565 "(function() {" | 607 "(function() {" |
566 " e = function(s) { return eval (s); }" | 608 " e = function(s) { return eval (s); }" |
567 "})();" | 609 "})();" |
568 "var o = this;" | 610 "var o = this;" |
569 "var r = Math.random() + Math.cos(0);" | 611 "var r = Math.random() + Math.cos(0);" |
570 "var f = (function(a, b) { return a + b; }).bind(1, 2, 3);" | 612 "var f = (function(a, b) { return a + b; }).bind(1, 2, 3);" |
571 "var s = parseInt('12345');"); | 613 "var s = parseInt('12345');"); |
| 614 |
| 615 Vector<const uint8_t> source = ConstructSource( |
| 616 STATIC_CHAR_VECTOR("function g() { return [,"), |
| 617 STATIC_CHAR_VECTOR("1,"), |
| 618 STATIC_CHAR_VECTOR("];} a = g(); b = g(); b.push(1);"), 100000); |
| 619 v8::Handle<v8::String> source_str = v8::String::NewFromOneByte( |
| 620 v8_isolate, source.start(), v8::String::kNormalString, |
| 621 source.length()); |
| 622 CompileRun(source_str); |
| 623 source.Dispose(); |
572 } | 624 } |
573 // Make sure all builtin scripts are cached. | 625 // Make sure all builtin scripts are cached. |
574 { | 626 { |
575 HandleScope scope(isolate); | 627 HandleScope scope(isolate); |
576 for (int i = 0; i < Natives::GetBuiltinsCount(); i++) { | 628 for (int i = 0; i < Natives::GetBuiltinsCount(); i++) { |
577 isolate->bootstrapper()->NativesSourceLookup(i); | 629 isolate->bootstrapper()->NativesSourceLookup(i); |
578 } | 630 } |
579 } | 631 } |
580 // If we don't do this then we end up with a stray root pointing at the | 632 // If we don't do this then we end up with a stray root pointing at the |
581 // context even after we have disposed of env. | 633 // context even after we have disposed of env. |
582 heap->CollectAllGarbage(Heap::kNoGCFlags); | 634 isolate->heap()->CollectAllAvailableGarbage("snapshotting"); |
583 | 635 |
584 int file_name_length = StrLength(FLAG_testing_serialization_file) + 10; | 636 int file_name_length = StrLength(FLAG_testing_serialization_file) + 10; |
585 Vector<char> startup_name = Vector<char>::New(file_name_length + 1); | 637 Vector<char> startup_name = Vector<char>::New(file_name_length + 1); |
586 SNPrintF(startup_name, "%s.startup", FLAG_testing_serialization_file); | 638 SNPrintF(startup_name, "%s.startup", FLAG_testing_serialization_file); |
587 | 639 |
588 { | 640 { |
589 v8::HandleScope handle_scope(v8_isolate); | 641 v8::HandleScope handle_scope(v8_isolate); |
590 v8::Local<v8::Context>::New(v8_isolate, env)->Exit(); | 642 v8::Local<v8::Context>::New(v8_isolate, env)->Exit(); |
591 } | 643 } |
592 | 644 |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
660 v8::Handle<v8::Context> v8_context = v8::Utils::ToLocal(context); | 712 v8::Handle<v8::Context> v8_context = v8::Utils::ToLocal(context); |
661 v8::Context::Scope context_scope(v8_context); | 713 v8::Context::Scope context_scope(v8_context); |
662 double r = CompileRun("r")->ToNumber(v8_isolate)->Value(); | 714 double r = CompileRun("r")->ToNumber(v8_isolate)->Value(); |
663 CHECK(r >= 1 && r <= 2); | 715 CHECK(r >= 1 && r <= 2); |
664 int f = CompileRun("f()")->ToNumber(v8_isolate)->Int32Value(); | 716 int f = CompileRun("f()")->ToNumber(v8_isolate)->Int32Value(); |
665 CHECK_EQ(5, f); | 717 CHECK_EQ(5, f); |
666 f = CompileRun("e('f()')")->ToNumber(v8_isolate)->Int32Value(); | 718 f = CompileRun("e('f()')")->ToNumber(v8_isolate)->Int32Value(); |
667 CHECK_EQ(5, f); | 719 CHECK_EQ(5, f); |
668 v8::Handle<v8::String> s = CompileRun("s")->ToString(v8_isolate); | 720 v8::Handle<v8::String> s = CompileRun("s")->ToString(v8_isolate); |
669 CHECK(s->Equals(v8_str("12345"))); | 721 CHECK(s->Equals(v8_str("12345"))); |
| 722 int a = CompileRun("a.length")->ToNumber(v8_isolate)->Int32Value(); |
| 723 CHECK_EQ(100001, a); |
| 724 int b = CompileRun("b.length")->ToNumber(v8_isolate)->Int32Value(); |
| 725 CHECK_EQ(100002, b); |
670 } | 726 } |
671 } | 727 } |
672 v8_isolate->Dispose(); | 728 v8_isolate->Dispose(); |
673 } | 729 } |
674 } | 730 } |
675 | 731 |
676 | 732 |
677 TEST(TestThatAlwaysSucceeds) { | 733 TEST(TestThatAlwaysSucceeds) { |
678 } | 734 } |
679 | 735 |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
812 Handle<String> expected = | 868 Handle<String> expected = |
813 isolate->factory()->NewStringFromAsciiChecked("string1"); | 869 isolate->factory()->NewStringFromAsciiChecked("string1"); |
814 | 870 |
815 CHECK(Handle<String>::cast(copy_result)->Equals(*expected)); | 871 CHECK(Handle<String>::cast(copy_result)->Equals(*expected)); |
816 CHECK_EQ(builtins_count, CountBuiltins()); | 872 CHECK_EQ(builtins_count, CountBuiltins()); |
817 | 873 |
818 delete cache; | 874 delete cache; |
819 } | 875 } |
820 | 876 |
821 | 877 |
822 Vector<const uint8_t> ConstructSource(Vector<const uint8_t> head, | |
823 Vector<const uint8_t> body, | |
824 Vector<const uint8_t> tail, int repeats) { | |
825 int source_length = head.length() + body.length() * repeats + tail.length(); | |
826 uint8_t* source = NewArray<uint8_t>(static_cast<size_t>(source_length)); | |
827 CopyChars(source, head.start(), head.length()); | |
828 for (int i = 0; i < repeats; i++) { | |
829 CopyChars(source + head.length() + i * body.length(), body.start(), | |
830 body.length()); | |
831 } | |
832 CopyChars(source + head.length() + repeats * body.length(), tail.start(), | |
833 tail.length()); | |
834 return Vector<const uint8_t>(const_cast<const uint8_t*>(source), | |
835 source_length); | |
836 } | |
837 | |
838 | |
839 TEST(SerializeToplevelLargeCodeObject) { | 878 TEST(SerializeToplevelLargeCodeObject) { |
840 FLAG_serialize_toplevel = true; | 879 FLAG_serialize_toplevel = true; |
841 LocalContext context; | 880 LocalContext context; |
842 Isolate* isolate = CcTest::i_isolate(); | 881 Isolate* isolate = CcTest::i_isolate(); |
843 isolate->compilation_cache()->Disable(); // Disable same-isolate code cache. | 882 isolate->compilation_cache()->Disable(); // Disable same-isolate code cache. |
844 | 883 |
845 v8::HandleScope scope(CcTest::isolate()); | 884 v8::HandleScope scope(CcTest::isolate()); |
846 | 885 |
847 Vector<const uint8_t> source = | 886 Vector<const uint8_t> source = |
848 ConstructSource(STATIC_CHAR_VECTOR("var j=1; try { if (j) throw 1;"), | 887 ConstructSource(STATIC_CHAR_VECTOR("var j=1; try { if (j) throw 1;"), |
(...skipping 511 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1360 { | 1399 { |
1361 DisallowCompilation no_compile(reinterpret_cast<Isolate*>(isolate2)); | 1400 DisallowCompilation no_compile(reinterpret_cast<Isolate*>(isolate2)); |
1362 script = v8::ScriptCompiler::CompileUnbound( | 1401 script = v8::ScriptCompiler::CompileUnbound( |
1363 isolate2, &source, v8::ScriptCompiler::kConsumeCodeCache); | 1402 isolate2, &source, v8::ScriptCompiler::kConsumeCodeCache); |
1364 } | 1403 } |
1365 v8::Local<v8::Value> result = script->BindToCurrentContext()->Run(); | 1404 v8::Local<v8::Value> result = script->BindToCurrentContext()->Run(); |
1366 CHECK(result->ToString(isolate2)->Equals(v8_str("XY"))); | 1405 CHECK(result->ToString(isolate2)->Equals(v8_str("XY"))); |
1367 } | 1406 } |
1368 isolate2->Dispose(); | 1407 isolate2->Dispose(); |
1369 } | 1408 } |
OLD | NEW |