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

Side by Side Diff: src/api.cc

Issue 2076083002: [snapshot] support including templates in the snapshot. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@externalref
Patch Set: fix build Created 4 years, 6 months 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
« no previous file with comments | « include/v8.h ('k') | src/heap/heap.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "src/api.h" 5 #include "src/api.h"
6 6
7 #include <string.h> // For memcpy, strlen. 7 #include <string.h> // For memcpy, strlen.
8 #ifdef V8_USE_ADDRESS_SANITIZER 8 #ifdef V8_USE_ADDRESS_SANITIZER
9 #include <sanitizer/asan_interface.h> 9 #include <sanitizer/asan_interface.h>
10 #endif // V8_USE_ADDRESS_SANITIZER 10 #endif // V8_USE_ADDRESS_SANITIZER
(...skipping 368 matching lines...) Expand 10 before | Expand all | Expand 10 after
379 i::PrintF("Executing custom snapshot script %s took %0.3f ms\n", name, 379 i::PrintF("Executing custom snapshot script %s took %0.3f ms\n", name,
380 timer.Elapsed().InMillisecondsF()); 380 timer.Elapsed().InMillisecondsF());
381 } 381 }
382 timer.Stop(); 382 timer.Stop();
383 CHECK(!try_catch.HasCaught()); 383 CHECK(!try_catch.HasCaught());
384 return true; 384 return true;
385 } 385 }
386 386
387 struct SnapshotCreatorData { 387 struct SnapshotCreatorData {
388 explicit SnapshotCreatorData(Isolate* isolate) 388 explicit SnapshotCreatorData(Isolate* isolate)
389 : isolate_(isolate), contexts_(isolate), created_(false) {} 389 : isolate_(isolate),
390 contexts_(isolate),
391 templates_(isolate),
392 created_(false) {}
390 393
391 static SnapshotCreatorData* cast(void* data) { 394 static SnapshotCreatorData* cast(void* data) {
392 return reinterpret_cast<SnapshotCreatorData*>(data); 395 return reinterpret_cast<SnapshotCreatorData*>(data);
393 } 396 }
394 397
395 ArrayBufferAllocator allocator_; 398 ArrayBufferAllocator allocator_;
396 Isolate* isolate_; 399 Isolate* isolate_;
397 PersistentValueVector<Context> contexts_; 400 PersistentValueVector<Context> contexts_;
401 PersistentValueVector<Template> templates_;
398 bool created_; 402 bool created_;
399 }; 403 };
400 404
401 } // namespace 405 } // namespace
402 406
403 SnapshotCreator::SnapshotCreator(intptr_t* external_references, 407 SnapshotCreator::SnapshotCreator(intptr_t* external_references,
404 StartupData* existing_snapshot) { 408 StartupData* existing_snapshot) {
405 i::Isolate* internal_isolate = new i::Isolate(true); 409 i::Isolate* internal_isolate = new i::Isolate(true);
406 Isolate* isolate = reinterpret_cast<Isolate*>(internal_isolate); 410 Isolate* isolate = reinterpret_cast<Isolate*>(internal_isolate);
407 SnapshotCreatorData* data = new SnapshotCreatorData(isolate); 411 SnapshotCreatorData* data = new SnapshotCreatorData(isolate);
(...skipping 27 matching lines...) Expand all
435 DCHECK(!context.IsEmpty()); 439 DCHECK(!context.IsEmpty());
436 SnapshotCreatorData* data = SnapshotCreatorData::cast(data_); 440 SnapshotCreatorData* data = SnapshotCreatorData::cast(data_);
437 DCHECK(!data->created_); 441 DCHECK(!data->created_);
438 Isolate* isolate = data->isolate_; 442 Isolate* isolate = data->isolate_;
439 CHECK_EQ(isolate, context->GetIsolate()); 443 CHECK_EQ(isolate, context->GetIsolate());
440 size_t index = static_cast<int>(data->contexts_.Size()); 444 size_t index = static_cast<int>(data->contexts_.Size());
441 data->contexts_.Append(context); 445 data->contexts_.Append(context);
442 return index; 446 return index;
443 } 447 }
444 448
449 size_t SnapshotCreator::AddTemplate(Local<Template> template_obj) {
450 DCHECK(!template_obj.IsEmpty());
451 SnapshotCreatorData* data = SnapshotCreatorData::cast(data_);
452 DCHECK(!data->created_);
453 DCHECK_EQ(reinterpret_cast<i::Isolate*>(data->isolate_),
454 Utils::OpenHandle(*template_obj)->GetIsolate());
455 size_t index = static_cast<int>(data->templates_.Size());
456 data->templates_.Append(template_obj);
457 return index;
458 }
459
445 StartupData SnapshotCreator::CreateBlob( 460 StartupData SnapshotCreator::CreateBlob(
446 SnapshotCreator::FunctionCodeHandling function_code_handling) { 461 SnapshotCreator::FunctionCodeHandling function_code_handling) {
447 SnapshotCreatorData* data = SnapshotCreatorData::cast(data_); 462 SnapshotCreatorData* data = SnapshotCreatorData::cast(data_);
448 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(data->isolate_); 463 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(data->isolate_);
449 DCHECK(!data->created_); 464 DCHECK(!data->created_);
450 465
466 {
467 int num_templates = static_cast<int>(data->templates_.Size());
468 i::HandleScope scope(isolate);
469 i::Handle<i::FixedArray> templates =
470 isolate->factory()->NewFixedArray(num_templates, i::TENURED);
471 for (int i = 0; i < num_templates; i++) {
472 templates->set(i, *v8::Utils::OpenHandle(*data->templates_.Get(i)));
473 }
474 isolate->heap()->SetSerializedTemplates(*templates);
475 data->templates_.Clear();
476 }
477
451 // If we don't do this then we end up with a stray root pointing at the 478 // If we don't do this then we end up with a stray root pointing at the
452 // context even after we have disposed of the context. 479 // context even after we have disposed of the context.
453 isolate->heap()->CollectAllAvailableGarbage("mksnapshot"); 480 isolate->heap()->CollectAllAvailableGarbage("mksnapshot");
454 isolate->heap()->CompactWeakFixedArrays(); 481 isolate->heap()->CompactWeakFixedArrays();
455 482
456 i::DisallowHeapAllocation no_gc_from_here_on; 483 i::DisallowHeapAllocation no_gc_from_here_on;
457 484
458 int num_contexts = static_cast<int>(data->contexts_.Size()); 485 int num_contexts = static_cast<int>(data->contexts_.Size());
459 i::List<i::Object*> contexts(num_contexts); 486 i::List<i::Object*> contexts(num_contexts);
460 for (int i = 0; i < num_contexts; i++) { 487 for (int i = 0; i < num_contexts; i++) {
(...skipping 705 matching lines...) Expand 10 before | Expand all | Expand 10 after
1166 int length) { 1193 int length) {
1167 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); 1194 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
1168 // Changes to the environment cannot be captured in the snapshot. Expect no 1195 // Changes to the environment cannot be captured in the snapshot. Expect no
1169 // function templates when the isolate is created for serialization. 1196 // function templates when the isolate is created for serialization.
1170 LOG_API(i_isolate, FunctionTemplate, New); 1197 LOG_API(i_isolate, FunctionTemplate, New);
1171 ENTER_V8(i_isolate); 1198 ENTER_V8(i_isolate);
1172 return FunctionTemplateNew(i_isolate, callback, nullptr, data, signature, 1199 return FunctionTemplateNew(i_isolate, callback, nullptr, data, signature,
1173 length, false); 1200 length, false);
1174 } 1201 }
1175 1202
1203 Local<FunctionTemplate> FunctionTemplate::FromSnapshot(Isolate* isolate,
1204 size_t index) {
1205 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
1206 i::FixedArray* templates = i_isolate->heap()->serialized_templates();
1207 int int_index = static_cast<int>(index);
1208 if (int_index < templates->length()) {
1209 i::Object* info = i_isolate->heap()->serialized_templates()->get(int_index);
1210 if (info->IsFunctionTemplateInfo()) {
1211 return Utils::ToLocal(i::Handle<i::FunctionTemplateInfo>(
1212 i::FunctionTemplateInfo::cast(info)));
1213 }
1214 }
1215 return Local<FunctionTemplate>();
1216 }
1176 1217
1177 Local<FunctionTemplate> FunctionTemplate::NewWithFastHandler( 1218 Local<FunctionTemplate> FunctionTemplate::NewWithFastHandler(
1178 Isolate* isolate, FunctionCallback callback, 1219 Isolate* isolate, FunctionCallback callback,
1179 experimental::FastAccessorBuilder* fast_handler, v8::Local<Value> data, 1220 experimental::FastAccessorBuilder* fast_handler, v8::Local<Value> data,
1180 v8::Local<Signature> signature, int length) { 1221 v8::Local<Signature> signature, int length) {
1181 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); 1222 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
1182 DCHECK(!i_isolate->serializer_enabled()); 1223 DCHECK(!i_isolate->serializer_enabled());
1183 LOG_API(i_isolate, FunctionTemplate, NewWithFastHandler); 1224 LOG_API(i_isolate, FunctionTemplate, NewWithFastHandler);
1184 ENTER_V8(i_isolate); 1225 ENTER_V8(i_isolate);
1185 return FunctionTemplateNew(i_isolate, callback, fast_handler, data, signature, 1226 return FunctionTemplateNew(i_isolate, callback, fast_handler, data, signature,
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after
1377 obj->set_constructor(*Utils::OpenHandle(*constructor)); 1418 obj->set_constructor(*Utils::OpenHandle(*constructor));
1378 obj->set_internal_field_count(i::Smi::FromInt(0)); 1419 obj->set_internal_field_count(i::Smi::FromInt(0));
1379 return Utils::ToLocal(obj); 1420 return Utils::ToLocal(obj);
1380 } 1421 }
1381 1422
1382 Local<ObjectTemplate> ObjectTemplate::New( 1423 Local<ObjectTemplate> ObjectTemplate::New(
1383 i::Isolate* isolate, v8::Local<FunctionTemplate> constructor) { 1424 i::Isolate* isolate, v8::Local<FunctionTemplate> constructor) {
1384 return ObjectTemplateNew(isolate, constructor, false); 1425 return ObjectTemplateNew(isolate, constructor, false);
1385 } 1426 }
1386 1427
1428 Local<ObjectTemplate> ObjectTemplate::FromSnapshot(Isolate* isolate,
1429 size_t index) {
1430 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
1431 i::FixedArray* templates = i_isolate->heap()->serialized_templates();
1432 int int_index = static_cast<int>(index);
1433 if (int_index < templates->length()) {
1434 i::Object* info = i_isolate->heap()->serialized_templates()->get(int_index);
1435 if (info->IsObjectTemplateInfo()) {
1436 return Utils::ToLocal(
1437 i::Handle<i::ObjectTemplateInfo>(i::ObjectTemplateInfo::cast(info)));
1438 }
1439 }
1440 return Local<ObjectTemplate>();
1441 }
1442
1387 // Ensure that the object template has a constructor. If no 1443 // Ensure that the object template has a constructor. If no
1388 // constructor is available we create one. 1444 // constructor is available we create one.
1389 static i::Handle<i::FunctionTemplateInfo> EnsureConstructor( 1445 static i::Handle<i::FunctionTemplateInfo> EnsureConstructor(
1390 i::Isolate* isolate, 1446 i::Isolate* isolate,
1391 ObjectTemplate* object_template) { 1447 ObjectTemplate* object_template) {
1392 i::Object* obj = Utils::OpenHandle(object_template)->constructor(); 1448 i::Object* obj = Utils::OpenHandle(object_template)->constructor();
1393 if (!obj->IsUndefined(isolate)) { 1449 if (!obj->IsUndefined(isolate)) {
1394 i::FunctionTemplateInfo* info = i::FunctionTemplateInfo::cast(obj); 1450 i::FunctionTemplateInfo* info = i::FunctionTemplateInfo::cast(obj);
1395 return i::Handle<i::FunctionTemplateInfo>(info, isolate); 1451 return i::Handle<i::FunctionTemplateInfo>(info, isolate);
1396 } 1452 }
(...skipping 7425 matching lines...) Expand 10 before | Expand all | Expand 10 after
8822 Address callback_address = 8878 Address callback_address =
8823 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback)); 8879 reinterpret_cast<Address>(reinterpret_cast<intptr_t>(callback));
8824 VMState<EXTERNAL> state(isolate); 8880 VMState<EXTERNAL> state(isolate);
8825 ExternalCallbackScope call_scope(isolate, callback_address); 8881 ExternalCallbackScope call_scope(isolate, callback_address);
8826 callback(info); 8882 callback(info);
8827 } 8883 }
8828 8884
8829 8885
8830 } // namespace internal 8886 } // namespace internal
8831 } // namespace v8 8887 } // namespace v8
OLDNEW
« no previous file with comments | « include/v8.h ('k') | src/heap/heap.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698