GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: js_native_api_v8.h Lines: 85 93 91.4 %
Date: 2022-12-31 04:22:30 Branches: 22 28 78.6 %

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
inline napi_status napi_clear_last_error(napi_env env);
8
9
namespace v8impl {
10
11
class RefTracker {
12
 public:
13
3340
  RefTracker() {}
14
6628
  virtual ~RefTracker() {}
15
  virtual void Finalize() {}
16
17
  typedef RefTracker RefList;
18
19
3146
  inline void Link(RefList* list) {
20
3146
    prev_ = list;
21
3146
    next_ = list->next_;
22
3146
    if (next_ != nullptr) {
23
2507
      next_->prev_ = this;
24
    }
25
3146
    list->next_ = this;
26
3146
  }
27
28
5224
  inline void Unlink() {
29
5224
    if (prev_ != nullptr) {
30
3136
      prev_->next_ = next_;
31
    }
32
5224
    if (next_ != nullptr) {
33
2469
      next_->prev_ = prev_;
34
    }
35
5224
    prev_ = nullptr;
36
5224
    next_ = nullptr;
37
5224
  }
38
39
757
  static void FinalizeAll(RefList* list) {
40
757
    while (list->next_ != nullptr) {
41
567
      list->next_->Finalize();
42
    }
43
190
  }
44
45
 private:
46
  RefList* next_ = nullptr;
47
  RefList* prev_ = nullptr;
48
};
49
50
class Finalizer;
51
}  // end of namespace v8impl
52
53
struct napi_env__ {
54
97
  explicit napi_env__(v8::Local<v8::Context> context)
55
291
      : isolate(context->GetIsolate()), context_persistent(isolate, context) {
56
97
    napi_clear_last_error(this);
57
97
  }
58
59
242717
  inline v8::Local<v8::Context> context() const {
60
242717
    return v8impl::PersistentToLocal::Strong(context_persistent);
61
  }
62
63
58
  inline void Ref() { refs++; }
64
153
  inline void Unref() {
65
153
    if (--refs == 0) DeleteMe();
66
153
  }
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
    if (env->terminatedOrTerminating()) {
76
      return;
77
    }
78
2
    env->isolate->ThrowException(value);
79
  }
80
81
  // i.e. whether v8 exited or is about to exit
82
1244
  inline bool terminatedOrTerminating() {
83

1244
    return this->isolate->IsExecutionTerminating() || !can_call_into_js();
84
  }
85
86
  // v8 uses a special exception to indicate termination, the
87
  // `handle_exception` callback should identify such case using
88
  // terminatedOrTerminating() before actually handle the exception
89
  template <typename T, typename U = decltype(HandleThrow)>
90
237149
  inline void CallIntoModule(T&& call, U&& handle_exception = HandleThrow) {
91
237149
    int open_handle_scopes_before = open_handle_scopes;
92
237149
    int open_callback_scopes_before = open_callback_scopes;
93
237149
    napi_clear_last_error(this);
94
237149
    call(this);
95
237149
    CHECK_EQ(open_handle_scopes, open_handle_scopes_before);
96
237149
    CHECK_EQ(open_callback_scopes, open_callback_scopes_before);
97
237149
    if (!last_exception.IsEmpty()) {
98
2510
      handle_exception(this, last_exception.Get(this->isolate));
99
1251
      last_exception.Reset();
100
    }
101
237145
  }
102
103
  // Call finalizer immediately.
104
  virtual void CallFinalizer(napi_finalize cb, void* data, void* hint) {
105
    v8::HandleScope handle_scope(isolate);
106
    CallIntoModule([&](napi_env env) { cb(env, data, hint); });
107
  }
108
109
  // Enqueue the finalizer to the napi_env's own queue of the second pass
110
  // weak callback.
111
  // Implementation should drain the queue at the time it is safe to call
112
  // into JavaScript.
113
2024
  virtual void EnqueueFinalizer(v8impl::RefTracker* finalizer) {
114
2024
    pending_finalizers.emplace(finalizer);
115
2024
  }
116
117
  // Remove the finalizer from the scheduled second pass weak callback queue.
118
  // The finalizer can be deleted after this call.
