Remove the Constructor class
This commit is contained in:
parent
a259d10bcb
commit
2d26eebca8
6 changed files with 103 additions and 259 deletions
|
@ -10,14 +10,10 @@
|
||||||
#define NATIVE_MATE_WRAPPABLE_CLASS_H_
|
#define NATIVE_MATE_WRAPPABLE_CLASS_H_
|
||||||
|
|
||||||
#include "base/bind.h"
|
#include "base/bind.h"
|
||||||
#include "base/compiler_specific.h"
|
|
||||||
#include "native_mate/wrappable.h"
|
|
||||||
#include "native_mate/function_template.h"
|
#include "native_mate/function_template.h"
|
||||||
|
|
||||||
namespace mate {
|
namespace mate {
|
||||||
|
|
||||||
class WrappableBase;
|
|
||||||
|
|
||||||
namespace internal {
|
namespace internal {
|
||||||
|
|
||||||
// This set of templates invokes a base::Callback by converting the Arguments
|
// This set of templates invokes a base::Callback by converting the Arguments
|
||||||
|
@ -120,37 +116,8 @@ inline WrappableBase* InvokeFactory(
|
||||||
return callback.Run(a1, a2, a3, a4, a5, a6);
|
return callback.Run(a1, a2, a3, a4, a5, a6);
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace internal
|
template<typename Sig>
|
||||||
|
MATE_METHOD_RETURN_TYPE InvokeNew(const base::Callback<Sig>& factory,
|
||||||
|
|
||||||
template<typename T, typename Sig>
|
|
||||||
class Constructor {
|
|
||||||
public:
|
|
||||||
typedef base::Callback<Sig> WrappableFactoryFunction;
|
|
||||||
|
|
||||||
Constructor(const base::StringPiece& name) : name_(name) {}
|
|
||||||
virtual ~Constructor() {
|
|
||||||
MATE_PERSISTENT_RESET(constructor_);
|
|
||||||
}
|
|
||||||
|
|
||||||
v8::Local<v8::FunctionTemplate> GetFunctionTemplate(
|
|
||||||
v8::Isolate* isolate, const WrappableFactoryFunction& factory) {
|
|
||||||
if (constructor_.IsEmpty()) {
|
|
||||||
v8::Local<v8::FunctionTemplate> constructor = CreateFunctionTemplate(
|
|
||||||
isolate, base::Bind(&Constructor::New, factory));
|
|
||||||
constructor->InstanceTemplate()->SetInternalFieldCount(1);
|
|
||||||
constructor->SetClassName(StringToV8(isolate, name_));
|
|
||||||
T::BuildPrototype(isolate, constructor->PrototypeTemplate());
|
|
||||||
MATE_PERSISTENT_ASSIGN(v8::FunctionTemplate, isolate, constructor_,
|
|
||||||
constructor);
|
|
||||||
}
|
|
||||||
|
|
||||||
return MATE_PERSISTENT_TO_LOCAL(
|
|
||||||
v8::FunctionTemplate, isolate, constructor_);
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
static MATE_METHOD_RETURN_TYPE New(const WrappableFactoryFunction& factory,
|
|
||||||
v8::Isolate* isolate, Arguments* args) {
|
v8::Isolate* isolate, Arguments* args) {
|
||||||
if (!args->IsConstructCall()) {
|
if (!args->IsConstructCall()) {
|
||||||
args->ThrowError("Requires constructor call");
|
args->ThrowError("Requires constructor call");
|
||||||
|
@ -172,24 +139,10 @@ class Constructor {
|
||||||
args->ThrowError();
|
args->ThrowError();
|
||||||
|
|
||||||
MATE_METHOD_RETURN_UNDEFINED();
|
MATE_METHOD_RETURN_UNDEFINED();
|
||||||
}
|
|
||||||
|
|
||||||
base::StringPiece name_;
|
|
||||||
v8::Global<v8::FunctionTemplate> constructor_;
|
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(Constructor);
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename T, typename Sig>
|
|
||||||
v8::Local<v8::Function> CreateConstructor(
|
|
||||||
v8::Isolate* isolate,
|
|
||||||
const base::StringPiece& name,
|
|
||||||
const base::Callback<Sig>& callback) {
|
|
||||||
v8::Local<v8::FunctionTemplate> constructor =
|
|
||||||
Constructor<T, Sig>(name).GetFunctionTemplate(isolate, callback);
|
|
||||||
return constructor->GetFunction();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // namespace internal
|
||||||
|
|
||||||
} // namespace mate
|
} // namespace mate
|
||||||
|
|
||||||
#endif // NATIVE_MATE_WRAPPABLE_CLASS_H_
|
#endif // NATIVE_MATE_WRAPPABLE_CLASS_H_
|
||||||
|
|
|
@ -1,139 +0,0 @@
|
||||||
$$ This is a pump file for generating file templates. Pump is a python
|
|
||||||
$$ script that is part of the Google Test suite of utilities. Description
|
|
||||||
$$ can be found here:
|
|
||||||
$$
|
|
||||||
$$ http://code.google.com/p/googletest/wiki/PumpManual
|
|
||||||
$$
|
|
||||||
$var MAX_ARITY = 6
|
|
||||||
// Copyright 2014 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_CLASS_H_
|
|
||||||
#define NATIVE_MATE_WRAPPABLE_CLASS_H_
|
|
||||||
|
|
||||||
#include "base/bind.h"
|
|
||||||
#include "base/compiler_specific.h"
|
|
||||||
#include "native_mate/wrappable.h"
|
|
||||||
#include "native_mate/function_template.h"
|
|
||||||
|
|
||||||
namespace mate {
|
|
||||||
|
|
||||||
class WrappableBase;
|
|
||||||
|
|
||||||
namespace internal {
|
|
||||||
|
|
||||||
// This set of templates invokes a base::Callback by converting the Arguments
|
|
||||||
// into native types. It relies on the function_template.h to provide helper
|
|
||||||
// templates.
|
|
||||||
$range ARITY 0..MAX_ARITY
|
|
||||||
$for ARITY [[
|
|
||||||
$range ARG 1..ARITY
|
|
||||||
|
|
||||||
$if ARITY == 0 [[
|
|
||||||
|
|
||||||
]] $else [[
|
|
||||||
|
|
||||||
template<$for ARG , [[typename P$(ARG)]]>
|
|
||||||
]]
|
|
||||||
|
|
||||||
inline WrappableBase* InvokeFactory(
|
|
||||||
Arguments* args,
|
|
||||||
const base::Callback<WrappableBase*($for ARG , [[P$(ARG)]])>& callback) {
|
|
||||||
$if ARITY != 0 [[
|
|
||||||
|
|
||||||
|
|
||||||
$for ARG [[ typename CallbackParamTraits<P$(ARG)>::LocalType a$(ARG);
|
|
||||||
|
|
||||||
]]
|
|
||||||
if ($for ARG ||
|
|
||||||
[[!GetNextArgument(args, 0, $if ARG == 1 [[true]] $else [[false]], &a$(ARG))]])
|
|
||||||
return nullptr;
|
|
||||||
]]
|
|
||||||
|
|
||||||
return callback.Run($for ARG , [[a$(ARG)]]);
|
|
||||||
};
|
|
||||||
|
|
||||||
]]
|
|
||||||
|
|
||||||
} // namespace internal
|
|
||||||
|
|
||||||
|
|
||||||
template<typename Sig>
|
|
||||||
class Constructor {
|
|
||||||
public:
|
|
||||||
typedef base::Callback<Sig> WrappableFactoryFunction;
|
|
||||||
|
|
||||||
Constructor(const base::StringPiece& name) : name_(name) {}
|
|
||||||
virtual ~Constructor() {
|
|
||||||
MATE_PERSISTENT_RESET(constructor_);
|
|
||||||
}
|
|
||||||
|
|
||||||
v8::Local<v8::FunctionTemplate> GetFunctionTemplate(
|
|
||||||
v8::Isolate* isolate, const WrappableFactoryFunction& factory) {
|
|
||||||
if (constructor_.IsEmpty()) {
|
|
||||||
v8::Local<v8::FunctionTemplate> constructor = CreateFunctionTemplate(
|
|
||||||
isolate, base::Bind(&Constructor::New, factory));
|
|
||||||
constructor->InstanceTemplate()->SetInternalFieldCount(1);
|
|
||||||
constructor->SetClassName(StringToV8(isolate, name_));
|
|
||||||
MATE_PERSISTENT_ASSIGN(v8::FunctionTemplate, isolate, constructor_,
|
|
||||||
constructor);
|
|
||||||
}
|
|
||||||
|
|
||||||
return MATE_PERSISTENT_TO_LOCAL(
|
|
||||||
v8::FunctionTemplate, isolate, constructor_);
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
static MATE_METHOD_RETURN_TYPE New(const WrappableFactoryFunction& factory,
|
|
||||||
v8::Isolate* isolate, Arguments* args) {
|
|
||||||
if (!args->IsConstructCall()) {
|
|
||||||
args->ThrowError("Requires constructor call");
|
|
||||||
MATE_METHOD_RETURN_UNDEFINED();
|
|
||||||
}
|
|
||||||
|
|
||||||
WrappableBase* object;
|
|
||||||
{
|
|
||||||
// Don't continue if the constructor throws an exception.
|
|
||||||
v8::TryCatch try_catch;
|
|
||||||
object = internal::InvokeFactory(args, factory);
|
|
||||||
if (try_catch.HasCaught()) {
|
|
||||||
try_catch.ReThrow();
|
|
||||||
MATE_METHOD_RETURN_UNDEFINED();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (object)
|
|
||||||
object->InitWith(isolate, args->GetThis());
|
|
||||||
else
|
|
||||||
args->ThrowError();
|
|
||||||
|
|
||||||
MATE_METHOD_RETURN_UNDEFINED();
|
|
||||||
}
|
|
||||||
|
|
||||||
base::StringPiece name_;
|
|
||||||
v8::Global<v8::FunctionTemplate> constructor_;
|
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(Constructor);
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
WrappableBase* NewOperatorFactory() {
|
|
||||||
return new T;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T, typename Sig>
|
|
||||||
v8::Local<v8::Function> CreateConstructor(
|
|
||||||
v8::Isolate* isolate,
|
|
||||||
const base::StringPiece& name,
|
|
||||||
const base::Callback<Sig>& callback) {
|
|
||||||
v8::Local<v8::FunctionTemplate> constructor =
|
|
||||||
Constructor<Sig>(name).GetFunctionTemplate(isolate, callback);
|
|
||||||
T::BuildPrototype(isolate, constructor->PrototypeTemplate());
|
|
||||||
return constructor->GetFunction();
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace mate
|
|
||||||
|
|
||||||
#endif // NATIVE_MATE_WRAPPABLE_CLASS_H_
|
|
|
@ -8,7 +8,7 @@
|
||||||
#include "base/callback.h"
|
#include "base/callback.h"
|
||||||
#include "base/logging.h"
|
#include "base/logging.h"
|
||||||
#include "native_mate/arguments.h"
|
#include "native_mate/arguments.h"
|
||||||
#include "native_mate/wrappable.h"
|
#include "native_mate/wrappable_base.h"
|
||||||
#include "v8/include/v8.h"
|
#include "v8/include/v8.h"
|
||||||
|
|
||||||
namespace mate {
|
namespace mate {
|
||||||
|
|
|
@ -5,75 +5,41 @@
|
||||||
#ifndef NATIVE_MATE_WRAPPABLE_H_
|
#ifndef NATIVE_MATE_WRAPPABLE_H_
|
||||||
#define NATIVE_MATE_WRAPPABLE_H_
|
#define NATIVE_MATE_WRAPPABLE_H_
|
||||||
|
|
||||||
|
#include "base/bind.h"
|
||||||
#include "native_mate/compat.h"
|
#include "native_mate/compat.h"
|
||||||
#include "native_mate/converter.h"
|
#include "native_mate/converter.h"
|
||||||
|
#include "native_mate/constructor.h"
|
||||||
#include "native_mate/template_util.h"
|
#include "native_mate/template_util.h"
|
||||||
|
|
||||||
namespace mate {
|
namespace mate {
|
||||||
|
|
||||||
namespace internal {
|
namespace internal {
|
||||||
|
|
||||||
struct Destroyable;
|
|
||||||
|
|
||||||
void* FromV8Impl(v8::Isolate* isolate, v8::Local<v8::Value> val);
|
void* FromV8Impl(v8::Isolate* isolate, v8::Local<v8::Value> val);
|
||||||
|
|
||||||
} // namespace internal
|
} // namespace internal
|
||||||
|
|
||||||
|
|
||||||
// Wrappable is a base class for C++ objects that have corresponding v8 wrapper
|
|
||||||
// objects. To retain a Wrappable object on the stack, use a gin::Handle.
|
|
||||||
//
|
|
||||||
// USAGE:
|
|
||||||
// // my_class.h
|
|
||||||
// class MyClass : Wrappable<MyClass> {
|
|
||||||
// public:
|
|
||||||
// ...
|
|
||||||
// };
|
|
||||||
//
|
|
||||||
// Subclasses should also typically have private constructors and expose a
|
|
||||||
// static Create function that returns a mate::Handle. Forcing creators through
|
|
||||||
// this static Create function will enforce that clients actually create a
|
|
||||||
// wrapper for the object. If clients fail to create a wrapper for a wrappable
|
|
||||||
// object, the object will leak because we use the weak callback from the
|
|
||||||
// wrapper as the signal to delete the wrapped object.
|
|
||||||
class WrappableBase {
|
|
||||||
public:
|
|
||||||
WrappableBase();
|
|
||||||
virtual ~WrappableBase();
|
|
||||||
|
|
||||||
// Retrieve the v8 wrapper object cooresponding to this object.
|
|
||||||
v8::Local<v8::Object> GetWrapper();
|
|
||||||
|
|
||||||
// Returns the Isolate this object is created in.
|
|
||||||
v8::Isolate* isolate() const { return isolate_; }
|
|
||||||
|
|
||||||
protected:
|
|
||||||
// Called after the "_init" method gets called in JavaScript.
|
|
||||||
virtual void AfterInit(v8::Isolate* isolate) {}
|
|
||||||
|
|
||||||
// Bind the C++ class to the JS wrapper.
|
|
||||||
// This method should only be called by classes using Constructor.
|
|
||||||
virtual void InitWith(v8::Isolate* isolate, v8::Local<v8::Object> wrapper);
|
|
||||||
|
|
||||||
private:
|
|
||||||
friend struct internal::Destroyable;
|
|
||||||
|
|
||||||
static void FirstWeakCallback(
|
|
||||||
const v8::WeakCallbackInfo<WrappableBase>& data);
|
|
||||||
static void SecondWeakCallback(
|
|
||||||
const v8::WeakCallbackInfo<WrappableBase>& data);
|
|
||||||
|
|
||||||
v8::Isolate* isolate_;
|
|
||||||
v8::Global<v8::Object> wrapper_; // Weak
|
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(WrappableBase);
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
class Wrappable : public WrappableBase {
|
class Wrappable : public WrappableBase {
|
||||||
public:
|
public:
|
||||||
Wrappable() {}
|
Wrappable() {}
|
||||||
|
|
||||||
|
template<typename Sig>
|
||||||
|
static void SetConstructor(v8::Isolate* isolate,
|
||||||
|
const std::string& name,
|
||||||
|
const base::Callback<Sig>& factory) {
|
||||||
|
v8::Local<v8::FunctionTemplate> constructor = CreateFunctionTemplate(
|
||||||
|
isolate, base::Bind(&internal::InvokeNew<Sig>, factory));
|
||||||
|
constructor->InstanceTemplate()->SetInternalFieldCount(1);
|
||||||
|
constructor->SetClassName(StringToV8(isolate, name));
|
||||||
|
T::BuildPrototype(isolate, constructor->PrototypeTemplate());
|
||||||
|
templ_ = new v8::Global<v8::FunctionTemplate>(isolate, constructor);
|
||||||
|
}
|
||||||
|
|
||||||
|
static v8::Local<v8::Object> GetConstructor(v8::Isolate* isolate) {
|
||||||
|
return v8::Local<v8::FunctionTemplate>::New(isolate, *templ_)->GetFunction();
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
// Init the class with T::BuildPrototype.
|
// Init the class with T::BuildPrototype.
|
||||||
void Init(v8::Isolate* isolate) {
|
void Init(v8::Isolate* isolate) {
|
||||||
|
|
63
native_mate/wrappable_base.h
Normal file
63
native_mate/wrappable_base.h
Normal file
|
@ -0,0 +1,63 @@
|
||||||
|
#ifndef NATIVE_MATE_WRAPPABLE_BASE_H_
|
||||||
|
#define NATIVE_MATE_WRAPPABLE_BASE_H_
|
||||||
|
|
||||||
|
#include "native_mate/compat.h"
|
||||||
|
|
||||||
|
namespace mate {
|
||||||
|
|
||||||
|
namespace internal {
|
||||||
|
struct Destroyable;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wrappable is a base class for C++ objects that have corresponding v8 wrapper
|
||||||
|
// objects. To retain a Wrappable object on the stack, use a gin::Handle.
|
||||||
|
//
|
||||||
|
// USAGE:
|
||||||
|
// // my_class.h
|
||||||
|
// class MyClass : Wrappable<MyClass> {
|
||||||
|
// public:
|
||||||
|
// ...
|
||||||
|
// };
|
||||||
|
//
|
||||||
|
// Subclasses should also typically have private constructors and expose a
|
||||||
|
// static Create function that returns a mate::Handle. Forcing creators through
|
||||||
|
// this static Create function will enforce that clients actually create a
|
||||||
|
// wrapper for the object. If clients fail to create a wrapper for a wrappable
|
||||||
|
// object, the object will leak because we use the weak callback from the
|
||||||
|
// wrapper as the signal to delete the wrapped object.
|
||||||
|
class WrappableBase {
|
||||||
|
public:
|
||||||
|
WrappableBase();
|
||||||
|
virtual ~WrappableBase();
|
||||||
|
|
||||||
|
// Retrieve the v8 wrapper object cooresponding to this object.
|
||||||
|
v8::Local<v8::Object> GetWrapper();
|
||||||
|
|
||||||
|
// Returns the Isolate this object is created in.
|
||||||
|
v8::Isolate* isolate() const { return isolate_; }
|
||||||
|
|
||||||
|
protected:
|
||||||
|
// Called after the "_init" method gets called in JavaScript.
|
||||||
|
virtual void AfterInit(v8::Isolate* isolate) {}
|
||||||
|
|
||||||
|
// Bind the C++ class to the JS wrapper.
|
||||||
|
// This method should only be called by classes using Constructor.
|
||||||
|
virtual void InitWith(v8::Isolate* isolate, v8::Local<v8::Object> wrapper);
|
||||||
|
|
||||||
|
private:
|
||||||
|
friend struct internal::Destroyable;
|
||||||
|
|
||||||
|
static void FirstWeakCallback(
|
||||||
|
const v8::WeakCallbackInfo<WrappableBase>& data);
|
||||||
|
static void SecondWeakCallback(
|
||||||
|
const v8::WeakCallbackInfo<WrappableBase>& data);
|
||||||
|
|
||||||
|
v8::Isolate* isolate_;
|
||||||
|
v8::Global<v8::Object> wrapper_; // Weak
|
||||||
|
|
||||||
|
DISALLOW_COPY_AND_ASSIGN(WrappableBase);
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace mate
|
||||||
|
|
||||||
|
#endif // NATIVE_MATE_WRAPPABLE_BASE_H_
|
|
@ -22,6 +22,7 @@
|
||||||
'native_mate/try_catch.h',
|
'native_mate/try_catch.h',
|
||||||
'native_mate/wrappable.cc',
|
'native_mate/wrappable.cc',
|
||||||
'native_mate/wrappable.h',
|
'native_mate/wrappable.h',
|
||||||
|
'native_mate/wrappable_base.h',
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue