GCC Code Coverage Report
Directory: ../src/ Exec Total Coverage
File: /home/node-core-coverage/node-core-coverage/workdir/node/src/async-wrap-inl.h Lines: 44 44 100.0 %
Date: 2016-07-26 Branches: 23 28 82.1 %

Line Exec Source
1
#ifndef SRC_ASYNC_WRAP_INL_H_
2
#define SRC_ASYNC_WRAP_INL_H_
3
4
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
6
#include "async-wrap.h"
7
#include "base-object.h"
8
#include "base-object-inl.h"
9
#include "env.h"
10
#include "env-inl.h"
11
#include "node_internals.h"
12
#include "util.h"
13
#include "util-inl.h"
14
#include "v8.h"
15
16
namespace node {
17
18
46777
inline AsyncWrap::AsyncWrap(Environment* env,
19
                            v8::Local<v8::Object> object,
20
                            ProviderType provider,
21
                            AsyncWrap* parent)
22
46777
    : BaseObject(env, object), bits_(static_cast<uint32_t>(provider) << 1),
23
140331
      uid_(env->get_async_wrap_uid()) {
24
46777
  CHECK_NE(provider, PROVIDER_NONE);
25
46777
  CHECK_GE(object->InternalFieldCount(), 1);
26
27
  // Shift provider value over to prevent id collision.
28
93554
  persistent().SetWrapperClassId(NODE_ASYNC_ID_OFFSET + provider);
29
30
46777
  v8::Local<v8::Function> init_fn = env->async_hooks_init_function();
31
32
  // No init callback exists, no reason to go on.
33
46777
  if (init_fn.IsEmpty())
34
46709
    return;
35
36
  // If async wrap callbacks are disabled and no parent was passed that has
37
  // run the init callback then return.
38
144
  if (!env->async_wrap_callbacks_enabled() &&
39
2
      (parent == nullptr || !parent->ran_init_callback()))
40
    return;
41
42
135
  v8::HandleScope scope(env->isolate());
43
44
  v8::Local<v8::Value> argv[] = {
45
68
    v8::Number::New(env->isolate(), get_uid()),
46
    v8::Int32::New(env->isolate(), provider),
47
    Null(env->isolate()),
48
    Null(env->isolate())
49
476
  };
50
51
68
  if (parent != nullptr) {
52
12
    argv[2] = v8::Number::New(env->isolate(), parent->get_uid());
53
18
    argv[3] = parent->object();
54
  }
55
56
135
  v8::TryCatch try_catch(env->isolate());
57
58
  v8::MaybeLocal<v8::Value> ret =
59
204
      init_fn->Call(env->context(), object, arraysize(argv), argv);
60
61
68
  if (ret.IsEmpty()) {
62
1
    ClearFatalExceptionHandlers(env);
63
1
    FatalException(env->isolate(), try_catch);
64
  }
65
66
67
  bits_ |= 1;  // ran_init_callback() is true now.
67
}
68
69
70
86724
inline AsyncWrap::~AsyncWrap() {
71
86672
  if (!ran_init_callback())
72
    return;
73
74
108
  v8::Local<v8::Function> fn = env()->async_hooks_destroy_function();
75
54
  if (!fn.IsEmpty()) {
76
5
    v8::HandleScope scope(env()->isolate());
77
6
    v8::Local<v8::Value> uid = v8::Number::New(env()->isolate(), get_uid());
78
5
    v8::TryCatch try_catch(env()->isolate());
79
    v8::MaybeLocal<v8::Value> ret =
80
12
        fn->Call(env()->context(), v8::Null(env()->isolate()), 1, &uid);
81
3
    if (ret.IsEmpty()) {
82
1
      ClearFatalExceptionHandlers(env());
83
1
      FatalException(env()->isolate(), try_catch);
84
    }
85
  }
86
43335
}
87
88
89
inline bool AsyncWrap::ran_init_callback() const {
90
246923
  return static_cast<bool>(bits_ & 1);
91
}
92
93
94
inline AsyncWrap::ProviderType AsyncWrap::provider_type() const {
95
  return static_cast<ProviderType>(bits_ >> 1);
96
}
97
98
99
inline int64_t AsyncWrap::get_uid() const {
100
  return uid_;
101
}
102
103
104
60744
inline v8::Local<v8::Value> AsyncWrap::MakeCallback(
105
    const v8::Local<v8::String> symbol,
106
    int argc,
107
    v8::Local<v8::Value>* argv) {
108
121488
  v8::Local<v8::Value> cb_v = object()->Get(symbol);
109
60744
  CHECK(cb_v->IsFunction());
110
60744
  return MakeCallback(cb_v.As<v8::Function>(), argc, argv);
111
}
112
113
114
3945
inline v8::Local<v8::Value> AsyncWrap::MakeCallback(
115
    uint32_t index,
116
    int argc,
117
    v8::Local<v8::Value>* argv) {
118
7890
  v8::Local<v8::Value> cb_v = object()->Get(index);
119
3945
  CHECK(cb_v->IsFunction());
120
3945
  return MakeCallback(cb_v.As<v8::Function>(), argc, argv);
121
}
122
123
}  // namespace node
124
125
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
126
127
#endif  // SRC_ASYNC_WRAP_INL_H_