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-02-19 22:14:06 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::HandleScope;
19
using v8::Integer;
20
using v8::Isolate;
21
using v8::Just;
22
using v8::Local;
23
using v8::MaybeLocal;
24
using v8::Name;
25
using v8::NewStringType;
26
using v8::None;
27
using v8::Object;
28
using v8::PropertyCallbackInfo;
29
using v8::SideEffectType;
30
using v8::String;
31
using v8::Value;
32
33
24
static void ProcessTitleGetter(Local<Name> property,
34
                               const PropertyCallbackInfo<Value>& info) {
35
48
  std::string title = GetProcessTitle("node");
36
48
  info.GetReturnValue().Set(
37
48
      String::NewFromUtf8(info.GetIsolate(), title.data(),
38
48
                          NewStringType::kNormal, title.size())
39
      .ToLocalChecked());
40
24
}
41
42
92
static void ProcessTitleSetter(Local<Name> property,
43
                               Local<Value> value,
44
                               const PropertyCallbackInfo<void>& info) {
45
184
  node::Utf8Value title(info.GetIsolate(), value);
46

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

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







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

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

17508
  CHECK(process
197
            ->SetAccessor(context,
198
                          FIXED_ONE_BYTE_STRING(isolate, "debugPort"),
199
                          DebugPortGetter,
200
                          env->owns_process_state() ? DebugPortSetter : nullptr,
201
                          env->as_callback_data())
202
            .FromJust());
203
4377
}
204
205
}  // namespace node