GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: js_native_api_v8.h Lines: 103 106 97.2 %
Date: 2021-10-07 04:12:46 Branches: 25 32 78.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
3244
  RefTracker() {}
16
6460
  virtual ~RefTracker() {}
17
  virtual void Finalize(bool isEnvTeardown) {}
18
19
  typedef RefTracker RefList;
20
21
3064
  inline void Link(RefList* list) {
22
3064
    prev_ = list;
23
3064
    next_ = list->next_;
24
3064
    if (next_ != nullptr) {
25
2438
      next_->prev_ = this;
26
    }
27
3064
    list->next_ = this;
28
3064
  }
29
30
3054
  inline void Unlink() {
31
3054
    if (prev_ != nullptr) {
32
3054
      prev_->next_ = next_;
33
    }
34
3054
    if (next_ != nullptr) {
35
1429
      next_->prev_ = prev_;
36
    }
37
3054
    prev_ = nullptr;
38
3054
    next_ = nullptr;
39
3054
  }
40
41
674
  static void FinalizeAll(RefList* list) {
42
674
    while (list->next_ != nullptr) {
43
498
      list->next_->Finalize(true);
44
    }
45
176
  }
46
47
 private:
48
  RefList* next_ = nullptr;
49
  RefList* prev_ = nullptr;
50
};
51
52
}  // end of namespace v8impl
53
54
struct napi_env__ {
55
90
  explicit napi_env__(v8::Local<v8::Context> context)
56
90
      : isolate(context->GetIsolate()),
57
360
        context_persistent(isolate, context) {
58
90
    CHECK_EQ(isolate, context->GetIsolate());
59
90
  }
60
352
  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
176
    v8impl::RefTracker::FinalizeAll(&finalizing_reflist);
67
176
    v8impl::RefTracker::FinalizeAll(&reflist);
68
  }
69
  v8::Isolate* const isolate;  // Shortcut for context()->GetIsolate()
70
  v8impl::Persistent<v8::Context> context_persistent;
71
72
243693
  inline v8::Local<v8::Context> context() const {
73
243693
    return v8impl::PersistentToLocal::Strong(context_persistent);
74
  }
75
76
1051
  inline void Ref() { refs++; }
77

1139
  inline void Unref() { if ( --refs == 0) delete this; }
78
79
2
  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
4
  HandleThrow(napi_env env, v8::Local<v8::Value> value) {
87
4
    env->isolate->ThrowException(value);
88
4
  }
89
90
  template <typename T, typename U = decltype(HandleThrow)>
91
238914
  inline void CallIntoModule(T&& call, U&& handle_exception = HandleThrow) {
92
238914
    int open_handle_scopes_before = open_handle_scopes;
93
238914
    int open_callback_scopes_before = open_callback_scopes;
94
238914
    napi_clear_last_error(this);
95
238914
    call(this);
96
238914
    CHECK_EQ(open_handle_scopes, open_handle_scopes_before);
97
238914
    CHECK_EQ(open_callback_scopes, open_callback_scopes_before);
98
238914
    if (!last_exception.IsEmpty()) {
99
2482
      handle_exception(this, last_exception.Get(this->isolate));
100
1239
      last_exception.Reset();
101
    }
102
238912
  }
