Use pump to generate source files.

This commit is contained in:
Cheng Zhao
2014-08-10 16:59:50 +08:00
parent cbb5004ff9
commit 5d6921ff2a
5 changed files with 1228 additions and 2 deletions

View File

@@ -1,3 +1,7 @@
// This file was GENERATED by command:
// pump.py constructor.h.pump
// DO NOT EDIT BY HAND!!!
// 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.
@@ -30,7 +34,7 @@ inline Wrappable* InvokeFactory(
Arguments* args,
const base::Callback<Wrappable*(P1)>& callback) {
typename CallbackParamTraits<P1>::LocalType a1;
if (!GetNextArgument(args, 0, false, &a1))
if (!GetNextArgument(args, 0, true, &a1))
return NULL;
return callback.Run(a1);
};

View File

@@ -0,0 +1,124 @@
$$ 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 Wrappable;
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 Wrappable* InvokeFactory(
Arguments* args,
const base::Callback<Wrappable*($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 NULL;
]]
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::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_));
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) {
Wrappable* object = internal::InvokeFactory(args, factory);
if (object)
object->Wrap(isolate, args->GetThis());
else
args->ThrowError();
MATE_METHOD_RETURN_UNDEFINED();
}
base::StringPiece name_;
v8::Persistent<v8::FunctionTemplate> constructor_;
DISALLOW_COPY_AND_ASSIGN(Constructor);
};
template<typename T>
Wrappable* 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_

View File

@@ -1,3 +1,7 @@
// This file was GENERATED by command:
// pump.py function_template.h.pump
// DO NOT EDIT BY HAND!!!
// 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.
@@ -116,6 +120,21 @@ struct Invoker<void, P1, P2, P3, P4, P5, P6, P7> {
}
};
template<typename R, typename P1, typename P2, typename P3, typename P4,
typename P5, typename P6>
struct Invoker<R, P1, P2, P3, P4, P5, P6, void> {
inline static MATE_METHOD_RETURN_TYPE Go(
Arguments& args,
const base::Callback<R(P1, P2, P3, P4, P5, P6)>& callback,
const P1& a1,
const P2& a2,
const P3& a3,
const P4& a4,
const P5& a5,
const P6& a6) {
MATE_METHOD_RETURN(callback.Run(a1, a2, a3, a4, a5, a6));
}
};
template<typename P1, typename P2, typename P3, typename P4, typename P5,
typename P6>
struct Invoker<void, P1, P2, P3, P4, P5, P6, void> {
@@ -529,7 +548,6 @@ struct Dispatcher<R(P1, P2, P3, P4, P5, P6, P7)> {
}
};
} // namespace internal

View File

