serializer-deserializer.cc 2.56 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/snapshot/serializer-deserializer.h"

#include "src/objects/foreign-inl.h"
#include "src/objects/objects-inl.h"

namespace v8 {
namespace internal {

13 14
// The startup object cache is terminated by undefined. We visit the context
// snapshot...
15 16
//  - during deserialization to populate it.
//  - during normal GC to keep its content alive.
17
//  - not during serialization. The context serializer adds to it explicitly.
18 19
DISABLE_CFI_PERF
void SerializerDeserializer::Iterate(Isolate* isolate, RootVisitor* visitor) {
20
  std::vector<Object>* cache = isolate->startup_object_cache();
21 22 23
  for (size_t i = 0;; ++i) {
    // Extend the array ready to get a value when deserializing.
    if (cache->size() <= i) cache->push_back(Smi::zero());
24
    // During deserialization, the visitor populates the startup object cache
25
    // and eventually terminates the cache with undefined.
26
    visitor->VisitRootPointer(Root::kStartupObjectCache, nullptr,
27 28 29 30 31 32
                              FullObjectSlot(&cache->at(i)));
    if (cache->at(i).IsUndefined(isolate)) break;
  }
}

bool SerializerDeserializer::CanBeDeferred(HeapObject o) {
33 34 35
  // 1. Maps cannot be deferred as objects are expected to have a valid map
  // immediately.
  // 2. Internalized strings cannot be deferred as they might be
36 37
  // converted to thin strings during post processing, at which point forward
  // references to the now-thin string will already have been written.
38 39 40
  // 3. JS objects with embedder fields cannot be deferred because the
  // serialize/deserialize callbacks need the back reference immediately to
  // identify the object.
41 42
  // TODO(leszeks): Could we defer string serialization if forward references
  // were resolved after object post processing?
43 44
  return !o.IsMap() && !o.IsInternalizedString() &&
         !(o.IsJSObject() && JSObject::cast(o).GetEmbedderFieldCount() > 0);
45 46
}

47 48
void SerializerDeserializer::RestoreExternalReferenceRedirector(
    Isolate* isolate, Handle<AccessorInfo> accessor_info) {
49
  // Restore wiped accessor infos.
50 51
  Foreign::cast(accessor_info->js_getter())
      .set_foreign_address(isolate, accessor_info->redirected_getter());
52 53
}

54 55 56 57
void SerializerDeserializer::RestoreExternalReferenceRedirector(
    Isolate* isolate, Handle<CallHandlerInfo> call_handler_info) {
  Foreign::cast(call_handler_info->js_callback())
      .set_foreign_address(isolate, call_handler_info->redirected_callback());
58 59 60 61
}

}  // namespace internal
}  // namespace v8