GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/js_native_api_v8.h Lines: 57 58 98.3 %
Date: 2019-02-23 22:23:05 Branches: 24 36 66.7 %

Line Branch Exec Source
1
#ifndef SRC_JS_NATIVE_API_V8_H_
2
#define SRC_JS_NATIVE_API_V8_H_
3
4
#include <string.h>
5
#include "js_native_api_types.h"
6
#include "js_native_api_v8_internals.h"
7
8
struct napi_env__ {
9
65
  explicit napi_env__(v8::Local<v8::Context> context)
10
65
      : isolate(context->GetIsolate()),
11
260
        context_persistent(isolate, context) {
12
130
    CHECK_EQ(isolate, context->GetIsolate());
13
65
  }
14
192
  virtual ~napi_env__() {}
15
  v8::Isolate* const isolate;  // Shortcut for context()->GetIsolate()
16
  v8impl::Persistent<v8::Context> context_persistent;
17
18
10762
  inline v8::Local<v8::Context> context() const {
19
10762
    return v8impl::PersistentToLocal::Strong(context_persistent);
20
  }
21
22
14
  inline void Ref() { refs++; }
23

78
  inline void Unref() { if ( --refs == 0) delete this; }
24
25
  virtual bool can_call_into_js() const { return true; }
26
27
  v8impl::Persistent<v8::Value> last_exception;
28
  napi_extended_error_info last_error;
29
  int open_handle_scopes = 0;
30
  int open_callback_scopes = 0;
31
  int refs = 1;
32
};
33
34
26636
static inline napi_status napi_clear_last_error(napi_env env) {
35
26636
  env->last_error.error_code = napi_ok;
36
37
  // TODO(boingoing): Should this be a callback?
38
26636
  env->last_error.engine_error_code = 0;
39
26636
  env->last_error.engine_reserved = nullptr;
40
26636
  return napi_ok;
41
}
42
43
static inline
44
1235
napi_status napi_set_last_error(napi_env env, napi_status error_code,
45
                                uint32_t engine_error_code = 0,
46
                                void* engine_reserved = nullptr) {
47
1235
  env->last_error.error_code = error_code;
48
1235
  env->last_error.engine_error_code = engine_error_code;
49
1235
  env->last_error.engine_reserved = engine_reserved;
50
1235
  return error_code;
51
}
52
53
#define RETURN_STATUS_IF_FALSE(env, condition, status)                  \
54
  do {                                                                  \
55
    if (!(condition)) {                                                 \
56
      return napi_set_last_error((env), (status));                      \
57
    }                                                                   \
58
  } while (0)
59
60
#define CHECK_ENV(env)          \
61
  do {                          \
62
    if ((env) == nullptr) {     \
63
      return napi_invalid_arg;  \
64
    }                           \
65
  } while (0)
66
67
#define CHECK_ARG(env, arg) \
68
  RETURN_STATUS_IF_FALSE((env), ((arg) != nullptr), napi_invalid_arg)
69
70
#define CHECK_MAYBE_EMPTY(env, maybe, status) \
71
  RETURN_STATUS_IF_FALSE((env), !((maybe).IsEmpty()), (status))
72
73
// NAPI_PREAMBLE is not wrapped in do..while: try_catch must have function scope
74
#define NAPI_PREAMBLE(env)                                          \
75
  CHECK_ENV((env));                                                 \
76
  RETURN_STATUS_IF_FALSE((env),                                     \
77
      (env)->last_exception.IsEmpty() && (env)->can_call_into_js(), \
78
      napi_pending_exception);                                      \
79
  napi_clear_last_error((env));                                     \
80
  v8impl::TryCatch try_catch((env))
81
82
#define CHECK_TO_TYPE(env, type, context, result, src, status)                \
83
  do {                                                                        \
84
    CHECK_ARG((env), (src));                                                  \
85
    auto maybe = v8impl::V8LocalValueFromJsValue((src))->To##type((context)); \
86
    CHECK_MAYBE_EMPTY((env), maybe, (status));                                \
87
    (result) = maybe.ToLocalChecked();                                        \
88
  } while (0)
89
90
#define CHECK_TO_FUNCTION(env, result, src)                                 \
91
  do {                                                                      \
92
    CHECK_ARG((env), (src));                                                \
93
    v8::Local<v8::Value> v8value = v8impl::V8LocalValueFromJsValue((src));  \
94
    RETURN_STATUS_IF_FALSE((env), v8value->IsFunction(), napi_invalid_arg); \
95
    (result) = v8value.As<v8::Function>();                                  \
96
  } while (0)
