2013-04-25 10:25:18 +00:00
|
|
|
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
|
|
|
|
// Copyright (c) 2012 Intel Corp. All rights reserved.
|
2014-04-25 09:49:37 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
2013-04-25 10:25:18 +00:00
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2014-03-16 00:30:26 +00:00
|
|
|
#include "atom/common/api/object_life_monitor.h"
|
2013-04-25 10:25:18 +00:00
|
|
|
|
2014-06-28 14:33:00 +00:00
|
|
|
#include "native_mate/compat.h"
|
|
|
|
|
2013-04-25 10:25:18 +00:00
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
// static
|
2014-06-28 14:33:00 +00:00
|
|
|
void ObjectLifeMonitor::BindTo(v8::Isolate* isolate,
|
|
|
|
v8::Handle<v8::Object> target,
|
2013-04-25 10:25:18 +00:00
|
|
|
v8::Handle<v8::Value> destructor) {
|
2014-06-28 14:33:00 +00:00
|
|
|
target->SetHiddenValue(MATE_STRING_NEW(isolate, "destructor"), destructor);
|
2013-04-25 10:25:18 +00:00
|
|
|
|
|
|
|
ObjectLifeMonitor* olm = new ObjectLifeMonitor();
|
2013-12-11 07:48:19 +00:00
|
|
|
olm->handle_.reset(target);
|
2014-06-28 14:33:00 +00:00
|
|
|
olm->handle_.SetWeak(olm, WeakCallback);
|
2013-04-25 10:25:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ObjectLifeMonitor::ObjectLifeMonitor() {
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2014-06-28 14:33:00 +00:00
|
|
|
void ObjectLifeMonitor::WeakCallback(
|
|
|
|
const v8::WeakCallbackData<v8::Object, ObjectLifeMonitor>& data) {
|
2013-04-25 10:25:18 +00:00
|
|
|
// destructor.call(object, object);
|
2014-06-28 14:33:00 +00:00
|
|
|
v8::Local<v8::Object> obj = data.GetValue();
|
|
|
|
v8::Local<v8::Function>::Cast(obj->GetHiddenValue(
|
|
|
|
MATE_STRING_NEW(data.GetIsolate(), "destructor")))->Call(obj, 0, NULL);
|
|
|
|
delete data.GetParameter();
|
2013-04-25 10:25:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace atom
|