Use V8's new SetWeak method
This commit is contained in:
parent
f7c75d36ba
commit
bd64f5ced2
5 changed files with 70 additions and 37 deletions
|
@ -5,32 +5,52 @@
|
||||||
|
|
||||||
#include "atom/common/api/object_life_monitor.h"
|
#include "atom/common/api/object_life_monitor.h"
|
||||||
|
|
||||||
#include "native_mate/compat.h"
|
#include "base/bind.h"
|
||||||
|
#include "base/message_loop/message_loop.h"
|
||||||
|
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
|
||||||
// static
|
// static
|
||||||
void ObjectLifeMonitor::BindTo(v8::Isolate* isolate,
|
void ObjectLifeMonitor::BindTo(v8::Isolate* isolate,
|
||||||
v8::Local<v8::Object> target,
|
v8::Local<v8::Object> target,
|
||||||
v8::Local<v8::Value> destructor) {
|
v8::Local<v8::Function> destructor) {
|
||||||
target->SetHiddenValue(MATE_STRING_NEW(isolate, "destructor"), destructor);
|
new ObjectLifeMonitor(isolate, target, destructor);
|
||||||
|
|
||||||
ObjectLifeMonitor* olm = new ObjectLifeMonitor();
|
|
||||||
olm->handle_.reset(isolate, target);
|
|
||||||
olm->handle_.SetWeak(olm, WeakCallback);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ObjectLifeMonitor::ObjectLifeMonitor() {
|
ObjectLifeMonitor::ObjectLifeMonitor(v8::Isolate* isolate,
|
||||||
|
v8::Local<v8::Object> target,
|
||||||
|
v8::Local<v8::Function> destructor)
|
||||||
|
: isolate_(isolate),
|
||||||
|
context_(isolate, isolate->GetCurrentContext()),
|
||||||
|
target_(isolate, target),
|
||||||
|
destructor_(isolate, destructor),
|
||||||
|
weak_ptr_factory_(this) {
|
||||||
|
target_.SetWeak(this, OnObjectGC, v8::WeakCallbackType::kParameter);
|
||||||
}
|
}
|
||||||
|
|
||||||
// static
|
// static
|
||||||
void ObjectLifeMonitor::WeakCallback(
|
void ObjectLifeMonitor::OnObjectGC(
|
||||||
const v8::WeakCallbackData<v8::Object, ObjectLifeMonitor>& data) {
|
const v8::WeakCallbackInfo<ObjectLifeMonitor>& data) {
|
||||||
// destructor.call(object, object);
|
// Usually FirstWeakCallback should do nothing other than reset |object_|
|
||||||
v8::Local<v8::Object> obj = data.GetValue();
|
// and then set a second weak callback to run later. We can sidestep that,
|
||||||
v8::Local<v8::Function>::Cast(obj->GetHiddenValue(
|
// because posting a task to the current message loop is all but free - but
|
||||||
MATE_STRING_NEW(data.GetIsolate(), "destructor")))->Call(obj, 0, NULL);
|
// DO NOT add any more work to this method. The only acceptable place to add
|
||||||
delete data.GetParameter();
|
// code is RunCallback.
|
||||||
|
ObjectLifeMonitor* self = data.GetParameter();
|
||||||
|
self->target_.Reset();
|
||||||
|
base::MessageLoop::current()->PostTask(
|
||||||
|
FROM_HERE, base::Bind(&ObjectLifeMonitor::RunCallback,
|
||||||
|
self->weak_ptr_factory_.GetWeakPtr()));
|
||||||
|
}
|
||||||
|
|
||||||
|
void ObjectLifeMonitor::RunCallback() {
|
||||||
|
v8::HandleScope handle_scope(isolate_);
|
||||||
|
v8::Local<v8::Context> context = v8::Local<v8::Context>::New(
|
||||||
|
isolate_, context_);
|
||||||
|
v8::Context::Scope context_scope(context);
|
||||||
|
v8::Local<v8::Function>::New(isolate_, destructor_)->Call(
|
||||||
|
context->Global(), 0, nullptr);
|
||||||
|
delete this;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace atom
|
} // namespace atom
|
||||||
|
|
|
@ -6,7 +6,8 @@
|
||||||
#define ATOM_COMMON_API_OBJECT_LIFE_MONITOR_H_
|
#define ATOM_COMMON_API_OBJECT_LIFE_MONITOR_H_
|
||||||
|
|
||||||
#include "base/basictypes.h"
|
#include "base/basictypes.h"
|
||||||
#include "native_mate/scoped_persistent.h"
|
#include "base/memory/weak_ptr.h"
|
||||||
|
#include "v8/include/v8.h"
|
||||||
|
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
|
||||||
|
@ -14,15 +15,23 @@ class ObjectLifeMonitor {
|
||||||
public:
|
public:
|
||||||
static void BindTo(v8::Isolate* isolate,
|
static void BindTo(v8::Isolate* isolate,
|
||||||
v8::Local<v8::Object> target,
|
v8::Local<v8::Object> target,
|
||||||
v8::Local<v8::Value> destructor);
|
v8::Local<v8::Function> destructor);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ObjectLifeMonitor();
|
ObjectLifeMonitor(v8::Isolate* isolate,
|
||||||
|
v8::Local<v8::Object> target,
|
||||||
|
v8::Local<v8::Function> destructor);
|
||||||
|
|
||||||
static void WeakCallback(
|
static void OnObjectGC(const v8::WeakCallbackInfo<ObjectLifeMonitor>& data);
|
||||||
const v8::WeakCallbackData<v8::Object, ObjectLifeMonitor>& data);
|
|
||||||
|
|
||||||
mate::ScopedPersistent<v8::Object> handle_;
|
void RunCallback();
|
||||||
|
|
||||||
|
v8::Isolate* isolate_;
|
||||||
|
v8::Global<v8::Context> context_;
|
||||||
|
v8::Global<v8::Object> target_;
|
||||||
|
v8::Global<v8::Function> destructor_;
|
||||||
|
|
||||||
|
base::WeakPtrFactory<ObjectLifeMonitor> weak_ptr_factory_;
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(ObjectLifeMonitor);
|
DISALLOW_COPY_AND_ASSIGN(ObjectLifeMonitor);
|
||||||
};
|
};
|
||||||
|
|
|
@ -10,6 +10,23 @@
|
||||||
|
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
struct ObjectKey {
|
||||||
|
ObjectKey(int id, IDWeakMap* map) : id(id), map(map) {}
|
||||||
|
int id;
|
||||||
|
IDWeakMap* map;
|
||||||
|
};
|
||||||
|
|
||||||
|
void OnObjectGC(const v8::WeakCallbackInfo<ObjectKey>& data) {
|
||||||
|
ObjectKey* key = data.GetParameter();
|
||||||
|
LOG(ERROR) << "OnObjectGC: " << key->id;
|
||||||
|
key->map->Remove(key->id);
|
||||||
|
delete key;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
IDWeakMap::IDWeakMap() : next_id_(0) {
|
IDWeakMap::IDWeakMap() : next_id_(0) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,11 +35,9 @@ IDWeakMap::~IDWeakMap() {
|
||||||
|
|
||||||
int32_t IDWeakMap::Add(v8::Isolate* isolate, v8::Local<v8::Object> object) {
|
int32_t IDWeakMap::Add(v8::Isolate* isolate, v8::Local<v8::Object> object) {
|
||||||
int32_t id = GetNextID();
|
int32_t id = GetNextID();
|
||||||
object->SetHiddenValue(mate::StringToSymbol(isolate, "IDWeakMapKey"),
|
|
||||||
mate::Converter<int32_t>::ToV8(isolate, id));
|
|
||||||
|
|
||||||
auto global = make_linked_ptr(new v8::Global<v8::Object>(isolate, object));
|
auto global = make_linked_ptr(new v8::Global<v8::Object>(isolate, object));
|
||||||
global->SetWeak(this, &WeakCallback);
|
ObjectKey* key = new ObjectKey(id, this);
|
||||||
|
global->SetWeak(key, OnObjectGC, v8::WeakCallbackType::kParameter);
|
||||||
map_[id] = global;
|
map_[id] = global;
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
@ -71,12 +86,4 @@ int32_t IDWeakMap::GetNextID() {
|
||||||
return ++next_id_;
|
return ++next_id_;
|
||||||
}
|
}
|
||||||
|
|
||||||
// static
|
|
||||||
void IDWeakMap::WeakCallback(
|
|
||||||
const v8::WeakCallbackData<v8::Object, IDWeakMap>& data) {
|
|
||||||
int32_t id = data.GetValue()->GetHiddenValue(
|
|
||||||
mate::StringToV8(data.GetIsolate(), "IDWeakMapKey"))->Int32Value();
|
|
||||||
data.GetParameter()->Remove(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace atom
|
} // namespace atom
|
||||||
|
|
|
@ -44,9 +44,6 @@ class IDWeakMap {
|
||||||
// Returns next available ID.
|
// Returns next available ID.
|
||||||
int32_t GetNextID();
|
int32_t GetNextID();
|
||||||
|
|
||||||
static void WeakCallback(
|
|
||||||
const v8::WeakCallbackData<v8::Object, IDWeakMap>& data);
|
|
||||||
|
|
||||||
// ID of next stored object.
|
// ID of next stored object.
|
||||||
int32_t next_id_;
|
int32_t next_id_;
|
||||||
|
|
||||||
|
|
2
vendor/native_mate
vendored
2
vendor/native_mate
vendored
|
@ -1 +1 @@
|
||||||
Subproject commit f5e34deb1a5226b4e7e620cb65fce0225471d4d9
|
Subproject commit 8ca005eb41591f583ebab804945311903f866ad6
|
Loading…
Add table
Add a link
Reference in a new issue