GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: js_native_api_v8.h Lines: 105 108 97.2 %
Date: 2022-01-19 04:14:09 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
3248
  RefTracker() {}
16
6468
  virtual ~RefTracker() {}
17
  virtual void Finalize(bool isEnvTeardown) {}
18
19
  typedef RefTracker RefList;
20
21
3068
  inline void Link(RefList* list) {
22
3068
    prev_ = list;
23
3068
    next_ = list->next_;
24
3068
    if (next_ != nullptr) {
25
2442
      next_->prev_ = this;
26
    }
27
3068
    list->next_ = this;
28
3068
  }
29
30
3058
  inline void Unlink() {
31
3058
    if (prev_ != nullptr) {
32
3058
      prev_->next_ = next_;
33
    }
34
3058
    if (next_ != nullptr) {
35
1433
      next_->prev_ = prev_;
36
    }
37
3058
    prev_ = nullptr;
38
3058
    next_ = nullptr;
39
3058
  }
40
41
679
  static void FinalizeAll(RefList* list) {
42
679
    while (list->next_ != nullptr) {
43
503
      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
    napi_clear_last_error(this);
60
90
  }
61
352
  virtual ~napi_env__() {
62
    // First we must finalize those references that have `napi_finalizer`
63
    // callbacks. The reason is that addons might store other references which
64
    // they delete during their `napi_finalizer` callbacks. If we deleted such
65
    // references here first, they would be doubly deleted when the
66
    // `napi_finalizer` deleted them subsequently.
67
176
    v8impl::RefTracker::FinalizeAll(&finalizing_reflist);
68
176
    v8impl::RefTracker::FinalizeAll(&reflist);
69
  }
70
  v8::Isolate* const isolate;  // Shortcut for context()->GetIsolate()
71
  v8impl::Persistent<v8::Context> context_persistent;
72
73
243701
  inline v8::Local<v8::Context> context() const {
74
243701
    return v8impl::PersistentToLocal::Strong(context_persistent);
75
  }
76
77
1050
  inline void Ref() { refs++; }
78

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