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: 83 91 91.2 %
Date: 2019-03-02 22:23:06 Branches: 58 86 67.4 %

Line Branch Exec Source
1
#include <climits>  // PATH_MAX
2
3
#include "env-inl.h"
4
#include "node_internals.h"
5
#include "node_options-inl.h"
6
#include "node_metadata.h"
7
#include "node_process.h"
8
#include "node_revert.h"
9
#include "util-inl.h"
10
11
namespace node {
12
using v8::Context;
13
using v8::DEFAULT;
14
using v8::EscapableHandleScope;
15
using v8::Function;
16
using v8::FunctionTemplate;
17
using v8::HandleScope;
18
using v8::Integer;
19
using v8::Isolate;
20
using v8::Just;
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
10
static void ProcessTitleGetter(Local<Name> property,
33
                               const PropertyCallbackInfo<Value>& info) {
34
  char buffer[512];
35
10
  uv_get_process_title(buffer, sizeof(buffer));
36
  info.GetReturnValue().Set(
37
10
      String::NewFromUtf8(info.GetIsolate(), buffer, NewStringType::kNormal)
38
30
          .ToLocalChecked());
39
10
}
40
41
88
static void ProcessTitleSetter(Local<Name> property,
42
                               Local<Value> value,
43
                               const PropertyCallbackInfo<void>& info) {
44
88
  node::Utf8Value title(info.GetIsolate(), value);
45

177
  TRACE_EVENT_METADATA1(
46
      "__metadata", "process_name", "name", TRACE_STR_COPY(*title));
47
88
  uv_set_process_title(*title);
48
88
}
49
50
83
static void DebugPortGetter(Local<Name> property,
51
                            const PropertyCallbackInfo<Value>& info) {
52
83
  Environment* env = Environment::GetCurrent(info);
53
83
  int port = env->inspector_host_port()->port();
54
166
  info.GetReturnValue().Set(port);
55
83
}
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
  env->inspector_host_port()->set_port(static_cast<int>(port));
63
}
64
65
5
static void GetParentProcessId(Local<Name> property,
66
                               const PropertyCallbackInfo<Value>& info) {
67
15
  info.GetReturnValue().Set(uv_os_getppid());
68
5
}
69
70
4417
MaybeLocal<Object> CreateProcessObject(
71
    Environment* env,
72
    const std::vector<std::string>& args,
73
    const std::vector<std::string>& exec_args) {
74
4417
  Isolate* isolate = env->isolate();
75
4417
  EscapableHandleScope scope(isolate);
76
4417
  Local<Context> context = env->context();
77
78
4417
  Local<FunctionTemplate> process_template = FunctionTemplate::New(isolate);
79
8834
  process_template->SetClassName(FIXED_ONE_BYTE_STRING(isolate, "process"));
80
  Local<Function> process_ctor;
81
  Local<Object> process;
82


26502
  if (!process_template->GetFunction(context).ToLocal(&process_ctor) ||
83
13251
      !process_ctor->NewInstance(context).ToLocal(&process)) {
84
    return MaybeLocal<Object>();
85
  }
86
87
  // process.title
88
4417
  auto title_string = FIXED_ONE_BYTE_STRING(env->isolate(), "title");
89

22085
  CHECK(process
90
            ->SetAccessor(
91
                env->context(),
92
                title_string,
93
                ProcessTitleGetter,
94
                env->owns_process_state() ? ProcessTitleSetter : nullptr,
95
                env->as_external(),
96
                DEFAULT,
97
                None,
98
                SideEffectType::kHasNoSideEffect)
99
            .FromJust());
100
101
  // process.version
102
17668
  READONLY_PROPERTY(process,
103
                    "version",
104
                    FIXED_ONE_BYTE_STRING(env->isolate(), NODE_VERSION));
105
106
  // process.versions
107
4417
  Local<Object> versions = Object::New(env->isolate());
108
13251
  READONLY_PROPERTY(process, "versions", versions);
109
110
#define V(key)                                                                 \
111
  if (!per_process::metadata.versions.key.empty()) {                           \
112
    READONLY_STRING_PROPERTY(                                                  \
113
        versions, #key, per_process::metadata.versions.key);                   \
114
  }
115








216433
  NODE_VERSIONS_KEYS(V)
116
#undef V
117
118
  // process.arch
119
17668
  READONLY_STRING_PROPERTY(process, "arch", per_process::metadata.arch);
120
121
  // process.platform
122
17668
  READONLY_STRING_PROPERTY(process, "platform", per_process::metadata.platform);
123
124
  // process.release
125
4417
  Local<Object> release = Object::New(env->isolate());
126
13251
  READONLY_PROPERTY(process, "release", release);
127
17668
  READONLY_STRING_PROPERTY(release, "name", per_process::metadata.release.name);
128
#if NODE_VERSION_IS_LTS
129
  READONLY_STRING_PROPERTY(release, "lts", per_process::metadata.release.lts);
130
#endif  // NODE_VERSION_IS_LTS
131
132
#ifdef NODE_HAS_RELEASE_URLS
133
  READONLY_STRING_PROPERTY(
134
      release, "sourceUrl", per_process::metadata.release.source_url);
135
  READONLY_STRING_PROPERTY(
136
      release, "headersUrl", per_process::metadata.release.headers_url);
137
#ifdef _WIN32
138
  READONLY_STRING_PROPERTY(
139
      release, "libUrl", per_process::metadata.release.lib_url);
140
#endif  // _WIN32
141
#endif  // NODE_HAS_RELEASE_URLS
142
143
  // process.argv
144
  process->Set(env->context(),
145
               FIXED_ONE_BYTE_STRING(env->isolate(), "argv"),
146
22085
               ToV8Value(env->context(), args).ToLocalChecked()).FromJust();
147
148
  // process.execArgv
149
  process->Set(env->context(),
150
               FIXED_ONE_BYTE_STRING(env->isolate(), "execArgv"),
151
4417
               ToV8Value(env->context(), exec_args)
152
22085
                   .ToLocalChecked()).FromJust();
153
154
  Local<Object> env_var_proxy;
155
13251
  if (!CreateEnvVarProxy(context, isolate, env->as_external())
156
13251
           .ToLocal(&env_var_proxy))
157
    return MaybeLocal<Object>();
158
159
  // process.env
160
  process
161
      ->Set(env->context(),
162
            FIXED_ONE_BYTE_STRING(env->isolate(), "env"),
163
17668
            env_var_proxy)
164
8834
      .FromJust();
165
166
17668
  READONLY_PROPERTY(process, "pid",
167
                    Integer::New(env->isolate(), uv_os_getpid()));
168
169
17668
  CHECK(process->SetAccessor(env->context(),
170
                             FIXED_ONE_BYTE_STRING(env->isolate(), "ppid"),
171
                             GetParentProcessId).FromJust());
172
173
  // TODO(joyeecheung): the following process properties that are set using
174
  // parsed CLI flags should be migrated to `internal/options` in JS land.
175
176
  // -e, --eval
177
  // TODO(addaleax): Remove this.
178
4417
  if (env->options()->has_eval_string) {
179
1152
    READONLY_PROPERTY(process,
180
                      "_eval",
181
                      String::NewFromUtf8(
182
                          env->isolate(),
183
                          env->options()->eval_string.c_str(),
184
                          NewStringType::kNormal).ToLocalChecked());
185
  }
186
187
  // -p, --print
188
  // TODO(addaleax): Remove this.
189
4417
  if (env->options()->print_eval) {
190
496
    READONLY_PROPERTY(process, "_print_eval", True(env->isolate()));
191
  }
192
193
  // -c, --check
194
  // TODO(addaleax): Remove this.
195
4417
  if (env->options()->syntax_check_only) {
196
120
    READONLY_PROPERTY(process, "_syntax_check_only", True(env->isolate()));
197
  }
198
199
  // -i, --interactive
200
  // TODO(addaleax): Remove this.
201
4417
  if (env->options()->force_repl) {
202
36
    READONLY_PROPERTY(process, "_forceRepl", True(env->isolate()));
203
  }
204
205
  // -r, --require
206
  // TODO(addaleax): Remove this.
207
  const std::vector<std::string>& preload_modules =
208
4417
      env->options()->preload_modules;
209
4417
  if (!preload_modules.empty()) {
210
100
    READONLY_PROPERTY(process,
211
                      "_preload_modules",
212
                      ToV8Value(env->context(), preload_modules)
213
                          .ToLocalChecked());
214
  }
215
216
  // --no-deprecation
217
4417
  if (env->options()->no_deprecation) {
218
16
    READONLY_PROPERTY(process, "noDeprecation", True(env->isolate()));
219
  }
220
221
  // --no-warnings
222
4417
  if (env->options()->no_warnings) {
223
168
    READONLY_PROPERTY(process, "noProcessWarnings", True(env->isolate()));
224
  }
225
226
  // --trace-warnings
227
4417
  if (env->options()->trace_warnings) {
228
16
    READONLY_PROPERTY(process, "traceProcessWarnings", True(env->isolate()));
229
  }
230
231
  // --throw-deprecation
232
4417
  if (env->options()->throw_deprecation) {
233
4
    READONLY_PROPERTY(process, "throwDeprecation", True(env->isolate()));
234
  }
235
236
#ifdef NODE_NO_BROWSER_GLOBALS
237
  // configure --no-browser-globals
238
  READONLY_PROPERTY(process, "_noBrowserGlobals", True(env->isolate()));
239
#endif  // NODE_NO_BROWSER_GLOBALS
240
241
  // --prof-process
242
  // TODO(addaleax): Remove this.
243
4417
  if (env->options()->prof_process) {
244
36
    READONLY_PROPERTY(process, "profProcess", True(env->isolate()));
245
  }
246
247
  // --trace-deprecation
248
4417
  if (env->options()->trace_deprecation) {
249
8
    READONLY_PROPERTY(process, "traceDeprecation", True(env->isolate()));
250
  }
251
252
  // --inspect-brk
253
4417
  if (env->options()->debug_options().wait_for_connect()) {
254
44
    READONLY_DONT_ENUM_PROPERTY(process,
255
                                "_breakFirstLine", True(env->isolate()));
256
  }
257
258
  // --inspect-brk-node
259
4417
  if (env->options()->debug_options().break_node_first_line) {
260
    READONLY_DONT_ENUM_PROPERTY(process,
261
                                "_breakNodeFirstLine", True(env->isolate()));
262
  }
263
264
  // --security-revert flags
265
#define V(code, _, __)                                                        \
266
  do {                                                                        \
267
    if (IsReverted(SECURITY_REVERT_ ## code)) {                               \
268
      READONLY_PROPERTY(process, "REVERT_" #code, True(env->isolate()));      \
269
    }                                                                         \
270
  } while (0);
271
  SECURITY_REVERSIONS(V)
272
#undef V
273
274
  // process.execPath
275
  {
276
    char exec_path_buf[2 * PATH_MAX];
277
4417
    size_t exec_path_len = sizeof(exec_path_buf);
278
4417
    std::string exec_path;
279
4417
    if (uv_exepath(exec_path_buf, &exec_path_len) == 0) {
280
4417
      exec_path = std::string(exec_path_buf, exec_path_len);
281
    } else {
282
      exec_path = args[0];
283
    }
284
    // On OpenBSD process.execPath will be relative unless we
285
    // get the full path before process.execPath is used.
286
#if defined(__OpenBSD__)
287
    uv_fs_t req;
288
    req.ptr = nullptr;
289
    if (0 ==
290
        uv_fs_realpath(env->event_loop(), &req, exec_path.c_str(), nullptr)) {
291
      CHECK_NOT_NULL(req.ptr);
292
      exec_path = std::string(static_cast<char*>(req.ptr));
293
    }
294
#endif
295
    process
296
        ->Set(env->context(),
297
              FIXED_ONE_BYTE_STRING(env->isolate(), "execPath"),
298
              String::NewFromUtf8(env->isolate(),
299
                                  exec_path.c_str(),
300
                                  NewStringType::kInternalized,
301
4417
                                  exec_path.size())
302
22085
                  .ToLocalChecked())
303
13251
        .FromJust();
304
  }
305
306
  // process.debugPort
307
4417
  auto debug_port_string = FIXED_ONE_BYTE_STRING(env->isolate(), "debugPort");
308

22085
  CHECK(process
309
            ->SetAccessor(env->context(),
310
                          debug_port_string,
311
                          DebugPortGetter,
312
                          env->owns_process_state() ? DebugPortSetter : nullptr,
313
                          env->as_external())
314
            .FromJust());
315
316
  // process._rawDebug: may be overwritten later in JS land, but should be
317
  // availbale from the begining for debugging purposes
318
4417
  env->SetMethod(process, "_rawDebug", RawDebug);
319
320
4417
  return scope.Escape(process);
321
}
322
323
}  // namespace node