GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../tools/snapshot/snapshot_builder.cc Lines: 51 53 96.2 %
Date: 2021-02-19 04:08:54 Branches: 14 20 70.0 %

Line Branch Exec Source
1
#include "snapshot_builder.h"
2
#include <iostream>
3
#include <sstream>
4
#include "debug_utils-inl.h"
5
#include "env-inl.h"
6
#include "node_external_reference.h"
7
#include "node_internals.h"
8
#include "node_main_instance.h"
9
#include "node_snapshotable.h"
10
#include "node_v8_platform-inl.h"
11
12
namespace node {
13
14
using v8::Context;
15
using v8::HandleScope;
16
using v8::Isolate;
17
using v8::Local;
18
using v8::SnapshotCreator;
19
using v8::StartupData;
20
21
template <typename T>
22
14
void WriteVector(std::stringstream* ss, const T* vec, size_t size) {
23

2940486
  for (size_t i = 0; i < size; i++) {
24

2940472
    *ss << std::to_string(vec[i]) << (i == size - 1 ? '\n' : ',');
25
  }
26
14
}
27
28
7
std::string FormatBlob(StartupData* blob,
29
                       const std::vector<size_t>& isolate_data_indexes,
30
                       const EnvSerializeInfo& env_info) {
31
14
  std::stringstream ss;
32
33
  ss << R"(#include <cstddef>
34
#include "env.h"
35
#include "node_main_instance.h"
36
#include "v8.h"
37
38
// This file is generated by tools/snapshot. Do not edit.
39
40
namespace node {
41
42
static const char blob_data[] = {
43
7
)";
44
7
  WriteVector(&ss, blob->data, blob->raw_size);
45
7
  ss << R"(};
46
47
static const int blob_size = )"
48
14
     << blob->raw_size << R"(;
49
static v8::StartupData blob = { blob_data, blob_size };
50
7
)";
51
52
  ss << R"(v8::StartupData* NodeMainInstance::GetEmbeddedSnapshotBlob() {
53
  return &blob;
54
}
55
56
static const std::vector<size_t> isolate_data_indexes {
57
7
)";
58
7
  WriteVector(&ss, isolate_data_indexes.data(), isolate_data_indexes.size());
59
7
  ss << R"(};
60
61
const std::vector<size_t>* NodeMainInstance::GetIsolateDataIndexes() {
62
  return &isolate_data_indexes;
63
}
64
65
static const EnvSerializeInfo env_info )"
66
7
     << env_info << R"(;
67
68
const EnvSerializeInfo* NodeMainInstance::GetEnvSerializeInfo() {
69
  return &env_info;
70
}
71
72
}  // namespace node
73
7
)";
74
75
14
  return ss.str();
76
}
77
78
7
std::string SnapshotBuilder::Generate(
79
    const std::vector<std::string> args,
80
    const std::vector<std::string> exec_args) {
81
7
  Isolate* isolate = Isolate::Allocate();
82
14
  per_process::v8_platform.Platform()->RegisterIsolate(isolate,
83
14
                                                       uv_default_loop());
84
14
  std::unique_ptr<NodeMainInstance> main_instance;
85
7
  std::string result;
86
87
  {
88
14
    std::vector<size_t> isolate_data_indexes;
89
14
    EnvSerializeInfo env_info;
90
91
    const std::vector<intptr_t>& external_references =
92
7
        NodeMainInstance::CollectExternalReferences();
93
14
    SnapshotCreator creator(isolate, external_references.data());
94
    Environment* env;
95
    {
96
      main_instance =
97
14
          NodeMainInstance::Create(isolate,
98
                                   uv_default_loop(),
99
7
                                   per_process::v8_platform.Platform(),
100
                                   args,
101
7
                                   exec_args);
102
103
14
      HandleScope scope(isolate);
104
14
      creator.SetDefaultContext(Context::New(isolate));
105
7
      isolate_data_indexes = main_instance->isolate_data()->Serialize(&creator);
106
107
7
      Local<Context> context = NewContext(isolate);
108
      Context::Scope context_scope(context);
109
110
14
      env = new Environment(main_instance->isolate_data(),
111
                            context,
112
                            args,
113
                            exec_args,
114
                            nullptr,
115
                            node::EnvironmentFlags::kDefaultFlags,
116
14
                            {});
117
14
      env->RunBootstrapping().ToLocalChecked();
118
7
      if (per_process::enabled_debug_list.enabled(DebugCategory::MKSNAPSHOT)) {
119
        env->PrintAllBaseObjects();
120
        printf("Environment = %p\n", env);
121
      }
122
7
      env_info = env->Serialize(&creator);
123
14
      size_t index = creator.AddContext(
124
7
          context, {SerializeNodeContextInternalFields, env});
125
7
      CHECK_EQ(index, NodeMainInstance::kNodeContextIndex);
126
    }
127
128
    // Must be out of HandleScope
129
    StartupData blob =
130
7
        creator.CreateBlob(SnapshotCreator::FunctionCodeHandling::kClear);
131
7
    CHECK(blob.CanBeRehashed());
132
    // Must be done while the snapshot creator isolate is entered i.e. the
133
    // creator is still alive.
134
7
    FreeEnvironment(env);
135
7
    main_instance->Dispose();
136
7
    result = FormatBlob(&blob, isolate_data_indexes, env_info);
137
7
    delete[] blob.data;
138
  }
139
140
7
  per_process::v8_platform.Platform()->UnregisterIsolate(isolate);
141
14
  return result;
142
}
143

21
}  // namespace node