5 #include "src/snapshot/serializer-common.h" 7 #include "src/external-reference-table.h" 8 #include "src/objects-inl.h" 9 #include "src/objects/slots.h" 14 ExternalReferenceEncoder::ExternalReferenceEncoder(Isolate* isolate) {
16 api_references_ = isolate->api_external_references();
17 if (api_references_ !=
nullptr) {
18 for (
uint32_t i = 0; api_references_[
i] != 0; ++
i) count_.push_back(0);
21 map_ = isolate->external_reference_map();
22 if (map_ !=
nullptr)
return;
23 map_ =
new AddressToIndexHashMap();
24 isolate->set_external_reference_map(map_);
26 ExternalReferenceTable* table = isolate->external_reference_table();
28 Address addr = table->address(
i);
31 if (map_->Get(addr).IsNothing()) map_->Set(addr, Value::Encode(
i,
false));
32 DCHECK(map_->Get(addr).IsJust());
35 const intptr_t* api_references = isolate->api_external_references();
36 if (api_references ==
nullptr)
return;
38 Address addr =
static_cast<Address
>(api_references[
i]);
41 if (map_->Get(addr).IsNothing()) map_->Set(addr, Value::Encode(
i,
true));
42 DCHECK(map_->Get(addr).IsJust());
46 ExternalReferenceEncoder::~ExternalReferenceEncoder() {
48 if (!i::FLAG_external_reference_stats)
return;
49 if (api_references_ ==
nullptr)
return;
50 for (
uint32_t i = 0; api_references_[
i] != 0; ++
i) {
51 Address addr =
static_cast<Address
>(api_references_[
i]);
52 DCHECK(map_->Get(addr).IsJust());
54 "index=%5d count=%5d %-60s\n",
i, count_[
i],
55 ExternalReferenceTable::ResolveSymbol(reinterpret_cast<void*>(addr)));
60 Maybe<ExternalReferenceEncoder::Value> ExternalReferenceEncoder::TryEncode(
62 Maybe<uint32_t> maybe_index = map_->Get(address);
63 if (maybe_index.IsNothing())
return Nothing<Value>();
64 Value result(maybe_index.FromJust());
66 if (result.is_from_api()) count_[result.index()]++;
68 return Just<Value>(result);
71 ExternalReferenceEncoder::Value ExternalReferenceEncoder::Encode(
73 Maybe<uint32_t> maybe_index = map_->Get(address);
74 if (maybe_index.IsNothing()) {
75 void* addr =
reinterpret_cast<void*
>(address);
76 v8::base::OS::PrintError(
"Unknown external reference %p.\n", addr);
77 v8::base::OS::PrintError(
"%s", ExternalReferenceTable::ResolveSymbol(addr));
78 v8::base::OS::Abort();
80 Value result(maybe_index.FromJust());
82 if (result.is_from_api()) count_[result.index()]++;
87 const char* ExternalReferenceEncoder::NameOfAddress(Isolate* isolate,
88 Address address)
const {
89 Maybe<uint32_t> maybe_index = map_->Get(address);
90 if (maybe_index.IsNothing())
return "<unknown>";
91 Value value(maybe_index.FromJust());
92 if (value.is_from_api())
return "<from api>";
93 return isolate->external_reference_table()->name(value.index());
96 void SerializedData::AllocateData(
uint32_t size) {
98 data_ = NewArray<byte>(size);
101 DCHECK(IsAligned(reinterpret_cast<intptr_t>(data_), kPointerAlignment));
105 uint32_t SerializedData::ComputeMagicNumber(Isolate* isolate) {
106 return ComputeMagicNumber(isolate->external_reference_table());
115 void SerializerDeserializer::Iterate(Isolate* isolate, RootVisitor* visitor) {
116 std::vector<Object*>* cache = isolate->partial_snapshot_cache();
117 for (
size_t i = 0;; ++
i) {
119 if (cache->size() <=
i) cache->push_back(Smi::kZero);
122 visitor->VisitRootPointer(Root::kPartialSnapshotCache,
nullptr,
123 ObjectSlot(&cache->at(
i)));
124 if (cache->at(
i)->IsUndefined(isolate))
break;
128 bool SerializerDeserializer::CanBeDeferred(HeapObject* o) {
129 return !o->IsString() && !o->IsScript() && !o->IsJSTypedArray();
132 void SerializerDeserializer::RestoreExternalReferenceRedirectors(
133 const std::vector<AccessorInfo*>& accessor_infos) {
135 for (AccessorInfo* info : accessor_infos) {
136 Foreign::cast(info->js_getter())
137 ->set_foreign_address(info->redirected_getter());
141 void SerializerDeserializer::RestoreExternalReferenceRedirectors(
142 const std::vector<CallHandlerInfo*>& call_handler_infos) {
143 for (CallHandlerInfo* info : call_handler_infos) {
144 Foreign::cast(info->js_callback())
145 ->set_foreign_address(info->redirected_callback());