GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: js_native_api_v8.h Lines: 94 101 93.1 %
Date: 2022-11-04 03:21:42 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
#include "js_native_api_types.h"
5
#include "js_native_api_v8_internals.h"
6
7
static napi_status napi_clear_last_error(napi_env env);
8
9
namespace v8impl {
10
11
class RefTracker {
12
 public:
13
3327
  RefTracker() {}
14
6626
  virtual ~RefTracker() {}
15
  virtual void Finalize(bool isEnvTeardown) {}
16
17
  typedef RefTracker RefList;
18
19
3133
  inline void Link(RefList* list) {
20
3133
    prev_ = list;
21
3133
    next_ = list->next_;
22
3133
    if (next_ != nullptr) {
23
2502
      next_->prev_ = this;
24
    }
25
3133
    list->next_ = this;
26
3133
  }
27
28
3123
  inline void Unlink() {
29
3123
    if (prev_ != nullptr) {
30
3123
      prev_->next_ = next_;
31
    }
32
3123
    if (next_ != nullptr) {
33
2493
      next_->prev_ = prev_;
34
    }
35
3123
    prev_ = nullptr;
36
3123
    next_ = nullptr;
37
3123
  }
38
39
761
  static void FinalizeAll(RefList* list) {
40
761
    while (list->next_ != nullptr) {
41
571
      list->next_->Finalize(true);
42
    }
43
190
  }
44
45
 private:
46
  RefList* next_ = nullptr;
47
  RefList* prev_ = nullptr;
48
};
49
50
}  // end of namespace v8impl
51
52
struct napi_env__ {
53
97
  explicit napi_env__(v8::Local<v8::Context> context)
54
291
      : isolate(context->GetIsolate()), context_persistent(isolate, context) {
55
97
    CHECK_EQ(isolate, context->GetIsolate());
56
97
    napi_clear_last_error(this);
57
97
  }
58
59
242434
  inline v8::Local<v8::Context> context() const {
60
242434
    return v8impl::PersistentToLocal::Strong(context_persistent);
61
  }
62
63
1069
  inline void Ref() { refs++; }
64
1163
  inline void Unref() {
65
1163
    if (--refs == 0) DeleteMe();
66
1163
  }
67
68
  virtual bool can_call_into_js() const { return true; }
69
  virtual v8::Maybe<bool> mark_arraybuffer_as_untransferable(
70
      v8::Local<v8::ArrayBuffer> ab) const {
71
    return v8::Just(true);
72
  }
73
74
2
  static inline void HandleThrow(napi_env env, v8::Local<v8::Value> value) {
75
2
    env->isolate->ThrowException(value);
76
2
  }
77
78
  template <typename T, typename U = decltype(HandleThrow)>
79
237123
  inline void CallIntoModule(T&& call, U&& handle_exception = HandleThrow) {
80
237123
    int open_handle_scopes_before = open_handle_scopes;
81
237123
    int open_callback_scopes_before = open_callback_scopes;
82
237123
    napi_clear_last_error(this);
83
237123
    call(this);
84
237123
    CHECK_EQ(open_handle_scopes, open_handle_scopes_before);
85
237123
    CHECK_EQ(open_callback_scopes, open_callback_scopes_before);
86
237123
    if (!last_exception.IsEmpty()) {
87
2508
      handle_exception(this, last_exception.Get(this->isolate));
88
1250
      last_exception.Reset();
89
    }
90
237119
  }
91
92
  // This should be overridden to schedule the finalization to a properiate
93
  // timing, like next tick of the event loop.
94
  virtual void CallFinalizer(napi_finalize cb, void* data, void* hint) {
95
    v8::HandleScope handle_scope(isolate);
96
    CallIntoModule([&](napi_env env) { cb(env, data, hint); });
97
  }
98
99
95
  virtual void DeleteMe() {
100
    // First we must finalize those references that have `napi_finalizer`
101
    // callbacks. The reason is that addons might store other references which
102
    // they delete during their `napi_finalizer` callbacks. If we deleted such
103
    // references here first, they would be doubly deleted when the
104
    // `napi_finalizer` deleted them subsequently.
105
95
    v8impl::RefTracker::FinalizeAll(&finalizing_reflist);
106
95
    v8impl::RefTracker::FinalizeAll(&reflist);
107
95
    delete this;
108
95
  }
109
110
  v8::Isolate* const isolate;  // Shortcut for context()->GetIsolate()
111
  v8impl::Persistent<v8::Context> context_persistent;
112
113
  v8impl::Persistent<v8::Value> last_exception;
114
115
  // We store references in two different lists, depending on whether they have
116
  // `napi_finalizer` callbacks, because we must first finalize the ones that
117
  // have such a callback. See `~napi_env__()` above for details.
118
  v8impl::RefTracker::RefList reflist;
119
  v8impl::RefTracker::RefList finalizing_reflist;
120
  napi_extended_error_info last_error;
121
  int open_handle_scopes = 0;
122
  int open_callback_scopes = 0;
123
  int refs = 1;
124
  void* instance_data = nullptr;
125
126
 protected:
127
  // Should not be deleted directly. Delete with `napi_env__::DeleteMe()`
128
  // instead.
129
380
  virtual ~napi_env__() = default;
130
};
131
132
// This class is used to keep a napi_env live in a way that
133
// is exception safe versus calling Ref/Unref directly
134
class EnvRefHolder {
135
 public:
136
1030
  explicit EnvRefHolder(napi_env env) : _env(env) { _env->Ref(); }
137
138
  explicit EnvRefHolder(const EnvRefHolder& other) : _env(other.env()) {
139
    _env->Ref();
140
  }
141
142
2060
  EnvRefHolder(EnvRefHolder&& other) {
143
2060
    _env = other._env;
144
2060
    other._env = nullptr;
145
2060
  }
146
147
6178
  ~EnvRefHolder() {
148
3089
    if (_env != nullptr) {
149
1029
      _env->Unref();
150
    }
151
3089
  }
152
153
1030
  napi_env env(void) const { return _env; }
154
155
 private:
156
  napi_env _env;
157
};
158
159
599752
inline napi_status napi_clear_last_error(napi_env env) {
160
599752
  env->last_error.error_code = napi_ok;
161
162
  // TODO(boingoing): Should this be a callback?
163
599752
  env->last_error.engine_error_code = 0;
164
599752
  env->last_error.engine_reserved = nullptr;
165
599752
  env->last_error.error_message = nullptr;
166
599752
  return napi_ok;
167
}
168
169
1351
inline napi_status napi_set_last_error(napi_env env,
170
                                       napi_status error_code,
171
                                       uint32_t engine_error_code = 0,
172
                                       void* engine_reserved = nullptr) {
173
1351
  env->last_error.error_code = error_code;
174
1351
  env->last_error.engine_error_code = engine_error_code;
175
1351
  env->last_error.engine_reserved = engine_reserved;
176
1351
  return error_code;
177
}
178
179
#define RETURN_STATUS_IF_FALSE(env, condition, status)                         \
180
  do {                                                                         \
181
    if (!(condition)) {                                                        \
182
      return napi_set_last_error((env), (status));                             \
183
    }                                                                          \
184
  } while (0)
