Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef GIN_OBJECT_TEMPLATE_BUILDER_H_ | 5 #ifndef GIN_OBJECT_TEMPLATE_BUILDER_H_ |
| 6 #define GIN_OBJECT_TEMPLATE_BUILDER_H_ | 6 #define GIN_OBJECT_TEMPLATE_BUILDER_H_ |
| 7 | 7 |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/callback.h" | |
| 9 #include "base/strings/string_piece.h" | 10 #include "base/strings/string_piece.h" |
| 11 #include "base/template_util.h" | |
| 10 #include "gin/converter.h" | 12 #include "gin/converter.h" |
| 11 #include "gin/function_template.h" | 13 #include "gin/function_template.h" |
| 12 #include "v8/include/v8.h" | 14 #include "v8/include/v8.h" |
| 13 | 15 |
| 14 namespace gin { | 16 namespace gin { |
| 15 | 17 |
| 18 namespace { | |
|
awong
2013/12/10 02:37:24
erp...I'm concerned about putting an anonymous nam
Jeffrey Yasskin
2013/12/10 02:42:45
Very good catch. It actually causes undefined beha
| |
| 19 | |
| 20 // Base template - used only for non-member function pointers. Other types | |
| 21 // either go to one of the below specializations, or go here and fail to compile | |
| 22 // because of base::Bind(). | |
| 23 template<typename T, typename Enable = void> | |
| 24 struct CallbackTraits { | |
| 25 static v8::Handle<v8::FunctionTemplate> CreateTemplate(v8::Isolate* isolate, | |
| 26 T callback) { | |
| 27 return CreateFunctionTemplate(isolate, base::Bind(callback)); | |
| 28 } | |
| 29 }; | |
| 30 | |
| 31 // Specialization for base::Callback. | |
| 32 template<typename T> | |
| 33 struct CallbackTraits<base::Callback<T> > { | |
| 34 static v8::Handle<v8::FunctionTemplate> CreateTemplate( | |
| 35 v8::Isolate* isolate, const base::Callback<T>& callback) { | |
| 36 return CreateFunctionTemplate(isolate, callback); | |
| 37 } | |
| 38 }; | |
| 39 | |
| 40 // Specialization for member function pointers. We need to handle this case | |
| 41 // specially because the first parameter for callbacks to MFP should typically | |
| 42 // come from the the JavaScript "this" object the function was called on, not | |
| 43 // from the first normal parameter. | |
| 44 template<typename T> | |
| 45 struct CallbackTraits<T, typename base::enable_if< | |
| 46 base::is_member_function_pointer<T>::value >::type> { | |
|
awong
2013/12/10 02:37:24
Why do we need this enable_if?
Also, can this be
Jeffrey Yasskin
2013/12/10 02:42:45
Probably not, because he's using the specializatio
| |
| 47 static v8::Handle<v8::FunctionTemplate> CreateTemplate(v8::Isolate* isolate, | |
| 48 T callback) { | |
| 49 return CreateFunctionTemplate(isolate, base::Bind(callback), | |
| 50 HolderIsFirstArgument); | |
| 51 } | |
| 52 }; | |
| 53 | |
| 54 // This specialization allows people to construct function templates directly if | |
| 55 // they need to do fancier stuff. | |
| 56 template<> | |
| 57 struct CallbackTraits<v8::Handle<v8::FunctionTemplate> > { | |
| 58 static v8::Handle<v8::FunctionTemplate> CreateTemplate( | |
| 59 v8::Handle<v8::FunctionTemplate> templ) { | |
| 60 return templ; | |
| 61 } | |
| 62 }; | |
| 63 | |
| 64 } // namespace | |
| 65 | |
| 66 | |
| 16 // ObjectTemplateBuilder provides a handy interface to creating | 67 // ObjectTemplateBuilder provides a handy interface to creating |
| 17 // v8::ObjectTemplate instances with various sorts of properties. | 68 // v8::ObjectTemplate instances with various sorts of properties. |
| 18 class ObjectTemplateBuilder { | 69 class ObjectTemplateBuilder { |
| 19 public: | 70 public: |
| 20 explicit ObjectTemplateBuilder(v8::Isolate* isolate); | 71 explicit ObjectTemplateBuilder(v8::Isolate* isolate); |
| 21 ~ObjectTemplateBuilder(); | 72 ~ObjectTemplateBuilder(); |
| 22 | 73 |
| 23 // It's against Google C++ style to return a non-const ref, but we take some | 74 // It's against Google C++ style to return a non-const ref, but we take some |
| 24 // poetic license here in order that all calls to Set() can be via the '.' | 75 // poetic license here in order that all calls to Set() can be via the '.' |
| 25 // operator and line up nicely. | 76 // operator and line up nicely. |
| 26 template<typename T> | 77 template<typename T> |
| 27 ObjectTemplateBuilder& SetValue(const base::StringPiece& name, T val) { | 78 ObjectTemplateBuilder& SetValue(const base::StringPiece& name, T val) { |
| 28 return SetImpl(name, ConvertToV8(isolate_, val)); | 79 return SetImpl(name, ConvertToV8(isolate_, val)); |
| 29 } | 80 } |
| 30 | 81 |
| 31 template<typename T> | 82 // In the following methods, T and U can be function pointer, member function |
|
awong
2013/12/10 02:37:24
nit: can be -> can be a
| |
| 32 ObjectTemplateBuilder& SetMethod(const base::StringPiece& name, T val) { | 83 // pointer, base::Callback, or v8::FunctionTemplate. Most clients will want to |
| 33 return SetMethod(name, base::Bind(val)); | 84 // use one of the first two options. Also see gin::CreateFunctionTemplate() |
| 34 } | 85 // for creating raw function templates. |
| 35 | |
| 36 template<typename T> | 86 template<typename T> |
| 37 ObjectTemplateBuilder& SetMethod(const base::StringPiece& name, | 87 ObjectTemplateBuilder& SetMethod(const base::StringPiece& name, |
| 38 const base::Callback<T>& callback) { | 88 const T& callback) { |
| 39 return SetImpl(name, CreateFunctionTemplate(isolate_, callback)); | 89 return SetImpl(name, CallbackTraits<T>::CreateTemplate(isolate_, callback)); |
| 90 } | |
| 91 template<typename T> | |
| 92 ObjectTemplateBuilder& SetProperty(const base::StringPiece& name, | |
| 93 const T& getter) { | |
| 94 return SetPropertyImpl(name, | |
| 95 CallbackTraits<T>::CreateTemplate(isolate_, getter), | |
| 96 v8::Local<v8::FunctionTemplate>()); | |
| 97 } | |
| 98 template<typename T, typename U> | |
| 99 ObjectTemplateBuilder& SetProperty(const base::StringPiece& name, | |
| 100 const T& getter, const U& setter) { | |
| 101 return SetPropertyImpl(name, | |
| 102 CallbackTraits<T>::CreateTemplate(isolate_, getter), | |
| 103 CallbackTraits<U>::CreateTemplate(isolate_, setter)); | |
| 40 } | 104 } |
| 41 | 105 |
| 42 v8::Local<v8::ObjectTemplate> Build(); | 106 v8::Local<v8::ObjectTemplate> Build(); |
| 43 | 107 |
| 44 private: | 108 private: |
| 45 ObjectTemplateBuilder& SetImpl(const base::StringPiece& name, | 109 ObjectTemplateBuilder& SetImpl(const base::StringPiece& name, |
| 46 v8::Handle<v8::Data> val); | 110 v8::Handle<v8::Data> val); |
| 111 ObjectTemplateBuilder& SetPropertyImpl( | |
| 112 const base::StringPiece& name, v8::Handle<v8::FunctionTemplate> getter, | |
| 113 v8::Handle<v8::FunctionTemplate> setter); | |
| 47 | 114 |
| 48 v8::Isolate* isolate_; | 115 v8::Isolate* isolate_; |
| 49 | 116 |
| 50 // ObjectTemplateBuilder should only be used on the stack. | 117 // ObjectTemplateBuilder should only be used on the stack. |
| 51 v8::Local<v8::ObjectTemplate> template_; | 118 v8::Local<v8::ObjectTemplate> template_; |
| 52 }; | 119 }; |
| 53 | 120 |
| 54 } // namespace gin | 121 } // namespace gin |
| 55 | 122 |
| 56 #endif // GIN_OBJECT_TEMPLATE_BUILDER_H_ | 123 #endif // GIN_OBJECT_TEMPLATE_BUILDER_H_ |
| OLD | NEW |