blob: b46f6755f0badab8e01e85b8b2eba317aeec724e [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"
Ben Murdoch61f157c2016-09-16 13:49:30 +01006#include "src/snapshot/startup-serializer.h"
Ben Murdochda12d292016-06-02 14:46:10 +01007
8#include "src/objects-inl.h"
9
10namespace v8 {
11namespace internal {
12
13PartialSerializer::PartialSerializer(Isolate* isolate,
Ben Murdoch61f157c2016-09-16 13:49:30 +010014 StartupSerializer* startup_serializer)
15 : Serializer(isolate), startup_serializer_(startup_serializer) {
Ben Murdochda12d292016-06-02 14:46:10 +010016 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);
Ben Murdochc5610432016-08-08 18:44:38 +010026 reference_map()->AddAttachedReference(context->global_proxy());
Ben Murdochda12d292016-06-02 14:46:10 +010027 // 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());
Ben Murdoch61f157c2016-09-16 13:49:30 +010035 DCHECK(!context->global_object()->IsUndefined(context->GetIsolate()));
Ben Murdochda12d292016-06-02 14:46:10 +010036 }
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
Ben Murdoch61f157c2016-09-16 13:49:30 +010054 if (SerializeHotObject(obj, how_to_code, where_to_point, skip)) return;
55
Ben Murdochda12d292016-06-02 14:46:10 +010056 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
Ben Murdoch61f157c2016-09-16 13:49:30 +010062 if (SerializeBackReference(obj, how_to_code, where_to_point, skip)) return;
63
Ben Murdochda12d292016-06-02 14:46:10 +010064 if (ShouldBeInThePartialSnapshotCache(obj)) {
65 FlushSkip(skip);
66
Ben Murdoch61f157c2016-09-16 13:49:30 +010067 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");
Ben Murdochda12d292016-06-02 14:46:10 +010071 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.
Ben Murdochc5610432016-08-08 18:44:38 +010077 DCHECK(!startup_serializer_->reference_map()->Lookup(obj).is_valid());
Ben Murdochda12d292016-06-02 14:46:10 +010078 // 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());
Ben Murdoch61f157c2016-09-16 13:49:30 +010081 // Function and object templates are not context specific.
82 DCHECK(!obj->IsTemplateInfo());
Ben Murdochda12d292016-06-02 14:46:10 +010083
84 FlushSkip(skip);
85
86 // Clear literal boilerplates.
87 if (obj->IsJSFunction()) {
Ben Murdoch61f157c2016-09-16 13:49:30 +010088 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();
Ben Murdochda12d292016-06-02 14:46:10 +010094 }
95
96 // Object has not yet been serialized. Serialize it here.
Ben Murdoch61f157c2016-09-16 13:49:30 +010097 ObjectSerializer serializer(this, obj, &sink_, how_to_code, where_to_point);
Ben Murdochda12d292016-06-02 14:46:10 +010098 serializer.Serialize();
99}
100
Ben Murdochda12d292016-06-02 14:46:10 +0100101bool 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