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: 84 88 95.5 %
Date: 2020-06-24 22:13:30 Branches: 36 52 69.2 %

Line Branch Exec Source
1
#ifndef SRC_JS_NATIVE_API_V8_H_
2
#define SRC_JS_NATIVE_API_V8_H_
3
4
// This file needs to be compatible with C compilers.
5
#include <string.h>  // NOLINT(modernize-deprecated-headers)
6
#include "js_native_api_types.h"
7
#include "js_native_api_v8_internals.h"
8
9
static napi_status napi_clear_last_error(napi_env env);
10
11
namespace v8impl {
12
13
class RefTracker {
14
 public:
15
3140
  RefTracker() {}
16
3134
  virtual ~RefTracker() {}
17
  virtual void Finalize(bool isEnvTeardown) {}
18
19
  typedef RefTracker RefList;
20
21
2990
  inline void Link(RefList* list) {
22
2990
    prev_ = list;
23
2990
    next_ = list->next_;
24
2990
    if (next_ != nullptr) {
25
2379
      next_->prev_ = this;
26
    }
27
2990
    list->next_ = this;
28
2990
  }
29
30
3992
  inline void Unlink() {
31
3992
    if (prev_ != nullptr) {
32
2986
      prev_->next_ = next_;
33
    }
34
3992
    if (next_ != nullptr) {
35
1375
      next_->prev_ = prev_;
36
    }
37
3992
    prev_ = nullptr;
38
3992
    next_ = nullptr;
39
3992
  }
40
41
582
  static void FinalizeAll(RefList* list) {
42
1016
    while (list->next_ != nullptr) {
43
434
      list->next_->Finalize(true);
44
    }
45
148
  }
46
47
 private:
48
  RefList* next_ = nullptr;
49
  RefList* prev_ = nullptr;
50
};
51
52
}  // end of namespace v8impl
53
54
struct napi_env__ {
55
75
  explicit napi_env__(v8::Local<v8::Context> context)
56
150
      : isolate(context->GetIsolate()),
57
300
        context_persistent(isolate, context) {
58
150
    CHECK_EQ(isolate, context->GetIsolate());
59
75
  }
60
296
  virtual ~napi_env__() {
61
    // First we must finalize those references that have `napi_finalizer`
62
    // callbacks. The reason is that addons might store other references which
63
    // they delete during their `napi_finalizer` callbacks. If we deleted such
64
    // references here first, they would be doubly deleted when the
65
    // `napi_finalizer` deleted them subsequently.
66
74
    v8impl::RefTracker::FinalizeAll(&finalizing_reflist);
67
74
    v8impl::RefTracker::FinalizeAll(&reflist);
68
74
  }
69
  v8::Isolate* const isolate;  // Shortcut for context()->GetIsolate()
70
  v8impl::Persistent<v8::Context> context_persistent;
71
72
11066
  inline v8::Local<v8::Context> context() const {
73
11066
    return v8impl::PersistentToLocal::Strong(context_persistent);
74
  }
75
76
30
  inline void Ref() { refs++; }
77

104
  inline void Unref() { if ( --refs == 0) delete this; }
78
79
  virtual bool can_call_into_js() const { return true; }
80
  virtual v8::Maybe<bool> mark_arraybuffer_as_untransferable(
81
      v8::Local<v8::ArrayBuffer> ab) const {
82
    return v8::Just(true);
83
  }
84
85
  static inline void
86
1230
  HandleThrow(napi_env env, v8::Local<v8::Value> value) {
87
1230
    env->isolate->ThrowException(value);
88
1230
  }
89
90
  template <typename T, typename U = decltype(HandleThrow)>
91
6757
  inline void CallIntoModule(T&& call, U&& handle_exception = HandleThrow) {
92
6757
    int open_handle_scopes_before = open_handle_scopes;
93
6757
    int open_callback_scopes_before = open_callback_scopes;
94
6757
    napi_clear_last_error(this);
95
6757
    call(this);
96


6757
    CHECK_EQ(open_handle_scopes, open_handle_scopes_before);
97


6757
    CHECK_EQ(open_callback_scopes, open_callback_scopes_before);
98


13514
    if (!last_exception.IsEmpty()) {
99
2464
      handle_exception(this, last_exception.Get(this->isolate));
100
1231
      last_exception.Reset();
101
    }
102
6756
  }
103
104
  v8impl::Persistent<v8::Value> last_exception;
105
106
  // We store references in two different lists, depending on whether they have
107
  // `napi_finalizer` callbacks, because we must first finalize the ones that
108
  // have such a callback. See `~napi_env__()` above for details.
109
  v8impl::RefTracker::RefList reflist;
110
  v8impl::RefTracker::RefList finalizing_reflist;
111
  napi_extended_error_info last_error;
112
  int open_handle_scopes = 0;
113
  int open_callback_scopes = 0;
114
  int refs = 1;
115
  void* instance_data = nullptr;
116
};
117
118
28915
static inline napi_status napi_clear_last_error(napi_env env) {
119
28915
  env->last_error.error_code = napi_ok;
120
121
  // TODO(boingoing): Should this be a callback?
122
28915
  env->last_error.engine_error_code = 0;
123
28915
  env->last_error.engine_reserved = nullptr;
124
28915
  return napi_ok;
125
}
126
127
static inline
128
1294
napi_status napi_set_last_error(napi_env env, napi_status error_code,
129
                                uint32_t engine_error_code = 0,
130
                                void* engine_reserved = nullptr) {
131
1294
  env->last_error.error_code = error_code;
132
1294
  env->last_error.engine_error_code = engine_error_code;
133
1294
  env->last_error.engine_reserved = engine_reserved;
134
1294
  return error_code;
135
}
136
137
#define RETURN_STATUS_IF_FALSE(env, condition, status)                  \
138
  do {                                                                  \
139
    if (!(condition)) {                                                 \
140
      return napi_set_last_error((env), (status));                      \
141
    }                                                                   \
142
  } while (0)
