GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_process_object.cc Lines: 60 66 90.9 %
Date: 2020-06-24 22:13:30 Branches: 29 52 55.8 %

Line Branch Exec Source
1
#include "env-inl.h"
2
#include "node_internals.h"
3
#include "node_options-inl.h"
4
#include "node_metadata.h"
5
#include "node_process.h"
6
#include "node_revert.h"
7
#include "util-inl.h"
8
9
#include <climits>  // PATH_MAX
10
11
namespace node {
12
using v8::Context;
13
using v8::DEFAULT;
14
using v8::EscapableHandleScope;
15
using v8::Function;
16
using v8::FunctionCallbackInfo;
17
using v8::FunctionTemplate;
18
using v8::Integer;
19
using v8::Isolate;
20
using v8::Local;
21
using v8::MaybeLocal;
22
using v8::Name;
23
using v8::NewStringType;
24
using v8::None;
25
using v8::Object;
26
using v8::PropertyCallbackInfo;
27
using v8::SideEffectType;
28
using v8::String;
29
using v8::Value;
30
31
24
static void ProcessTitleGetter(Local<Name> property,
32
                               const PropertyCallbackInfo<Value>& info) {
33
48
  std::string title = GetProcessTitle("node");
34
48
  info.GetReturnValue().Set(
35
48
      String::NewFromUtf8(info.GetIsolate(), title.data(),
36
48
                          NewStringType::kNormal, title.size())
37
      .ToLocalChecked());
38
24
}
39
40
93
static void ProcessTitleSetter(Local<Name> property,
41
                               Local<Value> value,
42
                               const PropertyCallbackInfo<void>& info) {
43
186
  node::Utf8Value title(info.GetIsolate(), value);
44

187
  TRACE_EVENT_METADATA1(
45
      "__metadata", "process_name", "name", TRACE_STR_COPY(*title));
46
93
  uv_set_process_title(*title);
47
93
}
48
49
101
static void DebugPortGetter(Local<Name> property,
50
                            const PropertyCallbackInfo<Value>& info) {
51
101
  Environment* env = Environment::GetCurrent(info);
52
202
  ExclusiveAccess<HostPort>::Scoped host_port(env->inspector_host_port());
53
101
  int port = host_port->port();
54
202
  info.GetReturnValue().Set(port);
55
101
}
56
57
static void DebugPortSetter(Local<Name> property,
58
                            Local<Value> value,
59
                            const PropertyCallbackInfo<void>& info) {
60
  Environment* env = Environment::GetCurrent(info);
61
  int32_t port = value->Int32Value(env->context()).FromMaybe(0);
62
  ExclusiveAccess<HostPort>::Scoped host_port(env->inspector_host_port());
63
  host_port->set_port(static_cast<int>(port));
64
}
65
66
18
static void GetParentProcessId(Local<Name> property,
67
                               const PropertyCallbackInfo<Value>& info) {
68
54
  info.GetReturnValue().Set(uv_os_getppid());
69
18
}
70
71
4703
MaybeLocal<Object> CreateProcessObject(Environment* env) {
72
4703
  Isolate* isolate = env->isolate();
73
4703
  EscapableHandleScope scope(isolate);
74
4703
  Local<Context> context = env->context();
75
76
4703
  Local<FunctionTemplate> process_template = FunctionTemplate::New(isolate);
77
9406
  process_template->SetClassName(env->process_string());
78
  Local<Function> process_ctor;
79
  Local<Object> process;
80

18812
  if (!process_template->GetFunction(context).ToLocal(&process_ctor) ||
81
14109
      !process_ctor->NewInstance(context).ToLocal(&process)) {
82
    return MaybeLocal<Object>();
83
  }
84
85
  // process.version
86
18812
  READONLY_PROPERTY(process,
87
                    "version",
88
                    FIXED_ONE_BYTE_STRING(env->isolate(), NODE_VERSION));
89
90
  // process.versions
91
4703
  Local<Object> versions = Object::New(env->isolate());
92
14109
  READONLY_PROPERTY(process, "versions", versions);
93
94
#define V(key)                                                                 \
95
  if (!per_process::metadata.versions.key.empty()) {                           \
96
    READONLY_STRING_PROPERTY(                                                  \
97
        versions, #key, per_process::metadata.versions.key);                   \
98
  }
99







216338
  NODE_VERSIONS_KEYS(V)
100
#undef V
101
102
  // process.arch
103
18812
  READONLY_STRING_PROPERTY(process, "arch", per_process::metadata.arch);
104
105
  // process.platform
106
18812
  READONLY_STRING_PROPERTY(process, "platform", per_process::metadata.platform);
107
108
  // process.release
109
4703
  Local<Object> release = Object::New(env->isolate());
110
14109
  READONLY_PROPERTY(process, "release", release);
111
18812
  READONLY_STRING_PROPERTY(release, "name", per_process::metadata.release.name);
112
#if NODE_VERSION_IS_LTS
113
  READONLY_STRING_PROPERTY(release, "lts", per_process::metadata.release.lts);
114
#endif  // NODE_VERSION_IS_LTS
115
116
#ifdef NODE_HAS_RELEASE_URLS
117
  READONLY_STRING_PROPERTY(
118
      release, "sourceUrl", per_process::metadata.release.source_url);
119
  READONLY_STRING_PROPERTY(
120
      release, "headersUrl", per_process::metadata.release.headers_url);
121
#ifdef _WIN32
122
  READONLY_STRING_PROPERTY(
123
      release, "libUrl", per_process::metadata.release.lib_url);
124
#endif  // _WIN32
125
#endif  // NODE_HAS_RELEASE_URLS
126
127
  // process._rawDebug: may be overwritten later in JS land, but should be
128
  // available from the beginning for debugging purposes
129
4703
  env->SetMethod(process, "_rawDebug", RawDebug);
130
131
4703
  return scope.Escape(process);
132
}
133
134
4679
void PatchProcessObject(const FunctionCallbackInfo<Value>& args) {
135
4679
  Isolate* isolate = args.GetIsolate();
136
4679
  Local<Context> context = isolate->GetCurrentContext();
137
4679
  Environment* env = Environment::GetCurrent(context);
138
9358
  CHECK(args[0]->IsObject());
139
9358
  Local<Object> process = args[0].As<Object>();
140
141
  // process.title
142

14037
  CHECK(process
143
            ->SetAccessor(
144
                context,
145
                FIXED_ONE_BYTE_STRING(isolate, "title"),
146
                ProcessTitleGetter,
147
                env->owns_process_state() ? ProcessTitleSetter : nullptr,
148
                Local<Value>(),
149
                DEFAULT,
150
                None,
151
                SideEffectType::kHasNoSideEffect)
152
            .FromJust());
153
154
  // process.argv
155
9358
  process->Set(context,
156
               FIXED_ONE_BYTE_STRING(isolate, "argv"),
157
18716
               ToV8Value(context, env->argv()).ToLocalChecked()).Check();
158
159
  // process.execArgv
160
9358
  process->Set(context,
161
               FIXED_ONE_BYTE_STRING(isolate, "execArgv"),
162
9358
               ToV8Value(context, env->exec_argv())
163
14037
                   .ToLocalChecked()).Check();
164
165
18716
  READONLY_PROPERTY(process, "pid",
166
                    Integer::New(isolate, uv_os_getpid()));
167
168
14037
  CHECK(process->SetAccessor(context,
169
                             FIXED_ONE_BYTE_STRING(isolate, "ppid"),
170
                             GetParentProcessId).FromJust());
171
172
  // --security-revert flags
173
#define V(code, _, __)                                                        \
174
  do {                                                                        \
175
    if (IsReverted(SECURITY_REVERT_ ## code)) {                               \
176
      READONLY_PROPERTY(process, "REVERT_" #code, True(isolate));             \
177
    }                                                                         \
178
  } while (0);
179
  SECURITY_REVERSIONS(V)
180
#undef V
181
182
  // process.execPath
183
  process
184
9358
      ->Set(context,
185
            FIXED_ONE_BYTE_STRING(isolate, "execPath"),
186
9358
            String::NewFromUtf8(isolate,
187
4679
                                env->exec_path().c_str(),
188
                                NewStringType::kInternalized,
189
9358
                                env->exec_path().size())
190
14037
                .ToLocalChecked())
191
      .Check();
192
193
  // process.debugPort
194

14037
  CHECK(process
195
            ->SetAccessor(context,
196
                          FIXED_ONE_BYTE_STRING(isolate, "debugPort"),
197
                          DebugPortGetter,
198
                          env->owns_process_state() ? DebugPortSetter : nullptr,
199
                          Local<Value>())
200
            .FromJust());
201
4679
}
202
203
}  // namespace node