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: 79 93 84.9 %
Date: 2021-02-19 04:08:54 Branches: 31 56 55.4 %

Line Branch Exec Source
1
#include "env-inl.h"
2
#include "node_external_reference.h"
3
#include "node_internals.h"
4
#include "node_metadata.h"
5
#include "node_options-inl.h"
6
#include "node_process.h"
7
#include "node_revert.h"
8
#include "util-inl.h"
9
10
#include <climits>  // PATH_MAX
11
12
namespace node {
13
using v8::Context;
14
using v8::DEFAULT;
15
using v8::EscapableHandleScope;
16
using v8::Function;
17
using v8::FunctionCallbackInfo;
18
using v8::FunctionTemplate;
19
using v8::Integer;
20
using v8::Isolate;
21
using v8::Local;
22
using v8::MaybeLocal;
23
using v8::Name;
24
using v8::NewStringType;
25
using v8::None;
26
using v8::Object;
27
using v8::PropertyCallbackInfo;
28
using v8::SideEffectType;
29
using v8::String;
30
using v8::Value;
31
32
2
static void ProcessTitleGetter(Local<Name> property,
33
                               const PropertyCallbackInfo<Value>& info) {
34
4
  std::string title = GetProcessTitle("node");
35
4
  info.GetReturnValue().Set(
36
4
      String::NewFromUtf8(info.GetIsolate(), title.data(),
37
4
                          NewStringType::kNormal, title.size())
38
      .ToLocalChecked());
39
2
}
40
41
95
static void ProcessTitleSetter(Local<Name> property,
42
                               Local<Value> value,
43
                               const PropertyCallbackInfo<void>& info) {
44
190
  node::Utf8Value title(info.GetIsolate(), value);
45
  TRACE_EVENT_METADATA1(
46
95
      "__metadata", "process_name", "name", TRACE_STR_COPY(*title));
47
95
  uv_set_process_title(*title);
48
188
}
49
95
50
static void DebugPortGetter(Local<Name> property,
51
                            const PropertyCallbackInfo<Value>& info) {
52
  Environment* env = Environment::GetCurrent(info);
53
  ExclusiveAccess<HostPort>::Scoped host_port(env->inspector_host_port());
54
  int port = host_port->port();
55
  info.GetReturnValue().Set(port);
56
}
57
58
static void DebugPortSetter(Local<Name> property,
59
                            Local<Value> value,
60
                            const PropertyCallbackInfo<void>& info) {
61
  Environment* env = Environment::GetCurrent(info);
62
  int32_t port = value->Int32Value(env->context()).FromMaybe(0);
63
  ExclusiveAccess<HostPort>::Scoped host_port(env->inspector_host_port());
64
  host_port->set_port(static_cast<int>(port));
65
}
66
67
static void GetParentProcessId(Local<Name> property,
68
                               const PropertyCallbackInfo<Value>& info) {
69
  info.GetReturnValue().Set(uv_os_getppid());
70
}
71
72
52
MaybeLocal<Object> CreateProcessObject(Environment* env) {
73
52
  Isolate* isolate = env->isolate();
74
52
  EscapableHandleScope scope(isolate);
75
52
  Local<Context> context = env->context();
76
77
52
  Local<FunctionTemplate> process_template = FunctionTemplate::New(isolate);
78
104
  process_template->SetClassName(env->process_string());
79
  Local<Function> process_ctor;
80
  Local<Object> process;
81

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

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

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

360
  CHECK(process
196
            ->SetAccessor(context,
197
                          FIXED_ONE_BYTE_STRING(isolate, "debugPort"),
198
                          DebugPortGetter,
199
                          env->owns_process_state() ? DebugPortSetter : nullptr,
200
                          Local<Value>())
201
            .FromJust());
202
120
}
203
204
107
void RegisterProcessExternalReferences(ExternalReferenceRegistry* registry) {
205
107
  registry->Register(RawDebug);
206
107
}
207
208
}  // namespace node
209
210

473
NODE_MODULE_EXTERNAL_REFERENCE(process_object,
211
                               node::RegisterProcessExternalReferences)