V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
object-deserializer.cc
1 // Copyright 2017 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/object-deserializer.h"
6 
7 #include "src/assembler-inl.h"
8 #include "src/code-stubs.h"
9 #include "src/isolate.h"
10 #include "src/objects.h"
11 #include "src/objects/slots.h"
12 #include "src/snapshot/code-serializer.h"
13 
14 namespace v8 {
15 namespace internal {
16 
17 ObjectDeserializer::ObjectDeserializer(const SerializedCodeData* data)
18  : Deserializer(data, true) {}
19 
20 MaybeHandle<SharedFunctionInfo>
21 ObjectDeserializer::DeserializeSharedFunctionInfo(
22  Isolate* isolate, const SerializedCodeData* data, Handle<String> source) {
23  ObjectDeserializer d(data);
24 
25  d.AddAttachedObject(source);
26 
27  Vector<const uint32_t> code_stub_keys = data->CodeStubKeys();
28  for (int i = 0; i < code_stub_keys.length(); i++) {
29  d.AddAttachedObject(
30  CodeStub::GetCode(isolate, code_stub_keys[i]).ToHandleChecked());
31  }
32 
33  Handle<HeapObject> result;
34  return d.Deserialize(isolate).ToHandle(&result)
35  ? Handle<SharedFunctionInfo>::cast(result)
36  : MaybeHandle<SharedFunctionInfo>();
37 }
38 
39 MaybeHandle<HeapObject> ObjectDeserializer::Deserialize(Isolate* isolate) {
40  Initialize(isolate);
41 
42  if (!allocator()->ReserveSpace()) return MaybeHandle<HeapObject>();
43 
44  DCHECK(deserializing_user_code());
45  HandleScope scope(isolate);
46  Handle<HeapObject> result;
47  {
48  DisallowHeapAllocation no_gc;
49  Object* root;
50  VisitRootPointer(Root::kPartialSnapshotCache, nullptr, ObjectSlot(&root));
51  DeserializeDeferredObjects();
52  FlushICache();
53  LinkAllocationSites();
54  LogNewMapEvents();
55  result = handle(HeapObject::cast(root), isolate);
56  Rehash();
57  allocator()->RegisterDeserializedObjectsForBlackAllocation();
58  }
59  CommitPostProcessedObjects();
60  return scope.CloseAndEscape(result);
61 }
62 
63 void ObjectDeserializer::FlushICache() {
64  DCHECK(deserializing_user_code());
65  for (Code code : new_code_objects()) {
66  // Record all references to embedded objects in the new code object.
67  WriteBarrierForCode(code);
68  Assembler::FlushICache(code->raw_instruction_start(),
69  code->raw_instruction_size());
70  }
71 }
72 
73 void ObjectDeserializer::CommitPostProcessedObjects() {
74  CHECK_LE(new_internalized_strings().size(), kMaxInt);
75  StringTable::EnsureCapacityForDeserialization(
76  isolate(), static_cast<int>(new_internalized_strings().size()));
77  for (Handle<String> string : new_internalized_strings()) {
78  DisallowHeapAllocation no_gc;
79  StringTableInsertionKey key(*string);
80  DCHECK(
81  StringTable::ForwardStringIfExists(isolate(), &key, *string).is_null());
82  StringTable::AddKeyNoResize(isolate(), &key);
83  }
84 
85  Heap* heap = isolate()->heap();
86  Factory* factory = isolate()->factory();
87  for (Handle<Script> script : new_scripts()) {
88  // Assign a new script id to avoid collision.
89  script->set_id(isolate()->heap()->NextScriptId());
90  LogScriptEvents(*script);
91  // Add script to list.
92  Handle<WeakArrayList> list = factory->script_list();
93  list = WeakArrayList::AddToEnd(isolate(), list,
94  MaybeObjectHandle::Weak(script));
95  heap->SetRootScriptList(*list);
96  }
97 }
98 
99 void ObjectDeserializer::LinkAllocationSites() {
100  DisallowHeapAllocation no_gc;
101  Heap* heap = isolate()->heap();
102  // Allocation sites are present in the snapshot, and must be linked into
103  // a list at deserialization time.
104  for (AllocationSite* site : new_allocation_sites()) {
105  if (!site->HasWeakNext()) continue;
106  // TODO(mvstanton): consider treating the heap()->allocation_sites_list()
107  // as a (weak) root. If this root is relocated correctly, this becomes
108  // unnecessary.
109  if (heap->allocation_sites_list() == Smi::kZero) {
110  site->set_weak_next(ReadOnlyRoots(heap).undefined_value());
111  } else {
112  site->set_weak_next(heap->allocation_sites_list());
113  }
114  heap->set_allocation_sites_list(site);
115  }
116 }
117 
118 } // namespace internal
119 } // namespace v8
Definition: libplatform.h:13