Skip to content

Commit 2b2ad96

Browse files
Gabriel SchulhofBethGriggs
Gabriel Schulhof
authored andcommittedMar 20, 2019
n-api: finalize during second-pass callback
Calling into the engine from a weak callback is unsafe, however, the engine offers a way to attach a second-pass weak callback which gets called when it is safe to call into JavaScript. This moves the point at which the N-API finalize callback gets called to this latter point. Fixes: #25927 PR-URL: #25992 Backport-PR-URL: #26060 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
1 parent ac9b8f7 commit 2b2ad96

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed
 

‎src/node_api.cc

+15
Original file line numberDiff line numberDiff line change
@@ -448,10 +448,25 @@ class Reference : private Finalizer {
448448
}
449449

450450
private:
451+
// The N-API finalizer callback may make calls into the engine. V8's heap is
452+
// not in a consistent state during the weak callback, and therefore it does
453+
// not support calls back into it. However, it provides a mechanism for adding
454+
// a finalizer which may make calls back into the engine by allowing us to
455+
// attach such a second-pass finalizer from the first pass finalizer. Thus,
456+
// we do that here to ensure that the N-API finalizer callback is free to call
457+
// into the engine.
451458
static void FinalizeCallback(const v8::WeakCallbackInfo<Reference>& data) {
452459
Reference* reference = data.GetParameter();
460+
461+
// The reference must be reset during the first pass.
453462
reference->_persistent.Reset();
454463

464+
data.SetSecondPassCallback(SecondPassCallback);
465+
}
466+
467+
static void SecondPassCallback(const v8::WeakCallbackInfo<Reference>& data) {
468+
Reference* reference = data.GetParameter();
469+
455470
// Check before calling the finalize callback, because the callback might
456471
// delete it.
457472
bool delete_self = reference->_delete_self;

0 commit comments

Comments
 (0)
Please sign in to comment.