electron/native_mate/wrappable.h

101 lines
3 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.
#ifndef NATIVE_MATE_WRAPPABLE_H_
#define NATIVE_MATE_WRAPPABLE_H_
2016-08-02 08:01:19 +00:00
#include "base/bind.h"
2014-04-15 03:04:36 +00:00
#include "native_mate/compat.h"
#include "native_mate/converter.h"
2016-08-02 08:01:19 +00:00
#include "native_mate/constructor.h"
#include "gin/per_isolate_data.h"
2014-04-15 03:04:36 +00:00
namespace mate {
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
} // namespace internal
2016-04-25 01:17:39 +00:00
template<typename T>
class Wrappable : public WrappableBase {
public:
Wrappable() {}
2016-08-02 08:01:19 +00:00
template<typename Sig>
static void SetConstructor(v8::Isolate* isolate,
const base::Callback<Sig>& constructor) {
v8::Local<v8::FunctionTemplate> templ = CreateFunctionTemplate(
isolate, base::Bind(&internal::InvokeNew<Sig>, constructor));
templ->InstanceTemplate()->SetInternalFieldCount(1);
T::BuildPrototype(isolate, templ);
gin::PerIsolateData::From(isolate)->SetFunctionTemplate(
&kWrapperInfo, templ);
2016-08-02 08:01:19 +00:00
}
static v8::Local<v8::FunctionTemplate> GetConstructor(v8::Isolate* isolate) {
2016-04-25 01:17:39 +00:00
// Fill the object template.
auto data = gin::PerIsolateData::From(isolate);
auto templ = data->GetFunctionTemplate(&kWrapperInfo);
if (templ.IsEmpty()) {
templ = v8::FunctionTemplate::New(isolate);
2016-08-02 07:08:00 +00:00
templ->InstanceTemplate()->SetInternalFieldCount(1);
T::BuildPrototype(isolate, templ);
data->SetFunctionTemplate(&kWrapperInfo, templ);
2016-04-25 01:17:39 +00:00
}
return templ;
}
protected:
// Init the class with T::BuildPrototype.
void Init(v8::Isolate* isolate) {
v8::Local<v8::FunctionTemplate> templ = GetConstructor(isolate);
2016-04-25 01:17:39 +00:00
// |wrapper| may be empty in some extreme cases, e.g., when
// Object.prototype.constructor is overwritten.
v8::Local<v8::Object> wrapper;
2016-08-02 07:08:00 +00:00
if (!templ->InstanceTemplate()->NewInstance(
isolate->GetCurrentContext()).ToLocal(&wrapper)) {
2016-04-25 01:17:39 +00:00
// The current wrappable object will be no longer managed by V8. Delete
// this now.
delete this;
return;
}
InitWith(isolate, wrapper);
}
private:
static gin::WrapperInfo kWrapperInfo;
2014-04-15 03:04:36 +00:00
DISALLOW_COPY_AND_ASSIGN(Wrappable);
};
2016-04-25 01:17:39 +00:00
// static
template<typename T>
gin::WrapperInfo Wrappable<T>::kWrapperInfo = { gin::kEmbedderNativeGin };
2014-04-15 03:04:36 +00:00
2014-04-16 03:58:17 +00:00
// This converter handles any subclass of Wrappable.
2016-04-25 01:17:39 +00:00
template <typename T>
struct Converter<T*,
typename std::enable_if<
std::is_convertible<T*, WrappableBase*>::value>::type> {
2015-05-22 11:11:02 +00:00
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, T* val) {
if (val)
2016-04-25 01:17:39 +00:00
return val->GetWrapper();
else
return v8::Null(isolate);
2014-04-15 03:04:36 +00:00
}
2015-05-22 11:11:02 +00:00
static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val, T** out) {
2016-04-25 01:17:39 +00:00
*out = static_cast<T*>(static_cast<WrappableBase*>(
2014-04-15 03:04:36 +00:00
internal::FromV8Impl(isolate, val)));
2016-04-25 01:17:39 +00:00
return *out != nullptr;
2014-04-15 03:04:36 +00:00
}
};
} // namespace mate
#endif // NATIVE_MATE_WRAPPABLE_H_