@@ -0,0 +1,225 @@
$$ 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 = 7
// 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_FUNCTION_TEMPLATE_H_
#define NATIVE_MATE_FUNCTION_TEMPLATE_H_
#include "base/callback.h"
#include "base/logging.h"
#include "native_mate/arguments.h"
#include "native_mate/compat.h"
#include "native_mate/converter.h"
#include "v8/include/v8.h"
namespace mate {
class PerIsolateData;
enum CreateFunctionTemplateFlags {
HolderIsFirstArgument = 1 << 0,
};
namespace internal {
template<typename T>
struct CallbackParamTraits {
typedef T LocalType;
};
template<typename T>
struct CallbackParamTraits<const T&> {
typedef T LocalType;
};
template<typename T>
struct CallbackParamTraits<const T*> {
typedef T* LocalType;
};
// CallbackHolder and CallbackHolderBase are used to pass a base::Callback from
// CreateFunctionTemplate through v8 (via v8::FunctionTemplate) to
// DispatchToCallback, where it is invoked.
// This simple base class is used so that we can share a single object template
// among every CallbackHolder instance.
class CallbackHolderBase {
public:
v8::Handle<v8::External> GetHandle(v8::Isolate* isolate);
protected:
explicit CallbackHolderBase(v8::Isolate* isolate);
virtual ~CallbackHolderBase();
private:
static MATE_WEAK_CALLBACK(WeakCallback, v8::External, CallbackHolderBase);
v8::Persistent<v8::External> v8_ref_;
DISALLOW_COPY_AND_ASSIGN(CallbackHolderBase);
};
template<typename Sig>
class CallbackHolder : public CallbackHolderBase {
public:
CallbackHolder(v8::Isolate* isolate,
const base::Callback<Sig>& callback,
int flags)
: CallbackHolderBase(isolate), callback(callback), flags(flags) {}
base::Callback<Sig> callback;
int flags;
private:
virtual ~CallbackHolder() {}
DISALLOW_COPY_AND_ASSIGN(CallbackHolder);
};
// This set of templates invokes a base::Callback, converts the return type to a
// JavaScript value, and returns that value to script via the provided
// mate::Arguments object.
//
// In C++, you can declare the function foo(void), but you can't pass a void
// expression to foo. As a result, we must specialize the case of Callbacks that
// have the void return type.
$range ARITY 0..MAX_ARITY
$for ARITY [[
$var INV_ARITY = MAX_ARITY - ARITY
$range ARG 1..INV_ARITY
$range VOID INV_ARITY+1..MAX_ARITY
$if ARITY == 0 [[
template<typename R$for ARG [[, typename P$(ARG) = void]]>
struct Invoker {
]] $else [[
template<typename R$for ARG [[, typename P$(ARG)]]>
struct Invoker<R$for ARG [[, P$(ARG)]]$for VOID [[, void]]> {
]]
inline static MATE_METHOD_RETURN_TYPE Go(
Arguments& args,
const base::Callback<R($for ARG , [[P$(ARG)]])>& callback$for ARG [[,
const P$(ARG)& a$(ARG)]]) {
MATE_METHOD_RETURN(callback.Run($for ARG, [[a$(ARG)]]));
}
};
template<$for ARG , [[typename P$(ARG)]]>
struct Invoker<void$for ARG [[, P$(ARG)]]$for VOID [[, void]]> {
inline static MATE_METHOD_RETURN_TYPE Go(
Arguments& args,
const base::Callback<void($for ARG , [[P$(ARG)]])>& callback$for ARG [[,
const P$(ARG)& a$(ARG)]]) {
callback.Run($for ARG, [[a$(ARG)]]);
MATE_METHOD_RETURN_UNDEFINED();
}
};
]]
template<typename T>
bool GetNextArgument(Arguments* args, int create_flags, bool is_first,
T* result) {
if (is_first && (create_flags & HolderIsFirstArgument) != 0) {
return args->GetHolder(result);
} else {
return args->GetNext(result);
}
}
// For advanced use cases, we allow callers to request the unparsed Arguments
// object and poke around in it directly.
inline bool GetNextArgument(Arguments* args, int create_flags, bool is_first,
Arguments* result) {
*result = *args;
return true;
}
inline bool GetNextArgument(Arguments* args, int create_flags, bool is_first,
Arguments** result) {
*result = args;
return true;
}
// It's common for clients to just need the isolate, so we make that easy.
inline bool GetNextArgument(Arguments* args, int create_flags,
bool is_first, v8::Isolate** result) {
*result = args->isolate();
return true;
}
// DispatchToCallback converts all the JavaScript arguments to C++ types and
// invokes the base::Callback.
template<typename Sig>
struct Dispatcher {
};
$range ARITY 0..MAX_ARITY
$for ARITY [[
$range ARG 1..ARITY
template<typename R$for ARG [[, typename P$(ARG)]]>
struct Dispatcher<R($for ARG , [[P$(ARG)]])> {
static MATE_METHOD(DispatchToCallback) {
Arguments args(info);
v8::Handle<v8::External> v8_holder;
CHECK(args.GetData(&v8_holder));
CallbackHolderBase* holder_base = reinterpret_cast<CallbackHolderBase*>(
v8_holder->Value());
typedef CallbackHolder<R($for ARG , [[P$(ARG)]])> HolderT;
HolderT* holder = static_cast<HolderT*>(holder_base);
$if ARITY != 0 [[
$for ARG [[ typename CallbackParamTraits<P$(ARG)>::LocalType a$(ARG);
]]
if ($for ARG ||
[[!GetNextArgument(&args, holder->flags, $if ARG == 1 [[true]] $else [[false]], &a$(ARG))]]) {
args.ThrowError();
MATE_METHOD_RETURN_UNDEFINED();
}
]]
return Invoker<R$for ARG [[, P$(ARG)]]>::Go(args, holder->callback$for ARG [[, a$(ARG)]]);
}
};
]]
} // namespace internal
// CreateFunctionTemplate creates a v8::FunctionTemplate that will create
// JavaScript functions that execute a provided C++ function or base::Callback.
// JavaScript arguments are automatically converted via mate::Converter, as is
// the return value of the C++ function, if any.
template<typename Sig>
v8::Local<v8::FunctionTemplate> CreateFunctionTemplate(
v8::Isolate* isolate, const base::Callback<Sig> callback,
int callback_flags = 0) {
typedef internal::CallbackHolder<Sig> HolderT;
HolderT* holder = new HolderT(isolate, callback, callback_flags);
return v8::FunctionTemplate::New(
#if NODE_VERSION_AT_LEAST(0, 11, 11)
isolate,
#endif
&internal::Dispatcher<Sig>::DispatchToCallback,
ConvertToV8<v8::Handle<v8::External> >(isolate,
holder->GetHandle(isolate)));
}
} // namespace mate
#endif // NATIVE_MATE_FUNCTION_TEMPLATE_H_