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: 90 94 95.7 %
Date: 2020-08-17 22:13:26 Branches: 43 64 67.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
3161
  RefTracker() {}
16
3149
  virtual ~RefTracker() {}
17
  virtual void Finalize(bool isEnvTeardown) {}
18
19
  typedef RefTracker RefList;
20
21
3007
  inline void Link(RefList* list) {
22
3007
    prev_ = list;
23
3007
    next_ = list->next_;
24
3007
    if (next_ != nullptr) {
25
2397
      next_->prev_ = this;
26
    }
27
3007
    list->next_ = this;
28
3007
  }
29
30
3003
  inline void Unlink() {
31
3003
    if (prev_ != nullptr) {
32
2999
      prev_->next_ = next_;
33
    }
34
3003
    if (next_ != nullptr) {
35
1390
      next_->prev_ = prev_;
36
    }
37
3003
    prev_ = nullptr;
38
3003
    next_ = nullptr;
39
3003
  }
40
41
593
  static void FinalizeAll(RefList* list) {
42
1036
    while (list->next_ != nullptr) {
43
443
      list->next_->Finalize(true);
44
    }
45
150
  }
46
47
 private:
48
  RefList* next_ = nullptr;
49
  RefList* prev_ = nullptr;
50
};
51
52
}  // end of namespace v8impl
53
54
struct napi_env__ {
55
77
  explicit napi_env__(v8::Local<v8::Context> context)
56
154
      : isolate(context->GetIsolate()),
57
308
        context_persistent(isolate, context) {
58
154
    CHECK_EQ(isolate, context->GetIsolate());
59
77
  }
60
300
  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
75
    v8impl::RefTracker::FinalizeAll(&finalizing_reflist);
67
75
    v8impl::RefTracker::FinalizeAll(&reflist);
68
75
  }
69
  v8::Isolate* const isolate;  // Shortcut for context()->GetIsolate()
70
  v8impl::Persistent<v8::Context> context_persistent;
71
72
13225
  inline v8::Local<v8::Context> context() const {
73
13225
    return v8impl::PersistentToLocal::Strong(context_persistent);
74
  }
75
76
33
  inline void Ref() { refs++; }
77

108
  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
1231
  HandleThrow(napi_env env, v8::Local<v8::Value> value) {
87
1231
    env->isolate->ThrowException(value);
88
1231
  }
89
90
  template <typename T, typename U = decltype(HandleThrow)>
91
6801
  inline void CallIntoModule(T&& call, U&& handle_exception = HandleThrow) {
92
6801
    int open_handle_scopes_before = open_handle_scopes;
93
6801
    int open_callback_scopes_before = open_callback_scopes;
94
6801
    napi_clear_last_error(this);
95
6801
    call(this);
96



6801
    CHECK_EQ(open_handle_scopes, open_handle_scopes_before);
97



6801
    CHECK_EQ(open_callback_scopes, open_callback_scopes_before);
98



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