GCC Code Coverage Report
Directory: ../src/ Exec Total Coverage
File: /home/node-core-coverage/node-core-coverage/workdir/node/out/../src/env-inl.h Lines: 138 138 100.0 %
Date: 2016-12-16 Branches: 37 52 71.2 %

Line Branch Exec Source
1
#ifndef SRC_ENV_INL_H_
2
#define SRC_ENV_INL_H_
3
4
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
6
#include "env.h"
7
#include "node.h"
8
#include "util.h"
9
#include "util-inl.h"
10
#include "uv.h"
11
#include "v8.h"
12
13
#include <stddef.h>
14
#include <stdint.h>
15
16
namespace node {
17
18
// Create string properties as internalized one byte strings.
19
//
20
// Internalized because it makes property lookups a little faster and because
21
// the string is created in the old space straight away.  It's going to end up
22
// in the old space sooner or later anyway but now it doesn't go through
23
// v8::Eternal's new space handling first.
24
//
25
// One byte because our strings are ASCII and we can safely skip V8's UTF-8
26
// decoding step.  It's a one-time cost, but why pay it when you don't have to?
27
1722
inline IsolateData::IsolateData(v8::Isolate* isolate, uv_loop_t* event_loop,
28
                                uint32_t* zero_fill_field)
29
    :
30
#define V(PropertyName, StringValue)                                          \
31
    PropertyName ## _(                                                        \
32
        isolate,                                                              \
33
        v8::Private::New(                                                     \
34
            isolate,                                                          \
35
            v8::String::NewFromOneByte(                                       \
36
                isolate,                                                      \
37
                reinterpret_cast<const uint8_t*>(StringValue),                \
38
                v8::NewStringType::kInternalized,                             \
39
                sizeof(StringValue) - 1).ToLocalChecked())),
40
13776
  PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(V)
41
#undef V
42
#define V(PropertyName, StringValue)                                          \
43
    PropertyName ## _(                                                        \
44
        isolate,                                                              \
45
        v8::String::NewFromOneByte(                                           \
46
            isolate,                                                          \
47
            reinterpret_cast<const uint8_t*>(StringValue),                    \
48
            v8::NewStringType::kInternalized,                                 \
49
            sizeof(StringValue) - 1).ToLocalChecked()),
50
275520
    PER_ISOLATE_STRING_PROPERTIES(V)
51
#undef V
52
578592
    event_loop_(event_loop), zero_fill_field_(zero_fill_field) {}