185
186
#define RETURN_STATUS_IF_FALSE_WITH_PREAMBLE(env, condition, status)           \
187
  do {                                                                         \
188
    if (!(condition)) {                                                        \
189
      return napi_set_last_error(                                              \
190
          (env), try_catch.HasCaught() ? napi_pending_exception : (status));   \
191
    }                                                                          \
192
  } while (0)
193
194
#define CHECK_ENV(env)                                                         \
195
  do {                                                                         \
196
    if ((env) == nullptr) {                                                    \
197
      return napi_invalid_arg;                                                 \
198
    }                                                                          \
199
  } while (0)
200
201
#define CHECK_ARG(env, arg)                                                    \
202
  RETURN_STATUS_IF_FALSE((env), ((arg) != nullptr), napi_invalid_arg)
203
204
#define CHECK_ARG_WITH_PREAMBLE(env, arg)                                      \
205
  RETURN_STATUS_IF_FALSE_WITH_PREAMBLE(                                        \
206
      (env), ((arg) != nullptr), napi_invalid_arg)
207
208
#define CHECK_MAYBE_EMPTY(env, maybe, status)                                  \
209
  RETURN_STATUS_IF_FALSE((env), !((maybe).IsEmpty()), (status))
210
211
#define CHECK_MAYBE_EMPTY_WITH_PREAMBLE(env, maybe, status)                    \
212
  RETURN_STATUS_IF_FALSE_WITH_PREAMBLE((env), !((maybe).IsEmpty()), (status))
