$$ 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 (c) 2014 GitHub, Inc. All rights reserved. // Use of this source code is governed by the MIT license that can be // found in the LICENSE file. #include "base/bind.h" #include "base/callback.h" #include "native_mate/function_template.h" #include "native_mate/locker.h" #include "native_mate/scoped_persistent.h" namespace mate { namespace internal { typedef scoped_refptr > SafeV8Function; // Helper to convert type to V8 with storage type (const T& to T). template v8::Handle ConvertToV8(v8::Isolate* isolate, T a) { return Converter::StorageType> ::ToV8(isolate, a); } // This set of templates invokes a V8::Function by converting the C++ types. template struct V8FunctionInvoker; $range ARITY 0..MAX_ARITY $for ARITY [[ $range ARG 1..ARITY template struct V8FunctionInvoker { static R Go(v8::Isolate* isolate, SafeV8Function function$for ARG [[, P$(ARG) a$(ARG)]]) { R ret; Locker locker(isolate); MATE_HANDLE_SCOPE(isolate); v8::Handle holder = function->NewHandle(); $if ARITY == 0 [[ v8::Handle val(holder->Call(holder, 0, NULL)); ]] $else [[ v8::Handle args[] = { $for ARG [[ ConvertToV8(isolate, a$(ARG)), ]] }; v8::Handle val(holder->Call(holder, arraysize(args), args)); ]] Converter::FromV8(isolate, val, &ret); return ret; } }; template<$for ARG , [[typename P$(ARG)]]> struct V8FunctionInvoker { static void Go(v8::Isolate* isolate, SafeV8Function function$for ARG [[, P$(ARG) a$(ARG)]]) { Locker locker(isolate); MATE_HANDLE_SCOPE(isolate); v8::Handle holder = function->NewHandle(); $if ARITY == 0 [[ holder->Call(holder, 0, NULL); ]] $else [[ v8::Handle args[] = { $for ARG [[ ConvertToV8(isolate, a$(ARG)), ]] }; holder->Call(holder, arraysize(args), args); ]] } }; ]] } // namespace internal template struct Converter > { static v8::Handle ToV8(v8::Isolate* isolate, const base::Callback& val) { return CreateFunctionTemplate(isolate, val)->GetFunction(); } static bool FromV8(v8::Isolate* isolate, v8::Handle val, base::Callback* out) { if (!val->IsFunction()) return false; internal::SafeV8Function function( new RefCountedPersistent(val)); *out = base::Bind(&internal::V8FunctionInvoker::Go, isolate, function); return true; } }; } // namespace mate