electron/native_mate/wrappable.cc

84 lines
2.2 KiB
C++
Raw Normal View History

2014-04-15 03:04:36 +00:00
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE.chromium file.
#include "native_mate/wrappable.h"
#include "base/logging.h"
2014-05-27 00:47:06 +00:00
#include "native_mate/dictionary.h"
2014-04-15 03:04:36 +00:00
#include "native_mate/object_template_builder.h"
namespace mate {
2016-04-25 01:17:39 +00:00
WrappableBase::WrappableBase()
: isolate_(nullptr), high_memory_(false) {
2014-04-15 03:04:36 +00:00
}
2016-04-25 01:17:39 +00:00
WrappableBase::~WrappableBase() {
if (wrapper_.IsEmpty())
return;
GetWrapper()->SetAlignedPointerInInternalField(0, nullptr);
wrapper_.ClearWeak();
2015-08-27 07:50:38 +00:00
wrapper_.Reset();
2014-04-15 03:04:36 +00:00
}
2016-04-25 01:17:39 +00:00
v8::Local<v8::Object> WrappableBase::GetWrapper() {
DCHECK(!wrapper_.IsEmpty());
if (!wrapper_.IsEmpty())
return v8::Local<v8::Object>::New(isolate_, wrapper_);
else
return v8::Local<v8::Object>();
2016-04-25 01:17:39 +00:00
}
2016-04-25 01:17:39 +00:00
void WrappableBase::InitWith(v8::Isolate* isolate,
v8::Local<v8::Object> wrapper) {
CHECK(wrapper_.IsEmpty());
2015-06-23 09:08:52 +00:00
isolate_ = isolate;
2015-08-27 07:50:38 +00:00
wrapper->SetAlignedPointerInInternalField(0, this);
wrapper_.Reset(isolate, wrapper);
wrapper_.SetWeak(this, FirstWeakCallback, v8::WeakCallbackType::kParameter);
wrapper_.MarkIndependent();
2014-05-27 00:47:06 +00:00
// Call object._init if we have one.
2015-05-22 11:11:02 +00:00
v8::Local<v8::Function> init;
2014-05-27 00:47:06 +00:00
if (Dictionary(isolate, wrapper).Get("_init", &init))
2015-08-27 07:50:38 +00:00
init->Call(wrapper, 0, nullptr);
2015-02-13 03:37:55 +00:00
AfterInit(isolate);
}
2014-04-16 03:58:17 +00:00
// static
2016-04-25 01:17:39 +00:00
void WrappableBase::FirstWeakCallback(
const v8::WeakCallbackInfo<WrappableBase>& data) {
WrappableBase* wrappable = data.GetParameter();
2015-08-27 07:50:38 +00:00
wrappable->wrapper_.Reset();
if (wrappable->high_memory_) {
delete wrappable;
} else {
data.SetSecondPassCallback(SecondWeakCallback);
}
2015-08-27 07:50:38 +00:00
}
2016-04-25 01:17:39 +00:00
// static
void WrappableBase::SecondWeakCallback(
const v8::WeakCallbackInfo<WrappableBase>& data) {
WrappableBase* wrappable = data.GetParameter();
2015-08-27 07:50:38 +00:00
delete wrappable;
2014-04-15 03:04:36 +00:00
}
namespace internal {
2015-05-22 11:11:02 +00:00
void* FromV8Impl(v8::Isolate* isolate, v8::Local<v8::Value> val) {
2014-04-15 03:04:36 +00:00
if (!val->IsObject())
2016-04-25 01:17:39 +00:00
return nullptr;
2015-05-22 11:11:02 +00:00
v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(val);
2015-02-11 14:09:42 +00:00
if (obj->InternalFieldCount() != 1)
2016-04-25 01:17:39 +00:00
return nullptr;
return obj->GetAlignedPointerFromInternalField(0);
2014-04-15 03:04:36 +00:00
}
} // namespace internal
} // namespace mate