103
104
486
  virtual void CallFinalizer(napi_finalize cb, void* data, void* hint) {
105
486
    v8::HandleScope handle_scope(isolate);
106
486
    CallIntoModule([&](napi_env env) {
107
486
      cb(env, data, hint);
108
486
    });
109
486
  }
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
// This class is used to keep a napi_env live in a way that
126
// is exception safe versus calling Ref/Unref directly
127
class EnvRefHolder {
128
 public:
129
1014
  explicit EnvRefHolder(napi_env env) : _env(env) {
130
1014
      _env->Ref();
131
1014
  }
132
133
  explicit EnvRefHolder(const EnvRefHolder& other): _env(other.env()) {
134
    _env->Ref();
135
  }
136
137
2028
  EnvRefHolder(EnvRefHolder&& other) {
138
2028
    _env = other._env;
139
2028
    other._env = nullptr;
140
2028
  }
141
142
6084
  ~EnvRefHolder() {
143
3042
    if (_env != nullptr) {
144
1014
      _env->Unref();
145
    }
146
3042
  }
147
148
1014
  napi_env env(void) const {
149
1014
    return _env;
150
  }
151
152
 private:
153
  napi_env _env;
154
};
155
156
604293
static inline napi_status napi_clear_last_error(napi_env env) {
157
604293
  env->last_error.error_code = napi_ok;
158
159
  // TODO(boingoing): Should this be a callback?
160
604293
  env->last_error.engine_error_code = 0;
161
604293
  env->last_error.engine_reserved = nullptr;
162
604293
  return napi_ok;
163
}
164
165
static inline
166
1338
napi_status napi_set_last_error(napi_env env, napi_status error_code,
167
                                uint32_t engine_error_code = 0,
168
                                void* engine_reserved = nullptr) {
169
1338
  env->last_error.error_code = error_code;
170
1338
  env->last_error.engine_error_code = engine_error_code;
171
1338
  env->last_error.engine_reserved = engine_reserved;
172
1338
  return error_code;
173
}
174
175
#define RETURN_STATUS_IF_FALSE(env, condition, status)                  \
176
  do {                                                                  \
177
    if (!(condition)) {                                                 \
178
      return napi_set_last_error((env), (status));                      \
179
    }                                                                   \
180
  } while (0)
181
182
#define RETURN_STATUS_IF_FALSE_WITH_PREAMBLE(env, condition, status)           \
183
  do {                                                                         \
184
    if (!(condition)) {                                                        \
185
      return napi_set_last_error(                                              \
186
          (env), try_catch.HasCaught() ? napi_pending_exception : (status));   \
187
    }                                                                          \
188
  } while (0)
189
190
#define CHECK_ENV(env)          \
191
  do {                          \
192
    if ((env) == nullptr) {     \
193
      return napi_invalid_arg;  \
194
    }                           \
195
  } while (0)
196
197
#define CHECK_ARG(env, arg) \
198
  RETURN_STATUS_IF_FALSE((env), ((arg) != nullptr), napi_invalid_arg)
199
200
#define CHECK_ARG_WITH_PREAMBLE(env, arg)                  \
201
  RETURN_STATUS_IF_FALSE_WITH_PREAMBLE((env),              \
202
                                       ((arg) != nullptr), \
203
                                       napi_invalid_arg)
204
205
#define CHECK_MAYBE_EMPTY(env, maybe, status) \
206
  RETURN_STATUS_IF_FALSE((env), !((maybe).IsEmpty()), (status))
207
208
#define CHECK_MAYBE_EMPTY_WITH_PREAMBLE(env, maybe, status)                    \
209
  RETURN_STATUS_IF_FALSE_WITH_PREAMBLE((env), !((maybe).IsEmpty()), (status))
210
211
// NAPI_PREAMBLE is not wrapped in do..while: try_catch must have function scope
212
#define NAPI_PREAMBLE(env)                                          \
213
  CHECK_ENV((env));                                                 \
214
  RETURN_STATUS_IF_FALSE((env),                                     \
215
      (env)->last_exception.IsEmpty() && (env)->can_call_into_js(), \
216
      napi_pending_exception);                                      \
217
  napi_clear_last_error((env));                                     \
218
  v8impl::TryCatch try_catch((env))
219
220
#define CHECK_TO_TYPE(env, type, context, result, src, status)                \
221
  do {                                                                        \
222
    CHECK_ARG((env), (src));                                                  \
223
    auto maybe = v8impl::V8LocalValueFromJsValue((src))->To##type((context)); \
224
    CHECK_MAYBE_EMPTY((env), maybe, (status));                                \
225
    (result) = maybe.ToLocalChecked();                                        \
226
  } while (0)
227
228
#define CHECK_TO_TYPE_WITH_PREAMBLE(env, type, context, result, src, status)  \
229
  do {                                                                        \
230
    CHECK_ARG_WITH_PREAMBLE((env), (src));                                    \
231
    auto maybe = v8impl::V8LocalValueFromJsValue((src))->To##type((context)); \
232
    CHECK_MAYBE_EMPTY_WITH_PREAMBLE((env), maybe, (status));                  \
233
    (result) = maybe.ToLocalChecked();                                        \
234
  } while (0)
