fix: remove catch-all HandleScope (#22531)
This commit is contained in:
parent
4bca5205bb
commit
19314d3caf
21 changed files with 131 additions and 75 deletions
|
@ -38,6 +38,7 @@ v8::Local<v8::Object> CreateEvent(v8::Isolate* isolate,
|
|||
}
|
||||
|
||||
v8::Local<v8::Context> context = isolate->GetCurrentContext();
|
||||
CHECK(!context.IsEmpty());
|
||||
v8::Local<v8::Object> event =
|
||||
v8::Local<v8::ObjectTemplate>::New(isolate, event_template)
|
||||
->NewInstance(context)
|
||||
|
|
|
@ -55,13 +55,16 @@ class TrackableObject : public TrackableObjectBase, public EventEmitter<T> {
|
|||
public:
|
||||
// Mark the JS object as destroyed.
|
||||
void MarkDestroyed() {
|
||||
v8::HandleScope scope(gin_helper::Wrappable<T>::isolate());
|
||||
v8::Local<v8::Object> wrapper = gin_helper::Wrappable<T>::GetWrapper();
|
||||
if (!wrapper.IsEmpty()) {
|
||||
wrapper->SetAlignedPointerInInternalField(0, nullptr);
|
||||
gin_helper::WrappableBase::wrapper_.ClearWeak();
|
||||
}
|
||||
}
|
||||
|
||||
bool IsDestroyed() {
|
||||
v8::HandleScope scope(gin_helper::Wrappable<T>::isolate());
|
||||
v8::Local<v8::Object> wrapper = gin_helper::Wrappable<T>::GetWrapper();
|
||||
return wrapper->InternalFieldCount() == 0 ||
|
||||
wrapper->GetAlignedPointerFromInternalField(0) == nullptr;
|
||||
|
@ -72,6 +75,7 @@ class TrackableObject : public TrackableObjectBase, public EventEmitter<T> {
|
|||
if (!weak_map_)
|
||||
return nullptr;
|
||||
|
||||
v8::HandleScope scope(isolate);
|
||||
v8::MaybeLocal<v8::Object> object = weak_map_->Get(isolate, id);
|
||||
if (object.IsEmpty())
|
||||
return nullptr;
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -52,6 +52,8 @@ class WrappableBase {
|
|||
// Helper to init with arguments.
|
||||
void InitWithArgs(gin::Arguments* args);
|
||||
|
||||
v8::Global<v8::Object> wrapper_; // Weak
|
||||
|
||||
private:
|
||||
static void FirstWeakCallback(
|
||||
const v8::WeakCallbackInfo<WrappableBase>& data);
|
||||
|
@ -59,7 +61,6 @@ class WrappableBase {
|
|||
const v8::WeakCallbackInfo<WrappableBase>& data);
|
||||
|
||||
v8::Isolate* isolate_ = nullptr;
|
||||
v8::Global<v8::Object> wrapper_; // Weak
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(WrappableBase);
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue