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: 134 134 100.0 %
Date: 2016-11-30 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
1698
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
13584
  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
271680
    PER_ISOLATE_STRING_PROPERTIES(V)
51
#undef V
52
570528
    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
3396
  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
141982
    : env_(env) {
84
141982
  env_->makecallback_cntr_++;
85
}
86
87
141778
inline Environment::AsyncCallbackScope::~AsyncCallbackScope() {
88
141778
  env_->makecallback_cntr_--;
89
}
90
91
inline bool Environment::AsyncCallbackScope::in_makecallback() {
92
112077
  return env_->makecallback_cntr_ > 1;
93
}
94
95
inline Environment::DomainFlag::DomainFlag() {
96
3396
  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
8490
  for (int i = 0; i < kFieldsCount; ++i)
113
3396
    fields_[i] = 0;
114
}
115
116
inline uint32_t* Environment::TickInfo::fields() {
117
1698
  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
47157
  fields_[kIndex] = value;
134
}
135
136
inline void Environment::AssignToContext(v8::Local<v8::Context> context) {
137
1959
  context->SetAlignedPointerInEmbedderData(kContextEmbedderDataIndex, this);
138
}
139
140
inline Environment* Environment::GetCurrent(v8::Isolate* isolate) {
141
13188
  return GetCurrent(isolate->GetCurrentContext());
142
}
143
144
inline Environment* Environment::GetCurrent(v8::Local<v8::Context> context) {
145
  return static_cast<Environment*>(
146
75226
      context->GetAlignedPointerFromEmbedderData(kContextEmbedderDataIndex));
147
}
148
149
5469435
inline Environment* Environment::GetCurrent(
150
    const v8::FunctionCallbackInfo<v8::Value>& info) {
151
5469435
  ASSERT(info.Data()->IsExternal());
152
10938870
  return static_cast<Environment*>(info.Data().As<v8::External>()->Value());
153
}
154
155
template <typename T>
156
9720
inline Environment* Environment::GetCurrent(
157
    const v8::PropertyCallbackInfo<T>& info) {
158

9720
  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
9720
  v8::Local<v8::Value> data = info.Data();
162
9720
  return static_cast<Environment*>(data.As<v8::External>()->Value());
163
}
164
165
1698
inline Environment::Environment(IsolateData* isolate_data,
166
                                v8::Local<v8::Context> context)
167
1698
    : isolate_(context->GetIsolate()),
168
      isolate_data_(isolate_data),
169
1698
      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
