2014-04-15 07:30:46 +00:00
|
|
|
// 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_
|
|
|
|
|
2014-04-16 01:50:51 +00:00
|
|
|
#include "base/bind.h"
|
|
|
|
#include "base/compiler_specific.h"
|
2014-04-18 08:45:44 +00:00
|
|
|
#include "native_mate/wrappable.h"
|
2014-04-16 01:50:51 +00:00
|
|
|
#include "native_mate/function_template.h"
|
2014-04-15 07:30:46 +00:00
|
|
|
|
|
|
|
namespace mate {
|
|
|
|
|
2014-04-16 03:58:17 +00:00
|
|
|
class Wrappable;
|
2014-04-16 01:50:51 +00:00
|
|
|
|
|
|
|
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.
|
2014-04-16 03:58:17 +00:00
|
|
|
inline Wrappable* InvokeFactory(
|
2014-04-16 01:50:51 +00:00
|
|
|
Arguments* args,
|
2014-04-16 03:58:17 +00:00
|
|
|
const base::Callback<Wrappable*()>& callback) {
|
2014-04-16 01:50:51 +00:00
|
|
|
return callback.Run();
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename P1>
|
2014-04-16 03:58:17 +00:00
|
|
|
inline Wrappable* InvokeFactory(
|
2014-04-16 01:50:51 +00:00
|
|
|
Arguments* args,
|
2014-04-16 03:58:17 +00:00
|
|
|
const base::Callback<Wrappable*(P1)>& callback) {
|
2014-04-16 01:50:51 +00:00
|
|
|
typename CallbackParamTraits<P1>::LocalType a1;
|
|
|
|
if (!GetNextArgument(args, 0, false, &a1))
|
|
|
|
return NULL;
|
|
|
|
return callback.Run(a1);
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename P1, typename P2>
|
2014-04-16 03:58:17 +00:00
|
|
|
inline Wrappable* InvokeFactory(
|
2014-04-16 01:50:51 +00:00
|
|
|
Arguments* args,
|
2014-04-16 03:58:17 +00:00
|
|
|
const base::Callback<Wrappable*(P1, P2)>& callback) {
|
2014-04-16 01:50:51 +00:00
|
|
|
typename CallbackParamTraits<P1>::LocalType a1;
|
|
|
|
typename CallbackParamTraits<P2>::LocalType a2;
|
|
|
|
if (!GetNextArgument(args, 0, true, &a1) ||
|
|
|
|
!GetNextArgument(args, 0, false, &a2))
|
|
|
|
return NULL;
|
|
|
|
return callback.Run(a1, a2);
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename P1, typename P2, typename P3>
|
2014-04-16 03:58:17 +00:00
|
|
|
inline Wrappable* InvokeFactory(
|
2014-04-16 01:50:51 +00:00
|
|
|
Arguments* args,
|
2014-04-16 03:58:17 +00:00
|
|
|
const base::Callback<Wrappable*(P1, P2, P3)>& callback) {
|
2014-04-16 01:50:51 +00:00
|
|
|
typename CallbackParamTraits<P1>::LocalType a1;
|
|
|
|
typename CallbackParamTraits<P2>::LocalType a2;
|
|
|
|
typename CallbackParamTraits<P3>::LocalType a3;
|
|
|
|
if (!GetNextArgument(args, 0, true, &a1) ||
|
|
|
|
!GetNextArgument(args, 0, false, &a2) ||
|
|
|
|
!GetNextArgument(args, 0, false, &a3))
|
|
|
|
return NULL;
|
|
|
|
return callback.Run(a1, a2, a3);
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename P1, typename P2, typename P3, typename P4>
|
2014-04-16 03:58:17 +00:00
|
|
|
inline Wrappable* InvokeFactory(
|
2014-04-16 01:50:51 +00:00
|
|
|
Arguments* args,
|
2014-04-16 03:58:17 +00:00
|
|
|
const base::Callback<Wrappable*(P1, P2, P3, P4)>& callback) {
|
2014-04-16 01:50:51 +00:00
|
|
|
typename CallbackParamTraits<P1>::LocalType a1;
|
|
|
|
typename CallbackParamTraits<P2>::LocalType a2;
|
|
|
|
typename CallbackParamTraits<P3>::LocalType a3;
|
|
|
|
typename CallbackParamTraits<P4>::LocalType a4;
|
|
|
|
if (!GetNextArgument(args, 0, true, &a1) ||
|
|
|
|
!GetNextArgument(args, 0, false, &a2) ||
|
|
|
|
!GetNextArgument(args, 0, false, &a3) ||
|
|
|
|
!GetNextArgument(args, 0, false, &a4))
|
|
|
|
return NULL;
|
|
|
|
return callback.Run(a1, a2, a3, a4);
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename P1, typename P2, typename P3, typename P4, typename P5>
|
2014-04-16 03:58:17 +00:00
|
|
|
inline Wrappable* InvokeFactory(
|
2014-04-16 01:50:51 +00:00
|
|
|
Arguments* args,
|
2014-04-16 03:58:17 +00:00
|
|
|
const base::Callback<Wrappable*(P1, P2, P3, P4, P5)>& callback) {
|
2014-04-16 01:50:51 +00:00
|
|
|
typename CallbackParamTraits<P1>::LocalType a1;
|
|
|
|
typename CallbackParamTraits<P2>::LocalType a2;
|
|
|
|
typename CallbackParamTraits<P3>::LocalType a3;
|
|
|
|
typename CallbackParamTraits<P4>::LocalType a4;
|
|
|
|
typename CallbackParamTraits<P5>::LocalType a5;
|
|
|
|
if (!GetNextArgument(args, 0, true, &a1) ||
|
|
|
|
!GetNextArgument(args, 0, false, &a2) ||
|
|
|
|
!GetNextArgument(args, 0, false, &a3) ||
|
|
|
|
!GetNextArgument(args, 0, false, &a4) ||
|
|
|
|
!GetNextArgument(args, 0, false, &a5))
|
|
|
|
return NULL;
|
|
|
|
return callback.Run(a1, a2, a3, a4, a5);
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename P1, typename P2, typename P3, typename P4, typename P5,
|
|
|
|
typename P6>
|
2014-04-16 03:58:17 +00:00
|
|
|
inline Wrappable* InvokeFactory(
|
2014-04-16 01:50:51 +00:00
|
|
|
Arguments* args,
|
2014-04-16 03:58:17 +00:00
|
|
|
const base::Callback<Wrappable*(P1, P2, P3, P4, P5, P6)>& callback) {
|
2014-04-16 01:50:51 +00:00
|
|
|
typename CallbackParamTraits<P1>::LocalType a1;
|
|
|
|
typename CallbackParamTraits<P2>::LocalType a2;
|
|
|
|
typename CallbackParamTraits<P3>::LocalType a3;
|
|
|
|
typename CallbackParamTraits<P4>::LocalType a4;
|
|
|
|
typename CallbackParamTraits<P5>::LocalType a5;
|
|
|
|
typename CallbackParamTraits<P6>::LocalType a6;
|
|
|
|
if (!GetNextArgument(args, 0, true, &a1) ||
|
|
|
|
!GetNextArgument(args, 0, false, &a2) ||
|
|
|
|
!GetNextArgument(args, 0, false, &a3) ||
|
|
|
|
!GetNextArgument(args, 0, false, &a4) ||
|
|
|
|
!GetNextArgument(args, 0, false, &a5) ||
|
|
|
|
!GetNextArgument(args, 0, false, &a6))
|
|
|
|
return NULL;
|
|
|
|
return callback.Run(a1, a2, a3, a4, a5, a6);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
|
|
|
|
|
|
|
|
template<typename Sig>
|
2014-04-15 07:30:46 +00:00
|
|
|
class Constructor {
|
|
|
|
public:
|
2014-04-16 01:50:51 +00:00
|
|
|
typedef base::Callback<Sig> WrappableFactoryFunction;
|
2014-04-15 07:30:46 +00:00
|
|
|
|
2014-04-16 01:50:51 +00:00
|
|
|
Constructor(const base::StringPiece& name) : name_(name) {}
|
|
|
|
virtual ~Constructor() {
|
2014-08-10 04:17:47 +00:00
|
|
|
MATE_PERSISTENT_RESET(constructor_);
|
2014-04-16 01:50:51 +00:00
|
|
|
}
|
2014-04-15 07:30:46 +00:00
|
|
|
|
2014-04-16 01:50:51 +00:00
|
|
|
v8::Handle<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_));
|
2014-08-10 04:17:47 +00:00
|
|
|
MATE_PERSISTENT_ASSIGN(v8::FunctionTemplate, isolate, constructor_,
|
|
|
|
constructor);
|
2014-04-16 01:50:51 +00:00
|
|
|
}
|
2014-04-15 07:30:46 +00:00
|
|
|
|
2014-04-16 01:50:51 +00:00
|
|
|
return MATE_PERSISTENT_TO_LOCAL(
|
|
|
|
v8::FunctionTemplate, isolate, constructor_);
|
|
|
|
}
|
2014-04-15 07:30:46 +00:00
|
|
|
|
|
|
|
private:
|
2014-08-10 04:17:47 +00:00
|
|
|
static MATE_METHOD_RETURN_TYPE New(const WrappableFactoryFunction& factory,
|
|
|
|
v8::Isolate* isolate, Arguments* args) {
|
2014-04-16 03:58:17 +00:00
|
|
|
Wrappable* object = internal::InvokeFactory(args, factory);
|
2014-04-16 01:50:51 +00:00
|
|
|
if (object)
|
2014-04-16 02:36:54 +00:00
|
|
|
object->Wrap(isolate, args->GetThis());
|
2014-04-16 01:50:51 +00:00
|
|
|
else
|
|
|
|
args->ThrowError();
|
|
|
|
|
|
|
|
MATE_METHOD_RETURN_UNDEFINED();
|
|
|
|
}
|
|
|
|
|
2014-04-15 07:30:46 +00:00
|
|
|
base::StringPiece name_;
|
|
|
|
v8::Persistent<v8::FunctionTemplate> constructor_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(Constructor);
|
|
|
|
};
|
|
|
|
|
2014-04-16 01:50:51 +00:00
|
|
|
|
|
|
|
template<typename T>
|
2014-04-16 03:58:17 +00:00
|
|
|
Wrappable* NewOperatorFactory() {
|
2014-04-16 01:50:51 +00:00
|
|
|
return new T;
|
|
|
|
}
|
|
|
|
|
2014-04-16 03:58:17 +00:00
|
|
|
template<typename T, typename Sig>
|
|
|
|
v8::Local<v8::Function> CreateConstructor(
|
2014-04-16 01:50:51 +00:00
|
|
|
v8::Isolate* isolate,
|
|
|
|
const base::StringPiece& name,
|
2014-04-16 03:58:17 +00:00
|
|
|
const base::Callback<Sig>& callback) {
|
|
|
|
v8::Local<v8::FunctionTemplate> constructor =
|
|
|
|
Constructor<Sig>(name).GetFunctionTemplate(isolate, callback);
|
|
|
|
T::BuildPrototype(isolate, constructor->PrototypeTemplate());
|
|
|
|
return constructor->GetFunction();
|
2014-04-16 01:50:51 +00:00
|
|
|
}
|
|
|
|
|
2014-04-15 07:30:46 +00:00
|
|
|
} // namespace mate
|
|
|
|
|
|
|
|
#endif // NATIVE_MATE_WRAPPABLE_CLASS_H_
|