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