1 // Copyright 2007-2010 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include "src/bootstrapper.h"
35 #include "src/compilation-cache.h"
36 #include "src/debug.h"
37 #include "src/heap/spaces.h"
38 #include "src/natives.h"
39 #include "src/objects.h"
40 #include "src/runtime/runtime.h"
41 #include "src/scopeinfo.h"
42 #include "src/serialize.h"
43 #include "src/snapshot.h"
44 #include "test/cctest/cctest.h"
46 using namespace v8::internal;
50 static Address AddressOf(T id) {
51 return ExternalReference(id, CcTest::i_isolate()).address();
56 static uint32_t Encode(const ExternalReferenceEncoder& encoder, T id) {
57 return encoder.Encode(AddressOf(id));
61 static uint32_t make_code(TypeCode type, int id) {
62 return static_cast<uint32_t>(type) << kReferenceTypeShift | id;
66 TEST(ExternalReferenceEncoder) {
67 Isolate* isolate = CcTest::i_isolate();
70 ExternalReferenceEncoder encoder(isolate);
71 CHECK_EQ(make_code(BUILTIN, Builtins::kArrayCode),
72 Encode(encoder, Builtins::kArrayCode));
73 CHECK_EQ(make_code(v8::internal::RUNTIME_FUNCTION, Runtime::kAbort),
74 Encode(encoder, Runtime::kAbort));
75 ExternalReference stack_limit_address =
76 ExternalReference::address_of_stack_limit(isolate);
77 CHECK_EQ(make_code(UNCLASSIFIED, 2),
78 encoder.Encode(stack_limit_address.address()));
79 ExternalReference real_stack_limit_address =
80 ExternalReference::address_of_real_stack_limit(isolate);
81 CHECK_EQ(make_code(UNCLASSIFIED, 3),
82 encoder.Encode(real_stack_limit_address.address()));
83 CHECK_EQ(make_code(UNCLASSIFIED, 8),
84 encoder.Encode(ExternalReference::debug_break(isolate).address()));
86 make_code(UNCLASSIFIED, 4),
87 encoder.Encode(ExternalReference::new_space_start(isolate).address()));
89 make_code(UNCLASSIFIED, 1),
90 encoder.Encode(ExternalReference::roots_array_start(isolate).address()));
91 CHECK_EQ(make_code(UNCLASSIFIED, 33),
92 encoder.Encode(ExternalReference::cpu_features().address()));
96 TEST(ExternalReferenceDecoder) {
97 Isolate* isolate = CcTest::i_isolate();
100 ExternalReferenceDecoder decoder(isolate);
101 CHECK_EQ(AddressOf(Builtins::kArrayCode),
102 decoder.Decode(make_code(BUILTIN, Builtins::kArrayCode)));
103 CHECK_EQ(AddressOf(Runtime::kAbort),
104 decoder.Decode(make_code(v8::internal::RUNTIME_FUNCTION,
106 CHECK_EQ(ExternalReference::address_of_stack_limit(isolate).address(),
107 decoder.Decode(make_code(UNCLASSIFIED, 2)));
108 CHECK_EQ(ExternalReference::address_of_real_stack_limit(isolate).address(),
109 decoder.Decode(make_code(UNCLASSIFIED, 3)));
110 CHECK_EQ(ExternalReference::debug_break(isolate).address(),
111 decoder.Decode(make_code(UNCLASSIFIED, 8)));
112 CHECK_EQ(ExternalReference::new_space_start(isolate).address(),
113 decoder.Decode(make_code(UNCLASSIFIED, 4)));
117 void WritePayload(const Vector<const byte>& payload, const char* file_name) {
118 FILE* file = v8::base::OS::FOpen(file_name, "wb");
120 PrintF("Unable to write to snapshot file \"%s\"\n", file_name);
123 size_t written = fwrite(payload.begin(), 1, payload.length(), file);
124 if (written != static_cast<size_t>(payload.length())) {
125 i::PrintF("Writing snapshot file failed.. Aborting.\n");
132 static bool WriteToFile(Isolate* isolate, const char* snapshot_file) {
133 SnapshotByteSink sink;
134 StartupSerializer ser(isolate, &sink);
136 SnapshotData snapshot_data(ser);
137 WritePayload(snapshot_data.RawData(), snapshot_file);
142 static void Serialize(v8::Isolate* isolate) {
143 // We have to create one context. One reason for this is so that the builtins
144 // can be loaded from v8natives.js and their addresses can be processed. This
145 // will clear the pending fixups array, which would otherwise contain GC roots
146 // that would confuse the serialization/deserialization process.
147 v8::Isolate::Scope isolate_scope(isolate);
149 v8::HandleScope scope(isolate);
150 v8::Context::New(isolate);
153 Isolate* internal_isolate = reinterpret_cast<Isolate*>(isolate);
154 internal_isolate->heap()->CollectAllAvailableGarbage("serialize");
155 WriteToFile(internal_isolate, FLAG_testing_serialization_file);
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(),
169 CopyChars(source + head.length() + repeats * body.length(), tail.start(),
171 return Vector<const uint8_t>(const_cast<const uint8_t*>(source),
176 // Test that the whole heap can be serialized.
177 UNINITIALIZED_TEST(Serialize) {
178 if (!Snapshot::HaveASnapshotToStartFrom()) {
179 v8::Isolate::CreateParams params;
180 params.enable_serializer = true;
181 v8::Isolate* isolate = v8::Isolate::New(params);
187 // Test that heap serialization is non-destructive.
188 UNINITIALIZED_TEST(SerializeTwice) {
189 if (!Snapshot::HaveASnapshotToStartFrom()) {
190 v8::Isolate::CreateParams params;
191 params.enable_serializer = true;
192 v8::Isolate* isolate = v8::Isolate::New(params);
199 //----------------------------------------------------------------------------
200 // Tests that the heap can be deserialized.
202 v8::Isolate* InitializeFromFile(const char* snapshot_file) {
204 byte* str = ReadBytes(snapshot_file, &len);
205 if (!str) return NULL;
206 v8::Isolate* v8_isolate = NULL;
208 SnapshotData snapshot_data(Vector<const byte>(str, len));
209 Deserializer deserializer(&snapshot_data);
210 Isolate* isolate = Isolate::NewForTesting();
211 v8_isolate = reinterpret_cast<v8::Isolate*>(isolate);
212 v8::Isolate::Scope isolate_scope(v8_isolate);
213 isolate->Init(&deserializer);
220 static v8::Isolate* Deserialize() {
221 v8::Isolate* isolate = InitializeFromFile(FLAG_testing_serialization_file);
227 static void SanityCheck(v8::Isolate* v8_isolate) {
228 Isolate* isolate = reinterpret_cast<Isolate*>(v8_isolate);
229 v8::HandleScope scope(v8_isolate);
231 isolate->heap()->Verify();
233 CHECK(isolate->global_object()->IsJSObject());
234 CHECK(isolate->native_context()->IsContext());
235 CHECK(isolate->heap()->string_table()->IsStringTable());
236 isolate->factory()->InternalizeOneByteString(STATIC_CHAR_VECTOR("Empty"));
240 UNINITIALIZED_DEPENDENT_TEST(Deserialize, Serialize) {
241 // The serialize-deserialize tests only work if the VM is built without
242 // serialization. That doesn't matter. We don't need to be able to
243 // serialize a snapshot in a VM that is booted from a snapshot.
244 if (!Snapshot::HaveASnapshotToStartFrom()) {
245 v8::Isolate* isolate = Deserialize();
247 v8::HandleScope handle_scope(isolate);
248 v8::Isolate::Scope isolate_scope(isolate);
250 v8::Local<v8::Context> env = v8::Context::New(isolate);
253 SanityCheck(isolate);
260 UNINITIALIZED_DEPENDENT_TEST(DeserializeFromSecondSerialization,
262 if (!Snapshot::HaveASnapshotToStartFrom()) {
263 v8::Isolate* isolate = Deserialize();
265 v8::Isolate::Scope isolate_scope(isolate);
266 v8::HandleScope handle_scope(isolate);
268 v8::Local<v8::Context> env = v8::Context::New(isolate);
271 SanityCheck(isolate);
278 UNINITIALIZED_DEPENDENT_TEST(DeserializeAndRunScript2, Serialize) {
279 if (!Snapshot::HaveASnapshotToStartFrom()) {
280 v8::Isolate* isolate = Deserialize();
282 v8::Isolate::Scope isolate_scope(isolate);
283 v8::HandleScope handle_scope(isolate);
286 v8::Local<v8::Context> env = v8::Context::New(isolate);
289 const char* c_source = "\"1234\".length";
290 v8::Local<v8::String> source = v8::String::NewFromUtf8(isolate, c_source);
291 v8::Local<v8::Script> script = v8::Script::Compile(source);
292 CHECK_EQ(4, script->Run()->Int32Value());
299 UNINITIALIZED_DEPENDENT_TEST(DeserializeFromSecondSerializationAndRunScript2,
301 if (!Snapshot::HaveASnapshotToStartFrom()) {
302 v8::Isolate* isolate = Deserialize();
304 v8::Isolate::Scope isolate_scope(isolate);
305 v8::HandleScope handle_scope(isolate);
307 v8::Local<v8::Context> env = v8::Context::New(isolate);
310 const char* c_source = "\"1234\".length";
311 v8::Local<v8::String> source = v8::String::NewFromUtf8(isolate, c_source);
312 v8::Local<v8::Script> script = v8::Script::Compile(source);
313 CHECK_EQ(4, script->Run()->Int32Value());
320 UNINITIALIZED_TEST(PartialSerialization) {
321 if (!Snapshot::HaveASnapshotToStartFrom()) {
322 v8::Isolate::CreateParams params;
323 params.enable_serializer = true;
324 v8::Isolate* v8_isolate = v8::Isolate::New(params);
325 Isolate* isolate = reinterpret_cast<Isolate*>(v8_isolate);
328 Heap* heap = isolate->heap();
330 v8::Persistent<v8::Context> env;
332 HandleScope scope(isolate);
333 env.Reset(v8_isolate, v8::Context::New(v8_isolate));
335 DCHECK(!env.IsEmpty());
337 v8::HandleScope handle_scope(v8_isolate);
338 v8::Local<v8::Context>::New(v8_isolate, env)->Enter();
340 // Make sure all builtin scripts are cached.
342 HandleScope scope(isolate);
343 for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
344 isolate->bootstrapper()->NativesSourceLookup(i);
347 heap->CollectAllGarbage(Heap::kNoGCFlags);
348 heap->CollectAllGarbage(Heap::kNoGCFlags);
352 v8::HandleScope handle_scope(v8_isolate);
353 v8::Local<v8::String> foo = v8::String::NewFromUtf8(v8_isolate, "foo");
354 DCHECK(!foo.IsEmpty());
355 raw_foo = *(v8::Utils::OpenHandle(*foo));
358 int file_name_length = StrLength(FLAG_testing_serialization_file) + 10;
359 Vector<char> startup_name = Vector<char>::New(file_name_length + 1);
360 SNPrintF(startup_name, "%s.startup", FLAG_testing_serialization_file);
363 v8::HandleScope handle_scope(v8_isolate);
364 v8::Local<v8::Context>::New(v8_isolate, env)->Exit();
368 SnapshotByteSink startup_sink;
369 StartupSerializer startup_serializer(isolate, &startup_sink);
370 startup_serializer.SerializeStrongReferences();
372 SnapshotByteSink partial_sink;
373 PartialSerializer partial_serializer(isolate, &startup_serializer,
375 partial_serializer.Serialize(&raw_foo);
377 startup_serializer.SerializeWeakReferences();
379 SnapshotData startup_snapshot(startup_serializer);
380 SnapshotData partial_snapshot(partial_serializer);
382 WritePayload(partial_snapshot.RawData(), FLAG_testing_serialization_file);
383 WritePayload(startup_snapshot.RawData(), startup_name.start());
385 startup_name.Dispose();
388 v8_isolate->Dispose();
393 UNINITIALIZED_DEPENDENT_TEST(PartialDeserialization, PartialSerialization) {
394 if (!Snapshot::HaveASnapshotToStartFrom()) {
395 int file_name_length = StrLength(FLAG_testing_serialization_file) + 10;
396 Vector<char> startup_name = Vector<char>::New(file_name_length + 1);
397 SNPrintF(startup_name, "%s.startup", FLAG_testing_serialization_file);
399 v8::Isolate* v8_isolate = InitializeFromFile(startup_name.start());
401 startup_name.Dispose();
403 v8::Isolate::Scope isolate_scope(v8_isolate);
405 const char* file_name = FLAG_testing_serialization_file;
407 int snapshot_size = 0;
408 byte* snapshot = ReadBytes(file_name, &snapshot_size);
410 Isolate* isolate = reinterpret_cast<Isolate*>(v8_isolate);
411 HandleScope handle_scope(isolate);
413 Handle<FixedArray> outdated_contexts;
414 // Intentionally empty handle. The deserializer should not come across
415 // any references to the global proxy in this test.
416 Handle<JSGlobalProxy> global_proxy = Handle<JSGlobalProxy>::null();
418 SnapshotData snapshot_data(Vector<const byte>(snapshot, snapshot_size));
419 Deserializer deserializer(&snapshot_data);
420 root = deserializer.DeserializePartial(isolate, global_proxy,
423 CHECK_EQ(0, outdated_contexts->length());
424 CHECK(root->IsString());
427 Handle<Object> root2;
429 SnapshotData snapshot_data(Vector<const byte>(snapshot, snapshot_size));
430 Deserializer deserializer(&snapshot_data);
431 root2 = deserializer.DeserializePartial(isolate, global_proxy,
434 CHECK(root2->IsString());
435 CHECK(root.is_identical_to(root2));
438 v8_isolate->Dispose();
443 UNINITIALIZED_TEST(ContextSerialization) {
444 if (!Snapshot::HaveASnapshotToStartFrom()) {
445 v8::Isolate::CreateParams params;
446 params.enable_serializer = true;
447 v8::Isolate* v8_isolate = v8::Isolate::New(params);
448 Isolate* isolate = reinterpret_cast<Isolate*>(v8_isolate);
449 Heap* heap = isolate->heap();
451 v8::Isolate::Scope isolate_scope(v8_isolate);
453 v8::Persistent<v8::Context> env;
455 HandleScope scope(isolate);
456 env.Reset(v8_isolate, v8::Context::New(v8_isolate));
458 DCHECK(!env.IsEmpty());
460 v8::HandleScope handle_scope(v8_isolate);
461 v8::Local<v8::Context>::New(v8_isolate, env)->Enter();
463 // Make sure all builtin scripts are cached.
465 HandleScope scope(isolate);
466 for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
467 isolate->bootstrapper()->NativesSourceLookup(i);
470 // If we don't do this then we end up with a stray root pointing at the
471 // context even after we have disposed of env.
472 heap->CollectAllGarbage(Heap::kNoGCFlags);
474 int file_name_length = StrLength(FLAG_testing_serialization_file) + 10;
475 Vector<char> startup_name = Vector<char>::New(file_name_length + 1);
476 SNPrintF(startup_name, "%s.startup", FLAG_testing_serialization_file);
479 v8::HandleScope handle_scope(v8_isolate);
480 v8::Local<v8::Context>::New(v8_isolate, env)->Exit();
483 i::Object* raw_context = *v8::Utils::OpenPersistent(env);
487 SnapshotByteSink startup_sink;
488 StartupSerializer startup_serializer(isolate, &startup_sink);
489 startup_serializer.SerializeStrongReferences();
491 SnapshotByteSink partial_sink;
492 PartialSerializer partial_serializer(isolate, &startup_serializer,
494 partial_serializer.Serialize(&raw_context);
495 startup_serializer.SerializeWeakReferences();
497 SnapshotData startup_snapshot(startup_serializer);
498 SnapshotData partial_snapshot(partial_serializer);
500 WritePayload(partial_snapshot.RawData(), FLAG_testing_serialization_file);
501 WritePayload(startup_snapshot.RawData(), startup_name.start());
503 startup_name.Dispose();
505 v8_isolate->Dispose();
510 UNINITIALIZED_DEPENDENT_TEST(ContextDeserialization, ContextSerialization) {
511 if (!Snapshot::HaveASnapshotToStartFrom()) {
512 int file_name_length = StrLength(FLAG_testing_serialization_file) + 10;
513 Vector<char> startup_name = Vector<char>::New(file_name_length + 1);
514 SNPrintF(startup_name, "%s.startup", FLAG_testing_serialization_file);
516 v8::Isolate* v8_isolate = InitializeFromFile(startup_name.start());
518 startup_name.Dispose();
520 v8::Isolate::Scope isolate_scope(v8_isolate);
522 const char* file_name = FLAG_testing_serialization_file;
524 int snapshot_size = 0;
525 byte* snapshot = ReadBytes(file_name, &snapshot_size);
527 Isolate* isolate = reinterpret_cast<Isolate*>(v8_isolate);
528 HandleScope handle_scope(isolate);
530 Handle<FixedArray> outdated_contexts;
531 Handle<JSGlobalProxy> global_proxy =
532 isolate->factory()->NewUninitializedJSGlobalProxy();
534 SnapshotData snapshot_data(Vector<const byte>(snapshot, snapshot_size));
535 Deserializer deserializer(&snapshot_data);
536 root = deserializer.DeserializePartial(isolate, global_proxy,
539 CHECK(root->IsContext());
540 CHECK(Handle<Context>::cast(root)->global_proxy() == *global_proxy);
541 CHECK_EQ(1, outdated_contexts->length());
544 Handle<Object> root2;
546 SnapshotData snapshot_data(Vector<const byte>(snapshot, snapshot_size));
547 Deserializer deserializer(&snapshot_data);
548 root2 = deserializer.DeserializePartial(isolate, global_proxy,
551 CHECK(root2->IsContext());
552 CHECK(!root.is_identical_to(root2));
555 v8_isolate->Dispose();
560 UNINITIALIZED_TEST(CustomContextSerialization) {
561 if (!Snapshot::HaveASnapshotToStartFrom()) {
562 v8::Isolate::CreateParams params;
563 params.enable_serializer = true;
564 v8::Isolate* v8_isolate = v8::Isolate::New(params);
565 Isolate* isolate = reinterpret_cast<Isolate*>(v8_isolate);
567 v8::Isolate::Scope isolate_scope(v8_isolate);
569 v8::Persistent<v8::Context> env;
571 HandleScope scope(isolate);
572 env.Reset(v8_isolate, v8::Context::New(v8_isolate));
574 DCHECK(!env.IsEmpty());
576 v8::HandleScope handle_scope(v8_isolate);
577 v8::Local<v8::Context>::New(v8_isolate, env)->Enter();
578 // After execution, e's function context refers to the global object.
582 " e = function(s) { return eval (s); }"
585 "var r = Math.random() + Math.cos(0);"
586 "var f = (function(a, b) { return a + b; }).bind(1, 2, 3);"
587 "var s = parseInt('12345');");
589 Vector<const uint8_t> source = ConstructSource(
590 STATIC_CHAR_VECTOR("function g() { return [,"),
591 STATIC_CHAR_VECTOR("1,"),
592 STATIC_CHAR_VECTOR("];} a = g(); b = g(); b.push(1);"), 100000);
593 v8::Handle<v8::String> source_str = v8::String::NewFromOneByte(
594 v8_isolate, source.start(), v8::String::kNormalString,
596 CompileRun(source_str);
599 // Make sure all builtin scripts are cached.
601 HandleScope scope(isolate);
602 for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
603 isolate->bootstrapper()->NativesSourceLookup(i);
606 // If we don't do this then we end up with a stray root pointing at the
607 // context even after we have disposed of env.
608 isolate->heap()->CollectAllAvailableGarbage("snapshotting");
610 int file_name_length = StrLength(FLAG_testing_serialization_file) + 10;
611 Vector<char> startup_name = Vector<char>::New(file_name_length + 1);
612 SNPrintF(startup_name, "%s.startup", FLAG_testing_serialization_file);
615 v8::HandleScope handle_scope(v8_isolate);
616 v8::Local<v8::Context>::New(v8_isolate, env)->Exit();
619 i::Object* raw_context = *v8::Utils::OpenPersistent(env);
623 SnapshotByteSink startup_sink;
624 StartupSerializer startup_serializer(isolate, &startup_sink);
625 startup_serializer.SerializeStrongReferences();
627 SnapshotByteSink partial_sink;
628 PartialSerializer partial_serializer(isolate, &startup_serializer,
630 partial_serializer.Serialize(&raw_context);
631 startup_serializer.SerializeWeakReferences();
633 SnapshotData startup_snapshot(startup_serializer);
634 SnapshotData partial_snapshot(partial_serializer);
636 WritePayload(partial_snapshot.RawData(), FLAG_testing_serialization_file);
637 WritePayload(startup_snapshot.RawData(), startup_name.start());
639 startup_name.Dispose();
641 v8_isolate->Dispose();
646 UNINITIALIZED_DEPENDENT_TEST(CustomContextDeserialization,
647 CustomContextSerialization) {
648 FLAG_crankshaft = false;
649 if (!Snapshot::HaveASnapshotToStartFrom()) {
650 int file_name_length = StrLength(FLAG_testing_serialization_file) + 10;
651 Vector<char> startup_name = Vector<char>::New(file_name_length + 1);
652 SNPrintF(startup_name, "%s.startup", FLAG_testing_serialization_file);
654 v8::Isolate* v8_isolate = InitializeFromFile(startup_name.start());
656 startup_name.Dispose();
658 v8::Isolate::Scope isolate_scope(v8_isolate);
660 const char* file_name = FLAG_testing_serialization_file;
662 int snapshot_size = 0;
663 byte* snapshot = ReadBytes(file_name, &snapshot_size);
665 Isolate* isolate = reinterpret_cast<Isolate*>(v8_isolate);
666 HandleScope handle_scope(isolate);
668 Handle<FixedArray> outdated_contexts;
669 Handle<JSGlobalProxy> global_proxy =
670 isolate->factory()->NewUninitializedJSGlobalProxy();
672 SnapshotData snapshot_data(Vector<const byte>(snapshot, snapshot_size));
673 Deserializer deserializer(&snapshot_data);
674 root = deserializer.DeserializePartial(isolate, global_proxy,
677 CHECK_EQ(2, outdated_contexts->length());
678 CHECK(root->IsContext());
679 Handle<Context> context = Handle<Context>::cast(root);
680 CHECK(context->global_proxy() == *global_proxy);
681 Handle<String> o = isolate->factory()->NewStringFromAsciiChecked("o");
682 Handle<JSObject> global_object(context->global_object(), isolate);
683 Handle<Object> property = JSObject::GetDataProperty(global_object, o);
684 CHECK(property.is_identical_to(global_proxy));
686 v8::Handle<v8::Context> v8_context = v8::Utils::ToLocal(context);
687 v8::Context::Scope context_scope(v8_context);
688 double r = CompileRun("r")->ToNumber(v8_isolate)->Value();
689 CHECK(r >= 1 && r <= 2);
690 int f = CompileRun("f()")->ToNumber(v8_isolate)->Int32Value();
692 f = CompileRun("e('f()')")->ToNumber(v8_isolate)->Int32Value();
694 v8::Handle<v8::String> s = CompileRun("s")->ToString(v8_isolate);
695 CHECK(s->Equals(v8_str("12345")));
696 int a = CompileRun("a.length")->ToNumber(v8_isolate)->Int32Value();
698 int b = CompileRun("b.length")->ToNumber(v8_isolate)->Int32Value();
702 v8_isolate->Dispose();
707 TEST(TestThatAlwaysSucceeds) {
711 TEST(TestThatAlwaysFails) {
712 bool ArtificialFailure = false;
713 CHECK(ArtificialFailure);
717 DEPENDENT_TEST(DependentTestThatAlwaysFails, TestThatAlwaysSucceeds) {
718 bool ArtificialFailure2 = false;
719 CHECK(ArtificialFailure2);
723 int CountBuiltins() {
724 // Check that we have not deserialized any additional builtin.
725 HeapIterator iterator(CcTest::heap());
726 DisallowHeapAllocation no_allocation;
728 for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) {
729 if (obj->IsCode() && Code::cast(obj)->kind() == Code::BUILTIN) counter++;
735 static Handle<SharedFunctionInfo> CompileScript(
736 Isolate* isolate, Handle<String> source, Handle<String> name,
737 ScriptData** cached_data, v8::ScriptCompiler::CompileOptions options) {
738 return Compiler::CompileScript(source, name, 0, 0, false, false,
739 Handle<Context>(isolate->native_context()),
740 NULL, cached_data, options, NOT_NATIVES_CODE,
745 TEST(SerializeToplevelOnePlusOne) {
746 FLAG_serialize_toplevel = true;
747 LocalContext context;
748 Isolate* isolate = CcTest::i_isolate();
749 isolate->compilation_cache()->Disable(); // Disable same-isolate code cache.
751 v8::HandleScope scope(CcTest::isolate());
753 const char* source = "1 + 1";
755 Handle<String> orig_source = isolate->factory()
756 ->NewStringFromUtf8(CStrVector(source))
758 Handle<String> copy_source = isolate->factory()
759 ->NewStringFromUtf8(CStrVector(source))
761 CHECK(!orig_source.is_identical_to(copy_source));
762 CHECK(orig_source->Equals(*copy_source));
764 ScriptData* cache = NULL;
766 Handle<SharedFunctionInfo> orig =
767 CompileScript(isolate, orig_source, Handle<String>(), &cache,
768 v8::ScriptCompiler::kProduceCodeCache);
770 int builtins_count = CountBuiltins();
772 Handle<SharedFunctionInfo> copy;
774 DisallowCompilation no_compile_expected(isolate);
775 copy = CompileScript(isolate, copy_source, Handle<String>(), &cache,
776 v8::ScriptCompiler::kConsumeCodeCache);
779 CHECK_NE(*orig, *copy);
780 CHECK(Script::cast(copy->script())->source() == *copy_source);
782 Handle<JSFunction> copy_fun =
783 isolate->factory()->NewFunctionFromSharedFunctionInfo(
784 copy, isolate->native_context());
785 Handle<JSObject> global(isolate->context()->global_object());
786 Handle<Object> copy_result =
787 Execution::Call(isolate, copy_fun, global, 0, NULL).ToHandleChecked();
788 CHECK_EQ(2, Handle<Smi>::cast(copy_result)->value());
790 CHECK_EQ(builtins_count, CountBuiltins());
796 TEST(CodeCachePromotedToCompilationCache) {
797 FLAG_serialize_toplevel = true;
798 LocalContext context;
799 Isolate* isolate = CcTest::i_isolate();
801 v8::HandleScope scope(CcTest::isolate());
803 const char* source = "1 + 1";
805 Handle<String> src = isolate->factory()
806 ->NewStringFromUtf8(CStrVector(source))
808 ScriptData* cache = NULL;
810 CompileScript(isolate, src, src, &cache,
811 v8::ScriptCompiler::kProduceCodeCache);
813 DisallowCompilation no_compile_expected(isolate);
814 Handle<SharedFunctionInfo> copy = CompileScript(
815 isolate, src, src, &cache, v8::ScriptCompiler::kConsumeCodeCache);
817 CHECK(isolate->compilation_cache()
818 ->LookupScript(src, src, 0, 0, false, false,
819 isolate->native_context(), SLOPPY)
821 .is_identical_to(copy));
827 TEST(SerializeToplevelInternalizedString) {
828 FLAG_serialize_toplevel = true;
829 LocalContext context;
830 Isolate* isolate = CcTest::i_isolate();
831 isolate->compilation_cache()->Disable(); // Disable same-isolate code cache.
833 v8::HandleScope scope(CcTest::isolate());
835 const char* source = "'string1'";
837 Handle<String> orig_source = isolate->factory()
838 ->NewStringFromUtf8(CStrVector(source))
840 Handle<String> copy_source = isolate->factory()
841 ->NewStringFromUtf8(CStrVector(source))
843 CHECK(!orig_source.is_identical_to(copy_source));
844 CHECK(orig_source->Equals(*copy_source));
846 Handle<JSObject> global(isolate->context()->global_object());
847 ScriptData* cache = NULL;
849 Handle<SharedFunctionInfo> orig =
850 CompileScript(isolate, orig_source, Handle<String>(), &cache,
851 v8::ScriptCompiler::kProduceCodeCache);
852 Handle<JSFunction> orig_fun =
853 isolate->factory()->NewFunctionFromSharedFunctionInfo(
854 orig, isolate->native_context());
855 Handle<Object> orig_result =
856 Execution::Call(isolate, orig_fun, global, 0, NULL).ToHandleChecked();
857 CHECK(orig_result->IsInternalizedString());
859 int builtins_count = CountBuiltins();
861 Handle<SharedFunctionInfo> copy;
863 DisallowCompilation no_compile_expected(isolate);
864 copy = CompileScript(isolate, copy_source, Handle<String>(), &cache,
865 v8::ScriptCompiler::kConsumeCodeCache);
867 CHECK_NE(*orig, *copy);
868 CHECK(Script::cast(copy->script())->source() == *copy_source);
870 Handle<JSFunction> copy_fun =
871 isolate->factory()->NewFunctionFromSharedFunctionInfo(
872 copy, isolate->native_context());
873 CHECK_NE(*orig_fun, *copy_fun);
874 Handle<Object> copy_result =
875 Execution::Call(isolate, copy_fun, global, 0, NULL).ToHandleChecked();
876 CHECK(orig_result.is_identical_to(copy_result));
877 Handle<String> expected =
878 isolate->factory()->NewStringFromAsciiChecked("string1");
880 CHECK(Handle<String>::cast(copy_result)->Equals(*expected));
881 CHECK_EQ(builtins_count, CountBuiltins());
887 TEST(SerializeToplevelLargeCodeObject) {
888 FLAG_serialize_toplevel = true;
889 LocalContext context;
890 Isolate* isolate = CcTest::i_isolate();
891 isolate->compilation_cache()->Disable(); // Disable same-isolate code cache.
893 v8::HandleScope scope(CcTest::isolate());
895 Vector<const uint8_t> source =
896 ConstructSource(STATIC_CHAR_VECTOR("var j=1; try { if (j) throw 1;"),
897 STATIC_CHAR_VECTOR("for(var i=0;i<1;i++)j++;"),
898 STATIC_CHAR_VECTOR("} catch (e) { j=7; } j"), 10000);
899 Handle<String> source_str =
900 isolate->factory()->NewStringFromOneByte(source).ToHandleChecked();
902 Handle<JSObject> global(isolate->context()->global_object());
903 ScriptData* cache = NULL;
905 Handle<SharedFunctionInfo> orig =
906 CompileScript(isolate, source_str, Handle<String>(), &cache,
907 v8::ScriptCompiler::kProduceCodeCache);
909 CHECK(isolate->heap()->InSpace(orig->code(), LO_SPACE));
911 Handle<SharedFunctionInfo> copy;
913 DisallowCompilation no_compile_expected(isolate);
914 copy = CompileScript(isolate, source_str, Handle<String>(), &cache,
915 v8::ScriptCompiler::kConsumeCodeCache);
917 CHECK_NE(*orig, *copy);
919 Handle<JSFunction> copy_fun =
920 isolate->factory()->NewFunctionFromSharedFunctionInfo(
921 copy, isolate->native_context());
923 Handle<Object> copy_result =
924 Execution::Call(isolate, copy_fun, global, 0, NULL).ToHandleChecked();
927 CHECK(copy_result->ToInt32(&result_int));
928 CHECK_EQ(7, result_int);
935 TEST(SerializeToplevelLargeStrings) {
936 FLAG_serialize_toplevel = true;
937 LocalContext context;
938 Isolate* isolate = CcTest::i_isolate();
939 Factory* f = isolate->factory();
940 isolate->compilation_cache()->Disable(); // Disable same-isolate code cache.
942 v8::HandleScope scope(CcTest::isolate());
944 Vector<const uint8_t> source_s = ConstructSource(
945 STATIC_CHAR_VECTOR("var s = \""), STATIC_CHAR_VECTOR("abcdef"),
946 STATIC_CHAR_VECTOR("\";"), 1000000);
947 Vector<const uint8_t> source_t = ConstructSource(
948 STATIC_CHAR_VECTOR("var t = \""), STATIC_CHAR_VECTOR("uvwxyz"),
949 STATIC_CHAR_VECTOR("\"; s + t"), 999999);
950 Handle<String> source_str =
951 f->NewConsString(f->NewStringFromOneByte(source_s).ToHandleChecked(),
952 f->NewStringFromOneByte(source_t).ToHandleChecked())
955 Handle<JSObject> global(isolate->context()->global_object());
956 ScriptData* cache = NULL;
958 Handle<SharedFunctionInfo> orig =
959 CompileScript(isolate, source_str, Handle<String>(), &cache,
960 v8::ScriptCompiler::kProduceCodeCache);
962 Handle<SharedFunctionInfo> copy;
964 DisallowCompilation no_compile_expected(isolate);
965 copy = CompileScript(isolate, source_str, Handle<String>(), &cache,
966 v8::ScriptCompiler::kConsumeCodeCache);
968 CHECK_NE(*orig, *copy);
970 Handle<JSFunction> copy_fun =
971 isolate->factory()->NewFunctionFromSharedFunctionInfo(
972 copy, isolate->native_context());
974 Handle<Object> copy_result =
975 Execution::Call(isolate, copy_fun, global, 0, NULL).ToHandleChecked();
977 CHECK_EQ(6 * 1999999, Handle<String>::cast(copy_result)->length());
978 Handle<Object> property = JSObject::GetDataProperty(
979 isolate->global_object(), f->NewStringFromAsciiChecked("s"));
980 CHECK(isolate->heap()->InSpace(HeapObject::cast(*property), LO_SPACE));
981 property = JSObject::GetDataProperty(isolate->global_object(),
982 f->NewStringFromAsciiChecked("t"));
983 CHECK(isolate->heap()->InSpace(HeapObject::cast(*property), LO_SPACE));
984 // Make sure we do not serialize too much, e.g. include the source string.
985 CHECK_LT(cache->length(), 13000000);
993 TEST(SerializeToplevelThreeBigStrings) {
994 FLAG_serialize_toplevel = true;
995 LocalContext context;
996 Isolate* isolate = CcTest::i_isolate();
997 Factory* f = isolate->factory();
998 isolate->compilation_cache()->Disable(); // Disable same-isolate code cache.
1000 v8::HandleScope scope(CcTest::isolate());
1002 Vector<const uint8_t> source_a =
1003 ConstructSource(STATIC_CHAR_VECTOR("var a = \""), STATIC_CHAR_VECTOR("a"),
1004 STATIC_CHAR_VECTOR("\";"), 700000);
1005 Handle<String> source_a_str =
1006 f->NewStringFromOneByte(source_a).ToHandleChecked();
1008 Vector<const uint8_t> source_b =
1009 ConstructSource(STATIC_CHAR_VECTOR("var b = \""), STATIC_CHAR_VECTOR("b"),
1010 STATIC_CHAR_VECTOR("\";"), 600000);
1011 Handle<String> source_b_str =
1012 f->NewStringFromOneByte(source_b).ToHandleChecked();
1014 Vector<const uint8_t> source_c =
1015 ConstructSource(STATIC_CHAR_VECTOR("var c = \""), STATIC_CHAR_VECTOR("c"),
1016 STATIC_CHAR_VECTOR("\";"), 500000);
1017 Handle<String> source_c_str =
1018 f->NewStringFromOneByte(source_c).ToHandleChecked();
1020 Handle<String> source_str =
1022 f->NewConsString(source_a_str, source_b_str).ToHandleChecked(),
1023 source_c_str).ToHandleChecked();
1025 Handle<JSObject> global(isolate->context()->global_object());
1026 ScriptData* cache = NULL;
1028 Handle<SharedFunctionInfo> orig =
1029 CompileScript(isolate, source_str, Handle<String>(), &cache,
1030 v8::ScriptCompiler::kProduceCodeCache);
1032 Handle<SharedFunctionInfo> copy;
1034 DisallowCompilation no_compile_expected(isolate);
1035 copy = CompileScript(isolate, source_str, Handle<String>(), &cache,
1036 v8::ScriptCompiler::kConsumeCodeCache);
1038 CHECK_NE(*orig, *copy);
1040 Handle<JSFunction> copy_fun =
1041 isolate->factory()->NewFunctionFromSharedFunctionInfo(
1042 copy, isolate->native_context());
1044 USE(Execution::Call(isolate, copy_fun, global, 0, NULL));
1046 CHECK_EQ(600000 + 700000, CompileRun("(a + b).length")->Int32Value());
1047 CHECK_EQ(500000 + 600000, CompileRun("(b + c).length")->Int32Value());
1048 Heap* heap = isolate->heap();
1049 CHECK(heap->InSpace(
1050 *v8::Utils::OpenHandle(*CompileRun("a")->ToString(CcTest::isolate())),
1052 CHECK(heap->InSpace(
1053 *v8::Utils::OpenHandle(*CompileRun("b")->ToString(CcTest::isolate())),
1055 CHECK(heap->InSpace(
1056 *v8::Utils::OpenHandle(*CompileRun("c")->ToString(CcTest::isolate())),
1066 class SerializerOneByteResource
1067 : public v8::String::ExternalOneByteStringResource {
1069 SerializerOneByteResource(const char* data, size_t length)
1070 : data_(data), length_(length) {}
1071 virtual const char* data() const { return data_; }
1072 virtual size_t length() const { return length_; }
1080 class SerializerTwoByteResource : public v8::String::ExternalStringResource {
1082 SerializerTwoByteResource(const char* data, size_t length)
1083 : data_(AsciiToTwoByteString(data)), length_(length) {}
1084 ~SerializerTwoByteResource() { DeleteArray<const uint16_t>(data_); }
1086 virtual const uint16_t* data() const { return data_; }
1087 virtual size_t length() const { return length_; }
1090 const uint16_t* data_;
1095 TEST(SerializeToplevelExternalString) {
1096 FLAG_serialize_toplevel = true;
1097 LocalContext context;
1098 Isolate* isolate = CcTest::i_isolate();
1099 isolate->compilation_cache()->Disable(); // Disable same-isolate code cache.
1101 v8::HandleScope scope(CcTest::isolate());
1103 // Obtain external internalized one-byte string.
1104 SerializerOneByteResource one_byte_resource("one_byte", 8);
1105 Handle<String> one_byte_string =
1106 isolate->factory()->NewStringFromAsciiChecked("one_byte");
1107 one_byte_string = isolate->factory()->InternalizeString(one_byte_string);
1108 one_byte_string->MakeExternal(&one_byte_resource);
1109 CHECK(one_byte_string->IsExternalOneByteString());
1110 CHECK(one_byte_string->IsInternalizedString());
1112 // Obtain external internalized two-byte string.
1113 SerializerTwoByteResource two_byte_resource("two_byte", 8);
1114 Handle<String> two_byte_string =
1115 isolate->factory()->NewStringFromAsciiChecked("two_byte");
1116 two_byte_string = isolate->factory()->InternalizeString(two_byte_string);
1117 two_byte_string->MakeExternal(&two_byte_resource);
1118 CHECK(two_byte_string->IsExternalTwoByteString());
1119 CHECK(two_byte_string->IsInternalizedString());
1121 const char* source =
1123 "o.one_byte = 7; \n"
1124 "o.two_byte = 8; \n"
1125 "o.one_byte + o.two_byte; \n";
1126 Handle<String> source_string = isolate->factory()
1127 ->NewStringFromUtf8(CStrVector(source))
1130 Handle<JSObject> global(isolate->context()->global_object());
1131 ScriptData* cache = NULL;
1133 Handle<SharedFunctionInfo> orig =
1134 CompileScript(isolate, source_string, Handle<String>(), &cache,
1135 v8::ScriptCompiler::kProduceCodeCache);
1137 Handle<SharedFunctionInfo> copy;
1139 DisallowCompilation no_compile_expected(isolate);
1140 copy = CompileScript(isolate, source_string, Handle<String>(), &cache,
1141 v8::ScriptCompiler::kConsumeCodeCache);
1143 CHECK_NE(*orig, *copy);
1145 Handle<JSFunction> copy_fun =
1146 isolate->factory()->NewFunctionFromSharedFunctionInfo(
1147 copy, isolate->native_context());
1149 Handle<Object> copy_result =
1150 Execution::Call(isolate, copy_fun, global, 0, NULL).ToHandleChecked();
1152 CHECK_EQ(15.0f, copy_result->Number());
1158 TEST(SerializeToplevelLargeExternalString) {
1159 FLAG_serialize_toplevel = true;
1160 LocalContext context;
1161 Isolate* isolate = CcTest::i_isolate();
1162 isolate->compilation_cache()->Disable(); // Disable same-isolate code cache.
1164 Factory* f = isolate->factory();
1166 v8::HandleScope scope(CcTest::isolate());
1168 // Create a huge external internalized string to use as variable name.
1169 Vector<const uint8_t> string =
1170 ConstructSource(STATIC_CHAR_VECTOR(""), STATIC_CHAR_VECTOR("abcdef"),
1171 STATIC_CHAR_VECTOR(""), 999999);
1172 Handle<String> name = f->NewStringFromOneByte(string).ToHandleChecked();
1173 SerializerOneByteResource one_byte_resource(
1174 reinterpret_cast<const char*>(string.start()), string.length());
1175 name = f->InternalizeString(name);
1176 name->MakeExternal(&one_byte_resource);
1177 CHECK(name->IsExternalOneByteString());
1178 CHECK(name->IsInternalizedString());
1179 CHECK(isolate->heap()->InSpace(*name, LO_SPACE));
1181 // Create the source, which is "var <literal> = 42; <literal>".
1182 Handle<String> source_str =
1184 f->NewConsString(f->NewStringFromAsciiChecked("var "), name)
1186 f->NewConsString(f->NewStringFromAsciiChecked(" = 42; "), name)
1187 .ToHandleChecked()).ToHandleChecked();
1189 Handle<JSObject> global(isolate->context()->global_object());
1190 ScriptData* cache = NULL;
1192 Handle<SharedFunctionInfo> orig =
1193 CompileScript(isolate, source_str, Handle<String>(), &cache,
1194 v8::ScriptCompiler::kProduceCodeCache);
1196 Handle<SharedFunctionInfo> copy;
1198 DisallowCompilation no_compile_expected(isolate);
1199 copy = CompileScript(isolate, source_str, Handle<String>(), &cache,
1200 v8::ScriptCompiler::kConsumeCodeCache);
1202 CHECK_NE(*orig, *copy);
1204 Handle<JSFunction> copy_fun =
1205 f->NewFunctionFromSharedFunctionInfo(copy, isolate->native_context());
1207 Handle<Object> copy_result =
1208 Execution::Call(isolate, copy_fun, global, 0, NULL).ToHandleChecked();
1210 CHECK_EQ(42.0f, copy_result->Number());
1217 TEST(SerializeToplevelExternalScriptName) {
1218 FLAG_serialize_toplevel = true;
1219 LocalContext context;
1220 Isolate* isolate = CcTest::i_isolate();
1221 isolate->compilation_cache()->Disable(); // Disable same-isolate code cache.
1223 Factory* f = isolate->factory();
1225 v8::HandleScope scope(CcTest::isolate());
1227 const char* source =
1228 "var a = [1, 2, 3, 4];"
1229 "a.reduce(function(x, y) { return x + y }, 0)";
1231 Handle<String> source_string =
1232 f->NewStringFromUtf8(CStrVector(source)).ToHandleChecked();
1234 const SerializerOneByteResource one_byte_resource("one_byte", 8);
1235 Handle<String> name =
1236 f->NewExternalStringFromOneByte(&one_byte_resource).ToHandleChecked();
1237 CHECK(name->IsExternalOneByteString());
1238 CHECK(!name->IsInternalizedString());
1240 Handle<JSObject> global(isolate->context()->global_object());
1241 ScriptData* cache = NULL;
1243 Handle<SharedFunctionInfo> orig =
1244 CompileScript(isolate, source_string, name, &cache,
1245 v8::ScriptCompiler::kProduceCodeCache);
1247 Handle<SharedFunctionInfo> copy;
1249 DisallowCompilation no_compile_expected(isolate);
1250 copy = CompileScript(isolate, source_string, name, &cache,
1251 v8::ScriptCompiler::kConsumeCodeCache);
1253 CHECK_NE(*orig, *copy);
1255 Handle<JSFunction> copy_fun =
1256 f->NewFunctionFromSharedFunctionInfo(copy, isolate->native_context());
1258 Handle<Object> copy_result =
1259 Execution::Call(isolate, copy_fun, global, 0, NULL).ToHandleChecked();
1261 CHECK_EQ(10.0f, copy_result->Number());
1267 static bool toplevel_test_code_event_found = false;
1270 static void SerializerCodeEventListener(const v8::JitCodeEvent* event) {
1271 if (event->type == v8::JitCodeEvent::CODE_ADDED &&
1272 memcmp(event->name.str, "Script:~test", 12) == 0) {
1273 toplevel_test_code_event_found = true;
1278 v8::ScriptCompiler::CachedData* ProduceCache(const char* source) {
1279 v8::ScriptCompiler::CachedData* cache;
1280 v8::Isolate* isolate1 = v8::Isolate::New();
1282 v8::Isolate::Scope iscope(isolate1);
1283 v8::HandleScope scope(isolate1);
1284 v8::Local<v8::Context> context = v8::Context::New(isolate1);
1285 v8::Context::Scope context_scope(context);
1287 v8::Local<v8::String> source_str = v8_str(source);
1288 v8::ScriptOrigin origin(v8_str("test"));
1289 v8::ScriptCompiler::Source source(source_str, origin);
1290 v8::Local<v8::UnboundScript> script = v8::ScriptCompiler::CompileUnbound(
1291 isolate1, &source, v8::ScriptCompiler::kProduceCodeCache);
1292 const v8::ScriptCompiler::CachedData* data = source.GetCachedData();
1294 // Persist cached data.
1295 uint8_t* buffer = NewArray<uint8_t>(data->length);
1296 MemCopy(buffer, data->data, data->length);
1297 cache = new v8::ScriptCompiler::CachedData(
1298 buffer, data->length, v8::ScriptCompiler::CachedData::BufferOwned);
1300 v8::Local<v8::Value> result = script->BindToCurrentContext()->Run();
1301 CHECK(result->ToString(isolate1)->Equals(v8_str("abcdef")));
1303 isolate1->Dispose();
1308 TEST(SerializeToplevelIsolates) {
1309 FLAG_serialize_toplevel = true;
1311 const char* source = "function f() { return 'abc'; }; f() + 'def'";
1312 v8::ScriptCompiler::CachedData* cache = ProduceCache(source);
1314 v8::Isolate* isolate2 = v8::Isolate::New();
1315 isolate2->SetJitCodeEventHandler(v8::kJitCodeEventDefault,
1316 SerializerCodeEventListener);
1317 toplevel_test_code_event_found = false;
1319 v8::Isolate::Scope iscope(isolate2);
1320 v8::HandleScope scope(isolate2);
1321 v8::Local<v8::Context> context = v8::Context::New(isolate2);
1322 v8::Context::Scope context_scope(context);
1324 v8::Local<v8::String> source_str = v8_str(source);
1325 v8::ScriptOrigin origin(v8_str("test"));
1326 v8::ScriptCompiler::Source source(source_str, origin, cache);
1327 v8::Local<v8::UnboundScript> script;
1329 DisallowCompilation no_compile(reinterpret_cast<Isolate*>(isolate2));
1330 script = v8::ScriptCompiler::CompileUnbound(
1331 isolate2, &source, v8::ScriptCompiler::kConsumeCodeCache);
1333 CHECK(!cache->rejected);
1334 v8::Local<v8::Value> result = script->BindToCurrentContext()->Run();
1335 CHECK(result->ToString(isolate2)->Equals(v8_str("abcdef")));
1337 DCHECK(toplevel_test_code_event_found);
1338 isolate2->Dispose();
1342 TEST(SerializeToplevelFlagChange) {
1343 FLAG_serialize_toplevel = true;
1345 const char* source = "function f() { return 'abc'; }; f() + 'def'";
1346 v8::ScriptCompiler::CachedData* cache = ProduceCache(source);
1348 v8::Isolate* isolate2 = v8::Isolate::New();
1349 FLAG_allow_natives_syntax = true; // Flag change should trigger cache reject.
1351 v8::Isolate::Scope iscope(isolate2);
1352 v8::HandleScope scope(isolate2);
1353 v8::Local<v8::Context> context = v8::Context::New(isolate2);
1354 v8::Context::Scope context_scope(context);
1356 v8::Local<v8::String> source_str = v8_str(source);
1357 v8::ScriptOrigin origin(v8_str("test"));
1358 v8::ScriptCompiler::Source source(source_str, origin, cache);
1359 v8::ScriptCompiler::CompileUnbound(isolate2, &source,
1360 v8::ScriptCompiler::kConsumeCodeCache);
1361 CHECK(cache->rejected);
1363 isolate2->Dispose();
1367 TEST(SerializeToplevelBitFlip) {
1368 FLAG_serialize_toplevel = true;
1370 const char* source = "function f() { return 'abc'; }; f() + 'def'";
1371 v8::ScriptCompiler::CachedData* cache = ProduceCache(source);
1374 const_cast<uint8_t*>(cache->data)[337] ^= 0x40;
1376 v8::Isolate* isolate2 = v8::Isolate::New();
1378 v8::Isolate::Scope iscope(isolate2);
1379 v8::HandleScope scope(isolate2);
1380 v8::Local<v8::Context> context = v8::Context::New(isolate2);
1381 v8::Context::Scope context_scope(context);
1383 v8::Local<v8::String> source_str = v8_str(source);
1384 v8::ScriptOrigin origin(v8_str("test"));
1385 v8::ScriptCompiler::Source source(source_str, origin, cache);
1386 v8::ScriptCompiler::CompileUnbound(isolate2, &source,
1387 v8::ScriptCompiler::kConsumeCodeCache);
1388 CHECK(cache->rejected);
1390 isolate2->Dispose();
1394 TEST(SerializeWithHarmonyScoping) {
1395 FLAG_serialize_toplevel = true;
1396 FLAG_harmony_scoping = true;
1398 const char* source1 = "'use strict'; let x = 'X'";
1399 const char* source2 = "'use strict'; let y = 'Y'";
1400 const char* source3 = "'use strict'; x + y";
1402 v8::ScriptCompiler::CachedData* cache;
1404 v8::Isolate* isolate1 = v8::Isolate::New();
1406 v8::Isolate::Scope iscope(isolate1);
1407 v8::HandleScope scope(isolate1);
1408 v8::Local<v8::Context> context = v8::Context::New(isolate1);
1409 v8::Context::Scope context_scope(context);
1411 CompileRun(source1);
1412 CompileRun(source2);
1414 v8::Local<v8::String> source_str = v8_str(source3);
1415 v8::ScriptOrigin origin(v8_str("test"));
1416 v8::ScriptCompiler::Source source(source_str, origin);
1417 v8::Local<v8::UnboundScript> script = v8::ScriptCompiler::CompileUnbound(
1418 isolate1, &source, v8::ScriptCompiler::kProduceCodeCache);
1419 const v8::ScriptCompiler::CachedData* data = source.GetCachedData();
1421 // Persist cached data.
1422 uint8_t* buffer = NewArray<uint8_t>(data->length);
1423 MemCopy(buffer, data->data, data->length);
1424 cache = new v8::ScriptCompiler::CachedData(
1425 buffer, data->length, v8::ScriptCompiler::CachedData::BufferOwned);
1427 v8::Local<v8::Value> result = script->BindToCurrentContext()->Run();
1428 CHECK(result->ToString(isolate1)->Equals(v8_str("XY")));
1430 isolate1->Dispose();
1432 v8::Isolate* isolate2 = v8::Isolate::New();
1434 v8::Isolate::Scope iscope(isolate2);
1435 v8::HandleScope scope(isolate2);
1436 v8::Local<v8::Context> context = v8::Context::New(isolate2);
1437 v8::Context::Scope context_scope(context);
1439 // Reverse order of prior running scripts.
1440 CompileRun(source2);
1441 CompileRun(source1);
1443 v8::Local<v8::String> source_str = v8_str(source3);
1444 v8::ScriptOrigin origin(v8_str("test"));
1445 v8::ScriptCompiler::Source source(source_str, origin, cache);
1446 v8::Local<v8::UnboundScript> script;
1448 DisallowCompilation no_compile(reinterpret_cast<Isolate*>(isolate2));
1449 script = v8::ScriptCompiler::CompileUnbound(
1450 isolate2, &source, v8::ScriptCompiler::kConsumeCodeCache);
1452 v8::Local<v8::Value> result = script->BindToCurrentContext()->Run();
1453 CHECK(result->ToString(isolate2)->Equals(v8_str("XY")));
1455 isolate2->Dispose();