GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_snapshotable.cc Lines: 51 56 91.1 %
Date: 2021-05-01 04:12:26 Branches: 13 20 65.0 %

Line Branch Exec Source
1
2
#include "node_snapshotable.h"
3
#include "base_object-inl.h"
4
#include "debug_utils-inl.h"
5
#include "node_file.h"
6
#include "node_v8.h"
7
8
namespace node {
9
10
using v8::Local;
11
using v8::Object;
12
using v8::SnapshotCreator;
13
using v8::StartupData;
14
15
10450
SnapshotableObject::SnapshotableObject(Environment* env,
16
                                       Local<Object> wrap,
17
10450
                                       EmbedderObjectType type)
18
10450
    : BaseObject(env, wrap), type_(type) {
19
10450
}
20
21
16
const char* SnapshotableObject::GetTypeNameChars() const {
22
16
  switch (type_) {
23
#define V(PropertyName, NativeTypeName)                                        \
24
  case EmbedderObjectType::k_##PropertyName: {                                 \
25
    return NativeTypeName::type_name.c_str();                                  \
26
  }
27
8
    SERIALIZABLE_OBJECT_TYPES(V)
28
#undef V
29
    default: { UNREACHABLE(); }
30
  }
31
}
32
33
16
bool IsSnapshotableType(FastStringKey key) {
34
#define V(PropertyName, NativeTypeName)                                        \
35
  if (key == NativeTypeName::type_name) {                                      \
36
    return true;                                                               \
37
  }
38

16
  SERIALIZABLE_OBJECT_TYPES(V)
39
#undef V
40
41
  return false;
42
}
43
44
9510
void DeserializeNodeInternalFields(Local<Object> holder,
45
                                   int index,
46
                                   StartupData payload,
47
                                   void* env) {
48
  per_process::Debug(DebugCategory::MKSNAPSHOT,
49
                     "Deserialize internal field %d of %p, size=%d\n",
50
19020
                     static_cast<int>(index),
51
19020
                     (*holder),
52
19020
                     static_cast<int>(payload.raw_size));
53
9510
  if (payload.raw_size == 0) {
54
    holder->SetAlignedPointerInInternalField(index, nullptr);
55
    return;
56
  }
57
58
9510
  Environment* env_ptr = static_cast<Environment*>(env);
59
  const InternalFieldInfo* info =
60
9510
      reinterpret_cast<const InternalFieldInfo*>(payload.data);
61
62
9510
  switch (info->type) {
63
#define V(PropertyName, NativeTypeName)                                        \
64
  case EmbedderObjectType::k_##PropertyName: {                                 \
65
    per_process::Debug(DebugCategory::MKSNAPSHOT,                              \
66
                       "Object %p is %s\n",                                    \
67
                       (*holder),                                              \
68
                       NativeTypeName::type_name.c_str());                     \
69
    env_ptr->EnqueueDeserializeRequest(                                        \
70
        NativeTypeName::Deserialize, holder, index, info->Copy());             \
71
    break;                                                                     \
72
  }
73
9510
    SERIALIZABLE_OBJECT_TYPES(V)
74
9510
#undef V
75
4755
    default: { UNREACHABLE(); }
76
14265
  }
77
4755
}
78
79
848
StartupData SerializeNodeContextInternalFields(Local<Object> holder,
80
                                               int index,
81
                                               void* env) {
82
  per_process::Debug(DebugCategory::MKSNAPSHOT,
83
                     "Serialize internal field, index=%d, holder=%p\n",
84
1696
                     static_cast<int>(index),
85
1696
                     *holder);
86
1696
  void* ptr = holder->GetAlignedPointerFromInternalField(BaseObject::kSlot);
87
848
  if (ptr == nullptr) {
88
832
    return StartupData{nullptr, 0};
89
  }
90
91
  DCHECK(static_cast<BaseObject*>(ptr)->is_snapshotable());
92
16
  SnapshotableObject* obj = static_cast<SnapshotableObject*>(ptr);
93
  per_process::Debug(DebugCategory::MKSNAPSHOT,
94
                     "Object %p is %s, ",
95
32
                     *holder,
96
32
                     obj->GetTypeNameChars());
97
16
  InternalFieldInfo* info = obj->Serialize(index);
98
  per_process::Debug(DebugCategory::MKSNAPSHOT,
99
                     "payload size=%d\n",
100
32
                     static_cast<int>(info->length));
101
  return StartupData{reinterpret_cast<const char*>(info),
102
16
                     static_cast<int>(info->length)};
103
}
104
105
8
void SerializeBindingData(Environment* env,
106
                          SnapshotCreator* creator,
107
                          EnvSerializeInfo* info) {
108
8
  size_t i = 0;
109
16
  env->ForEachBindingData([&](FastStringKey key,
110
16
                              BaseObjectPtr<BaseObject> binding) {
111
    per_process::Debug(DebugCategory::MKSNAPSHOT,
112
                       "Serialize binding %i, %p, type=%s\n",
113
64
                       static_cast<int>(i),
114
48
                       *(binding->object()),
115
32
                       key.c_str());
116
117
16
    if (IsSnapshotableType(key)) {
118
64
      size_t index = creator->AddData(env->context(), binding->object());
119
      per_process::Debug(DebugCategory::MKSNAPSHOT,
120
                         "Serialized with index=%d\n",
121
32
                         static_cast<int>(index));
122
32
      info->bindings.push_back({key.c_str(), i, index});
123
16
      SnapshotableObject* ptr = static_cast<SnapshotableObject*>(binding.get());
124
48
      ptr->PrepareForSerialization(env->context(), creator);
125
    } else {
126
      UNREACHABLE();
127
    }
128
129
16
    i++;
130
24
  });
131
8
}
132
133

14487
}  // namespace node