53
54
inline uv_loop_t* IsolateData::event_loop() const {
55
  return event_loop_;
56
}
57
58
inline uint32_t* IsolateData::zero_fill_field() const {
59
  return zero_fill_field_;
60
}
61
62
inline Environment::AsyncHooks::AsyncHooks() {
63
3444
  for (int i = 0; i < kFieldsCount; i++) fields_[i] = 0;
64
}
65
66
inline uint32_t* Environment::AsyncHooks::fields() {
67
  return fields_;
68
}
69
70
inline int Environment::AsyncHooks::fields_count() const {
71
  return kFieldsCount;
72
}
73
74
inline bool Environment::AsyncHooks::callbacks_enabled() {
75
86
  return fields_[kEnableCallbacks] != 0;
76
}
77
78
inline void Environment::AsyncHooks::set_enable_callbacks(uint32_t flag) {
79
13
  fields_[kEnableCallbacks] = flag;
80
}
81
82
inline Environment::AsyncCallbackScope::AsyncCallbackScope(Environment* env)
83
143350
    : env_(env) {
84
143350
  env_->makecallback_cntr_++;
85
}
86
87
143146
inline Environment::AsyncCallbackScope::~AsyncCallbackScope() {
88
143146
  env_->makecallback_cntr_--;
89
}
90
91
inline bool Environment::AsyncCallbackScope::in_makecallback() {
92
113397
  return env_->makecallback_cntr_ > 1;
93
}
94
95
inline Environment::DomainFlag::DomainFlag() {
96
3444
  for (int i = 0; i < kFieldsCount; ++i) fields_[i] = 0;
97
}
98
99
inline uint32_t* Environment::DomainFlag::fields() {
100
128
  return fields_;
101
}
102
103
inline int Environment::DomainFlag::fields_count() const {
104
  return kFieldsCount;
105
}
106
107
inline uint32_t Environment::DomainFlag::count() const {
108
209
  return fields_[kCount];
109
}
110
111
inline Environment::TickInfo::TickInfo() {
112
8610
  for (int i = 0; i < kFieldsCount; ++i)
113
3444
    fields_[i] = 0;
114
}
115
116
inline uint32_t* Environment::TickInfo::fields() {
117
1722
  return fields_;
118
}
119
120
inline int Environment::TickInfo::fields_count() const {
121
  return kFieldsCount;
122
}
123
124
inline uint32_t Environment::TickInfo::index() const {
125
  return fields_[kIndex];
126
}
127
128
inline uint32_t Environment::TickInfo::length() const {
129
  return fields_[kLength];
130
}
131
132
inline void Environment::TickInfo::set_index(uint32_t value) {
133
47421
  fields_[kIndex] = value;
134
}
135
136
inline void Environment::AssignToContext(v8::Local<v8::Context> context) {
137
1984
  context->SetAlignedPointerInEmbedderData(kContextEmbedderDataIndex, this);
138
}
139
140
inline Environment* Environment::GetCurrent(v8::Isolate* isolate) {
141
13304
  return GetCurrent(isolate->GetCurrentContext());
142
}
143
144
inline Environment* Environment::GetCurrent(v8::Local<v8::Context> context) {
145
  return static_cast<Environment*>(
146
76244
      context->GetAlignedPointerFromEmbedderData(kContextEmbedderDataIndex));
147
}
148
149
5225289
inline Environment* Environment::GetCurrent(
150
    const v8::FunctionCallbackInfo<v8::Value>& info) {
151
5225289
  ASSERT(info.Data()->IsExternal());
152
10450578
  return static_cast<Environment*>(info.Data().As<v8::External>()->Value());
153
}
154
155
template <typename T>
156
10279
inline Environment* Environment::GetCurrent(
157
    const v8::PropertyCallbackInfo<T>& info) {
158

10279
  ASSERT(info.Data()->IsExternal());
159
  // XXX(bnoordhuis) Work around a g++ 4.9.2 template type inferrer bug
160
  // when the expression is written as info.Data().As<v8::External>().
161
10279
  v8::Local<v8::Value> data = info.Data();
162
10279
  return static_cast<Environment*>(data.As<v8::External>()->Value());
163
}
164
165
1722
inline Environment::Environment(IsolateData* isolate_data,
166
                                v8::Local<v8::Context> context)
167
1722
    : isolate_(context->GetIsolate()),
168
      isolate_data_(isolate_data),
169
1722
      timer_base_(uv_now(isolate_data->event_loop())),
170
      using_domains_(false),
171
      printed_error_(false),
172
      trace_sync_io_(false),
173
      makecallback_cntr_(0),
174
      async_wrap_uid_(0),
175
      debugger_agent_(this),
176
#if HAVE_INSPECTOR
177
      inspector_agent_(this),
178
#endif
179
      handle_cleanup_waiting_(0),
180
      http_parser_buffer_(nullptr),
