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 {
|
|
|
|
|
2015-06-23 09:08:52 +00:00
|
|
|
Wrappable::Wrappable() : isolate_(NULL) {
|
2014-04-15 03:04:36 +00:00
|
|
|
}
|
|
|
|
|
2014-04-16 03:58:17 +00:00
|
|
|
Wrappable::~Wrappable() {
|
2016-02-02 11:50:27 +00:00
|
|
|
if (!wrapper_.IsEmpty())
|
|
|
|
GetWrapper(isolate())->SetAlignedPointerInInternalField(0, nullptr);
|
2015-08-27 07:50:38 +00:00
|
|
|
wrapper_.Reset();
|
2014-04-15 03:04:36 +00:00
|
|
|
}
|
|
|
|
|
2015-05-22 11:11:02 +00:00
|
|
|
void Wrappable::Wrap(v8::Isolate* isolate, v8::Local<v8::Object> wrapper) {
|
2014-04-22 07:57:23 +00:00
|
|
|
if (!wrapper_.IsEmpty())
|
|
|
|
return;
|
|
|
|
|
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);
|
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 02:36:54 +00:00
|
|
|
}
|
|
|
|
|
2014-04-16 03:58:17 +00:00
|
|
|
// static
|
|
|
|
void Wrappable::BuildPrototype(v8::Isolate* isolate,
|
2015-05-22 11:11:02 +00:00
|
|
|
v8::Local<v8::ObjectTemplate> prototype) {
|
2014-04-16 03:58:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ObjectTemplateBuilder Wrappable::GetObjectTemplateBuilder(
|
2014-04-15 03:04:36 +00:00
|
|
|
v8::Isolate* isolate) {
|
|
|
|
return ObjectTemplateBuilder(isolate);
|
|
|
|
}
|
|
|
|
|
2015-08-27 07:50:38 +00:00
|
|
|
void Wrappable::FirstWeakCallback(const v8::WeakCallbackInfo<Wrappable>& data) {
|
|
|
|
Wrappable* wrappable = data.GetParameter();
|
|
|
|
wrappable->wrapper_.Reset();
|
|
|
|
data.SetSecondPassCallback(SecondWeakCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Wrappable::SecondWeakCallback(
|
|
|
|
const v8::WeakCallbackInfo<Wrappable>& data) {
|
|
|
|
Wrappable* wrappable = data.GetParameter();
|
|
|
|
delete wrappable;
|
2014-04-15 03:04:36 +00:00
|
|
|
}
|
|
|
|
|
2015-05-22 11:11:02 +00:00
|
|
|
v8::Local<v8::Object> Wrappable::GetWrapper(v8::Isolate* isolate) {
|
2014-04-22 07:57:23 +00:00
|
|
|
if (!wrapper_.IsEmpty())
|
2014-04-15 03:04:36 +00:00
|
|
|
return MATE_PERSISTENT_TO_LOCAL(v8::Object, isolate, wrapper_);
|
|
|
|
|
|
|
|
v8::Local<v8::ObjectTemplate> templ =
|
|
|
|
GetObjectTemplateBuilder(isolate).Build();
|
|
|
|
CHECK(!templ.IsEmpty());
|
|
|
|
CHECK_EQ(1, templ->InternalFieldCount());
|
2015-08-27 07:50:38 +00:00
|
|
|
v8::Local<v8::Object> wrapper;
|
|
|
|
// |wrapper| may be empty in some extreme cases, e.g., when
|
|
|
|
// Object.prototype.constructor is overwritten.
|
|
|
|
if (!templ->NewInstance(isolate->GetCurrentContext()).ToLocal(&wrapper)) {
|
|
|
|
// The current wrappable object will be no longer managed by V8. Delete this
|
|
|
|
// now.
|
|
|
|
delete this;
|
|
|
|
return wrapper;
|
|
|
|
}
|
2014-04-16 02:36:54 +00:00
|
|
|
Wrap(isolate, wrapper);
|
2014-04-15 03:04:36 +00:00
|
|
|
return wrapper;
|
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
|
|
|
return NULL;
|
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)
|
|
|
|
return NULL;
|
2014-04-15 03:04:36 +00:00
|
|
|
return MATE_GET_INTERNAL_FIELD_POINTER(obj, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
|
|
|
|
} // namespace mate
|