partial-serializer.cc 6.04 KB
Newer Older
1 2 3 4 5
// Copyright 2016 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/partial-serializer.h"
6
#include "src/snapshot/startup-serializer.h"
7 8 9 10 11 12

#include "src/objects-inl.h"

namespace v8 {
namespace internal {

13 14 15 16 17 18
PartialSerializer::PartialSerializer(
    Isolate* isolate, StartupSerializer* startup_serializer,
    v8::SerializeInternalFieldsCallback callback)
    : Serializer(isolate),
      startup_serializer_(startup_serializer),
      serialize_internal_fields_(callback) {
19 20 21 22 23 24 25 26 27 28
  InitializeCodeAddressMap();
}

PartialSerializer::~PartialSerializer() {
  OutputStatistics("PartialSerializer");
}

void PartialSerializer::Serialize(Object** o) {
  if ((*o)->IsContext()) {
    Context* context = Context::cast(*o);
29
    reference_map()->AddAttachedReference(context->global_proxy());
30 31 32 33 34 35 36 37
    // The bootstrap snapshot has a code-stub context. When serializing the
    // partial snapshot, it is chained into the weak context list on the isolate
    // and it's next context pointer may point to the code-stub context.  Clear
    // it before serializing, it will get re-added to the context list
    // explicitly when it's loaded.
    if (context->IsNativeContext()) {
      context->set(Context::NEXT_CONTEXT_LINK,
                   isolate_->heap()->undefined_value());
38
      DCHECK(!context->global_object()->IsUndefined(context->GetIsolate()));
39 40 41
      // Reset math random cache to get fresh random numbers.
      context->set_math_random_index(Smi::kZero);
      context->set_math_random_cache(isolate_->heap()->undefined_value());
42 43 44 45
    }
  }
  VisitPointer(o);
  SerializeDeferredObjects();
46
  SerializeInternalFields();
47 48 49 50 51 52 53 54 55 56 57 58 59 60
  Pad();
}

void PartialSerializer::SerializeObject(HeapObject* obj, HowToCode how_to_code,
                                        WhereToPoint where_to_point, int skip) {
  if (obj->IsMap()) {
    // The code-caches link to context-specific code objects, which
    // the startup and context serializes cannot currently handle.
    DCHECK(Map::cast(obj)->code_cache() == obj->GetHeap()->empty_fixed_array());
  }

  // Replace typed arrays by undefined.
  if (obj->IsJSTypedArray()) obj = isolate_->heap()->undefined_value();

61 62
  if (SerializeHotObject(obj, how_to_code, where_to_point, skip)) return;

63 64 65 66 67 68
  int root_index = root_index_map_.Lookup(obj);
  if (root_index != RootIndexMap::kInvalidRootIndex) {
    PutRoot(root_index, obj, how_to_code, where_to_point, skip);
    return;
  }

69 70
  if (SerializeBackReference(obj, how_to_code, where_to_point, skip)) return;

71 72 73
  if (ShouldBeInThePartialSnapshotCache(obj)) {
    FlushSkip(skip);

74
    int cache_index = startup_serializer_->PartialSnapshotCacheIndex(obj);
75 76 77
    sink_.Put(kPartialSnapshotCache + how_to_code + where_to_point,
              "PartialSnapshotCache");
    sink_.PutInt(cache_index, "partial_snapshot_cache_index");
78 79 80 81 82 83
    return;
  }

  // Pointers from the partial snapshot to the objects in the startup snapshot
  // should go through the root array or through the partial snapshot cache.
  // If this is not the case you may have to add something to the root array.
84
  DCHECK(!startup_serializer_->reference_map()->Lookup(obj).is_valid());
85 86 87
  // All the internalized strings that the partial snapshot needs should be
  // either in the root table or in the partial snapshot cache.
  DCHECK(!obj->IsInternalizedString());
88 89
  // Function and object templates are not context specific.
  DCHECK(!obj->IsTemplateInfo());
90 91 92 93 94

  FlushSkip(skip);

  // Clear literal boilerplates.
  if (obj->IsJSFunction()) {
95 96
    JSFunction* function = JSFunction::cast(obj);
    LiteralsArray* literals = function->literals();
97 98 99
    for (int i = 0; i < literals->literals_count(); i++) {
      literals->set_literal_undefined(i);
    }
100
    function->ClearTypeFeedbackInfo();
101 102
  }

103 104 105 106 107
  if (obj->IsJSObject()) {
    JSObject* jsobj = JSObject::cast(obj);
    if (jsobj->GetInternalFieldCount() > 0) internal_field_holders_.Add(jsobj);
  }

108
  // Object has not yet been serialized.  Serialize it here.
109
  ObjectSerializer serializer(this, obj, &sink_, how_to_code, where_to_point);
110 111 112 113 114 115 116 117 118 119 120
  serializer.Serialize();
}

bool PartialSerializer::ShouldBeInThePartialSnapshotCache(HeapObject* o) {
  // Scripts should be referred only through shared function infos.  We can't
  // allow them to be part of the partial snapshot because they contain a
  // unique ID, and deserializing several partial snapshots containing script
  // would cause dupes.
  DCHECK(!o->IsScript());
  return o->IsName() || o->IsSharedFunctionInfo() || o->IsHeapNumber() ||
         o->IsCode() || o->IsScopeInfo() || o->IsAccessorInfo() ||
121
         o->IsTemplateInfo() ||
122 123 124 125
         o->map() ==
             startup_serializer_->isolate()->heap()->fixed_cow_array_map();
}

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
void PartialSerializer::SerializeInternalFields() {
  int count = internal_field_holders_.length();
  if (count == 0) return;
  DisallowHeapAllocation no_gc;
  DisallowJavascriptExecution no_js(isolate());
  DisallowCompilation no_compile(isolate());
  DCHECK_NOT_NULL(serialize_internal_fields_);
  sink_.Put(kInternalFieldsData, "internal fields data");
  while (internal_field_holders_.length() > 0) {
    HandleScope scope(isolate());
    Handle<JSObject> obj(internal_field_holders_.RemoveLast(), isolate());
    SerializerReference reference = reference_map_.Lookup(*obj);
    DCHECK(reference.is_back_reference());
    int internal_fields_count = obj->GetInternalFieldCount();
    for (int i = 0; i < internal_fields_count; i++) {
      if (obj->GetInternalField(i)->IsHeapObject()) continue;
      StartupData data = serialize_internal_fields_(v8::Utils::ToLocal(obj), i);
      sink_.Put(kNewObject + reference.space(), "internal field holder");
      PutBackReference(*obj, reference);
      sink_.PutInt(i, "internal field index");
      sink_.PutInt(data.raw_size, "internal fields data size");
      sink_.PutRaw(reinterpret_cast<const byte*>(data.data), data.raw_size,
                   "internal fields data");
      delete[] data.data;
    }
  }
  sink_.Put(kSynchronize, "Finished with internal fields data");
}

155 156
}  // namespace internal
}  // namespace v8