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: 59 88 67.0 %
Date: 2019-02-01 22:03:38 Branches: 45 86 52.3 %

Line Branch Exec Source
1
#include <limits.h>  // 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
static void ProcessTitleGetter(Local<Name> property,
33
                               const PropertyCallbackInfo<Value>& info) {
34
  char buffer[512];
35
  uv_get_process_title(buffer, sizeof(buffer));
36
  info.GetReturnValue().Set(
37
      String::NewFromUtf8(info.GetIsolate(), buffer, NewStringType::kNormal)
38
          .ToLocalChecked());
39
}
40
41
83
static void ProcessTitleSetter(Local<Name> property,
42
                               Local<Value> value,
43
                               const PropertyCallbackInfo<void>& info) {
44
83
  node::Utf8Value title(info.GetIsolate(), value);
45

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


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

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








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

820
  CHECK(process->SetAccessor(env->context(),
294
                             debug_port_string,
295
                             DebugPortGetter,
296
                             env->is_main_thread() ? DebugPortSetter : nullptr,
297
                             env->as_external()).FromJust());
298
299
  // process._rawDebug: may be overwritten later in JS land, but should be
300
  // availbale from the begining for debugging purposes
301
164
  env->SetMethod(process, "_rawDebug", RawDebug);
302
303
164
  return scope.Escape(process);
304
}
305
306
}  // namespace node