213
214
// NAPI_PREAMBLE is not wrapped in do..while: try_catch must have function scope
215
#define NAPI_PREAMBLE(env)                                                     \
216
  CHECK_ENV((env));                                                            \
217
  RETURN_STATUS_IF_FALSE(                                                      \
218
      (env),                                                                   \
219
      (env)->last_exception.IsEmpty() && (env)->can_call_into_js(),            \
220
      napi_pending_exception);                                                 \
221
  napi_clear_last_error((env));                                                \
222
  v8impl::TryCatch try_catch((env))
223
224
#define CHECK_TO_TYPE(env, type, context, result, src, status)                 \
225
  do {                                                                         \
226
    CHECK_ARG((env), (src));                                                   \
227
    auto maybe = v8impl::V8LocalValueFromJsValue((src))->To##type((context));  \
228
    CHECK_MAYBE_EMPTY((env), maybe, (status));                                 \
229
    (result) = maybe.ToLocalChecked();                                         \
230
  } while (0)
231
232
#define CHECK_TO_TYPE_WITH_PREAMBLE(env, type, context, result, src, status)   \
233
  do {                                                                         \
234
    CHECK_ARG_WITH_PREAMBLE((env), (src));                                     \
235
    auto maybe = v8impl::V8LocalValueFromJsValue((src))->To##type((context));  \
236
    CHECK_MAYBE_EMPTY_WITH_PREAMBLE((env), maybe, (status));                   \
237
    (result) = maybe.ToLocalChecked();                                         \
238
  } while (0)
239
240
#define CHECK_TO_FUNCTION(env, result, src)                                    \
241
  do {                                                                         \
242
    CHECK_ARG((env), (src));                                                   \
243
    v8::Local<v8::Value> v8value = v8impl::V8LocalValueFromJsValue((src));     \
244
    RETURN_STATUS_IF_FALSE((env), v8value->IsFunction(), napi_invalid_arg);    \
245
    (result) = v8value.As<v8::Function>();                                     \
246
  } while (0)
247
248
#define CHECK_TO_OBJECT(env, context, result, src)                             \
249
  CHECK_TO_TYPE((env), Object, (context), (result), (src), napi_object_expected)
250
251
#define CHECK_TO_OBJECT_WITH_PREAMBLE(env, context, result, src)               \
252
  CHECK_TO_TYPE_WITH_PREAMBLE(                                                 \
253
      (env), Object, (context), (result), (src), 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()                                                      \
260
       ? napi_ok                                                               \
261
       : napi_set_last_error((env), napi_pending_exception))
262
263
#define THROW_RANGE_ERROR_IF_FALSE(env, condition, error, message)             \
264
  do {                                                                         \
265
    if (!(condition)) {                                                        \
266
      napi_throw_range_error((env), (error), (message));                       \
267
      return napi_set_last_error((env), napi_generic_failure);                 \
268
    }                                                                          \
269
  } while (0)
270
271
#define RETURN_STATUS_IF_FALSE_WITH_PREAMBLE(env, condition, status)           \
272
  do {                                                                         \
273
    if (!(condition)) {                                                        \
274
      return napi_set_last_error(                                              \
275
          (env), try_catch.HasCaught() ? napi_pending_exception : (status));   \
276
    }                                                                          \
277
  } while (0)
278
279
#define CHECK_MAYBE_EMPTY_WITH_PREAMBLE(env, maybe, status)                    \
280
  RETURN_STATUS_IF_FALSE_WITH_PREAMBLE((env), !((maybe).IsEmpty()), (status))
281
282
#define STATUS_CALL(call)                                                      \
283
  do {                                                                         \
284
    napi_status status = (call);                                               \
285
    if (status != napi_ok) return status;                                      \
286
  } while (0)