119
3124
  virtual void DequeueFinalizer(v8impl::RefTracker* finalizer) {
120
3124
    pending_finalizers.erase(finalizer);
121
3124
  }
122
123
95
  virtual void DeleteMe() {
124
    // First we must finalize those references that have `napi_finalizer`
125
    // callbacks. The reason is that addons might store other references which
126
    // they delete during their `napi_finalizer` callbacks. If we deleted such
127
    // references here first, they would be doubly deleted when the
128
    // `napi_finalizer` deleted them subsequently.
129
95
    v8impl::RefTracker::FinalizeAll(&finalizing_reflist);
130
95
    v8impl::RefTracker::FinalizeAll(&reflist);
131
95
    delete this;
132
95
  }
133
134
  v8::Isolate* const isolate;  // Shortcut for context()->GetIsolate()
135
  v8impl::Persistent<v8::Context> context_persistent;
136
137
  v8impl::Persistent<v8::Value> last_exception;
138
139
  // We store references in two different lists, depending on whether they have
140
  // `napi_finalizer` callbacks, because we must first finalize the ones that
141
  // have such a callback. See `~napi_env__()` above for details.
142
  v8impl::RefTracker::RefList reflist;
143
  v8impl::RefTracker::RefList finalizing_reflist;
144
  // The invocation order of the finalizers is not determined.
145
  std::unordered_set<v8impl::RefTracker*> pending_finalizers;
146
  napi_extended_error_info last_error;
147
  int open_handle_scopes = 0;
148
  int open_callback_scopes = 0;
149
  int refs = 1;
150
  void* instance_data = nullptr;
151
152
 protected:
153
  // Should not be deleted directly. Delete with `napi_env__::DeleteMe()`
154
  // instead.
155
380
  virtual ~napi_env__() = default;
156
};
157
158
599806
inline napi_status napi_clear_last_error(napi_env env) {
159
599806
  env->last_error.error_code = napi_ok;
160
599806
  env->last_error.engine_error_code = 0;
161
599806
  env->last_error.engine_reserved = nullptr;
162
599806
  env->last_error.error_message = nullptr;
163
599806
  return napi_ok;
164
}
165
166
1350
inline napi_status napi_set_last_error(napi_env env,
167
                                       napi_status error_code,
168
                                       uint32_t engine_error_code = 0,
169
                                       void* engine_reserved = nullptr) {
170
1350
  env->last_error.error_code = error_code;
171
1350
  env->last_error.engine_error_code = engine_error_code;
172
1350
  env->last_error.engine_reserved = engine_reserved;
173
1350
  return error_code;
174
}
175
176
#define RETURN_STATUS_IF_FALSE(env, condition, status)                         \
177
  do {                                                                         \
178
    if (!(condition)) {                                                        \
179
      return napi_set_last_error((env), (status));                             \
180
    }                                                                          \
181
  } while (0)
182
183
#define RETURN_STATUS_IF_FALSE_WITH_PREAMBLE(env, condition, status)           \
184
  do {                                                                         \
185
    if (!(condition)) {                                                        \
186
      return napi_set_last_error(                                              \
187
          (env), try_catch.HasCaught() ? napi_pending_exception : (status));   \
188
    }                                                                          \
189
  } while (0)
190
191
#define CHECK_ENV(env)                                                         \
192
  do {                                                                         \
193
    if ((env) == nullptr) {                                                    \
194
      return napi_invalid_arg;                                                 \
195
    }                                                                          \
196
  } while (0)
197
198
#define CHECK_ARG(env, arg)                                                    \
199
  RETURN_STATUS_IF_FALSE((env), ((arg) != nullptr), napi_invalid_arg)