235
236
#define CHECK_TO_FUNCTION(env, result, src)                                 \
237
  do {                                                                      \
238
    CHECK_ARG((env), (src));                                                \
239
    v8::Local<v8::Value> v8value = v8impl::V8LocalValueFromJsValue((src));  \
240
    RETURN_STATUS_IF_FALSE((env), v8value->IsFunction(), napi_invalid_arg); \
241
    (result) = v8value.As<v8::Function>();                                  \
242
  } while (0)
243
244
#define CHECK_TO_OBJECT(env, context, result, src) \
245
  CHECK_TO_TYPE((env), Object, (context), (result), (src), napi_object_expected)
246
247
#define CHECK_TO_OBJECT_WITH_PREAMBLE(env, context, result, src) \
248
  CHECK_TO_TYPE_WITH_PREAMBLE((env),                             \
249
                              Object,                            \
250
                              (context),                         \
251
                              (result),                          \
252
                              (src),                             \
253
                              napi_object_expected)
254
255
#define CHECK_TO_STRING(env, context, result, src) \
256
  CHECK_TO_TYPE((env), String, (context), (result), (src), napi_string_expected)
257
258
#define GET_RETURN_STATUS(env)      \
259
  (!try_catch.HasCaught() ? napi_ok \
260
                         : napi_set_last_error((env), napi_pending_exception))
261
262
#define THROW_RANGE_ERROR_IF_FALSE(env, condition, error, message) \
263
  do {                                                             \
264
    if (!(condition)) {                                            \
265
      napi_throw_range_error((env), (error), (message));           \
266
      return napi_set_last_error((env), napi_generic_failure);     \
267
    }                                                              \
268
  } while (0)
269
270
#define RETURN_STATUS_IF_FALSE_WITH_PREAMBLE(env, condition, status)           \
271
  do {                                                                         \
272
    if (!(condition)) {                                                        \
273
      return napi_set_last_error(                                              \
274
          (env), try_catch.HasCaught() ? napi_pending_exception : (status));   \
275
    }                                                                          \
276
  } while (0)
277
278
#define CHECK_MAYBE_EMPTY_WITH_PREAMBLE(env, maybe, status)                    \
279
  RETURN_STATUS_IF_FALSE_WITH_PREAMBLE((env), !((maybe).IsEmpty()), (status))
