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