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

Side by Side Diff: test/cctest/test-serialize.cc

Issue 2452333002: [serializer] introduce API to serialize internal fields (Closed)
Patch Set: Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 269 matching lines...) Expand 10 before | Expand all | Expand 10 after
280 { 280 {
281 v8::HandleScope handle_scope(v8_isolate); 281 v8::HandleScope handle_scope(v8_isolate);
282 v8::Local<v8::Context>::New(v8_isolate, env)->Exit(); 282 v8::Local<v8::Context>::New(v8_isolate, env)->Exit();
283 } 283 }
284 env.Reset(); 284 env.Reset();
285 285
286 StartupSerializer startup_serializer( 286 StartupSerializer startup_serializer(
287 isolate, v8::SnapshotCreator::FunctionCodeHandling::kClear); 287 isolate, v8::SnapshotCreator::FunctionCodeHandling::kClear);
288 startup_serializer.SerializeStrongReferences(); 288 startup_serializer.SerializeStrongReferences();
289 289
290 PartialSerializer partial_serializer(isolate, &startup_serializer); 290 PartialSerializer partial_serializer(isolate, &startup_serializer, nullptr);
291 partial_serializer.Serialize(&raw_foo); 291 partial_serializer.Serialize(&raw_foo);
292 292
293 startup_serializer.SerializeWeakReferencesAndDeferred(); 293 startup_serializer.SerializeWeakReferencesAndDeferred();
294 294
295 SnapshotData startup_snapshot(&startup_serializer); 295 SnapshotData startup_snapshot(&startup_serializer);
296 SnapshotData partial_snapshot(&partial_serializer); 296 SnapshotData partial_snapshot(&partial_serializer);
297 297
298 *partial_blob_out = WritePayload(partial_snapshot.RawData()); 298 *partial_blob_out = WritePayload(partial_snapshot.RawData());
299 *startup_blob_out = WritePayload(startup_snapshot.RawData()); 299 *startup_blob_out = WritePayload(startup_snapshot.RawData());
300 } 300 }
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
380 i::Object* raw_context = *v8::Utils::OpenPersistent(env); 380 i::Object* raw_context = *v8::Utils::OpenPersistent(env);
381 381
382 env.Reset(); 382 env.Reset();
383 383
384 SnapshotByteSink startup_sink; 384 SnapshotByteSink startup_sink;
385 StartupSerializer startup_serializer( 385 StartupSerializer startup_serializer(
386 isolate, v8::SnapshotCreator::FunctionCodeHandling::kClear); 386 isolate, v8::SnapshotCreator::FunctionCodeHandling::kClear);
387 startup_serializer.SerializeStrongReferences(); 387 startup_serializer.SerializeStrongReferences();
388 388
389 SnapshotByteSink partial_sink; 389 SnapshotByteSink partial_sink;
390 PartialSerializer partial_serializer(isolate, &startup_serializer); 390 PartialSerializer partial_serializer(isolate, &startup_serializer, nullptr);
391 partial_serializer.Serialize(&raw_context); 391 partial_serializer.Serialize(&raw_context);
392 startup_serializer.SerializeWeakReferencesAndDeferred(); 392 startup_serializer.SerializeWeakReferencesAndDeferred();
393 393
394 SnapshotData startup_snapshot(&startup_serializer); 394 SnapshotData startup_snapshot(&startup_serializer);
395 SnapshotData partial_snapshot(&partial_serializer); 395 SnapshotData partial_snapshot(&partial_serializer);
396 396
397 *partial_blob_out = WritePayload(partial_snapshot.RawData()); 397 *partial_blob_out = WritePayload(partial_snapshot.RawData());
398 *startup_blob_out = WritePayload(startup_snapshot.RawData()); 398 *startup_blob_out = WritePayload(startup_snapshot.RawData());
399 } 399 }
400 v8_isolate->Dispose(); 400 v8_isolate->Dispose();
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
499 i::Object* raw_context = *v8::Utils::OpenPersistent(env); 499 i::Object* raw_context = *v8::Utils::OpenPersistent(env);
500 500
501 env.Reset(); 501 env.Reset();
502 502
503 SnapshotByteSink startup_sink; 503 SnapshotByteSink startup_sink;
504 StartupSerializer startup_serializer( 504 StartupSerializer startup_serializer(
505 isolate, v8::SnapshotCreator::FunctionCodeHandling::kClear); 505 isolate, v8::SnapshotCreator::FunctionCodeHandling::kClear);
506 startup_serializer.SerializeStrongReferences(); 506 startup_serializer.SerializeStrongReferences();
507 507
508 SnapshotByteSink partial_sink; 508 SnapshotByteSink partial_sink;
509 PartialSerializer partial_serializer(isolate, &startup_serializer); 509 PartialSerializer partial_serializer(isolate, &startup_serializer, nullptr);
510 partial_serializer.Serialize(&raw_context); 510 partial_serializer.Serialize(&raw_context);
511 startup_serializer.SerializeWeakReferencesAndDeferred(); 511 startup_serializer.SerializeWeakReferencesAndDeferred();
512 512
513 SnapshotData startup_snapshot(&startup_serializer); 513 SnapshotData startup_snapshot(&startup_serializer);
514 SnapshotData partial_snapshot(&partial_serializer); 514 SnapshotData partial_snapshot(&partial_serializer);
515 515
516 *partial_blob_out = WritePayload(partial_snapshot.RawData()); 516 *partial_blob_out = WritePayload(partial_snapshot.RawData());
517 *startup_blob_out = WritePayload(startup_snapshot.RawData()); 517 *startup_blob_out = WritePayload(startup_snapshot.RawData());
518 } 518 }
519 v8_isolate->Dispose(); 519 v8_isolate->Dispose();
(...skipping 1582 matching lines...) Expand 10 before | Expand all | Expand 10 after
2102 ExpectInt32("f()", 42); 2102 ExpectInt32("f()", 42);
2103 2103
2104 CHECK_EQ(0, creator.AddContext(context)); 2104 CHECK_EQ(0, creator.AddContext(context));
2105 } 2105 }
2106 v8::StartupData blob = 2106 v8::StartupData blob =
2107 creator.CreateBlob(v8::SnapshotCreator::FunctionCodeHandling::kClear); 2107 creator.CreateBlob(v8::SnapshotCreator::FunctionCodeHandling::kClear);
2108 2108
2109 delete[] blob.data; 2109 delete[] blob.data;
2110 } 2110 }
2111 2111
2112 struct InternalFieldData {
2113 uint32_t data;
2114 };
2115
2116 v8::StartupData SerializeInternalFields(v8::Local<v8::Object> holder,
2117 int index) {
2118 InternalFieldData* data = static_cast<InternalFieldData*>(
2119 holder->GetAlignedPointerFromInternalField(index));
2120 int size = sizeof(*data);
2121 char* payload = new char[size];
2122 // We simply use memcpy to serialize the content.
2123 memcpy(payload, data, size);
2124 return {payload, size};
2125 }
2126
2127 void DeserializeInternalFields(v8::Local<v8::Object> holder, int index,
2128 v8::StartupData payload) {
2129 InternalFieldData* data = new InternalFieldData{0};
2130 memcpy(data, payload.data, payload.raw_size);
2131 holder->SetAlignedPointerInInternalField(index, data);
2132 }
2133
2112 TEST(SnapshotCreatorTemplates) { 2134 TEST(SnapshotCreatorTemplates) {
2113 DisableTurbofan(); 2135 DisableTurbofan();
2114 v8::StartupData blob; 2136 v8::StartupData blob;
2137
2115 { 2138 {
2139 InternalFieldData* a1 = new InternalFieldData{11};
2140 InternalFieldData* b0 = new InternalFieldData{20};
2141 InternalFieldData* c0 = new InternalFieldData{30};
2142 InternalFieldData* c1 = new InternalFieldData{31};
2143
2116 v8::SnapshotCreator creator(original_external_references); 2144 v8::SnapshotCreator creator(original_external_references);
2117 v8::Isolate* isolate = creator.GetIsolate(); 2145 v8::Isolate* isolate = creator.GetIsolate();
2118 { 2146 {
2119 v8::HandleScope handle_scope(isolate); 2147 v8::HandleScope handle_scope(isolate);
2120 v8::ExtensionConfiguration* no_extension = nullptr; 2148 v8::ExtensionConfiguration* no_extension = nullptr;
2121 v8::Local<v8::ObjectTemplate> global_template = 2149 v8::Local<v8::ObjectTemplate> global_template =
2122 v8::ObjectTemplate::New(isolate); 2150 v8::ObjectTemplate::New(isolate);
2123 v8::Local<v8::FunctionTemplate> callback = 2151 v8::Local<v8::FunctionTemplate> callback =
2124 v8::FunctionTemplate::New(isolate, SerializedCallback); 2152 v8::FunctionTemplate::New(isolate, SerializedCallback);
2125 global_template->Set(v8_str("f"), callback); 2153 global_template->Set(v8_str("f"), callback);
2126 v8::Local<v8::Context> context = 2154 v8::Local<v8::Context> context =
2127 v8::Context::New(isolate, no_extension, global_template); 2155 v8::Context::New(isolate, no_extension, global_template);
2156 v8::Local<v8::ObjectTemplate> object_template =
2157 v8::ObjectTemplate::New(isolate);
2158 object_template->SetInternalFieldCount(2);
2159
2128 v8::Context::Scope context_scope(context); 2160 v8::Context::Scope context_scope(context);
2129 ExpectInt32("f()", 42); 2161 ExpectInt32("f()", 42);
2162
2163 v8::Local<v8::Object> a =
2164 object_template->NewInstance(context).ToLocalChecked();
2165 v8::Local<v8::Object> b =
2166 object_template->NewInstance(context).ToLocalChecked();
2167 v8::Local<v8::Object> c =
2168 object_template->NewInstance(context).ToLocalChecked();
2169 a->SetInternalField(0, b);
2170 a->SetAlignedPointerInInternalField(1, a1);
2171 b->SetAlignedPointerInInternalField(0, b0);
2172 b->SetInternalField(1, c);
2173 c->SetAlignedPointerInInternalField(0, c0);
2174 c->SetAlignedPointerInInternalField(1, c1);
2175 CHECK(context->Global()->Set(context, v8_str("a"), a).FromJust());
2176
2130 CHECK_EQ(0, creator.AddContext(context)); 2177 CHECK_EQ(0, creator.AddContext(context));
2131 CHECK_EQ(0, creator.AddTemplate(callback)); 2178 CHECK_EQ(0, creator.AddTemplate(callback));
2132 CHECK_EQ(1, creator.AddTemplate(global_template)); 2179 CHECK_EQ(1, creator.AddTemplate(global_template));
2133 } 2180 }
2134 blob = 2181 blob = creator.CreateBlob(v8::SnapshotCreator::FunctionCodeHandling::kClear,
2135 creator.CreateBlob(v8::SnapshotCreator::FunctionCodeHandling::kClear); 2182 SerializeInternalFields);
2183
2184 delete a1;
2185 delete b0;
2186 delete c1;
2187 delete c0;
2136 } 2188 }
2137 2189
2138 { 2190 {
2139 v8::Isolate::CreateParams params; 2191 v8::Isolate::CreateParams params;
2140 params.snapshot_blob = &blob; 2192 params.snapshot_blob = &blob;
2141 params.array_buffer_allocator = CcTest::array_buffer_allocator(); 2193 params.array_buffer_allocator = CcTest::array_buffer_allocator();
2142 params.external_references = original_external_references; 2194 params.external_references = original_external_references;
2195 params.deserialize_internal_fields_callback = DeserializeInternalFields;
2143 v8::Isolate* isolate = v8::Isolate::New(params); 2196 v8::Isolate* isolate = v8::Isolate::New(params);
2144 { 2197 {
2145 v8::Isolate::Scope isolate_scope(isolate); 2198 v8::Isolate::Scope isolate_scope(isolate);
2146 { 2199 {
2147 // Create a new context without a new object template. 2200 // Create a new context without a new object template.
2148 v8::HandleScope handle_scope(isolate); 2201 v8::HandleScope handle_scope(isolate);
2149 v8::Local<v8::Context> context = 2202 v8::Local<v8::Context> context =
2150 v8::Context::FromSnapshot(isolate, 0).ToLocalChecked(); 2203 v8::Context::FromSnapshot(isolate, 0).ToLocalChecked();
2151 v8::Context::Scope context_scope(context); 2204 v8::Context::Scope context_scope(context);
2152 ExpectInt32("f()", 42); 2205 ExpectInt32("f()", 42);
(...skipping 13 matching lines...) Expand all
2166 v8::Local<v8::FunctionTemplate> fun_template = 2219 v8::Local<v8::FunctionTemplate> fun_template =
2167 v8::FunctionTemplate::FromSnapshot(isolate, 0).ToLocalChecked(); 2220 v8::FunctionTemplate::FromSnapshot(isolate, 0).ToLocalChecked();
2168 CHECK(!fun_template.IsEmpty()); 2221 CHECK(!fun_template.IsEmpty());
2169 v8::Local<v8::Function> fun = 2222 v8::Local<v8::Function> fun =
2170 fun_template->GetFunction(context).ToLocalChecked(); 2223 fun_template->GetFunction(context).ToLocalChecked();
2171 CHECK(context->Global()->Set(context, v8_str("g"), fun).FromJust()); 2224 CHECK(context->Global()->Set(context, v8_str("g"), fun).FromJust());
2172 ExpectInt32("g()", 42); 2225 ExpectInt32("g()", 42);
2173 // Check that it instantiates to the same prototype. 2226 // Check that it instantiates to the same prototype.
2174 ExpectTrue("g.prototype === f.prototype"); 2227 ExpectTrue("g.prototype === f.prototype");
2175 2228
2229 // Retrieve internal fields.
2230 v8::Local<v8::Object> a = context->Global()
2231 ->Get(context, v8_str("a"))
2232 .ToLocalChecked()
2233 ->ToObject(context)
2234 .ToLocalChecked();
2235 v8::Local<v8::Object> b =
2236 a->GetInternalField(0)->ToObject(context).ToLocalChecked();
2237 InternalFieldData* a1 = reinterpret_cast<InternalFieldData*>(
2238 a->GetAlignedPointerFromInternalField(1));
2239 InternalFieldData* b0 = reinterpret_cast<InternalFieldData*>(
2240 b->GetAlignedPointerFromInternalField(0));
2241 v8::Local<v8::Object> c =
2242 b->GetInternalField(1)->ToObject(context).ToLocalChecked();
2243 InternalFieldData* c0 = reinterpret_cast<InternalFieldData*>(
2244 c->GetAlignedPointerFromInternalField(0));
2245 InternalFieldData* c1 = reinterpret_cast<InternalFieldData*>(
2246 c->GetAlignedPointerFromInternalField(1));
2247
2248 CHECK_EQ(11, a1->data);
2249 CHECK_EQ(20, b0->data);
2250 CHECK_EQ(30, c0->data);
2251 CHECK_EQ(31, c1->data);
2252
2176 // Accessing out of bound returns empty MaybeHandle. 2253 // Accessing out of bound returns empty MaybeHandle.
2177 CHECK(v8::ObjectTemplate::FromSnapshot(isolate, 2).IsEmpty()); 2254 CHECK(v8::ObjectTemplate::FromSnapshot(isolate, 2).IsEmpty());
2178 CHECK(v8::FunctionTemplate::FromSnapshot(isolate, 2).IsEmpty()); 2255 CHECK(v8::FunctionTemplate::FromSnapshot(isolate, 2).IsEmpty());
2179 CHECK(v8::Context::FromSnapshot(isolate, 2).IsEmpty()); 2256 CHECK(v8::Context::FromSnapshot(isolate, 2).IsEmpty());
2257
2258 delete a1;
2259 delete b0;
2260 delete c1;
2261 delete c0;
2180 } 2262 }
2181 2263
2182 { 2264 {
2183 // Create a context with a new object template. It is merged into the 2265 // Create a context with a new object template. It is merged into the
2184 // deserialized global object. 2266 // deserialized global object.
2185 v8::HandleScope handle_scope(isolate); 2267 v8::HandleScope handle_scope(isolate);
2186 v8::ExtensionConfiguration* no_extension = nullptr; 2268 v8::ExtensionConfiguration* no_extension = nullptr;
2187 v8::Local<v8::ObjectTemplate> global_template = 2269 v8::Local<v8::ObjectTemplate> global_template =
2188 v8::ObjectTemplate::New(isolate); 2270 v8::ObjectTemplate::New(isolate);
2189 global_template->Set( 2271 global_template->Set(
(...skipping 11 matching lines...) Expand all
2201 } 2283 }
2202 delete[] blob.data; 2284 delete[] blob.data;
2203 } 2285 }
2204 2286
2205 TEST(SerializationMemoryStats) { 2287 TEST(SerializationMemoryStats) {
2206 FLAG_profile_deserialization = true; 2288 FLAG_profile_deserialization = true;
2207 FLAG_always_opt = false; 2289 FLAG_always_opt = false;
2208 v8::StartupData blob = v8::V8::CreateSnapshotDataBlob(); 2290 v8::StartupData blob = v8::V8::CreateSnapshotDataBlob();
2209 delete[] blob.data; 2291 delete[] blob.data;
2210 } 2292 }
OLDNEW
« src/snapshot/partial-serializer.cc ('K') | « src/snapshot/serializer-common.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698