280
281
namespace v8impl {
282
283
//=== Conversion between V8 Handles and napi_value ========================
284
285
// This asserts v8::Local<> will always be implemented with a single
286
// pointer field so that we can pass it around as a void*.
287
static_assert(sizeof(v8::Local<v8::Value>) == sizeof(napi_value),
288
  "Cannot convert between v8::Local<v8::Value> and napi_value");
289
290
351661
inline napi_value JsValueFromV8LocalValue(v8::Local<v8::Value> local) {
291
351661
  return reinterpret_cast<napi_value>(*local);
292
}
293
294
243733
inline v8::Local<v8::Value> V8LocalValueFromJsValue(napi_value v) {
295
  v8::Local<v8::Value> local;
296
243733
  memcpy(static_cast<void*>(&local), &v, sizeof(v));
297
243733
  return local;
298
}
299
300
// Adapter for napi_finalize callbacks.
301
class Finalizer {
302
 public:
303
  // Some Finalizers are run during shutdown when the napi_env is destroyed,
304
  // and some need to keep an explicit reference to the napi_env because they
305
  // are run independently.
306
  enum EnvReferenceMode {
307
    kNoEnvReference,
308
    kKeepEnvReference
309
  };
310
311
 protected:
312
3077
  Finalizer(napi_env env,
313
            napi_finalize finalize_callback,
314
            void* finalize_data,
315
            void* finalize_hint,
316
            EnvReferenceMode refmode = kNoEnvReference)
317
3077
    : _env(env),
318
      _finalize_callback(finalize_callback),
319
      _finalize_data(finalize_data),
320
      _finalize_hint(finalize_hint),
321
3077
      _has_env_reference(refmode == kKeepEnvReference) {
322
3077
    if (_has_env_reference)
323
13
      _env->Ref();
324
3077
  }
325
326
6134
  ~Finalizer() {
327
3067
    if (_has_env_reference)
328
13
      _env->Unref();
329
3067
  }
330
331
 public:
332
13
  static Finalizer* New(napi_env env,
333
                        napi_finalize finalize_callback = nullptr,
334
                        void* finalize_data = nullptr,
335
                        void* finalize_hint = nullptr,
336
                        EnvReferenceMode refmode = kNoEnvReference) {
337
    return new Finalizer(
338
13
        env, finalize_callback, finalize_data, finalize_hint, refmode);
339
  }
340
341
13
  static void Delete(Finalizer* finalizer) {
342
13
    delete finalizer;
343
13
  }
344
345
 protected:
346
  napi_env _env;
347
  napi_finalize _finalize_callback;
348
  void* _finalize_data;
349
  void* _finalize_hint;
350
  bool _finalize_ran = false;
351
  bool _has_env_reference = false;
352
};
353
354
class TryCatch : public v8::TryCatch {
355
 public:
356
119759
  explicit TryCatch(napi_env env)
357
119759
      : v8::TryCatch(env->isolate), _env(env) {}
358
359
119759
  ~TryCatch() {
360
119759
    if (HasCaught()) {
361
2474
      _env->last_exception.Reset(_env->isolate, Exception());
362
    }
363
119759
  }
364
365
 private:
366
  napi_env _env;
367
};
368
369
// Wrapper around v8impl::Persistent that implements reference counting.
370
class RefBase : protected Finalizer, RefTracker {
371
 protected:
372
  RefBase(napi_env env,
373
          uint32_t initial_refcount,
374
          bool delete_self,
375
          napi_finalize finalize_callback,
376
          void* finalize_data,
377
          void* finalize_hint);
378
379
 public:
380
  static RefBase* New(napi_env env,
381
                      uint32_t initial_refcount,
382
                      bool delete_self,
383
                      napi_finalize finalize_callback,
384
                      void* finalize_data,
385
                      void* finalize_hint);
386
387
  static inline void Delete(RefBase* reference);
388
389
  virtual ~RefBase();
390
  void* Data();
391
  uint32_t Ref();
392
  uint32_t Unref();
393
  uint32_t RefCount();
394
395
 protected:
396
  void Finalize(bool is_env_teardown = false) override;
397
398
 private:
399
  uint32_t _refcount;
400
  bool _delete_self;
401
};
402
403
class Reference : public RefBase {
404
  using SecondPassCallParameterRef = Reference*;
405
406
 protected:
407
  template <typename... Args>
408
  Reference(napi_env env, v8::Local<v8::Value> value, Args&&... args);
409
410
 public:
411
  static Reference* New(napi_env env,
412
                        v8::Local<v8::Value> value,
413
                        uint32_t initial_refcount,
414
                        bool delete_self,
415
                        napi_finalize finalize_callback = nullptr,
416
                        void* finalize_data = nullptr,
417
                        void* finalize_hint = nullptr);
418
419
  virtual ~Reference();
420
  uint32_t Ref();
421
  uint32_t Unref();
422
  v8::Local<v8::Value> Get();
423
424
 protected:
425
  void Finalize(bool is_env_teardown = false) override;
426
427
 private:
428
  void ClearWeak();
429
  void SetWeak();
430
431
  static void FinalizeCallback(
432
      const v8::WeakCallbackInfo<SecondPassCallParameterRef>& data);
433
  static void SecondPassCallback(
434
      const v8::WeakCallbackInfo<SecondPassCallParameterRef>& data);
435
436
  v8impl::Persistent<v8::Value> _persistent;
437
  SecondPassCallParameterRef* _secondPassParameter;
438
  bool _secondPassScheduled;
439
440
  FRIEND_TEST(JsNativeApiV8Test, Reference);
441
};
442
443
}  // end of namespace v8impl
444
445
#define STATUS_CALL(call)                 \
446
  do {                                    \
447
    napi_status status = (call);          \
448
    if (status != napi_ok) return status; \
449
  } while (0)
450
451
#endif  // SRC_JS_NATIVE_API_V8_H_