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: 55 60 91.7 %
Date: 2019-09-25 22:36:03 Branches: 31 56 55.4 %

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
11
static void ProcessTitleGetter(Local<Name> property,
34
                               const PropertyCallbackInfo<Value>& info) {
35
  char buffer[512];
36
11
  uv_get_process_title(buffer, sizeof(buffer));
37
  info.GetReturnValue().Set(
38
11
      String::NewFromUtf8(info.GetIsolate(), buffer, NewStringType::kNormal)
39
33
          .ToLocalChecked());
40
11
}
41
42
92
static void ProcessTitleSetter(Local<Name> property,
43
                               Local<Value> value,
44
                               const PropertyCallbackInfo<void>& info) {
45
92
  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
84
static void DebugPortGetter(Local<Name> property,
52
                            const PropertyCallbackInfo<Value>& info) {
53
84
  Environment* env = Environment::GetCurrent(info);
54
84
  int port = env->inspector_host_port()->port();
55
168
  info.GetReturnValue().Set(port);
56
84
}
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
  env->inspector_host_port()->set_port(static_cast<int>(port));
64
}
65
66
6
static void GetParentProcessId(Local<Name> property,
67
                               const PropertyCallbackInfo<Value>& info) {
68
18
  info.GetReturnValue().Set(uv_os_getppid());
69
6
}
70
71
5159
MaybeLocal<Object> CreateProcessObject(Environment* env) {
72
5159
  Isolate* isolate = env->isolate();
73
5159
  EscapableHandleScope scope(isolate);
74
5159
  Local<Context> context = env->context();
75
76
5159
  Local<FunctionTemplate> process_template = FunctionTemplate::New(isolate);
77
10318
  process_template->SetClassName(FIXED_ONE_BYTE_STRING(isolate, "process"));
78
  Local<Function> process_ctor;
79
  Local<Object> process;
80


30954
  if (!process_template->GetFunction(context).ToLocal(&process_ctor) ||
81
15477
      !process_ctor->NewInstance(context).ToLocal(&process)) {
82
    return MaybeLocal<Object>();
83
  }
84
85
  // process.version
86
20636
  READONLY_PROPERTY(process,
87
                    "version",
88
                    FIXED_ONE_BYTE_STRING(env->isolate(), NODE_VERSION));
89
90
  // process.versions
91
5159
  Local<Object> versions = Object::New(env->isolate());
92
15477
  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







237314
  NODE_VERSIONS_KEYS(V)
100
#undef V
101
102
  // process.arch
103
20636
  READONLY_STRING_PROPERTY(process, "arch", per_process::metadata.arch);
104
105
  // process.platform
106
20636
  READONLY_STRING_PROPERTY(process, "platform", per_process::metadata.platform);
107
108
  // process.release
109
5159
  Local<Object> release = Object::New(env->isolate());
110
15477
  READONLY_PROPERTY(process, "release", release);
111
20636
  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
5159
  env->SetMethod(process, "_rawDebug", RawDebug);
130
131
5159
  return scope.Escape(process);
132
}
133
134
5156
void PatchProcessObject(const FunctionCallbackInfo<Value>& args) {
135
5156
  Isolate* isolate = args.GetIsolate();
136
5156
  Local<Context> context = isolate->GetCurrentContext();
137
5156
  Environment* env = Environment::GetCurrent(context);
138
10312
  CHECK(args[0]->IsObject());
139
10312
  Local<Object> process = args[0].As<Object>();
140
141
  // process.title
142

20624
  CHECK(process
143
            ->SetAccessor(
144
                context,
145
                FIXED_ONE_BYTE_STRING(isolate, "title"),
146
                ProcessTitleGetter,
147
                env->owns_process_state() ? ProcessTitleSetter : nullptr,
148
                env->as_callback_data(),
149
                DEFAULT,
150
                None,
151
                SideEffectType::kHasNoSideEffect)
152
            .FromJust());
153
154
  // process.argv
155
  process->Set(context,
156
               FIXED_ONE_BYTE_STRING(isolate, "argv"),
157
20624
               ToV8Value(context, env->argv()).ToLocalChecked()).Check();
158
159
  // process.execArgv
160
  process->Set(context,
161
               FIXED_ONE_BYTE_STRING(isolate, "execArgv"),
162
5156
               ToV8Value(context, env->exec_argv())
163
20624
                   .ToLocalChecked()).Check();
164
165
20624
  READONLY_PROPERTY(process, "pid",
166
                    Integer::New(isolate, uv_os_getpid()));
167
168
15468
  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
      ->Set(context,
185
            FIXED_ONE_BYTE_STRING(isolate, "execPath"),
186
            String::NewFromUtf8(isolate,
187
5156
                                env->exec_path().c_str(),
188
                                NewStringType::kInternalized,
189
10312
                                env->exec_path().size())
190
20624
                .ToLocalChecked())
191
10312
      .Check();
192
193
  // process.debugPort
194

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