fix: remove catch-all HandleScope (#22531)

This commit is contained in:
Jeremy Apthorp 2020-03-10 18:16:58 -07:00 committed by GitHub
parent 4bca5205bb
commit 19314d3caf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 131 additions and 75 deletions

View file

@ -16,6 +16,7 @@ WrappableBase::~WrappableBase() {
if (wrapper_.IsEmpty())
return;
v8::HandleScope scope(isolate());
GetWrapper()->SetAlignedPointerInInternalField(0, nullptr);
wrapper_.ClearWeak();
wrapper_.Reset();
@ -49,7 +50,8 @@ void WrappableBase::InitWith(v8::Isolate* isolate,
isolate_ = isolate;
wrapper->SetAlignedPointerInInternalField(0, this);
wrapper_.Reset(isolate, wrapper);
wrapper_.SetWeak(this, FirstWeakCallback, v8::WeakCallbackType::kParameter);
wrapper_.SetWeak(this, FirstWeakCallback,
v8::WeakCallbackType::kInternalFields);
// Call object._init if we have one.
v8::Local<v8::Function> init;
@ -62,24 +64,21 @@ void WrappableBase::InitWith(v8::Isolate* isolate,
// static
void WrappableBase::FirstWeakCallback(
const v8::WeakCallbackInfo<WrappableBase>& data) {
WrappableBase* wrappable = data.GetParameter();
wrappable->wrapper_.Reset();
data.SetSecondPassCallback(SecondWeakCallback);
WrappableBase* wrappable =
static_cast<WrappableBase*>(data.GetInternalField(0));
if (wrappable) {
wrappable->wrapper_.Reset();
data.SetSecondPassCallback(SecondWeakCallback);
}
}
// static
void WrappableBase::SecondWeakCallback(
const v8::WeakCallbackInfo<WrappableBase>& data) {
// Certain classes (for example api::WebContents and api::WebContentsView)
// are running JS code in the destructor, while V8 may crash when JS code
// runs inside weak callback.
//
// We work around this problem by delaying the deletion to next tick where
// garbage collection is done.
base::ThreadTaskRunnerHandle::Get()->PostNonNestableTask(
FROM_HERE,
base::BindOnce([](WrappableBase* wrappable) { delete wrappable; },
base::Unretained(data.GetParameter())));
WrappableBase* wrappable =
static_cast<WrappableBase*>(data.GetInternalField(0));
if (wrappable)
delete wrappable;
}
namespace internal {