54336
      context_(context->GetIsolate(), context) {
182
  // We'll be creating new objects so make sure we've entered the context.
183
3396
  v8::HandleScope handle_scope(isolate());
184
3396
  v8::Context::Scope context_scope(context);
185
1698
  set_as_external(v8::External::New(isolate(), this));
186
1698
  set_binding_cache_object(v8::Object::New(isolate()));
187
1698
  set_module_load_list_array(v8::Array::New(isolate()));
188
189
3396
  v8::Local<v8::FunctionTemplate> fn = v8::FunctionTemplate::New(isolate());
190
1698
  fn->SetClassName(FIXED_ONE_BYTE_STRING(isolate(), "InternalFieldObject"));
191
1698
  v8::Local<v8::ObjectTemplate> obj = fn->InstanceTemplate();
192
1698
  obj->SetInternalFieldCount(1);
193
1698
  set_generic_internal_field_template(obj);
194
195
1698
  RB_INIT(&cares_task_list_);
196
1698
  AssignToContext(context);
197
1698
}
198
199
50728
inline Environment::~Environment() {
200
2984
  v8::HandleScope handle_scope(isolate());
201
202
17876
  while (HandleCleanup* hc = handle_cleanup_queue_.PopFront()) {
203
7446
    handle_cleanup_waiting_++;
204
7446
    hc->cb_(this, hc->handle_, hc->arg_);
205
14892
    delete hc;
206
  }
207
208
1492
  while (handle_cleanup_waiting_ != 0)
209
1492
    uv_run(event_loop(), UV_RUN_ONCE);
210
211
1492
  context()->SetAlignedPointerInEmbedderData(kContextEmbedderDataIndex,
212
1492
                                             nullptr);
213
#define V(PropertyName, TypeName) PropertyName ## _.Reset();
214
44760
  ENVIRONMENT_STRONG_PERSISTENT_PROPERTIES(V)
215
#undef V
216
217
1492
  delete[] heap_statistics_buffer_;
218
1492
  delete[] heap_space_statistics_buffer_;
219
1492
  delete[] http_parser_buffer_;
220
1492
}
221
222
inline v8::Isolate* Environment::isolate() const {
223
  return isolate_;
224
}
225
226
inline bool Environment::async_wrap_callbacks_enabled() const {
227
  // The const_cast is okay, it doesn't violate conceptual const-ness.
228
144
  return const_cast<Environment*>(this)->async_hooks()->callbacks_enabled();
229
}
230
231
inline bool Environment::in_domain() const {
232
  // The const_cast is okay, it doesn't violate conceptual const-ness.
233



36158
  return using_domains() &&
234
418
         const_cast<Environment*>(this)->domain_flag()->count() > 0;
235
}
236
237
inline Environment* Environment::from_immediate_check_handle(
238
    uv_check_t* handle) {
239
3151
  return ContainerOf(&Environment::immediate_check_handle_, handle);
240
}
241
242
inline uv_check_t* Environment::immediate_check_handle() {
243
10580
  return &immediate_check_handle_;
244
}
245
246
inline uv_idle_t* Environment::immediate_idle_handle() {
247
5200
  return &immediate_idle_handle_;
248
}
249
250
8439
inline void Environment::RegisterHandleCleanup(uv_handle_t* handle,
251
                                               HandleCleanupCb cb,
252
                                               void *arg) {
253
25317
  handle_cleanup_queue_.PushBack(new HandleCleanup(handle, cb, arg));
254
8439
}
255
256
inline void Environment::FinishHandleCleanup(uv_handle_t* handle) {
257
7446
  handle_cleanup_waiting_--;
258
}
259
260
inline uv_loop_t* Environment::event_loop() const {
261
9447493
  return isolate_data()->event_loop();
262
}
263
264
inline Environment::AsyncHooks* Environment::async_hooks() {
265
  return &async_hooks_;
266
}
267
268
inline Environment::DomainFlag* Environment::domain_flag() {
269
  return &domain_flag_;
270
}
271
272
inline Environment::TickInfo* Environment::tick_info() {
273
77625
  return &tick_info_;
274
}
275
276
inline uint64_t Environment::timer_base() const {
277
  return timer_base_;
278
}
279
280
inline bool Environment::using_domains() const {
281
  return using_domains_;
282
}
283
284
inline void Environment::set_using_domains(bool value) {
285
128
  using_domains_ = value;
286
}
287
288
inline bool Environment::printed_error() const {
289
  return printed_error_;
290
}
291
292
inline void Environment::set_printed_error(bool value) {
293
10
  printed_error_ = value;
294
}
295
296
inline void Environment::set_trace_sync_io(bool value) {
297
3099
  trace_sync_io_ = value;
298
}
299
300
inline int64_t Environment::get_async_wrap_uid() {
301
48852
  return ++async_wrap_uid_;
302
}
303
304
inline uint32_t* Environment::heap_statistics_buffer() const {
305

4
  CHECK_NE(heap_statistics_buffer_, nullptr);
306
  return heap_statistics_buffer_;
307
}
308
309
inline void Environment::set_heap_statistics_buffer(uint32_t* pointer) {
310
3
  CHECK_EQ(heap_statistics_buffer_, nullptr);  // Should be set only once.
311
3
  heap_statistics_buffer_ = pointer;
312
}
313
314
inline uint32_t* Environment::heap_space_statistics_buffer() const {
315

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