This source file includes following definitions.
- GetObjectTemplateBuilder
- WeakCallback
- GetWrapperImpl
- FromV8Impl
#include "gin/wrappable.h"
#include "base/logging.h"
#include "gin/object_template_builder.h"
#include "gin/per_isolate_data.h"
namespace gin {
WrappableBase::WrappableBase() {
}
WrappableBase::~WrappableBase() {
wrapper_.Reset();
}
ObjectTemplateBuilder WrappableBase::GetObjectTemplateBuilder(
v8::Isolate* isolate) {
return ObjectTemplateBuilder(isolate);
}
void WrappableBase::WeakCallback(
const v8::WeakCallbackData<v8::Object, WrappableBase>& data) {
WrappableBase* wrappable = data.GetParameter();
wrappable->wrapper_.Reset();
delete wrappable;
}
v8::Handle<v8::Object> WrappableBase::GetWrapperImpl(v8::Isolate* isolate,
WrapperInfo* info) {
if (!wrapper_.IsEmpty()) {
return v8::Local<v8::Object>::New(isolate, wrapper_);
}
PerIsolateData* data = PerIsolateData::From(isolate);
v8::Local<v8::ObjectTemplate> templ = data->GetObjectTemplate(info);
if (templ.IsEmpty()) {
templ = GetObjectTemplateBuilder(isolate).Build();
CHECK(!templ.IsEmpty());
data->SetObjectTemplate(info, templ);
}
CHECK_EQ(kNumberOfInternalFields, templ->InternalFieldCount());
v8::Handle<v8::Object> wrapper = templ->NewInstance();
if (wrapper.IsEmpty()) {
delete this;
return wrapper;
}
wrapper->SetAlignedPointerInInternalField(kWrapperInfoIndex, info);
wrapper->SetAlignedPointerInInternalField(kEncodedValueIndex, this);
wrapper_.Reset(isolate, wrapper);
wrapper_.SetWeak(this, WeakCallback);
return wrapper;
}
namespace internal {
void* FromV8Impl(v8::Isolate* isolate, v8::Handle<v8::Value> val,
WrapperInfo* wrapper_info) {
if (!val->IsObject())
return NULL;
v8::Handle<v8::Object> obj = v8::Handle<v8::Object>::Cast(val);
WrapperInfo* info = WrapperInfo::From(obj);
if (!info)
return NULL;
if (info != wrapper_info)
return NULL;
return obj->GetAlignedPointerFromInternalField(kEncodedValueIndex);
}
}
}