OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "src/snapshot/partial-serializer.h" |
| 6 |
| 7 #include "src/objects-inl.h" |
| 8 |
| 9 namespace v8 { |
| 10 namespace internal { |
| 11 |
| 12 PartialSerializer::PartialSerializer(Isolate* isolate, |
| 13 Serializer* startup_snapshot_serializer, |
| 14 SnapshotByteSink* sink) |
| 15 : Serializer(isolate, sink), |
| 16 startup_serializer_(startup_snapshot_serializer), |
| 17 global_object_(NULL) { |
| 18 InitializeCodeAddressMap(); |
| 19 } |
| 20 |
| 21 PartialSerializer::~PartialSerializer() { |
| 22 OutputStatistics("PartialSerializer"); |
| 23 } |
| 24 |
| 25 void PartialSerializer::Serialize(Object** o) { |
| 26 if ((*o)->IsContext()) { |
| 27 Context* context = Context::cast(*o); |
| 28 global_object_ = context->global_object(); |
| 29 back_reference_map()->AddGlobalProxy(context->global_proxy()); |
| 30 // The bootstrap snapshot has a code-stub context. When serializing the |
| 31 // partial snapshot, it is chained into the weak context list on the isolate |
| 32 // and it's next context pointer may point to the code-stub context. Clear |
| 33 // it before serializing, it will get re-added to the context list |
| 34 // explicitly when it's loaded. |
| 35 if (context->IsNativeContext()) { |
| 36 context->set(Context::NEXT_CONTEXT_LINK, |
| 37 isolate_->heap()->undefined_value()); |
| 38 DCHECK(!context->global_object()->IsUndefined()); |
| 39 } |
| 40 } |
| 41 VisitPointer(o); |
| 42 SerializeDeferredObjects(); |
| 43 Pad(); |
| 44 } |
| 45 |
| 46 void PartialSerializer::SerializeObject(HeapObject* obj, HowToCode how_to_code, |
| 47 WhereToPoint where_to_point, int skip) { |
| 48 if (obj->IsMap()) { |
| 49 // The code-caches link to context-specific code objects, which |
| 50 // the startup and context serializes cannot currently handle. |
| 51 DCHECK(Map::cast(obj)->code_cache() == obj->GetHeap()->empty_fixed_array()); |
| 52 } |
| 53 |
| 54 // Replace typed arrays by undefined. |
| 55 if (obj->IsJSTypedArray()) obj = isolate_->heap()->undefined_value(); |
| 56 |
| 57 int root_index = root_index_map_.Lookup(obj); |
| 58 if (root_index != RootIndexMap::kInvalidRootIndex) { |
| 59 PutRoot(root_index, obj, how_to_code, where_to_point, skip); |
| 60 return; |
| 61 } |
| 62 |
| 63 if (ShouldBeInThePartialSnapshotCache(obj)) { |
| 64 FlushSkip(skip); |
| 65 |
| 66 int cache_index = PartialSnapshotCacheIndex(obj); |
| 67 sink_->Put(kPartialSnapshotCache + how_to_code + where_to_point, |
| 68 "PartialSnapshotCache"); |
| 69 sink_->PutInt(cache_index, "partial_snapshot_cache_index"); |
| 70 return; |
| 71 } |
| 72 |
| 73 // Pointers from the partial snapshot to the objects in the startup snapshot |
| 74 // should go through the root array or through the partial snapshot cache. |
| 75 // If this is not the case you may have to add something to the root array. |
| 76 DCHECK(!startup_serializer_->back_reference_map()->Lookup(obj).is_valid()); |
| 77 // All the internalized strings that the partial snapshot needs should be |
| 78 // either in the root table or in the partial snapshot cache. |
| 79 DCHECK(!obj->IsInternalizedString()); |
| 80 |
| 81 if (SerializeKnownObject(obj, how_to_code, where_to_point, skip)) return; |
| 82 |
| 83 FlushSkip(skip); |
| 84 |
| 85 // Clear literal boilerplates. |
| 86 if (obj->IsJSFunction()) { |
| 87 FixedArray* literals = JSFunction::cast(obj)->literals(); |
| 88 for (int i = 0; i < literals->length(); i++) literals->set_undefined(i); |
| 89 } |
| 90 |
| 91 // Object has not yet been serialized. Serialize it here. |
| 92 ObjectSerializer serializer(this, obj, sink_, how_to_code, where_to_point); |
| 93 serializer.Serialize(); |
| 94 } |
| 95 |
| 96 int PartialSerializer::PartialSnapshotCacheIndex(HeapObject* heap_object) { |
| 97 Isolate* isolate = this->isolate(); |
| 98 List<Object*>* cache = isolate->partial_snapshot_cache(); |
| 99 int new_index = cache->length(); |
| 100 |
| 101 int index = partial_cache_index_map_.LookupOrInsert(heap_object, new_index); |
| 102 if (index == PartialCacheIndexMap::kInvalidIndex) { |
| 103 // We didn't find the object in the cache. So we add it to the cache and |
| 104 // then visit the pointer so that it becomes part of the startup snapshot |
| 105 // and we can refer to it from the partial snapshot. |
| 106 cache->Add(heap_object); |
| 107 startup_serializer_->VisitPointer(reinterpret_cast<Object**>(&heap_object)); |
| 108 // We don't recurse from the startup snapshot generator into the partial |
| 109 // snapshot generator. |
| 110 return new_index; |
| 111 } |
| 112 return index; |
| 113 } |
| 114 |
| 115 bool PartialSerializer::ShouldBeInThePartialSnapshotCache(HeapObject* o) { |
| 116 // Scripts should be referred only through shared function infos. We can't |
| 117 // allow them to be part of the partial snapshot because they contain a |
| 118 // unique ID, and deserializing several partial snapshots containing script |
| 119 // would cause dupes. |
| 120 DCHECK(!o->IsScript()); |
| 121 return o->IsName() || o->IsSharedFunctionInfo() || o->IsHeapNumber() || |
| 122 o->IsCode() || o->IsScopeInfo() || o->IsAccessorInfo() || |
| 123 o->map() == |
| 124 startup_serializer_->isolate()->heap()->fixed_cow_array_map(); |
| 125 } |
| 126 |
| 127 } // namespace internal |
| 128 } // namespace v8 |
OLD | NEW |