src: do not crash when accessing empty WeakRefs
Making `.incRef()` and `.decRef()` fail silently leads to better error messages when trying to access the underlying value (as opposed to crashing inside these methods). Refs: https://github.com/nodejs/node/pull/25461#issuecomment-524481482 PR-URL: https://github.com/nodejs/node/pull/29289 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Gus Caplan <me@gus.host>
This commit is contained in:
parent
0f3aa81eab
commit
2cc757f0c7
@ -191,14 +191,16 @@ class WeakReference : public BaseObject {
|
|||||||
|
|
||||||
static void IncRef(const FunctionCallbackInfo<Value>& args) {
|
static void IncRef(const FunctionCallbackInfo<Value>& args) {
|
||||||
WeakReference* weak_ref = Unwrap<WeakReference>(args.Holder());
|
WeakReference* weak_ref = Unwrap<WeakReference>(args.Holder());
|
||||||
if (weak_ref->reference_count_ == 0) weak_ref->target_.ClearWeak();
|
|
||||||
weak_ref->reference_count_++;
|
weak_ref->reference_count_++;
|
||||||
|
if (weak_ref->target_.IsEmpty()) return;
|
||||||
|
if (weak_ref->reference_count_ == 1) weak_ref->target_.ClearWeak();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void DecRef(const FunctionCallbackInfo<Value>& args) {
|
static void DecRef(const FunctionCallbackInfo<Value>& args) {
|
||||||
WeakReference* weak_ref = Unwrap<WeakReference>(args.Holder());
|
WeakReference* weak_ref = Unwrap<WeakReference>(args.Holder());
|
||||||
CHECK_GE(weak_ref->reference_count_, 1);
|
CHECK_GE(weak_ref->reference_count_, 1);
|
||||||
weak_ref->reference_count_--;
|
weak_ref->reference_count_--;
|
||||||
|
if (weak_ref->target_.IsEmpty()) return;
|
||||||
if (weak_ref->reference_count_ == 0) weak_ref->target_.SetWeak();
|
if (weak_ref->reference_count_ == 0) weak_ref->target_.SetWeak();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user