97
98
#define CHECK_TO_OBJECT(env, context, result, src) \
99
  CHECK_TO_TYPE((env), Object, (context), (result), (src), napi_object_expected)
100
101
#define CHECK_TO_STRING(env, context, result, src) \
102
  CHECK_TO_TYPE((env), String, (context), (result), (src), napi_string_expected)
103
104
#define GET_RETURN_STATUS(env)      \
105
  (!try_catch.HasCaught() ? napi_ok \
106
                         : napi_set_last_error((env), napi_pending_exception))
107
108
#define THROW_RANGE_ERROR_IF_FALSE(env, condition, error, message) \
109
  do {                                                             \
110
    if (!(condition)) {                                            \
111
      napi_throw_range_error((env), (error), (message));           \
112
      return napi_set_last_error((env), napi_generic_failure);     \
113
    }                                                              \
114
  } while (0)
115
116
template <typename T, typename U>
117
6091
void NapiCallIntoModule(napi_env env, T&& call, U&& handle_exception) {
118
6091
  int open_handle_scopes = env->open_handle_scopes;
119
6091
  int open_callback_scopes = env->open_callback_scopes;
120
6091
  napi_clear_last_error(env);
121
6091
  call();
122


6091
  CHECK_EQ(env->open_handle_scopes, open_handle_scopes);
123


6091
  CHECK_EQ(env->open_callback_scopes, open_callback_scopes);
124


12182
  if (!env->last_exception.IsEmpty()) {
125
2464
    handle_exception(env->last_exception.Get(env->isolate));
126
1231
    env->last_exception.Reset();
127
  }
128
6090
}
129
130
template <typename T>
131
5581
void NapiCallIntoModuleThrow(napi_env env, T&& call) {
132
6811
  NapiCallIntoModule(env, call, [&](v8::Local<v8::Value> value) {
133
1230
    env->isolate->ThrowException(value);
134
12392
  });
135
5581
}
136
137
namespace v8impl {
138
139
//=== Conversion between V8 Handles and napi_value ========================
140
141
// This asserts v8::Local<> will always be implemented with a single
142
// pointer field so that we can pass it around as a void*.
143
static_assert(sizeof(v8::Local<v8::Value>) == sizeof(napi_value),
144
  "Cannot convert between v8::Local<v8::Value> and napi_value");
145
146
15313
inline napi_value JsValueFromV8LocalValue(v8::Local<v8::Value> local) {
147
15313
  return reinterpret_cast<napi_value>(*local);
148
}
149
150
13291
inline v8::Local<v8::Value> V8LocalValueFromJsValue(napi_value v) {
151
  v8::Local<v8::Value> local;
152
13291
  memcpy(&local, &v, sizeof(v));
153
13291
  return local;
154
}
155
156
// Adapter for napi_finalize callbacks.
157
class Finalizer {
158
 protected:
159
2559
  Finalizer(napi_env env,
160
            napi_finalize finalize_callback,
161
            void* finalize_data,
162
            void* finalize_hint)
163
    : _env(env),
164
      _finalize_callback(finalize_callback),
165
      _finalize_data(finalize_data),
166
2559
      _finalize_hint(finalize_hint) {
167
2559
  }
168
169
2024
  ~Finalizer() {
170
2024
  }
171
172
 public:
173
3
  static Finalizer* New(napi_env env,
174
                        napi_finalize finalize_callback = nullptr,
175
                        void* finalize_data = nullptr,
176
                        void* finalize_hint = nullptr) {
177
    return new Finalizer(
178
3
      env, finalize_callback, finalize_data, finalize_hint);
179
  }
180
181
3
  static void Delete(Finalizer* finalizer) {
182
3
    delete finalizer;
183
3
  }
184
185
 protected:
186
  napi_env _env;
187
  napi_finalize _finalize_callback;
188
  void* _finalize_data;
189
  void* _finalize_hint;
190
  bool _finalize_ran = false;
191
};
192
193
class TryCatch : public v8::TryCatch {
194
 public:
195
4608
  explicit TryCatch(napi_env env)
196
4608
      : v8::TryCatch(env->isolate), _env(env) {}
197
198
9216
  ~TryCatch() {
199
4608
    if (HasCaught()) {
200
2466
      _env->last_exception.Reset(_env->isolate, Exception());
201
    }
202
4608
  }
203
204
 private:
205
  napi_env _env;
206
};
207
208
}  // end of namespace v8impl
209
210
#endif  // SRC_JS_NATIVE_API_V8_H_