181
56826
      context_(context->GetIsolate(), context) {
182
  // We'll be creating new objects so make sure we've entered the context.
183
3444
  v8::HandleScope handle_scope(isolate());
184
3444
  v8::Context::Scope context_scope(context);
185
1722
  set_as_external(v8::External::New(isolate(), this));
186
1722
  set_binding_cache_object(v8::Object::New(isolate()));
187
1722
  set_module_load_list_array(v8::Array::New(isolate()));
188
189
3444
  v8::Local<v8::FunctionTemplate> fn = v8::FunctionTemplate::New(isolate());
190
1722
  fn->SetClassName(FIXED_ONE_BYTE_STRING(isolate(), "InternalFieldObject"));
191
1722
  v8::Local<v8::ObjectTemplate> obj = fn->InstanceTemplate();
192
1722
  obj->SetInternalFieldCount(1);
193
1722
  set_generic_internal_field_template(obj);
194
195
1722
  RB_INIT(&cares_task_list_);
196
1722
  AssignToContext(context);
197
198
1722
  destroy_ids_list_.reserve(512);
199
1722
}
200
201
53061
inline Environment::~Environment() {
202
3033
  v8::HandleScope handle_scope(isolate());
203
204
18176
  while (HandleCleanup* hc = handle_cleanup_queue_.PopFront()) {
205
7571
    handle_cleanup_waiting_++;
206
7571
    hc->cb_(this, hc->handle_, hc->arg_);
207
15142
    delete hc;
208
  }
209
210
1516
  while (handle_cleanup_waiting_ != 0)
211
1517
    uv_run(event_loop(), UV_RUN_ONCE);
212
213
1516
  context()->SetAlignedPointerInEmbedderData(kContextEmbedderDataIndex,
214
1516
                                             nullptr);
215
#define V(PropertyName, TypeName) PropertyName ## _.Reset();
216
45480
  ENVIRONMENT_STRONG_PERSISTENT_PROPERTIES(V)
217
#undef V
218
219
1516
  delete[] heap_statistics_buffer_;
220
1516
  delete[] heap_space_statistics_buffer_;
221
1516
  delete[] http_parser_buffer_;
222
1516
}
223
224
inline v8::Isolate* Environment::isolate() const {
225
  return isolate_;
226
}
227
228
inline bool Environment::async_wrap_callbacks_enabled() const {
229
  // The const_cast is okay, it doesn't violate conceptual const-ness.
230
144
  return const_cast<Environment*>(this)->async_hooks()->callbacks_enabled();
231
}
232
233
inline bool Environment::in_domain() const {
234
  // The const_cast is okay, it doesn't violate conceptual const-ness.
235



36552
  return using_domains() &&
236
418
         const_cast<Environment*>(this)->domain_flag()->count() > 0;
237
}
238
239
inline Environment* Environment::from_immediate_check_handle(
240
    uv_check_t* handle) {
241
3667
  return ContainerOf(&Environment::immediate_check_handle_, handle);
242
}
243
244
inline uv_check_t* Environment::immediate_check_handle() {
245
11162
  return &immediate_check_handle_;
246
}
247
248
inline uv_idle_t* Environment::immediate_idle_handle() {
249
5229
  return &immediate_idle_handle_;
250
}
251
252
inline Environment* Environment::from_destroy_ids_idle_handle(
253
    uv_idle_t* handle) {
254
32
  return ContainerOf(&Environment::destroy_ids_idle_handle_, handle);
255
}
256
257
inline uv_idle_t* Environment::destroy_ids_idle_handle() {
258
1754
  return &destroy_ids_idle_handle_;
259
}
260
261
8559
inline void Environment::RegisterHandleCleanup(uv_handle_t* handle,
262
                                               HandleCleanupCb cb,
263
                                               void *arg) {
264
25677
  handle_cleanup_queue_.PushBack(new HandleCleanup(handle, cb, arg));
265
8559
}
266
267
inline void Environment::FinishHandleCleanup(uv_handle_t* handle) {
268
7566
  handle_cleanup_waiting_--;
269
}
270
271
inline uv_loop_t* Environment::event_loop() const {
272
8948513
  return isolate_data()->event_loop();
273
}
274
275
inline Environment::AsyncHooks* Environment::async_hooks() {
276
  return &async_hooks_;
277
}
278
279
inline Environment::DomainFlag* Environment::domain_flag() {
280
  return &domain_flag_;
281
}
282
283
inline Environment::TickInfo* Environment::tick_info() {
284
78906
  return &tick_info_;
285
}
286
287
inline uint64_t Environment::timer_base() const {
288
  return timer_base_;
289
}
290
291
inline bool Environment::using_domains() const {
292
  return using_domains_;
293
}
294
295
inline void Environment::set_using_domains(bool value) {
296
128
  using_domains_ = value;
297
}
298
299
inline bool Environment::printed_error() const {
300
  return printed_error_;
301
}
302
303
inline void Environment::set_printed_error(bool value) {
304
10
  printed_error_ = value;
305
}
306
307
inline void Environment::set_trace_sync_io(bool value) {
308
3148
  trace_sync_io_ = value;
309
}
310
311
inline int64_t Environment::get_async_wrap_uid() {
312
49332
  return ++async_wrap_uid_;
313
}
314
315
inline std::vector<int64_t>* Environment::destroy_ids_list() {
316
52
  return &destroy_ids_list_;
317
}
318
319
inline double* Environment::heap_statistics_buffer() const {
320

4
  CHECK_NE(heap_statistics_buffer_, nullptr);
321
  return heap_statistics_buffer_;
322
}
323
324
inline void Environment::set_heap_statistics_buffer(double* pointer) {
325
3
  CHECK_EQ(heap_statistics_buffer_, nullptr);  // Should be set only once.
326
3
  heap_statistics_buffer_ = pointer;
327
}
328
329
inline double* Environment::heap_space_statistics_buffer() const {
330

4
  CHECK_NE(heap_space_statistics_buffer_, nullptr);
331
  return heap_space_statistics_buffer_;
332
}
333
334
inline void Environment::set_heap_space_statistics_buffer(double* pointer) {
335
3
  CHECK_EQ(heap_space_statistics_buffer_, nullptr);  // Should be set only once.
336
3
  heap_space_statistics_buffer_ = pointer;
337
}
338
339
340
inline char* Environment::http_parser_buffer() const {
341
  return http_parser_buffer_;
342
}
343
344
inline void Environment::set_http_parser_buffer(char* buffer) {
345
189
  CHECK_EQ(http_parser_buffer_, nullptr);  // Should be set only once.
346
189
  http_parser_buffer_ = buffer;
347
}
348
349
inline Environment* Environment::from_cares_timer_handle(uv_timer_t* handle) {
350
1502
  return ContainerOf(&Environment::cares_timer_handle_, handle);
351
}
352
353
inline uv_timer_t* Environment::cares_timer_handle() {
354
1677
  return &cares_timer_handle_;
355
}
356
357
inline ares_channel Environment::cares_channel() {
358
  return cares_channel_;
359
}
360
361
// Only used in the call to ares_init_options().
362
inline ares_channel* Environment::cares_channel_ptr() {
363
1671
  return &cares_channel_;
364
}
365
366
inline node_ares_task_list* Environment::cares_task_list() {
367
4
  return &cares_task_list_;
368
}
369
370
inline IsolateData* Environment::isolate_data() const {
371
  return isolate_data_;
372
}
373
374
inline void Environment::ThrowError(const char* errmsg) {
375
678
  ThrowError(v8::Exception::Error, errmsg);
376
}
377
378
inline void Environment::ThrowTypeError(const char* errmsg) {
379
108
  ThrowError(v8::Exception::TypeError, errmsg);
380
}
381
382
inline void Environment::ThrowRangeError(const char* errmsg) {
383
17
  ThrowError(v8::Exception::RangeError, errmsg);
384
}
385
386
803
inline void Environment::ThrowError(
387
    v8::Local<v8::Value> (*fun)(v8::Local<v8::String>),
388
    const char* errmsg) {
389
1606
  v8::HandleScope handle_scope(isolate());
390
803
  isolate()->ThrowException(fun(OneByteString(isolate(), errmsg)));
391
803
}
392
393
1
inline void Environment::ThrowErrnoException(int errorno,
394
                                             const char* syscall,
395
                                             const char* message,
396
                                             const char* path) {
397
  isolate()->ThrowException(
398
1
      ErrnoException(isolate(), errorno, syscall, message, path));
399
1
}
400
401
2053
inline void Environment::ThrowUVException(int errorno,
402
                                          const char* syscall,
403
                                          const char* message,
404
                                          const char* path,
405
                                          const char* dest) {
406
  isolate()->ThrowException(
407
2053
      UVException(isolate(), errorno, syscall, message, path, dest));
408
2053
}
409
410
inline v8::Local<v8::FunctionTemplate>
411
    Environment::NewFunctionTemplate(v8::FunctionCallback callback,
412
                                     v8::Local<v8::Signature> signature) {
413
476909
  v8::Local<v8::External> external = as_external();
414
476909
  return v8::FunctionTemplate::New(isolate(), callback, external, signature);
415
}
416
417
277609
inline void Environment::SetMethod(v8::Local<v8::Object> that,
418
                                   const char* name,
419
                                   v8::FunctionCallback callback) {
420
  v8::Local<v8::Function> function =
421
555218
      NewFunctionTemplate(callback)->GetFunction();
422
  // kInternalized strings are created in the old space.
423
277609
  const v8::NewStringType type = v8::NewStringType::kInternalized;
424
  v8::Local<v8::String> name_string =
425
555218
      v8::String::NewFromUtf8(isolate(), name, type).ToLocalChecked();
426
832827
  that->Set(name_string, function);
427
277609
  function->SetName(name_string);  // NODE_SET_METHOD() compatibility.
428
277609
}
429
430
176475
inline void Environment::SetProtoMethod(v8::Local<v8::FunctionTemplate> that,
431
                                        const char* name,
432
                                        v8::FunctionCallback callback) {
433
176475
  v8::Local<v8::Signature> signature = v8::Signature::New(isolate(), that);
434
176475
  v8::Local<v8::FunctionTemplate> t = NewFunctionTemplate(callback, signature);
435
  // kInternalized strings are created in the old space.
436
176475
  const v8::NewStringType type = v8::NewStringType::kInternalized;
437
  v8::Local<v8::String> name_string =
438
352950
      v8::String::NewFromUtf8(isolate(), name, type).ToLocalChecked();
439
705900
  that->PrototypeTemplate()->Set(name_string, t);
440
176475
  t->SetClassName(name_string);  // NODE_SET_PROTOTYPE_METHOD() compatibility.
441
176475
}
442
443
1722
inline void Environment::SetTemplateMethod(v8::Local<v8::FunctionTemplate> that,
444
                                           const char* name,
445
                                           v8::FunctionCallback callback) {
446
3444
  v8::Local<v8::FunctionTemplate> t = NewFunctionTemplate(callback);
447
  // kInternalized strings are created in the old space.
448
1722
  const v8::NewStringType type = v8::NewStringType::kInternalized;
449
  v8::Local<v8::String> name_string =
450
3444
      v8::String::NewFromUtf8(isolate(), name, type).ToLocalChecked();
451
5166
  that->Set(name_string, t);
452
1722
  t->SetClassName(name_string);  // NODE_SET_METHOD() compatibility.
453
1722
}
454
455
69
inline v8::Local<v8::Object> Environment::NewInternalFieldObject() {
456
  v8::MaybeLocal<v8::Object> m_obj =
457
138
      generic_internal_field_template()->NewInstance(context());
458
69
  return m_obj.ToLocalChecked();
459
}
460
461
#define VP(PropertyName, StringValue) V(v8::Private, PropertyName)
462
#define VS(PropertyName, StringValue) V(v8::String, PropertyName)
463
#define V(TypeName, PropertyName)                                             \
464
  inline                                                                      \