287
288
namespace v8impl {
289
290
//=== Conversion between V8 Handles and napi_value ========================
291
292
// This asserts v8::Local<> will always be implemented with a single
293
// pointer field so that we can pass it around as a void*.
294
static_assert(sizeof(v8::Local<v8::Value>) == sizeof(napi_value),
295
              "Cannot convert between v8::Local<v8::Value> and napi_value");
296
297
348824
inline napi_value JsValueFromV8LocalValue(v8::Local<v8::Value> local) {
298
348824
  return reinterpret_cast<napi_value>(*local);
299
}
300
301
241855
inline v8::Local<v8::Value> V8LocalValueFromJsValue(napi_value v) {
302
  v8::Local<v8::Value> local;
303
241855
  memcpy(static_cast<void*>(&local), &v, sizeof(v));
304
241855
  return local;
305
}
306
307
// Adapter for napi_finalize callbacks.
308
class Finalizer {
309
 public:
310
  // Some Finalizers are run during shutdown when the napi_env is destroyed,
311
  // and some need to keep an explicit reference to the napi_env because they
312
  // are run independently.
313
  enum EnvReferenceMode { kNoEnvReference, kKeepEnvReference };
314
315
 protected:
316
3147
  Finalizer(napi_env env,
317
            napi_finalize finalize_callback,
318
            void* finalize_data,
319
            void* finalize_hint,
320
            EnvReferenceMode refmode = kNoEnvReference)
321
3147
      : _env(env),
322
        _finalize_callback(finalize_callback),
323
        _finalize_data(finalize_data),
324
        _finalize_hint(finalize_hint),
325
3147
        _has_env_reference(refmode == kKeepEnvReference) {
326
3147
    if (_has_env_reference) _env->Ref();
327
3147
  }
328
329
6274
  ~Finalizer() {
330
3137
    if (_has_env_reference) _env->Unref();
331
3137
  }
332
333
 public:
334
14
  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
14
        env, finalize_callback, finalize_data, finalize_hint, refmode);
341
  }
342
343
14
  static void Delete(Finalizer* finalizer) { delete finalizer; }
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
118802
  explicit TryCatch(napi_env env) : v8::TryCatch(env->isolate), _env(env) {}
357
358
118802
  ~TryCatch() {
359
118802
    if (HasCaught()) {
360
2490
      _env->last_exception.Reset(_env->isolate, Exception());
361
    }
362
118802
  }
363
364
 private:
365
  napi_env _env;
366
};
367
368
// Wrapper around v8impl::Persistent that implements reference counting.
369
class RefBase : protected Finalizer, RefTracker {
370
 protected:
371
  RefBase(napi_env env,
372
          uint32_t initial_refcount,
373
          bool delete_self,
374
          napi_finalize finalize_callback,
375
          void* finalize_data,
376
          void* finalize_hint);
377
378
 public:
379
  static RefBase* New(napi_env env,
380
                      uint32_t initial_refcount,
381
                      bool delete_self,
382
                      napi_finalize finalize_callback,
383
                      void* finalize_data,
384
                      void* finalize_hint);
385
386
  static inline void Delete(RefBase* reference);
387
388
  virtual ~RefBase();
389
  void* Data();
390
  uint32_t Ref();
391
  uint32_t Unref();
392
  uint32_t RefCount();
393
394
 protected:
395
  void Finalize(bool is_env_teardown = false) override;
396
397
 private:
398
  uint32_t _refcount;
399
  bool _delete_self;
400
};
401
402
class Reference : public RefBase {
403
  using SecondPassCallParameterRef = Reference*;
404
405
 protected:
406
  template <typename... Args>
407
  Reference(napi_env env, v8::Local<v8::Value> value, Args&&... args);
408
409
 public:
410
  static Reference* New(napi_env env,
411
                        v8::Local<v8::Value> value,
412
                        uint32_t initial_refcount,
413
                        bool delete_self,
414
                        napi_finalize finalize_callback = nullptr,
415
                        void* finalize_data = nullptr,
416
                        void* finalize_hint = nullptr);
417
418
  virtual ~Reference();
419
  uint32_t Ref();
420
  uint32_t Unref();
421
  v8::Local<v8::Value> Get();
422
423
 protected:
424
  void Finalize(bool is_env_teardown = false) override;
425
426
 private:
427
  void ClearWeak();
428
  void SetWeak();
429
430
  static void FinalizeCallback(
431
      const v8::WeakCallbackInfo<SecondPassCallParameterRef>& data);
432
  static void SecondPassCallback(
433
      const v8::WeakCallbackInfo<SecondPassCallParameterRef>& data);
434
435
  v8impl::Persistent<v8::Value> _persistent;
436
  SecondPassCallParameterRef* _secondPassParameter;
437
  bool _secondPassScheduled;
438
439
  FRIEND_TEST(JsNativeApiV8Test, Reference);
440
};
441
442
}  // end of namespace v8impl
443
444
#endif  // SRC_JS_NATIVE_API_V8_H_