• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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:
RefTracker()15   RefTracker() {}
~RefTracker()16   virtual ~RefTracker() {}
Finalize(bool isEnvTeardown)17   virtual void Finalize(bool isEnvTeardown) {}
18 
19   typedef RefTracker RefList;
20 
Link(RefList * list)21   inline void Link(RefList* list) {
22     prev_ = list;
23     next_ = list->next_;
24     if (next_ != nullptr) {
25       next_->prev_ = this;
26     }
27     list->next_ = this;
28   }
29 
Unlink()30   inline void Unlink() {
31     if (prev_ != nullptr) {
32       prev_->next_ = next_;
33     }
34     if (next_ != nullptr) {
35       next_->prev_ = prev_;
36     }
37     prev_ = nullptr;
38     next_ = nullptr;
39   }
40 
FinalizeAll(RefList * list)41   static void FinalizeAll(RefList* list) {
42     while (list->next_ != nullptr) {
43       list->next_->Finalize(true);
44     }
45   }
46 
47  private:
48   RefList* next_ = nullptr;
49   RefList* prev_ = nullptr;
50 };
51 
52 }  // end of namespace v8impl
53 
54 struct napi_env__ {
napi_env__napi_env__55   explicit napi_env__(v8::Local<v8::Context> context)
56       : isolate(context->GetIsolate()),
57         context_persistent(isolate, context) {
58     CHECK_EQ(isolate, context->GetIsolate());
59   }
~napi_env__napi_env__60   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     v8impl::RefTracker::FinalizeAll(&finalizing_reflist);
67     v8impl::RefTracker::FinalizeAll(&reflist);
68   }
69   v8::Isolate* const isolate;  // Shortcut for context()->GetIsolate()
70   v8impl::Persistent<v8::Context> context_persistent;
71 
contextnapi_env__72   inline v8::Local<v8::Context> context() const {
73     return v8impl::PersistentToLocal::Strong(context_persistent);
74   }
75 
Refnapi_env__76   inline void Ref() { refs++; }
Unrefnapi_env__77   inline void Unref() { if ( --refs == 0) delete this; }
78 
can_call_into_jsnapi_env__79   virtual bool can_call_into_js() const { return true; }
mark_arraybuffer_as_untransferablenapi_env__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
HandleThrownapi_env__86   HandleThrow(napi_env env, v8::Local<v8::Value> value) {
87     env->isolate->ThrowException(value);
88   }
89 
90   template <typename T, typename U = decltype(HandleThrow)>
91   inline void CallIntoModule(T&& call, U&& handle_exception = HandleThrow) {
92     int open_handle_scopes_before = open_handle_scopes;
93     int open_callback_scopes_before = open_callback_scopes;
94     napi_clear_last_error(this);
95     call(this);
96     CHECK_EQ(open_handle_scopes, open_handle_scopes_before);
97     CHECK_EQ(open_callback_scopes, open_callback_scopes_before);
98     if (!last_exception.IsEmpty()) {
99       handle_exception(this, last_exception.Get(this->isolate));
100       last_exception.Reset();
101     }
102   }
103 
CallFinalizernapi_env__104   virtual void CallFinalizer(napi_finalize cb, void* data, void* hint) {
105     v8::HandleScope handle_scope(isolate);
106     CallIntoModule([&](napi_env env) {
107       cb(env, data, hint);
108     });
109   }
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:
EnvRefHolder(napi_env env)129   explicit EnvRefHolder(napi_env env) : _env(env) {
130       _env->Ref();
131   }
132 
EnvRefHolder(const EnvRefHolder & other)133   explicit EnvRefHolder(const EnvRefHolder& other): _env(other.env()) {
134     _env->Ref();
135   }
136 
EnvRefHolder(EnvRefHolder && other)137   EnvRefHolder(EnvRefHolder&& other) {
138     _env = other._env;
139     other._env = nullptr;
140   }
141 
~EnvRefHolder()142   ~EnvRefHolder() {
143     if (_env != nullptr) {
144       _env->Unref();
145     }
146   }
147 
env(void)148   napi_env env(void) const {
149     return _env;
150   }
151 
152  private:
153   napi_env _env;
154 };
155 
napi_clear_last_error(napi_env env)156 static inline napi_status napi_clear_last_error(napi_env env) {
157   env->last_error.error_code = napi_ok;
158 
159   // TODO(boingoing): Should this be a callback?
160   env->last_error.engine_error_code = 0;
161   env->last_error.engine_reserved = nullptr;
162   return napi_ok;
163 }
164 
165 static inline
166 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   env->last_error.error_code = error_code;
170   env->last_error.engine_error_code = engine_error_code;
171   env->last_error.engine_reserved = engine_reserved;
172   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 
JsValueFromV8LocalValue(v8::Local<v8::Value> local)290 inline napi_value JsValueFromV8LocalValue(v8::Local<v8::Value> local) {
291   return reinterpret_cast<napi_value>(*local);
292 }
293 
V8LocalValueFromJsValue(napi_value v)294 inline v8::Local<v8::Value> V8LocalValueFromJsValue(napi_value v) {
295   v8::Local<v8::Value> local;
296   memcpy(static_cast<void*>(&local), &v, sizeof(v));
297   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   Finalizer(napi_env env,
313             napi_finalize finalize_callback,
314             void* finalize_data,
315             void* finalize_hint,
316             EnvReferenceMode refmode = kNoEnvReference)
_env(env)317     : _env(env),
318       _finalize_callback(finalize_callback),
319       _finalize_data(finalize_data),
320       _finalize_hint(finalize_hint),
321       _has_env_reference(refmode == kKeepEnvReference) {
322     if (_has_env_reference)
323       _env->Ref();
324   }
325 
~Finalizer()326   ~Finalizer() {
327     if (_has_env_reference)
328       _env->Unref();
329   }
330 
331  public:
332   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         env, finalize_callback, finalize_data, finalize_hint, refmode);
339   }
340 
Delete(Finalizer * finalizer)341   static void Delete(Finalizer* finalizer) {
342     delete finalizer;
343   }
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:
TryCatch(napi_env env)356   explicit TryCatch(napi_env env)
357       : v8::TryCatch(env->isolate), _env(env) {}
358 
~TryCatch()359   ~TryCatch() {
360     if (HasCaught()) {
361       _env->last_exception.Reset(_env->isolate, Exception());
362     }
363   }
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_
452