Add v8_util.setDestructor.
This API can monitor when the object is GCed by V8, this is required by the RPC API since we want to make sure the remote object got destroied when object in renderer is GCed.
This commit is contained in:
parent
a7ddf57620
commit
cc37431a1f
4 changed files with 94 additions and 0 deletions
2
atom.gyp
2
atom.gyp
|
@ -48,6 +48,8 @@
|
|||
'common/api/atom_bindings.h',
|
||||
'common/api/atom_extensions.cc',
|
||||
'common/api/atom_extensions.h',
|
||||
'common/api/object_life_monitor.cc',
|
||||
'common/api/object_life_monitor.h',
|
||||
'common/node_bindings.cc',
|
||||
'common/node_bindings.h',
|
||||
'common/node_bindings_mac.h',
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "common/api/object_life_monitor.h"
|
||||
#include "vendor/node/src/node.h"
|
||||
|
||||
namespace atom {
|
||||
|
@ -38,6 +39,11 @@ v8::Handle<v8::Value> GetObjectHash(const v8::Arguments& args) {
|
|||
args[0]->ToObject()->GetIdentityHash()));
|
||||
}
|
||||
|
||||
v8::Handle<v8::Value> SetDestructor(const v8::Arguments& args) {
|
||||
ObjectLifeMonitor::BindTo(args[0]->ToObject(), args[1]);
|
||||
return v8::Undefined();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void InitializeV8Util(v8::Handle<v8::Object> target) {
|
||||
|
@ -45,6 +51,7 @@ void InitializeV8Util(v8::Handle<v8::Object> target) {
|
|||
NODE_SET_METHOD(target, "getHiddenValue", GetHiddenValue);
|
||||
NODE_SET_METHOD(target, "setHiddenValue", SetHiddenValue);
|
||||
NODE_SET_METHOD(target, "getObjectHash", GetObjectHash);
|
||||
NODE_SET_METHOD(target, "setDestructor", SetDestructor);
|
||||
}
|
||||
|
||||
} // namespace api
|
||||
|
|
51
common/api/object_life_monitor.cc
Normal file
51
common/api/object_life_monitor.cc
Normal file
|
@ -0,0 +1,51 @@
|
|||
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
|
||||
// Copyright (c) 2012 Intel Corp. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "common/api/object_life_monitor.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
// static
|
||||
void ObjectLifeMonitor::BindTo(v8::Handle<v8::Object> target,
|
||||
v8::Handle<v8::Value> destructor) {
|
||||
v8::Isolate* isolate = v8::Isolate::GetCurrent();
|
||||
target->SetHiddenValue(v8::String::New("destructor"), destructor);
|
||||
|
||||
ObjectLifeMonitor* olm = new ObjectLifeMonitor();
|
||||
olm->handle_ = v8::Persistent<v8::Object>::New(isolate, target);
|
||||
olm->handle_.MakeWeak(isolate, olm, WeakCallback);
|
||||
}
|
||||
|
||||
ObjectLifeMonitor::ObjectLifeMonitor() {
|
||||
}
|
||||
|
||||
ObjectLifeMonitor::~ObjectLifeMonitor() {
|
||||
if (!handle_.IsEmpty()) {
|
||||
v8::Isolate* isolate = v8::Isolate::GetCurrent();
|
||||
handle_.ClearWeak(isolate);
|
||||
handle_.Dispose(isolate);
|
||||
handle_.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
// static
|
||||
void ObjectLifeMonitor::WeakCallback(v8::Isolate* isolate,
|
||||
v8::Persistent<v8::Value> value,
|
||||
void *data) {
|
||||
// destructor.call(object, object);
|
||||
{
|
||||
v8::HandleScope scope;
|
||||
|
||||
v8::Local<v8::Object> obj = value->ToObject();
|
||||
v8::Local<v8::Value> args[] = { obj };
|
||||
v8::Local<v8::Function>::Cast(obj->GetHiddenValue(
|
||||
v8::String::New("destructor")))->Call(obj, 1, args);
|
||||
}
|
||||
|
||||
ObjectLifeMonitor* obj = static_cast<ObjectLifeMonitor*>(data);
|
||||
delete obj;
|
||||
}
|
||||
|
||||
} // namespace atom
|
34
common/api/object_life_monitor.h
Normal file
34
common/api/object_life_monitor.h
Normal file
|
@ -0,0 +1,34 @@
|
|||
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
|
||||
// Copyright (c) 2012 Intel Corp. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef ATOM_COMMON_API_OBJECT_LIFE_MONITOR_H_
|
||||
#define ATOM_COMMON_API_OBJECT_LIFE_MONITOR_H_
|
||||
|
||||
#include "base/basictypes.h"
|
||||
#include "v8/include/v8.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
class ObjectLifeMonitor {
|
||||
public:
|
||||
static void BindTo(v8::Handle<v8::Object> target,
|
||||
v8::Handle<v8::Value> destructor);
|
||||
|
||||
private:
|
||||
ObjectLifeMonitor();
|
||||
virtual ~ObjectLifeMonitor();
|
||||
|
||||
static void WeakCallback(v8::Isolate* isolate,
|
||||
v8::Persistent<v8::Value> value,
|
||||
void *data);
|
||||
|
||||
v8::Persistent<v8::Object> handle_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(ObjectLifeMonitor);
|
||||
};
|
||||
|
||||
} // namespace atom
|
||||
|
||||
#endif // ATOM_COMMON_API_OBJECT_LIFE_MONITOR_H_
|
Loading…
Reference in a new issue