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 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
279 v8::HandleScope handle_scope(v8_isolate); | 279 v8::HandleScope handle_scope(v8_isolate); |
280 v8::Local<v8::Context>::New(v8_isolate, env)->Exit(); | 280 v8::Local<v8::Context>::New(v8_isolate, env)->Exit(); |
281 } | 281 } |
282 env.Reset(); | 282 env.Reset(); |
283 | 283 |
284 StartupSerializer startup_serializer( | 284 StartupSerializer startup_serializer( |
285 isolate, v8::SnapshotCreator::FunctionCodeHandling::kClear); | 285 isolate, v8::SnapshotCreator::FunctionCodeHandling::kClear); |
286 startup_serializer.SerializeStrongReferences(); | 286 startup_serializer.SerializeStrongReferences(); |
287 | 287 |
288 PartialSerializer partial_serializer(isolate, &startup_serializer, nullptr); | 288 PartialSerializer partial_serializer(isolate, &startup_serializer, nullptr); |
289 partial_serializer.Serialize(&raw_foo); | 289 partial_serializer.Serialize(&raw_foo, false); |
290 | 290 |
291 startup_serializer.SerializeWeakReferencesAndDeferred(); | 291 startup_serializer.SerializeWeakReferencesAndDeferred(); |
292 | 292 |
293 SnapshotData startup_snapshot(&startup_serializer); | 293 SnapshotData startup_snapshot(&startup_serializer); |
294 SnapshotData partial_snapshot(&partial_serializer); | 294 SnapshotData partial_snapshot(&partial_serializer); |
295 | 295 |
296 *partial_blob_out = WritePayload(partial_snapshot.RawData()); | 296 *partial_blob_out = WritePayload(partial_snapshot.RawData()); |
297 *startup_blob_out = WritePayload(startup_snapshot.RawData()); | 297 *startup_blob_out = WritePayload(startup_snapshot.RawData()); |
298 } | 298 } |
299 v8_isolate->Exit(); | 299 v8_isolate->Exit(); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
379 | 379 |
380 env.Reset(); | 380 env.Reset(); |
381 | 381 |
382 SnapshotByteSink startup_sink; | 382 SnapshotByteSink startup_sink; |
383 StartupSerializer startup_serializer( | 383 StartupSerializer startup_serializer( |
384 isolate, v8::SnapshotCreator::FunctionCodeHandling::kClear); | 384 isolate, v8::SnapshotCreator::FunctionCodeHandling::kClear); |
385 startup_serializer.SerializeStrongReferences(); | 385 startup_serializer.SerializeStrongReferences(); |
386 | 386 |
387 SnapshotByteSink partial_sink; | 387 SnapshotByteSink partial_sink; |
388 PartialSerializer partial_serializer(isolate, &startup_serializer, nullptr); | 388 PartialSerializer partial_serializer(isolate, &startup_serializer, nullptr); |
389 partial_serializer.Serialize(&raw_context); | 389 partial_serializer.Serialize(&raw_context, false); |
390 startup_serializer.SerializeWeakReferencesAndDeferred(); | 390 startup_serializer.SerializeWeakReferencesAndDeferred(); |
391 | 391 |
392 SnapshotData startup_snapshot(&startup_serializer); | 392 SnapshotData startup_snapshot(&startup_serializer); |
393 SnapshotData partial_snapshot(&partial_serializer); | 393 SnapshotData partial_snapshot(&partial_serializer); |
394 | 394 |
395 *partial_blob_out = WritePayload(partial_snapshot.RawData()); | 395 *partial_blob_out = WritePayload(partial_snapshot.RawData()); |
396 *startup_blob_out = WritePayload(startup_snapshot.RawData()); | 396 *startup_blob_out = WritePayload(startup_snapshot.RawData()); |
397 } | 397 } |
398 v8_isolate->Dispose(); | 398 v8_isolate->Dispose(); |
399 } | 399 } |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
499 | 499 |
500 env.Reset(); | 500 env.Reset(); |
501 | 501 |
502 SnapshotByteSink startup_sink; | 502 SnapshotByteSink startup_sink; |
503 StartupSerializer startup_serializer( | 503 StartupSerializer startup_serializer( |
504 isolate, v8::SnapshotCreator::FunctionCodeHandling::kClear); | 504 isolate, v8::SnapshotCreator::FunctionCodeHandling::kClear); |
505 startup_serializer.SerializeStrongReferences(); | 505 startup_serializer.SerializeStrongReferences(); |
506 | 506 |
507 SnapshotByteSink partial_sink; | 507 SnapshotByteSink partial_sink; |
508 PartialSerializer partial_serializer(isolate, &startup_serializer, nullptr); | 508 PartialSerializer partial_serializer(isolate, &startup_serializer, nullptr); |
509 partial_serializer.Serialize(&raw_context); | 509 partial_serializer.Serialize(&raw_context, false); |
510 startup_serializer.SerializeWeakReferencesAndDeferred(); | 510 startup_serializer.SerializeWeakReferencesAndDeferred(); |
511 | 511 |
512 SnapshotData startup_snapshot(&startup_serializer); | 512 SnapshotData startup_snapshot(&startup_serializer); |
513 SnapshotData partial_snapshot(&partial_serializer); | 513 SnapshotData partial_snapshot(&partial_serializer); |
514 | 514 |
515 *partial_blob_out = WritePayload(partial_snapshot.RawData()); | 515 *partial_blob_out = WritePayload(partial_snapshot.RawData()); |
516 *startup_blob_out = WritePayload(startup_snapshot.RawData()); | 516 *startup_blob_out = WritePayload(startup_snapshot.RawData()); |
517 } | 517 } |
518 v8_isolate->Dispose(); | 518 v8_isolate->Dispose(); |
519 } | 519 } |
(...skipping 1435 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1955 DisableAlwaysOpt(); | 1955 DisableAlwaysOpt(); |
1956 v8::StartupData blob; | 1956 v8::StartupData blob; |
1957 { | 1957 { |
1958 v8::SnapshotCreator creator; | 1958 v8::SnapshotCreator creator; |
1959 v8::Isolate* isolate = creator.GetIsolate(); | 1959 v8::Isolate* isolate = creator.GetIsolate(); |
1960 { | 1960 { |
1961 v8::HandleScope handle_scope(isolate); | 1961 v8::HandleScope handle_scope(isolate); |
1962 v8::Local<v8::Context> context = v8::Context::New(isolate); | 1962 v8::Local<v8::Context> context = v8::Context::New(isolate); |
1963 v8::Context::Scope context_scope(context); | 1963 v8::Context::Scope context_scope(context); |
1964 CompileRun("var f = function() { return 1; }"); | 1964 CompileRun("var f = function() { return 1; }"); |
1965 CHECK_EQ(0u, creator.AddContext(context)); | 1965 creator.SetDefaultContext(context); |
1966 } | 1966 } |
1967 { | 1967 { |
1968 v8::HandleScope handle_scope(isolate); | 1968 v8::HandleScope handle_scope(isolate); |
1969 v8::Local<v8::Context> context = v8::Context::New(isolate); | 1969 v8::Local<v8::Context> context = v8::Context::New(isolate); |
1970 v8::Context::Scope context_scope(context); | 1970 v8::Context::Scope context_scope(context); |
1971 CompileRun("var f = function() { return 2; }"); | 1971 CompileRun("var f = function() { return 2; }"); |
1972 CHECK_EQ(1u, creator.AddContext(context)); | 1972 CHECK_EQ(0u, creator.AddContext(context)); |
1973 } | 1973 } |
1974 { | 1974 { |
1975 v8::HandleScope handle_scope(isolate); | 1975 v8::HandleScope handle_scope(isolate); |
1976 v8::Local<v8::Context> context = v8::Context::New(isolate); | 1976 v8::Local<v8::Context> context = v8::Context::New(isolate); |
1977 CHECK_EQ(2u, creator.AddContext(context)); | 1977 CHECK_EQ(1u, creator.AddContext(context)); |
1978 } | 1978 } |
1979 blob = | 1979 blob = |
1980 creator.CreateBlob(v8::SnapshotCreator::FunctionCodeHandling::kClear); | 1980 creator.CreateBlob(v8::SnapshotCreator::FunctionCodeHandling::kClear); |
1981 } | 1981 } |
1982 | 1982 |
1983 v8::Isolate::CreateParams params; | 1983 v8::Isolate::CreateParams params; |
1984 params.snapshot_blob = &blob; | 1984 params.snapshot_blob = &blob; |
1985 params.array_buffer_allocator = CcTest::array_buffer_allocator(); | 1985 params.array_buffer_allocator = CcTest::array_buffer_allocator(); |
1986 v8::Isolate* isolate = v8::Isolate::New(params); | 1986 v8::Isolate* isolate = v8::Isolate::New(params); |
1987 { | 1987 { |
1988 v8::Isolate::Scope isolate_scope(isolate); | 1988 v8::Isolate::Scope isolate_scope(isolate); |
1989 { | 1989 { |
1990 v8::HandleScope handle_scope(isolate); | 1990 v8::HandleScope handle_scope(isolate); |
1991 v8::Local<v8::Context> context = | 1991 v8::Local<v8::Context> context = v8::Context::New(isolate); |
1992 v8::Context::FromSnapshot(isolate, 0).ToLocalChecked(); | |
1993 v8::Context::Scope context_scope(context); | 1992 v8::Context::Scope context_scope(context); |
1994 ExpectInt32("f()", 1); | 1993 ExpectInt32("f()", 1); |
1995 } | 1994 } |
1996 { | 1995 { |
1997 v8::HandleScope handle_scope(isolate); | 1996 v8::HandleScope handle_scope(isolate); |
1998 v8::Local<v8::Context> context = | 1997 v8::Local<v8::Context> context = |
1999 v8::Context::FromSnapshot(isolate, 1).ToLocalChecked(); | 1998 v8::Context::FromSnapshot(isolate, 0).ToLocalChecked(); |
2000 v8::Context::Scope context_scope(context); | 1999 v8::Context::Scope context_scope(context); |
2001 ExpectInt32("f()", 2); | 2000 ExpectInt32("f()", 2); |
2002 } | 2001 } |
2003 { | 2002 { |
2004 v8::HandleScope handle_scope(isolate); | 2003 v8::HandleScope handle_scope(isolate); |
2005 v8::Local<v8::Context> context = | 2004 v8::Local<v8::Context> context = |
2006 v8::Context::FromSnapshot(isolate, 2).ToLocalChecked(); | 2005 v8::Context::FromSnapshot(isolate, 1).ToLocalChecked(); |
2007 v8::Context::Scope context_scope(context); | 2006 v8::Context::Scope context_scope(context); |
2008 ExpectUndefined("this.f"); | 2007 ExpectUndefined("this.f"); |
2009 } | 2008 } |
2010 } | 2009 } |
2011 | 2010 |
2012 isolate->Dispose(); | 2011 isolate->Dispose(); |
2013 delete[] blob.data; | 2012 delete[] blob.data; |
2014 } | 2013 } |
2015 | 2014 |
2016 void SerializedCallback(const v8::FunctionCallbackInfo<v8::Value>& args) { | 2015 static void SerializedCallback( |
| 2016 const v8::FunctionCallbackInfo<v8::Value>& args) { |
2017 args.GetReturnValue().Set(v8_num(42)); | 2017 args.GetReturnValue().Set(v8_num(42)); |
2018 } | 2018 } |
2019 | 2019 |
2020 void SerializedCallbackReplacement( | 2020 static void SerializedCallbackReplacement( |
2021 const v8::FunctionCallbackInfo<v8::Value>& args) { | 2021 const v8::FunctionCallbackInfo<v8::Value>& args) { |
2022 args.GetReturnValue().Set(v8_num(1337)); | 2022 args.GetReturnValue().Set(v8_num(1337)); |
2023 } | 2023 } |
2024 | 2024 |
| 2025 static void NamedPropertyGetterForSerialization( |
| 2026 v8::Local<v8::Name> name, const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 2027 if (name->Equals(info.GetIsolate()->GetCurrentContext(), v8_str("x")) |
| 2028 .FromJust()) { |
| 2029 info.GetReturnValue().Set(v8_num(2016)); |
| 2030 } |
| 2031 } |
| 2032 |
| 2033 static void AccessorForSerialization( |
| 2034 v8::Local<v8::String> property, |
| 2035 const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 2036 info.GetReturnValue().Set(v8_num(2017)); |
| 2037 } |
| 2038 |
2025 static int serialized_static_field = 314; | 2039 static int serialized_static_field = 314; |
2026 | 2040 |
| 2041 class SerializedExtension : public v8::Extension { |
| 2042 public: |
| 2043 SerializedExtension() |
| 2044 : v8::Extension("serialized extension", |
| 2045 "native function g();" |
| 2046 "function h() { return 13; };" |
| 2047 "function i() { return 14; };" |
| 2048 "var o = { p: 7 };") {} |
| 2049 |
| 2050 virtual v8::Local<v8::FunctionTemplate> GetNativeFunctionTemplate( |
| 2051 v8::Isolate* isolate, v8::Local<v8::String> name) { |
| 2052 CHECK(name->Equals(isolate->GetCurrentContext(), v8_str("g")).FromJust()); |
| 2053 return v8::FunctionTemplate::New(isolate, FunctionCallback); |
| 2054 } |
| 2055 |
| 2056 static void FunctionCallback( |
| 2057 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 2058 args.GetReturnValue().Set(v8_num(12)); |
| 2059 } |
| 2060 }; |
| 2061 |
2027 intptr_t original_external_references[] = { | 2062 intptr_t original_external_references[] = { |
2028 reinterpret_cast<intptr_t>(SerializedCallback), | 2063 reinterpret_cast<intptr_t>(SerializedCallback), |
2029 reinterpret_cast<intptr_t>(&serialized_static_field), 0}; | 2064 reinterpret_cast<intptr_t>(&serialized_static_field), |
| 2065 reinterpret_cast<intptr_t>(&NamedPropertyGetterForSerialization), |
| 2066 reinterpret_cast<intptr_t>(&AccessorForSerialization), |
| 2067 reinterpret_cast<intptr_t>(&SerializedExtension::FunctionCallback), |
| 2068 0}; |
2030 | 2069 |
2031 intptr_t replaced_external_references[] = { | 2070 intptr_t replaced_external_references[] = { |
2032 reinterpret_cast<intptr_t>(SerializedCallbackReplacement), | 2071 reinterpret_cast<intptr_t>(SerializedCallbackReplacement), |
2033 reinterpret_cast<intptr_t>(&serialized_static_field), 0}; | 2072 reinterpret_cast<intptr_t>(&serialized_static_field), |
| 2073 reinterpret_cast<intptr_t>(&NamedPropertyGetterForSerialization), |
| 2074 reinterpret_cast<intptr_t>(&AccessorForSerialization), |
| 2075 reinterpret_cast<intptr_t>(&SerializedExtension::FunctionCallback), |
| 2076 0}; |
2034 | 2077 |
2035 TEST(SnapshotCreatorExternalReferences) { | 2078 TEST(SnapshotCreatorExternalReferences) { |
2036 DisableAlwaysOpt(); | 2079 DisableAlwaysOpt(); |
2037 v8::StartupData blob; | 2080 v8::StartupData blob; |
2038 { | 2081 { |
2039 v8::SnapshotCreator creator(original_external_references); | 2082 v8::SnapshotCreator creator(original_external_references); |
2040 v8::Isolate* isolate = creator.GetIsolate(); | 2083 v8::Isolate* isolate = creator.GetIsolate(); |
2041 { | 2084 { |
2042 v8::HandleScope handle_scope(isolate); | 2085 v8::HandleScope handle_scope(isolate); |
2043 v8::Local<v8::Context> context = v8::Context::New(isolate); | 2086 v8::Local<v8::Context> context = v8::Context::New(isolate); |
2044 v8::Context::Scope context_scope(context); | 2087 v8::Context::Scope context_scope(context); |
2045 v8::Local<v8::FunctionTemplate> callback = | 2088 v8::Local<v8::FunctionTemplate> callback = |
2046 v8::FunctionTemplate::New(isolate, SerializedCallback); | 2089 v8::FunctionTemplate::New(isolate, SerializedCallback); |
2047 v8::Local<v8::Value> function = | 2090 v8::Local<v8::Value> function = |
2048 callback->GetFunction(context).ToLocalChecked(); | 2091 callback->GetFunction(context).ToLocalChecked(); |
2049 CHECK(context->Global()->Set(context, v8_str("f"), function).FromJust()); | 2092 CHECK(context->Global()->Set(context, v8_str("f"), function).FromJust()); |
2050 ExpectInt32("f()", 42); | 2093 ExpectInt32("f()", 42); |
2051 CHECK_EQ(0u, creator.AddContext(context)); | 2094 creator.SetDefaultContext(context); |
2052 } | 2095 } |
2053 blob = | 2096 blob = |
2054 creator.CreateBlob(v8::SnapshotCreator::FunctionCodeHandling::kClear); | 2097 creator.CreateBlob(v8::SnapshotCreator::FunctionCodeHandling::kClear); |
2055 } | 2098 } |
2056 | 2099 |
2057 // Deserialize with the original external reference. | 2100 // Deserialize with the original external reference. |
2058 { | 2101 { |
2059 v8::Isolate::CreateParams params; | 2102 v8::Isolate::CreateParams params; |
2060 params.snapshot_blob = &blob; | 2103 params.snapshot_blob = &blob; |
2061 params.array_buffer_allocator = CcTest::array_buffer_allocator(); | 2104 params.array_buffer_allocator = CcTest::array_buffer_allocator(); |
2062 params.external_references = original_external_references; | 2105 params.external_references = original_external_references; |
2063 v8::Isolate* isolate = v8::Isolate::New(params); | 2106 v8::Isolate* isolate = v8::Isolate::New(params); |
2064 { | 2107 { |
2065 v8::Isolate::Scope isolate_scope(isolate); | 2108 v8::Isolate::Scope isolate_scope(isolate); |
2066 v8::HandleScope handle_scope(isolate); | 2109 v8::HandleScope handle_scope(isolate); |
2067 v8::Local<v8::Context> context = | 2110 v8::Local<v8::Context> context = v8::Context::New(isolate); |
2068 v8::Context::FromSnapshot(isolate, 0).ToLocalChecked(); | |
2069 v8::Context::Scope context_scope(context); | 2111 v8::Context::Scope context_scope(context); |
2070 ExpectInt32("f()", 42); | 2112 ExpectInt32("f()", 42); |
2071 } | 2113 } |
2072 isolate->Dispose(); | 2114 isolate->Dispose(); |
2073 } | 2115 } |
2074 | 2116 |
2075 // Deserialize with the some other external reference. | 2117 // Deserialize with the some other external reference. |
2076 { | 2118 { |
2077 v8::Isolate::CreateParams params; | 2119 v8::Isolate::CreateParams params; |
2078 params.snapshot_blob = &blob; | 2120 params.snapshot_blob = &blob; |
2079 params.array_buffer_allocator = CcTest::array_buffer_allocator(); | 2121 params.array_buffer_allocator = CcTest::array_buffer_allocator(); |
2080 params.external_references = replaced_external_references; | 2122 params.external_references = replaced_external_references; |
2081 v8::Isolate* isolate = v8::Isolate::New(params); | 2123 v8::Isolate* isolate = v8::Isolate::New(params); |
2082 { | 2124 { |
2083 v8::Isolate::Scope isolate_scope(isolate); | 2125 v8::Isolate::Scope isolate_scope(isolate); |
2084 v8::HandleScope handle_scope(isolate); | 2126 v8::HandleScope handle_scope(isolate); |
2085 v8::Local<v8::Context> context = | 2127 v8::Local<v8::Context> context = v8::Context::New(isolate); |
2086 v8::Context::FromSnapshot(isolate, 0).ToLocalChecked(); | |
2087 v8::Context::Scope context_scope(context); | 2128 v8::Context::Scope context_scope(context); |
2088 ExpectInt32("f()", 1337); | 2129 ExpectInt32("f()", 1337); |
2089 } | 2130 } |
2090 isolate->Dispose(); | 2131 isolate->Dispose(); |
2091 } | 2132 } |
2092 delete[] blob.data; | 2133 delete[] blob.data; |
2093 } | 2134 } |
2094 | 2135 |
2095 TEST(SnapshotCreatorUnknownExternalReferences) { | 2136 TEST(SnapshotCreatorUnknownExternalReferences) { |
2096 DisableAlwaysOpt(); | 2137 DisableAlwaysOpt(); |
2097 v8::SnapshotCreator creator; | 2138 v8::SnapshotCreator creator; |
2098 v8::Isolate* isolate = creator.GetIsolate(); | 2139 v8::Isolate* isolate = creator.GetIsolate(); |
2099 { | 2140 { |
2100 v8::HandleScope handle_scope(isolate); | 2141 v8::HandleScope handle_scope(isolate); |
2101 v8::Local<v8::Context> context = v8::Context::New(isolate); | 2142 v8::Local<v8::Context> context = v8::Context::New(isolate); |
2102 v8::Context::Scope context_scope(context); | 2143 v8::Context::Scope context_scope(context); |
2103 | 2144 |
2104 v8::Local<v8::FunctionTemplate> callback = | 2145 v8::Local<v8::FunctionTemplate> callback = |
2105 v8::FunctionTemplate::New(isolate, SerializedCallback); | 2146 v8::FunctionTemplate::New(isolate, SerializedCallback); |
2106 v8::Local<v8::Value> function = | 2147 v8::Local<v8::Value> function = |
2107 callback->GetFunction(context).ToLocalChecked(); | 2148 callback->GetFunction(context).ToLocalChecked(); |
2108 CHECK(context->Global()->Set(context, v8_str("f"), function).FromJust()); | 2149 CHECK(context->Global()->Set(context, v8_str("f"), function).FromJust()); |
2109 ExpectInt32("f()", 42); | 2150 ExpectInt32("f()", 42); |
2110 | 2151 |
2111 CHECK_EQ(0u, creator.AddContext(context)); | 2152 creator.SetDefaultContext(context); |
2112 } | 2153 } |
2113 v8::StartupData blob = | 2154 v8::StartupData blob = |
2114 creator.CreateBlob(v8::SnapshotCreator::FunctionCodeHandling::kClear); | 2155 creator.CreateBlob(v8::SnapshotCreator::FunctionCodeHandling::kClear); |
2115 | 2156 |
2116 delete[] blob.data; | 2157 delete[] blob.data; |
2117 } | 2158 } |
2118 | 2159 |
2119 struct InternalFieldData { | 2160 struct InternalFieldData { |
2120 uint32_t data; | 2161 uint32_t data; |
2121 }; | 2162 }; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2178 v8::External::New(isolate, &serialized_static_field); | 2219 v8::External::New(isolate, &serialized_static_field); |
2179 a->SetInternalField(0, b); | 2220 a->SetInternalField(0, b); |
2180 a->SetAlignedPointerInInternalField(1, a1); | 2221 a->SetAlignedPointerInInternalField(1, a1); |
2181 b->SetAlignedPointerInInternalField(0, b0); | 2222 b->SetAlignedPointerInInternalField(0, b0); |
2182 b->SetInternalField(1, c); | 2223 b->SetInternalField(1, c); |
2183 c->SetAlignedPointerInInternalField(0, c0); | 2224 c->SetAlignedPointerInInternalField(0, c0); |
2184 c->SetInternalField(1, null_external); | 2225 c->SetInternalField(1, null_external); |
2185 c->SetInternalField(2, field_external); | 2226 c->SetInternalField(2, field_external); |
2186 CHECK(context->Global()->Set(context, v8_str("a"), a).FromJust()); | 2227 CHECK(context->Global()->Set(context, v8_str("a"), a).FromJust()); |
2187 | 2228 |
2188 CHECK_EQ(0u, creator.AddContext(context)); | 2229 creator.SetDefaultContext(context); |
2189 CHECK_EQ(0u, creator.AddTemplate(callback)); | 2230 CHECK_EQ(0u, creator.AddTemplate(callback)); |
2190 CHECK_EQ(1u, creator.AddTemplate(global_template)); | 2231 CHECK_EQ(1u, creator.AddTemplate(global_template)); |
2191 } | 2232 } |
2192 blob = creator.CreateBlob(v8::SnapshotCreator::FunctionCodeHandling::kClear, | 2233 blob = creator.CreateBlob(v8::SnapshotCreator::FunctionCodeHandling::kClear, |
2193 SerializeInternalFields); | 2234 SerializeInternalFields); |
2194 | 2235 |
2195 delete a1; | 2236 delete a1; |
2196 delete b0; | 2237 delete b0; |
2197 delete c0; | 2238 delete c0; |
2198 } | 2239 } |
2199 | 2240 |
2200 { | 2241 { |
2201 v8::Isolate::CreateParams params; | 2242 v8::Isolate::CreateParams params; |
2202 params.snapshot_blob = &blob; | 2243 params.snapshot_blob = &blob; |
2203 params.array_buffer_allocator = CcTest::array_buffer_allocator(); | 2244 params.array_buffer_allocator = CcTest::array_buffer_allocator(); |
2204 params.external_references = original_external_references; | 2245 params.external_references = original_external_references; |
2205 params.deserialize_internal_fields_callback = DeserializeInternalFields; | 2246 params.deserialize_internal_fields_callback = DeserializeInternalFields; |
2206 v8::Isolate* isolate = v8::Isolate::New(params); | 2247 v8::Isolate* isolate = v8::Isolate::New(params); |
2207 { | 2248 { |
2208 v8::Isolate::Scope isolate_scope(isolate); | 2249 v8::Isolate::Scope isolate_scope(isolate); |
2209 { | 2250 { |
2210 // Create a new context without a new object template. | 2251 // Create a new context without a new object template. |
2211 v8::HandleScope handle_scope(isolate); | 2252 v8::HandleScope handle_scope(isolate); |
2212 v8::Local<v8::Context> context = | 2253 v8::Local<v8::Context> context = v8::Context::New(isolate); |
2213 v8::Context::FromSnapshot(isolate, 0).ToLocalChecked(); | |
2214 v8::Context::Scope context_scope(context); | 2254 v8::Context::Scope context_scope(context); |
2215 ExpectInt32("f()", 42); | 2255 ExpectInt32("f()", 42); |
2216 | 2256 |
2217 // Retrieve the snapshotted object template. | 2257 // Retrieve the snapshotted object template. |
2218 v8::Local<v8::ObjectTemplate> obj_template = | 2258 v8::Local<v8::ObjectTemplate> obj_template = |
2219 v8::ObjectTemplate::FromSnapshot(isolate, 1).ToLocalChecked(); | 2259 v8::ObjectTemplate::FromSnapshot(isolate, 1).ToLocalChecked(); |
2220 CHECK(!obj_template.IsEmpty()); | 2260 CHECK(!obj_template.IsEmpty()); |
2221 v8::Local<v8::Object> object = | 2261 v8::Local<v8::Object> object = |
2222 obj_template->NewInstance(context).ToLocalChecked(); | 2262 obj_template->NewInstance(context).ToLocalChecked(); |
2223 CHECK(context->Global()->Set(context, v8_str("o"), object).FromJust()); | 2263 CHECK(context->Global()->Set(context, v8_str("o"), object).FromJust()); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2265 CHECK(b2->IsUndefined()); | 2305 CHECK(b2->IsUndefined()); |
2266 CHECK_EQ(30u, c0->data); | 2306 CHECK_EQ(30u, c0->data); |
2267 CHECK(c1->IsExternal()); | 2307 CHECK(c1->IsExternal()); |
2268 CHECK_NULL(v8::Local<v8::External>::Cast(c1)->Value()); | 2308 CHECK_NULL(v8::Local<v8::External>::Cast(c1)->Value()); |
2269 CHECK_EQ(static_cast<void*>(&serialized_static_field), | 2309 CHECK_EQ(static_cast<void*>(&serialized_static_field), |
2270 v8::Local<v8::External>::Cast(c2)->Value()); | 2310 v8::Local<v8::External>::Cast(c2)->Value()); |
2271 | 2311 |
2272 // Accessing out of bound returns empty MaybeHandle. | 2312 // Accessing out of bound returns empty MaybeHandle. |
2273 CHECK(v8::ObjectTemplate::FromSnapshot(isolate, 2).IsEmpty()); | 2313 CHECK(v8::ObjectTemplate::FromSnapshot(isolate, 2).IsEmpty()); |
2274 CHECK(v8::FunctionTemplate::FromSnapshot(isolate, 2).IsEmpty()); | 2314 CHECK(v8::FunctionTemplate::FromSnapshot(isolate, 2).IsEmpty()); |
2275 CHECK(v8::Context::FromSnapshot(isolate, 2).IsEmpty()); | 2315 CHECK(v8::Context::FromSnapshot(isolate, 1).IsEmpty()); |
2276 | 2316 |
2277 delete a1; | 2317 delete a1; |
2278 delete b0; | 2318 delete b0; |
2279 delete c0; | 2319 delete c0; |
2280 } | 2320 } |
2281 | 2321 |
2282 { | 2322 { |
2283 // Create a context with a new object template. It is merged into the | 2323 // Create a context with a new object template. It is merged into the |
2284 // deserialized global object. | 2324 // deserialized global object. |
2285 v8::HandleScope handle_scope(isolate); | 2325 v8::HandleScope handle_scope(isolate); |
2286 v8::ExtensionConfiguration* no_extension = nullptr; | 2326 v8::ExtensionConfiguration* no_extension = nullptr; |
2287 v8::Local<v8::ObjectTemplate> global_template = | 2327 v8::Local<v8::ObjectTemplate> global_template = |
2288 v8::ObjectTemplate::New(isolate); | 2328 v8::ObjectTemplate::New(isolate); |
2289 global_template->Set( | 2329 global_template->Set( |
2290 v8_str("g"), | 2330 v8_str("g"), |
2291 v8::FunctionTemplate::New(isolate, SerializedCallbackReplacement)); | 2331 v8::FunctionTemplate::New(isolate, SerializedCallbackReplacement)); |
2292 v8::Local<v8::Context> context = | 2332 v8::Local<v8::Context> context = |
2293 v8::Context::FromSnapshot(isolate, 0, no_extension, global_template) | 2333 v8::Context::New(isolate, no_extension, global_template); |
2294 .ToLocalChecked(); | |
2295 v8::Context::Scope context_scope(context); | 2334 v8::Context::Scope context_scope(context); |
2296 ExpectInt32("g()", 1337); | 2335 ExpectInt32("g()", 1337); |
2297 ExpectInt32("f()", 42); | 2336 ExpectInt32("f()", 42); |
2298 } | 2337 } |
2299 } | 2338 } |
2300 isolate->Dispose(); | 2339 isolate->Dispose(); |
2301 } | 2340 } |
2302 delete[] blob.data; | 2341 delete[] blob.data; |
2303 } | 2342 } |
2304 | 2343 |
| 2344 TEST(SnapshotCreatorIncludeGlobalProxy) { |
| 2345 DisableAlwaysOpt(); |
| 2346 v8::StartupData blob; |
| 2347 |
| 2348 { |
| 2349 v8::SnapshotCreator creator(original_external_references); |
| 2350 v8::Isolate* isolate = creator.GetIsolate(); |
| 2351 v8::RegisterExtension(new SerializedExtension); |
| 2352 const char* extension_names[] = {"serialized extension"}; |
| 2353 v8::ExtensionConfiguration extensions(1, extension_names); |
| 2354 { |
| 2355 // Set default context. This context implicitly does *not* serialize |
| 2356 // the global proxy, and upon deserialization one has to be created |
| 2357 // in the bootstrapper from the global object template. |
| 2358 // Side effects from extensions are persisted though. |
| 2359 v8::HandleScope handle_scope(isolate); |
| 2360 v8::Local<v8::ObjectTemplate> global_template = |
| 2361 v8::ObjectTemplate::New(isolate); |
| 2362 v8::Local<v8::FunctionTemplate> callback = |
| 2363 v8::FunctionTemplate::New(isolate, SerializedCallback); |
| 2364 global_template->Set(v8_str("f"), callback); |
| 2365 global_template->SetHandler(v8::NamedPropertyHandlerConfiguration( |
| 2366 NamedPropertyGetterForSerialization)); |
| 2367 v8::Local<v8::Context> context = |
| 2368 v8::Context::New(isolate, &extensions, global_template); |
| 2369 v8::Context::Scope context_scope(context); |
| 2370 ExpectInt32("f()", 42); |
| 2371 ExpectInt32("g()", 12); |
| 2372 ExpectInt32("h()", 13); |
| 2373 ExpectInt32("o.p", 7); |
| 2374 ExpectInt32("x", 2016); |
| 2375 creator.SetDefaultContext(context); |
| 2376 } |
| 2377 { |
| 2378 // Add additional context. This context implicitly *does* serialize |
| 2379 // the global proxy, and upon deserialization one has to be created |
| 2380 // in the bootstrapper from the global object template. |
| 2381 // Side effects from extensions are persisted. |
| 2382 v8::HandleScope handle_scope(isolate); |
| 2383 v8::Local<v8::ObjectTemplate> global_template = |
| 2384 v8::ObjectTemplate::New(isolate); |
| 2385 v8::Local<v8::FunctionTemplate> callback = |
| 2386 v8::FunctionTemplate::New(isolate, SerializedCallback); |
| 2387 global_template->Set(v8_str("f"), callback); |
| 2388 global_template->SetHandler(v8::NamedPropertyHandlerConfiguration( |
| 2389 NamedPropertyGetterForSerialization)); |
| 2390 global_template->SetAccessor(v8_str("y"), AccessorForSerialization); |
| 2391 v8::Local<v8::Context> context = |
| 2392 v8::Context::New(isolate, &extensions, global_template); |
| 2393 v8::Context::Scope context_scope(context); |
| 2394 ExpectInt32("f()", 42); |
| 2395 ExpectInt32("g()", 12); |
| 2396 ExpectInt32("h()", 13); |
| 2397 ExpectInt32("o.p", 7); |
| 2398 ExpectInt32("x", 2016); |
| 2399 ExpectInt32("y", 2017); |
| 2400 CHECK_EQ(0u, creator.AddContext(context)); |
| 2401 } |
| 2402 blob = creator.CreateBlob(v8::SnapshotCreator::FunctionCodeHandling::kClear, |
| 2403 SerializeInternalFields); |
| 2404 } |
| 2405 |
| 2406 { |
| 2407 v8::Isolate::CreateParams params; |
| 2408 params.snapshot_blob = &blob; |
| 2409 params.array_buffer_allocator = CcTest::array_buffer_allocator(); |
| 2410 params.external_references = original_external_references; |
| 2411 params.deserialize_internal_fields_callback = DeserializeInternalFields; |
| 2412 v8::Isolate* isolate = v8::Isolate::New(params); |
| 2413 { |
| 2414 v8::Isolate::Scope isolate_scope(isolate); |
| 2415 // We can introduce new extensions, which could override the already |
| 2416 // snapshotted extension. |
| 2417 v8::Extension* extension = new v8::Extension("new extension", |
| 2418 "function i() { return 24; }" |
| 2419 "function j() { return 25; }" |
| 2420 "if (o.p == 7) o.p++;"); |
| 2421 extension->set_auto_enable(true); |
| 2422 v8::RegisterExtension(extension); |
| 2423 { |
| 2424 // Create a new context from default context snapshot. This will |
| 2425 // create a new global object from a new global object template |
| 2426 // without the interceptor. |
| 2427 v8::HandleScope handle_scope(isolate); |
| 2428 v8::Local<v8::Context> context = v8::Context::New(isolate); |
| 2429 v8::Context::Scope context_scope(context); |
| 2430 ExpectInt32("f()", 42); |
| 2431 ExpectInt32("g()", 12); |
| 2432 ExpectInt32("h()", 13); |
| 2433 ExpectInt32("i()", 24); |
| 2434 ExpectInt32("j()", 25); |
| 2435 ExpectInt32("o.p", 8); |
| 2436 v8::TryCatch try_catch(isolate); |
| 2437 CHECK(CompileRun("x").IsEmpty()); |
| 2438 CHECK(try_catch.HasCaught()); |
| 2439 } |
| 2440 { |
| 2441 // Create a new context from first additional context snapshot. This |
| 2442 // will use the global object from the snapshot, including interceptor. |
| 2443 v8::HandleScope handle_scope(isolate); |
| 2444 v8::Local<v8::Context> context = |
| 2445 v8::Context::FromSnapshot(isolate, 0).ToLocalChecked(); |
| 2446 v8::Context::Scope context_scope(context); |
| 2447 ExpectInt32("f()", 42); |
| 2448 ExpectInt32("g()", 12); |
| 2449 ExpectInt32("h()", 13); |
| 2450 ExpectInt32("i()", 24); |
| 2451 ExpectInt32("j()", 25); |
| 2452 ExpectInt32("o.p", 8); |
| 2453 ExpectInt32("x", 2016); |
| 2454 ExpectInt32("y", 2017); |
| 2455 } |
| 2456 } |
| 2457 isolate->Dispose(); |
| 2458 } |
| 2459 delete[] blob.data; |
| 2460 } |
| 2461 |
2305 TEST(SerializationMemoryStats) { | 2462 TEST(SerializationMemoryStats) { |
2306 FLAG_profile_deserialization = true; | 2463 FLAG_profile_deserialization = true; |
2307 FLAG_always_opt = false; | 2464 FLAG_always_opt = false; |
2308 v8::StartupData blob = v8::V8::CreateSnapshotDataBlob(); | 2465 v8::StartupData blob = v8::V8::CreateSnapshotDataBlob(); |
2309 delete[] blob.data; | 2466 delete[] blob.data; |
2310 } | 2467 } |
OLD | NEW |