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: 86 90 95.6 %
Date: 2020-02-27 22:14:15 Branches: 38 52 73.1 %

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
3139
  RefTracker() {}
16
3133
  virtual ~RefTracker() {}
17
  virtual void Finalize(bool isEnvTeardown) {}
18
19
  typedef RefTracker RefList;
20
21
2991
  inline void Link(RefList* list) {
22
2991
    prev_ = list;
23
2991
    next_ = list->next_;
24
2991
    if (next_ != nullptr) {
25
2382
      next_->prev_ = this;
26
    }
27
2991
    list->next_ = this;
28
2991
  }
29
30
3993
  inline void Unlink() {
31
3993
    if (prev_ != nullptr) {
32
2987
      prev_->next_ = next_;
33
    }
34
3993
    if (next_ != nullptr) {
35
1378
      next_->prev_ = prev_;
36
    }
37
3993
    prev_ = nullptr;
38
3993
    next_ = nullptr;
39
3993
  }
40
41
582
  static void FinalizeAll(RefList* list) {
42
1018
    while (list->next_ != nullptr) {
43
436
      list->next_->Finalize(true);
44
    }
45
146
  }
46
47
 private:
48
  RefList* next_ = nullptr;
49
  RefList* prev_ = nullptr;
50
};
51
52
}  // end of namespace v8impl
53
54
struct napi_env__ {
55
74
  explicit napi_env__(v8::Local<v8::Context> context)
56
148
      : isolate(context->GetIsolate()),
57
296
        context_persistent(isolate, context) {
58
148
    CHECK_EQ(isolate, context->GetIsolate());
59
74
  }
60
292
  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
73
    v8impl::RefTracker::FinalizeAll(&finalizing_reflist);
67
73
    v8impl::RefTracker::FinalizeAll(&reflist);
68
73
  }
69
  v8::Isolate* const isolate;  // Shortcut for context()->GetIsolate()
70
  v8impl::Persistent<v8::Context> context_persistent;
71
72
11060
  inline v8::Local<v8::Context> context() const {
73
11060
    return v8impl::PersistentToLocal::Strong(context_persistent);
74
  }
75
76
23
  inline void Ref() { refs++; }
77

96
  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
  template <typename T, typename U>
86
6727
  void CallIntoModule(T&& call, U&& handle_exception) {
87
6727
    int open_handle_scopes_before = open_handle_scopes;
88
6727
    int open_callback_scopes_before = open_callback_scopes;
89
6727
    napi_clear_last_error(this);
90
6727
    call(this);
91


6727
    CHECK_EQ(open_handle_scopes, open_handle_scopes_before);
92


6727
    CHECK_EQ(open_callback_scopes, open_callback_scopes_before);
93


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