GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: js_native_api_v8.h Lines: 93 100 93.0 %
Date: 2022-06-10 04:15:47 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
3325
  RefTracker() {}
16
6622
  virtual ~RefTracker() {}
17
  virtual void Finalize(bool isEnvTeardown) {}
18
19
  typedef RefTracker RefList;
20
21
3133
  inline void Link(RefList* list) {
22
3133
    prev_ = list;
23
3133
    next_ = list->next_;
24
3133
    if (next_ != nullptr) {
25
2501
      next_->prev_ = this;
26
    }
27
3133
    list->next_ = this;
28
3133
  }
29
30
3123
  inline void Unlink() {
31
3123
    if (prev_ != nullptr) {
32
3123
      prev_->next_ = next_;
33
    }
34
3123
    if (next_ != nullptr) {
35
2491
      next_->prev_ = prev_;
36
    }
37
3123
    prev_ = nullptr;
38
3123
    next_ = nullptr;
39
3123
  }
40
41
948
  static void FinalizeAll(RefList* list) {
42
948
    while (list->next_ != nullptr) {
43
572
      list->next_->Finalize(true);
44
    }
45
376
  }
46
47
 private:
48
  RefList* next_ = nullptr;
49
  RefList* prev_ = nullptr;
50
};
51
52
}  // end of namespace v8impl
53
54
struct napi_env__ {
55
96
  explicit napi_env__(v8::Local<v8::Context> context)
56
288
      : isolate(context->GetIsolate()), context_persistent(isolate, context) {
57
96
    CHECK_EQ(isolate, context->GetIsolate());
58
96
    napi_clear_last_error(this);
59
96
  }
60
376
  virtual ~napi_env__() { FinalizeAll(); }
61
  v8::Isolate* const isolate;  // Shortcut for context()->GetIsolate()
62
  v8impl::Persistent<v8::Context> context_persistent;
63
64
244379
  inline v8::Local<v8::Context> context() const {
65
244379
    return v8impl::PersistentToLocal::Strong(context_persistent);
66
  }
67
68
1067
  inline void Ref() { refs++; }
69
1160
  inline void Unref() {
70

1160
    if (--refs == 0) delete this;
71
1160
  }
72
73
  virtual bool can_call_into_js() const { return true; }
74
  virtual v8::Maybe<bool> mark_arraybuffer_as_untransferable(
75
      v8::Local<v8::ArrayBuffer> ab) const {
76
    return v8::Just(true);
77
  }
78
79
2
  static inline void HandleThrow(napi_env env, v8::Local<v8::Value> value) {
80
2
    env->isolate->ThrowException(value);
81
2
  }
82
83
  template <typename T, typename U = decltype(HandleThrow)>
84
239088
  inline void CallIntoModule(T&& call, U&& handle_exception = HandleThrow) {
85
239088
    int open_handle_scopes_before = open_handle_scopes;
86
239088
    int open_callback_scopes_before = open_callback_scopes;
87
239088
    napi_clear_last_error(this);
88
239088
    call(this);
89
239088
    CHECK_EQ(open_handle_scopes, open_handle_scopes_before);
90
239088
    CHECK_EQ(open_callback_scopes, open_callback_scopes_before);
91
239088
    if (!last_exception.IsEmpty()) {
92
2508
      handle_exception(this, last_exception.Get(this->isolate));
93
1250
      last_exception.Reset();
94
    }
95
239084
  }
96
97
  // This should be overridden to schedule the finalization to a properiate
98
  // timing, like next tick of the event loop.
99
  virtual void CallFinalizer(napi_finalize cb, void* data, void* hint) {
100
    v8::HandleScope handle_scope(isolate);
101
    CallIntoModule([&](napi_env env) { cb(env, data, hint); });
102
  }
103
104
188
  void FinalizeAll() {
105
    // First we must finalize those references that have `napi_finalizer`
106
    // callbacks. The reason is that addons might store other references which
107
    // they delete during their `napi_finalizer` callbacks. If we deleted such
108
    // references here first, they would be doubly deleted when the
109
    // `napi_finalizer` deleted them subsequently.
110
188
    v8impl::RefTracker::FinalizeAll(&finalizing_reflist);
111
188
    v8impl::RefTracker::FinalizeAll(&reflist);
112
188
  }
113
114
  v8impl::Persistent<v8::Value> last_exception;
115
116
  // We store references in two different lists, depending on whether they have
117
  // `napi_finalizer` callbacks, because we must first finalize the ones that
118
  // have such a callback. See `~napi_env__()` above for details.
119
  v8impl::RefTracker::RefList reflist;
120
  v8impl::RefTracker::RefList finalizing_reflist;
121
  napi_extended_error_info last_error;
122
  int open_handle_scopes = 0;
123
  int open_callback_scopes = 0;
124
  int refs = 1;
125
  void* instance_data = nullptr;
126
};
127
128
// This class is used to keep a napi_env live in a way that
129
// is exception safe versus calling Ref/Unref directly
130
class EnvRefHolder {
131
 public:
132
1027
  explicit EnvRefHolder(napi_env env) : _env(env) { _env->Ref(); }
133
134
  explicit EnvRefHolder(const EnvRefHolder& other) : _env(other.env()) {
135
    _env->Ref();
136
  }
137
138
2054
  EnvRefHolder(EnvRefHolder&& other) {
139
2054
    _env = other._env;
140
2054
    other._env = nullptr;
141
2054
  }
142
143
6160
  ~EnvRefHolder() {
144
3080
    if (_env != nullptr) {
145
1026
      _env->Unref();
146
    }
147
3080
  }
148
149
1027
  napi_env env(void) const { return _env; }
150
151
 private:
152
  napi_env _env;
153
};
154
155
604655
static inline napi_status napi_clear_last_error(napi_env env) {
156
604655
  env->last_error.error_code = napi_ok;
157
158
  // TODO(boingoing): Should this be a callback?
159
604655
  env->last_error.engine_error_code = 0;
160
604655
  env->last_error.engine_reserved = nullptr;
161
604655
  env->last_error.error_message = nullptr;
162
604655
  return napi_ok;
163
}
164
165
1350
static inline napi_status napi_set_last_error(napi_env env,
166
                                              napi_status error_code,
167
                                              uint32_t engine_error_code = 0,
168
                                              void* engine_reserved = nullptr) {
169
1350
  env->last_error.error_code = error_code;
170
1350
  env->last_error.engine_error_code = engine_error_code;
171
1350
  env->last_error.engine_reserved = engine_reserved;
172
1350
  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(                                        \
202
      (env), ((arg) != nullptr), napi_invalid_arg)
203
204
#define CHECK_MAYBE_EMPTY(env, maybe, status)                                  \
205
  RETURN_STATUS_IF_FALSE((env), !((maybe).IsEmpty()), (status))
206
207
#define CHECK_MAYBE_EMPTY_WITH_PREAMBLE(env, maybe, status)                    \
208
  RETURN_STATUS_IF_FALSE_WITH_PREAMBLE((env), !((maybe).IsEmpty()), (status))
209
210
// NAPI_PREAMBLE is not wrapped in do..while: try_catch must have function scope
211
#define NAPI_PREAMBLE(env)                                                     \
212
  CHECK_ENV((env));                                                            \
213
  RETURN_STATUS_IF_FALSE(                                                      \
214
      (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(                                                 \
249
      (env), Object, (context), (result), (src), napi_object_expected)
250
251
#define CHECK_TO_STRING(env, context, result, src)                             \
252
  CHECK_TO_TYPE((env), String, (context), (result), (src), napi_string_expected)
253
254
#define GET_RETURN_STATUS(env)                                                 \
255
  (!try_catch.HasCaught()                                                      \
256
       ? napi_ok                                                               \
257
       : napi_set_last_error((env), napi_pending_exception))
258
259
#define THROW_RANGE_ERROR_IF_FALSE(env, condition, error, message)             \
260
  do {                                                                         \
261
    if (!(condition)) {                                                        \
262
      napi_throw_range_error((env), (error), (message));                       \
263
      return napi_set_last_error((env), napi_generic_failure);                 \
264
    }                                                                          \
265
  } while (0)
266
267
#define RETURN_STATUS_IF_FALSE_WITH_PREAMBLE(env, condition, status)           \
268
  do {                                                                         \
269
    if (!(condition)) {                                                        \
270
      return napi_set_last_error(                                              \
271
          (env), try_catch.HasCaught() ? napi_pending_exception : (status));   \
272
    }                                                                          \
273
  } while (0)
274
275
#define CHECK_MAYBE_EMPTY_WITH_PREAMBLE(env, maybe, status)                    \
276
  RETURN_STATUS_IF_FALSE_WITH_PREAMBLE((env), !((maybe).IsEmpty()), (status))
277
278
namespace v8impl {
279
280
//=== Conversion between V8 Handles and napi_value ========================
281
282
// This asserts v8::Local<> will always be implemented with a single
283
// pointer field so that we can pass it around as a void*.
284
static_assert(sizeof(v8::Local<v8::Value>) == sizeof(napi_value),
285
              "Cannot convert between v8::Local<v8::Value> and napi_value");
286
287
351760
inline napi_value JsValueFromV8LocalValue(v8::Local<v8::Value> local) {
288
351760
  return reinterpret_cast<napi_value>(*local);
289
}
290
291
243808
inline v8::Local<v8::Value> V8LocalValueFromJsValue(napi_value v) {
292
  v8::Local<v8::Value> local;
293
243808
  memcpy(static_cast<void*>(&local), &v, sizeof(v));
294
243808
  return local;
295
}
296
297
// Adapter for napi_finalize callbacks.
298
class Finalizer {
299
 public:
300
  // Some Finalizers are run during shutdown when the napi_env is destroyed,
301
  // and some need to keep an explicit reference to the napi_env because they
302
  // are run independently.
303
  enum EnvReferenceMode { kNoEnvReference, kKeepEnvReference };
304
305
 protected:
306
3147
  Finalizer(napi_env env,
307
            napi_finalize finalize_callback,
308
            void* finalize_data,
309
            void* finalize_hint,
310
            EnvReferenceMode refmode = kNoEnvReference)
311
3147
      : _env(env),
312
        _finalize_callback(finalize_callback),
313
        _finalize_data(finalize_data),
314
        _finalize_hint(finalize_hint),
315
3147
        _has_env_reference(refmode == kKeepEnvReference) {
316
3147
    if (_has_env_reference) _env->Ref();
317
3147
  }
318
319
6274
  ~Finalizer() {
320
3137
    if (_has_env_reference) _env->Unref();
321
3137
  }
322
323
 public:
324
14
  static Finalizer* New(napi_env env,
325
                        napi_finalize finalize_callback = nullptr,
326
                        void* finalize_data = nullptr,
327
                        void* finalize_hint = nullptr,
328
                        EnvReferenceMode refmode = kNoEnvReference) {
329
    return new Finalizer(
330
14
        env, finalize_callback, finalize_data, finalize_hint, refmode);
331
  }
332
333
14
  static void Delete(Finalizer* finalizer) { delete finalizer; }
334
335
 protected:
336
  napi_env _env;
337
  napi_finalize _finalize_callback;
338
  void* _finalize_data;
339
  void* _finalize_hint;
340
  bool _finalize_ran = false;
341
  bool _has_env_reference = false;
342
};
343
344
class TryCatch : public v8::TryCatch {
345
 public:
346
119779
  explicit TryCatch(napi_env env) : v8::TryCatch(env->isolate), _env(env) {}
347
348
119779
  ~TryCatch() {
349
119779
    if (HasCaught()) {
350
2490
      _env->last_exception.Reset(_env->isolate, Exception());
351
    }
352
119779
  }
353
354
 private:
355
  napi_env _env;
356
};
357
358
// Wrapper around v8impl::Persistent that implements reference counting.
359
class RefBase : protected Finalizer, RefTracker {
360
 protected:
361
  RefBase(napi_env env,
362
          uint32_t initial_refcount,
363
          bool delete_self,
364
          napi_finalize finalize_callback,
365
          void* finalize_data,
366
          void* finalize_hint);
367
368
 public:
369
  static RefBase* New(napi_env env,
370
                      uint32_t initial_refcount,
371
                      bool delete_self,
372
                      napi_finalize finalize_callback,
373
                      void* finalize_data,
374
                      void* finalize_hint);
375
376
  static inline void Delete(RefBase* reference);
377
378
  virtual ~RefBase();
379
  void* Data();
380
  uint32_t Ref();
381
  uint32_t Unref();
382
  uint32_t RefCount();
383
384
 protected:
385
  void Finalize(bool is_env_teardown = false) override;
386
387
 private:
388
  uint32_t _refcount;
389
  bool _delete_self;
390
};
391
392
class Reference : public RefBase {
393
  using SecondPassCallParameterRef = Reference*;
394
395
 protected:
396
  template <typename... Args>
397
  Reference(napi_env env, v8::Local<v8::Value> value, Args&&... args);
398
399
 public:
400
  static Reference* New(napi_env env,
401
                        v8::Local<v8::Value> value,
402
                        uint32_t initial_refcount,
403
                        bool delete_self,
404
                        napi_finalize finalize_callback = nullptr,
405
                        void* finalize_data = nullptr,
406
                        void* finalize_hint = nullptr);
407
408
  virtual ~Reference();
409
  uint32_t Ref();
410
  uint32_t Unref();
411
  v8::Local<v8::Value> Get();
412
413
 protected:
414
  void Finalize(bool is_env_teardown = false) override;
415
416
 private:
417
  void ClearWeak();
418
  void SetWeak();
419
420
  static void FinalizeCallback(
421
      const v8::WeakCallbackInfo<SecondPassCallParameterRef>& data);
422
  static void SecondPassCallback(
423
      const v8::WeakCallbackInfo<SecondPassCallParameterRef>& data);
424
425
  v8impl::Persistent<v8::Value> _persistent;
426
  SecondPassCallParameterRef* _secondPassParameter;
427
  bool _secondPassScheduled;
428
429
  FRIEND_TEST(JsNativeApiV8Test, Reference);
430
};
431
432
}  // end of namespace v8impl
433
434
#define STATUS_CALL(call)                                                      \
435
  do {                                                                         \
436
    napi_status status = (call);                                               \
437
    if (status != napi_ok) return status;                                      \
438
  } while (0)
439
440
#endif  // SRC_JS_NATIVE_API_V8_H_