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#include "src/snapshot/startup-serializer.h"
7
8#include "src/objects-inl.h"
9
10namespace v8 {
11namespace internal {
12
13PartialSerializer::PartialSerializer(Isolate* isolate,
14                                     StartupSerializer* startup_serializer)
15    : Serializer(isolate), startup_serializer_(startup_serializer) {
16  InitializeCodeAddressMap();
17}
18
19PartialSerializer::~PartialSerializer() {
20  OutputStatistics("PartialSerializer");
21}
22
23void PartialSerializer::Serialize(Object** o) {
24  if ((*o)->IsContext()) {
25    Context* context = Context::cast(*o);
26    reference_map()->AddAttachedReference(context->global_proxy());
27    // The bootstrap snapshot has a code-stub context. When serializing the
28    // partial snapshot, it is chained into the weak context list on the isolate
29    // and it's next context pointer may point to the code-stub context.  Clear
30    // it before serializing, it will get re-added to the context list
31    // explicitly when it's loaded.
32    if (context->IsNativeContext()) {
33      context->set(Context::NEXT_CONTEXT_LINK,
34                   isolate_->heap()->undefined_value());
35      DCHECK(!context->global_object()->IsUndefined(context->GetIsolate()));
36    }
37  }
38  VisitPointer(o);
39  SerializeDeferredObjects();
40  Pad();
41}
42
43void PartialSerializer::SerializeObject(HeapObject* obj, HowToCode how_to_code,
44                                        WhereToPoint where_to_point, int skip) {
45  if (obj->IsMap()) {
46    // The code-caches link to context-specific code objects, which
47    // the startup and context serializes cannot currently handle.
48    DCHECK(Map::cast(obj)->code_cache() == obj->GetHeap()->empty_fixed_array());
49  }
50
51  // Replace typed arrays by undefined.
52  if (obj->IsJSTypedArray()) obj = isolate_->heap()->undefined_value();
53
54  if (SerializeHotObject(obj, how_to_code, where_to_point, skip)) return;
55
56  int root_index = root_index_map_.Lookup(obj);
57  if (root_index != RootIndexMap::kInvalidRootIndex) {
58    PutRoot(root_index, obj, how_to_code, where_to_point, skip);
59    return;
60  }
61
62  if (SerializeBackReference(obj, how_to_code, where_to_point, skip)) return;
63
64  if (ShouldBeInThePartialSnapshotCache(obj)) {
65    FlushSkip(skip);
66
67    int cache_index = startup_serializer_->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_->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  // Function and object templates are not context specific.
82  DCHECK(!obj->IsTemplateInfo());
83
84  FlushSkip(skip);
85
86  // Clear literal boilerplates.
87  if (obj->IsJSFunction()) {
88    JSFunction* function = JSFunction::cast(obj);
89    LiteralsArray* literals = function->literals();
90    for (int i = 0; i < literals->literals_count(); i++) {
91      literals->set_literal_undefined(i);
92    }
93    function->ClearTypeFeedbackInfo();
94  }
95
96  // Object has not yet been serialized.  Serialize it here.
97  ObjectSerializer serializer(this, obj, &sink_, how_to_code, where_to_point);
98  serializer.Serialize();
99}
100
101bool PartialSerializer::ShouldBeInThePartialSnapshotCache(HeapObject* o) {
102  // Scripts should be referred only through shared function infos.  We can't
103  // allow them to be part of the partial snapshot because they contain a
104  // unique ID, and deserializing several partial snapshots containing script
105  // would cause dupes.
106  DCHECK(!o->IsScript());
107  return o->IsName() || o->IsSharedFunctionInfo() || o->IsHeapNumber() ||
108         o->IsCode() || o->IsScopeInfo() || o->IsAccessorInfo() ||
109         o->map() ==
110             startup_serializer_->isolate()->heap()->fixed_cow_array_map();
111}
112
113}  // namespace internal
114}  // namespace v8
115