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