465
  v8::Local<TypeName> IsolateData::PropertyName(v8::Isolate* isolate) const { \
466
    /* Strings are immutable so casting away const-ness here is okay. */      \
467
    return const_cast<IsolateData*>(this)->PropertyName ## _.Get(isolate);    \
468
  }
469
8362
  PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP)
470
1150242
  PER_ISOLATE_STRING_PROPERTIES(VS)
471
#undef V
472
#undef VS
473
#undef VP
474
475
#define VP(PropertyName, StringValue) V(v8::Private, PropertyName)
476
#define VS(PropertyName, StringValue) V(v8::String, PropertyName)
477
#define V(TypeName, PropertyName)                                             \
478
  inline v8::Local<TypeName> Environment::PropertyName() const {              \
479
    return isolate_data()->PropertyName(isolate());                           \
480
  }
481
8362
  PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP)
482
1150242
  PER_ISOLATE_STRING_PROPERTIES(VS)
483
#undef V
484
#undef VS
485
#undef VP
486
487
#define V(PropertyName, TypeName)                                             \
488
  inline v8::Local<TypeName> Environment::PropertyName() const {              \
489
    return StrongPersistentToLocal(PropertyName ## _);                        \
490
  }                                                                           \
491
  inline void Environment::set_ ## PropertyName(v8::Local<TypeName> value) {  \
492
    PropertyName ## _.Reset(isolate(), value);                                \
493
  }
494
4061679
  ENVIRONMENT_STRONG_PERSISTENT_PROPERTIES(V)
495
#undef V
496
497
}  // namespace node
498
499
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
500
501
#endif  // SRC_ENV_INL_H_