143
144
#define CHECK_ENV(env)          \
145
  do {                          \
146
    if ((env) == nullptr) {     \
147
      return napi_invalid_arg;  \
148
    }                           \
149
  } while (0)
150
151
#define CHECK_ARG(env, arg) \
152
  RETURN_STATUS_IF_FALSE((env), ((arg) != nullptr), napi_invalid_arg)
153
154
#define CHECK_MAYBE_EMPTY(env, maybe, status) \
155
  RETURN_STATUS_IF_FALSE((env), !((maybe).IsEmpty()), (status))
156
157
// NAPI_PREAMBLE is not wrapped in do..while: try_catch must have function scope
158
#define NAPI_PREAMBLE(env)                                          \
159
  CHECK_ENV((env));                                                 \
160
  RETURN_STATUS_IF_FALSE((env),                                     \
161
      (env)->last_exception.IsEmpty() && (env)->can_call_into_js(), \
162
      napi_pending_exception);                                      \
163
  napi_clear_last_error((env));                                     \
164
  v8impl::TryCatch try_catch((env))
165
166
#define CHECK_TO_TYPE(env, type, context, result, src, status)                \
167
  do {                                                                        \
168
    CHECK_ARG((env), (src));                                                  \
169
    auto maybe = v8impl::V8LocalValueFromJsValue((src))->To##type((context)); \
170
    CHECK_MAYBE_EMPTY((env), maybe, (status));                                \
171
    (result) = maybe.ToLocalChecked();                                        \
172
  } while (0)
173
174
#define CHECK_TO_FUNCTION(env, result, src)                                 \
175
  do {                                                                      \
176
    CHECK_ARG((env), (src));                                                \
177
    v8::Local<v8::Value> v8value = v8impl::V8LocalValueFromJsValue((src));  \
178
    RETURN_STATUS_IF_FALSE((env), v8value->IsFunction(), napi_invalid_arg); \
179
    (result) = v8value.As<v8::Function>();                                  \
180
  } while (0)
181
182
#define CHECK_TO_OBJECT(env, context, result, src) \
183
  CHECK_TO_TYPE((env), Object, (context), (result), (src), napi_object_expected)
184
185
#define CHECK_TO_STRING(env, context, result, src) \
186
  CHECK_TO_TYPE((env), String, (context), (result), (src), napi_string_expected)
187
188
#define GET_RETURN_STATUS(env)      \
189
  (!try_catch.HasCaught() ? napi_ok \
190
                         : napi_set_last_error((env), napi_pending_exception))
