Index: src/api.cc |
diff --git a/src/api.cc b/src/api.cc |
index 1de62f2e4cc5c0346292ec8e0fc20b4d7df7371a..e72abc87de14ecf523fe5d892b694287db3cbc26 100644 |
--- a/src/api.cc |
+++ b/src/api.cc |
@@ -386,7 +386,10 @@ bool RunExtraCode(Isolate* isolate, Local<Context> context, |
struct SnapshotCreatorData { |
explicit SnapshotCreatorData(Isolate* isolate) |
- : isolate_(isolate), contexts_(isolate), created_(false) {} |
+ : isolate_(isolate), |
+ contexts_(isolate), |
+ templates_(isolate), |
+ created_(false) {} |
static SnapshotCreatorData* cast(void* data) { |
return reinterpret_cast<SnapshotCreatorData*>(data); |
@@ -395,6 +398,7 @@ struct SnapshotCreatorData { |
ArrayBufferAllocator allocator_; |
Isolate* isolate_; |
PersistentValueVector<Context> contexts_; |
+ PersistentValueVector<Template> templates_; |
bool created_; |
}; |
@@ -442,12 +446,35 @@ size_t SnapshotCreator::AddContext(Local<Context> context) { |
return index; |
} |
+size_t SnapshotCreator::AddTemplate(Local<Template> template_obj) { |
+ DCHECK(!template_obj.IsEmpty()); |
+ SnapshotCreatorData* data = SnapshotCreatorData::cast(data_); |
+ DCHECK(!data->created_); |
+ DCHECK_EQ(reinterpret_cast<i::Isolate*>(data->isolate_), |
+ Utils::OpenHandle(*template_obj)->GetIsolate()); |
+ size_t index = static_cast<int>(data->templates_.Size()); |
+ data->templates_.Append(template_obj); |
+ return index; |
+} |
+ |
StartupData SnapshotCreator::CreateBlob( |
SnapshotCreator::FunctionCodeHandling function_code_handling) { |
SnapshotCreatorData* data = SnapshotCreatorData::cast(data_); |
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(data->isolate_); |
DCHECK(!data->created_); |
+ { |
+ int num_templates = static_cast<int>(data->templates_.Size()); |
+ i::HandleScope scope(isolate); |
+ i::Handle<i::FixedArray> templates = |
+ isolate->factory()->NewFixedArray(num_templates, i::TENURED); |
+ for (int i = 0; i < num_templates; i++) { |
+ templates->set(i, *v8::Utils::OpenHandle(*data->templates_.Get(i))); |
+ } |
+ isolate->heap()->SetSerializedTemplates(*templates); |
+ data->templates_.Clear(); |
+ } |
+ |
// If we don't do this then we end up with a stray root pointing at the |
// context even after we have disposed of the context. |
isolate->heap()->CollectAllAvailableGarbage("mksnapshot"); |
@@ -1173,6 +1200,20 @@ Local<FunctionTemplate> FunctionTemplate::New(Isolate* isolate, |
length, false); |
} |
+Local<FunctionTemplate> FunctionTemplate::FromSnapshot(Isolate* isolate, |
+ size_t index) { |
+ i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); |
+ i::FixedArray* templates = i_isolate->heap()->serialized_templates(); |
+ int int_index = static_cast<int>(index); |
+ if (int_index < templates->length()) { |
+ i::Object* info = i_isolate->heap()->serialized_templates()->get(int_index); |
+ if (info->IsFunctionTemplateInfo()) { |
+ return Utils::ToLocal(i::Handle<i::FunctionTemplateInfo>( |
+ i::FunctionTemplateInfo::cast(info))); |
+ } |
+ } |
+ return Local<FunctionTemplate>(); |
+} |
Local<FunctionTemplate> FunctionTemplate::NewWithFastHandler( |
Isolate* isolate, FunctionCallback callback, |
@@ -1384,6 +1425,21 @@ Local<ObjectTemplate> ObjectTemplate::New( |
return ObjectTemplateNew(isolate, constructor, false); |
} |
+Local<ObjectTemplate> ObjectTemplate::FromSnapshot(Isolate* isolate, |
+ size_t index) { |
+ i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); |
+ i::FixedArray* templates = i_isolate->heap()->serialized_templates(); |
+ int int_index = static_cast<int>(index); |
+ if (int_index < templates->length()) { |
+ i::Object* info = i_isolate->heap()->serialized_templates()->get(int_index); |
+ if (info->IsObjectTemplateInfo()) { |
+ return Utils::ToLocal( |
+ i::Handle<i::ObjectTemplateInfo>(i::ObjectTemplateInfo::cast(info))); |
+ } |
+ } |
+ return Local<ObjectTemplate>(); |
+} |
+ |
// Ensure that the object template has a constructor. If no |
// constructor is available we create one. |
static i::Handle<i::FunctionTemplateInfo> EnsureConstructor( |