200
201
#define CHECK_ARG_WITH_PREAMBLE(env, arg)                                      \
202
  RETURN_STATUS_IF_FALSE_WITH_PREAMBLE(                                        \
203
      (env), ((arg) != nullptr), 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(                                                      \
215
      (env),                                                                   \
216
      (env)->last_exception.IsEmpty() && (env)->can_call_into_js(),            \
217
      napi_pending_exception);                                                 \
218
  napi_clear_last_error((env));                                                \
219
  v8impl::TryCatch try_catch((env))
220
221
#define CHECK_TO_TYPE(env, type, context, result, src, status)                 \
222
  do {                                                                         \
223
    CHECK_ARG((env), (src));                                                   \
224
    auto maybe = v8impl::V8LocalValueFromJsValue((src))->To##type((context));  \
225
    CHECK_MAYBE_EMPTY((env), maybe, (status));                                 \
226
    (result) = maybe.ToLocalChecked();                                         \
227
  } while (0)
228
229
#define CHECK_TO_TYPE_WITH_PREAMBLE(env, type, context, result, src, status)   \
230
  do {                                                                         \
231
    CHECK_ARG_WITH_PREAMBLE((env), (src));                                     \
232
    auto maybe = v8impl::V8LocalValueFromJsValue((src))->To##type((context));  \
233
    CHECK_MAYBE_EMPTY_WITH_PREAMBLE((env), maybe, (status));                   \
234
    (result) = maybe.ToLocalChecked();                                         \
235
  } while (0)
236
237
#define CHECK_TO_FUNCTION(env, result, src)                                    \
238
  do {                                                                         \
239
    CHECK_ARG((env), (src));                                                   \
240
    v8::Local<v8::Value> v8value = v8impl::V8LocalValueFromJsValue((src));     \
241
    RETURN_STATUS_IF_FALSE((env), v8value->IsFunction(), napi_invalid_arg);    \
242
    (result) = v8value.As<v8::Function>();                                     \
243
  } while (0)
244
245
#define CHECK_TO_OBJECT(env, context, result, src)                             \
246
  CHECK_TO_TYPE((env), Object, (context), (result), (src), napi_object_expected)
247
248
#define CHECK_TO_OBJECT_WITH_PREAMBLE(env, context, result, src)               \
249
  CHECK_TO_TYPE_WITH_PREAMBLE(                                                 \
250
      (env), Object, (context), (result), (src), napi_object_expected)
251
252
#define CHECK_TO_STRING(env, context, result, src)                             \
253
  CHECK_TO_TYPE((env), String, (context), (result), (src), napi_string_expected)
254
255
#define GET_RETURN_STATUS(env)                                                 \
256
  (!try_catch.HasCaught()                                                      \
257
       ? napi_ok                                                               \
258
       : napi_set_last_error((env), napi_pending_exception))
259
260
#define THROW_RANGE_ERROR_IF_FALSE(env, condition, error, message)             \
261
  do {                                                                         \
262
    if (!(condition)) {                                                        \
263
      napi_throw_range_error((env), (error), (message));                       \
264
      return napi_set_last_error((env), napi_generic_failure);                 \
265
    }                                                                          \
266
  } while (0)
267
268
#define RETURN_STATUS_IF_FALSE_WITH_PREAMBLE(env, condition, status)           \
269
  do {                                                                         \
270
    if (!(condition)) {                                                        \
271
      return napi_set_last_error(                                              \
272
          (env), try_catch.HasCaught() ? napi_pending_exception : (status));   \
273
    }                                                                          \
274
  } while (0)
275
276
#define CHECK_MAYBE_EMPTY_WITH_PREAMBLE(env, maybe, status)                    \
277
  RETURN_STATUS_IF_FALSE_WITH_PREAMBLE((env), !((maybe).IsEmpty()), (status))
278
279
#define STATUS_CALL(call)                                                      \
280
  do {                                                                         \
281
    napi_status status = (call);                                               \
282
    if (status != napi_ok) return status;                                      \
283
  } while (0)
284
285
namespace v8impl {
286
287
//=== Conversion between V8 Handles and napi_value ========================
288
289
// This asserts v8::Local<> will always be implemented with a single
290
// pointer field so that we can pass it around as a void*.
291
static_assert(sizeof(v8::Local<v8::Value>) == sizeof(napi_value),
292
              "Cannot convert between v8::Local<v8::Value> and napi_value");
293
294
348837
inline napi_value JsValueFromV8LocalValue(v8::Local<v8::Value> local) {
295
348837
  return reinterpret_cast<napi_value>(*local);
296
}
297
298
241873
inline v8::Local<v8::Value> V8LocalValueFromJsValue(napi_value v) {
299
  v8::Local<v8::Value> local;
300
241873
  memcpy(static_cast<void*>(&local), &v, sizeof(v));
301
241873
  return local;
302
}
303
304
// Adapter for napi_finalize callbacks.
305
class Finalizer {
306
 protected:
307
3160
  Finalizer(napi_env env,
308
            napi_finalize finalize_callback,
309
            void* finalize_data,
310
            void* finalize_hint)
311
3160
      : env_(env),
312
        finalize_callback_(finalize_callback),
313
        finalize_data_(finalize_data),
314
3160
        finalize_hint_(finalize_hint) {}
315
316
6276
  virtual ~Finalizer() = default;
317
318
 public:
319
  static Finalizer* New(napi_env env,
320
                        napi_finalize finalize_callback = nullptr,
321
                        void* finalize_data = nullptr,
322
                        void* finalize_hint = nullptr) {
323
    return new Finalizer(env, finalize_callback, finalize_data, finalize_hint);
324
  }
325
326
  napi_finalize callback() { return finalize_callback_; }
327
  void* data() { return finalize_data_; }
328
  void* hint() { return finalize_hint_; }
329
330
  void ResetFinalizer();
331
332
 protected:
333
  napi_env env_;
334
  napi_finalize finalize_callback_;
335
  void* finalize_data_;
336
  void* finalize_hint_;
337
};
338
339
class TryCatch : public v8::TryCatch {
340
 public:
341
118812
  explicit TryCatch(napi_env env) : v8::TryCatch(env->isolate), _env(env) {}
342
343
118812
  ~TryCatch() {
344
118812
    if (HasCaught()) {
345
2492
      _env->last_exception.Reset(_env->isolate, Exception());
346
    }
347
118812
  }
348
349
 private:
350
  napi_env _env;
351
};
352
353
// Ownership of a reference.
354
enum class Ownership {
355
  // The reference is owned by the runtime. No userland call is needed to
356
  // destruct the reference.
357
  kRuntime,
358
  // The reference is owned by the userland. User code is responsible to delete
359
  // the reference with appropriate node-api calls.
360
  kUserland,
361
};
362
363
// Wrapper around Finalizer that implements reference counting.
364
class RefBase : public Finalizer, public RefTracker {
365
 protected:
366
  RefBase(napi_env env,
367
          uint32_t initial_refcount,
368
          Ownership ownership,
369
          napi_finalize finalize_callback,
370
          void* finalize_data,
371
          void* finalize_hint);
372
373
 public:
374
  static RefBase* New(napi_env env,
375
                      uint32_t initial_refcount,
376
                      Ownership ownership,
377
                      napi_finalize finalize_callback,
378
                      void* finalize_data,
379
                      void* finalize_hint);
380
  virtual ~RefBase();
381
382
  void* Data();
383
  uint32_t Ref();
384
  uint32_t Unref();
385
  uint32_t RefCount();
386
387
10
  Ownership ownership() { return ownership_; }
388
389
 protected:
390
  void Finalize() override;
391
392
 private:
393
  uint32_t refcount_;
394
  Ownership ownership_;
395
};
396
397
// Wrapper around v8impl::Persistent.
398
class Reference : public RefBase {
399
 protected:
400
  template <typename... Args>
401
  Reference(napi_env env, v8::Local<v8::Value> value, Args&&... args);
402
403
 public:
404
  static Reference* New(napi_env env,
405
                        v8::Local<v8::Value> value,
406
                        uint32_t initial_refcount,
407
                        Ownership ownership,
408
                        napi_finalize finalize_callback = nullptr,
409
                        void* finalize_data = nullptr,
410
                        void* finalize_hint = nullptr);
411
412
  virtual ~Reference();
413
  uint32_t Ref();
414
  uint32_t Unref();
415
  v8::Local<v8::Value> Get();
416
417
 protected:
418
  void Finalize() override;
419
420
 private:
421
  static void WeakCallback(const v8::WeakCallbackInfo<Reference>& data);
422
423
  void SetWeak();
424
425
  v8impl::Persistent<v8::Value> persistent_;
426
};
427
428
}  // end of namespace v8impl
429
430
#endif  // SRC_JS_NATIVE_API_V8_H_