191
192
#define THROW_RANGE_ERROR_IF_FALSE(env, condition, error, message) \
193
  do {                                                             \
194
    if (!(condition)) {                                            \
195
      napi_throw_range_error((env), (error), (message));           \
196
      return napi_set_last_error((env), napi_generic_failure);     \
197
    }                                                              \
198
  } while (0)
199
200
#define RETURN_STATUS_IF_FALSE_WITH_PREAMBLE(env, condition, status)           \
201
  do {                                                                         \
202
    if (!(condition)) {                                                        \
203
      return napi_set_last_error(                                              \
204
          (env), try_catch.HasCaught() ? napi_pending_exception : (status));   \
205
    }                                                                          \
206
  } while (0)
207
208
#define CHECK_MAYBE_EMPTY_WITH_PREAMBLE(env, maybe, status)                    \
209
  RETURN_STATUS_IF_FALSE_WITH_PREAMBLE((env), !((maybe).IsEmpty()), (status))
210
211
namespace v8impl {
212
213
//=== Conversion between V8 Handles and napi_value ========================
214
215
// This asserts v8::Local<> will always be implemented with a single
216
// pointer field so that we can pass it around as a void*.
217
static_assert(sizeof(v8::Local<v8::Value>) == sizeof(napi_value),
218
  "Cannot convert between v8::Local<v8::Value> and napi_value");
219
220
16706
inline napi_value JsValueFromV8LocalValue(v8::Local<v8::Value> local) {
221
16706
  return reinterpret_cast<napi_value>(*local);
222
}
223
224
13684
inline v8::Local<v8::Value> V8LocalValueFromJsValue(napi_value v) {
225
  v8::Local<v8::Value> local;
226
13684
  memcpy(static_cast<void*>(&local), &v, sizeof(v));
227
13684
  return local;
228
}
229
230
// Adapter for napi_finalize callbacks.
231
class Finalizer {
232
 public:
233
  // Some Finalizers are run during shutdown when the napi_env is destroyed,
234
  // and some need to keep an explicit reference to the napi_env because they
235
  // are run independently.
236
  enum EnvReferenceMode {
237
    kNoEnvReference,
238
    kKeepEnvReference
239
  };
240
241
 protected:
242
3003
  Finalizer(napi_env env,
243
            napi_finalize finalize_callback,
244
            void* finalize_data,
245
            void* finalize_hint,
246
            EnvReferenceMode refmode = kNoEnvReference)
247
3003
    : _env(env),
248
      _finalize_callback(finalize_callback),
249
      _finalize_data(finalize_data),
250
      _finalize_hint(finalize_hint),
251
3003
      _has_env_reference(refmode == kKeepEnvReference) {
252
3003
    if (_has_env_reference)
253
13
      _env->Ref();
254
3003
  }
255
256
5998
  ~Finalizer() {
257
2999
    if (_has_env_reference)
258
13
      _env->Unref();
259
2999
  }
260
261
 public:
262
13
  static Finalizer* New(napi_env env,
263
                        napi_finalize finalize_callback = nullptr,
264
                        void* finalize_data = nullptr,
265
                        void* finalize_hint = nullptr,
266
                        EnvReferenceMode refmode = kNoEnvReference) {
267
    return new Finalizer(
268
13
        env, finalize_callback, finalize_data, finalize_hint, refmode);
269
  }
270
271
13
  static void Delete(Finalizer* finalizer) {
272
13
    delete finalizer;
273
13
  }
274
275
 protected:
276
  napi_env _env;
277
  napi_finalize _finalize_callback;
278
  void* _finalize_data;
279
  void* _finalize_hint;
280
  bool _finalize_ran = false;
281
  bool _has_env_reference = false;
282
};
283
284
class TryCatch : public v8::TryCatch {
285
 public:
286
4744
  explicit TryCatch(napi_env env)
287
4744
      : v8::TryCatch(env->isolate), _env(env) {}
288
289
9488
  ~TryCatch() {
290
4744
    if (HasCaught()) {
291
2466
      _env->last_exception.Reset(_env->isolate, Exception());
292
    }
293
4744
  }
294
295
 private:
296
  napi_env _env;
297
};
298
299
}  // end of namespace v8impl
300
301
#endif  // SRC_JS_NATIVE_API_V8_H_