• 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 
napi_clear_last_error(napi_env env)125 static inline napi_status napi_clear_last_error(napi_env env) {
126   env->last_error.error_code = napi_ok;
127 
128   // TODO(boingoing): Should this be a callback?
129   env->last_error.engine_error_code = 0;
130   env->last_error.engine_reserved = nullptr;
131   return napi_ok;
132 }
133 
134 static inline
135 napi_status napi_set_last_error(napi_env env, napi_status error_code,
136                                 uint32_t engine_error_code = 0,
137                                 void* engine_reserved = nullptr) {
138   env->last_error.error_code = error_code;
139   env->last_error.engine_error_code = engine_error_code;
140   env->last_error.engine_reserved = engine_reserved;
141   return error_code;
142 }
143 
144 #define RETURN_STATUS_IF_FALSE(env, condition, status)                  \
145   do {                                                                  \
146     if (!(condition)) {                                                 \
147       return napi_set_last_error((env), (status));                      \
148     }                                                                   \
149   } while (0)
150 
151 #define RETURN_STATUS_IF_FALSE_WITH_PREAMBLE(env, condition, status)           \
152   do {                                                                         \
153     if (!(condition)) {                                                        \
154       return napi_set_last_error(                                              \
155           (env), try_catch.HasCaught() ? napi_pending_exception : (status));   \
156     }                                                                          \
157   } while (0)
158 
159 #define CHECK_ENV(env)          \
160   do {                          \
161     if ((env) == nullptr) {     \
162       return napi_invalid_arg;  \
163     }                           \
164   } while (0)
165 
166 #define CHECK_ARG(env, arg) \
167   RETURN_STATUS_IF_FALSE((env), ((arg) != nullptr), napi_invalid_arg)
168 
169 #define CHECK_ARG_WITH_PREAMBLE(env, arg)                  \
170   RETURN_STATUS_IF_FALSE_WITH_PREAMBLE((env),              \
171                                        ((arg) != nullptr), \
172                                        napi_invalid_arg)
173 
174 #define CHECK_MAYBE_EMPTY(env, maybe, status) \
175   RETURN_STATUS_IF_FALSE((env), !((maybe).IsEmpty()), (status))
176 
177 #define CHECK_MAYBE_EMPTY_WITH_PREAMBLE(env, maybe, status)                    \
178   RETURN_STATUS_IF_FALSE_WITH_PREAMBLE((env), !((maybe).IsEmpty()), (status))
179 
180 // NAPI_PREAMBLE is not wrapped in do..while: try_catch must have function scope
181 #define NAPI_PREAMBLE(env)                                          \
182   CHECK_ENV((env));                                                 \
183   RETURN_STATUS_IF_FALSE((env),                                     \
184       (env)->last_exception.IsEmpty() && (env)->can_call_into_js(), \
185       napi_pending_exception);                                      \
186   napi_clear_last_error((env));                                     \
187   v8impl::TryCatch try_catch((env))
188 
189 #define CHECK_TO_TYPE(env, type, context, result, src, status)                \
190   do {                                                                        \
191     CHECK_ARG((env), (src));                                                  \
192     auto maybe = v8impl::V8LocalValueFromJsValue((src))->To##type((context)); \
193     CHECK_MAYBE_EMPTY((env), maybe, (status));                                \
194     (result) = maybe.ToLocalChecked();                                        \
195   } while (0)
196 
197 #define CHECK_TO_TYPE_WITH_PREAMBLE(env, type, context, result, src, status)  \
198   do {                                                                        \
199     CHECK_ARG_WITH_PREAMBLE((env), (src));                                    \
200     auto maybe = v8impl::V8LocalValueFromJsValue((src))->To##type((context)); \
201     CHECK_MAYBE_EMPTY_WITH_PREAMBLE((env), maybe, (status));                  \
202     (result) = maybe.ToLocalChecked();                                        \
203   } while (0)
204 
205 #define CHECK_TO_FUNCTION(env, result, src)                                 \
206   do {                                                                      \
207     CHECK_ARG((env), (src));                                                \
208     v8::Local<v8::Value> v8value = v8impl::V8LocalValueFromJsValue((src));  \
209     RETURN_STATUS_IF_FALSE((env), v8value->IsFunction(), napi_invalid_arg); \
210     (result) = v8value.As<v8::Function>();                                  \
211   } while (0)
212 
213 #define CHECK_TO_OBJECT(env, context, result, src) \
214   CHECK_TO_TYPE((env), Object, (context), (result), (src), napi_object_expected)
215 
216 #define CHECK_TO_OBJECT_WITH_PREAMBLE(env, context, result, src) \
217   CHECK_TO_TYPE_WITH_PREAMBLE((env),                             \
218                               Object,                            \
219                               (context),                         \
220                               (result),                          \
221                               (src),                             \
222                               napi_object_expected)
223 
224 #define CHECK_TO_STRING(env, context, result, src) \
225   CHECK_TO_TYPE((env), String, (context), (result), (src), napi_string_expected)
226 
227 #define GET_RETURN_STATUS(env)      \
228   (!try_catch.HasCaught() ? napi_ok \
229                          : napi_set_last_error((env), napi_pending_exception))
230 
231 #define THROW_RANGE_ERROR_IF_FALSE(env, condition, error, message) \
232   do {                                                             \
233     if (!(condition)) {                                            \
234       napi_throw_range_error((env), (error), (message));           \
235       return napi_set_last_error((env), napi_generic_failure);     \
236     }                                                              \
237   } while (0)
238 
239 #define RETURN_STATUS_IF_FALSE_WITH_PREAMBLE(env, condition, status)           \
240   do {                                                                         \
241     if (!(condition)) {                                                        \
242       return napi_set_last_error(                                              \
243           (env), try_catch.HasCaught() ? napi_pending_exception : (status));   \
244     }                                                                          \
245   } while (0)
246 
247 #define CHECK_MAYBE_EMPTY_WITH_PREAMBLE(env, maybe, status)                    \
248   RETURN_STATUS_IF_FALSE_WITH_PREAMBLE((env), !((maybe).IsEmpty()), (status))
249 
250 namespace v8impl {
251 
252 //=== Conversion between V8 Handles and napi_value ========================
253 
254 // This asserts v8::Local<> will always be implemented with a single
255 // pointer field so that we can pass it around as a void*.
256 static_assert(sizeof(v8::Local<v8::Value>) == sizeof(napi_value),
257   "Cannot convert between v8::Local<v8::Value> and napi_value");
258 
JsValueFromV8LocalValue(v8::Local<v8::Value> local)259 inline napi_value JsValueFromV8LocalValue(v8::Local<v8::Value> local) {
260   return reinterpret_cast<napi_value>(*local);
261 }
262 
V8LocalValueFromJsValue(napi_value v)263 inline v8::Local<v8::Value> V8LocalValueFromJsValue(napi_value v) {
264   v8::Local<v8::Value> local;
265   memcpy(static_cast<void*>(&local), &v, sizeof(v));
266   return local;
267 }
268 
269 // Adapter for napi_finalize callbacks.
270 class Finalizer {
271  public:
272   // Some Finalizers are run during shutdown when the napi_env is destroyed,
273   // and some need to keep an explicit reference to the napi_env because they
274   // are run independently.
275   enum EnvReferenceMode {
276     kNoEnvReference,
277     kKeepEnvReference
278   };
279 
280  protected:
281   Finalizer(napi_env env,
282             napi_finalize finalize_callback,
283             void* finalize_data,
284             void* finalize_hint,
285             EnvReferenceMode refmode = kNoEnvReference)
_env(env)286     : _env(env),
287       _finalize_callback(finalize_callback),
288       _finalize_data(finalize_data),
289       _finalize_hint(finalize_hint),
290       _has_env_reference(refmode == kKeepEnvReference) {
291     if (_has_env_reference)
292       _env->Ref();
293   }
294 
~Finalizer()295   ~Finalizer() {
296     if (_has_env_reference)
297       _env->Unref();
298   }
299 
300  public:
301   static Finalizer* New(napi_env env,
302                         napi_finalize finalize_callback = nullptr,
303                         void* finalize_data = nullptr,
304                         void* finalize_hint = nullptr,
305                         EnvReferenceMode refmode = kNoEnvReference) {
306     return new Finalizer(
307         env, finalize_callback, finalize_data, finalize_hint, refmode);
308   }
309 
Delete(Finalizer * finalizer)310   static void Delete(Finalizer* finalizer) {
311     delete finalizer;
312   }
313 
314  protected:
315   napi_env _env;
316   napi_finalize _finalize_callback;
317   void* _finalize_data;
318   void* _finalize_hint;
319   bool _finalize_ran = false;
320   bool _has_env_reference = false;
321 };
322 
323 class TryCatch : public v8::TryCatch {
324  public:
TryCatch(napi_env env)325   explicit TryCatch(napi_env env)
326       : v8::TryCatch(env->isolate), _env(env) {}
327 
~TryCatch()328   ~TryCatch() {
329     if (HasCaught()) {
330       _env->last_exception.Reset(_env->isolate, Exception());
331     }
332   }
333 
334  private:
335   napi_env _env;
336 };
337 
338 }  // end of namespace v8impl
339 
340 #endif  // SRC_JS_NATIVE_API_V8_H_
341