OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_FUNCTION_TEMPLATE_H_ | 5 #ifndef GIN_FUNCTION_TEMPLATE_H_ |
6 #define GIN_FUNCTION_TEMPLATE_H_ | 6 #define GIN_FUNCTION_TEMPLATE_H_ |
7 | 7 |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "gin/arguments.h" | 10 #include "gin/arguments.h" |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 } | 213 } |
214 }; | 214 }; |
215 | 215 |
216 } // namespace internal | 216 } // namespace internal |
217 | 217 |
218 | 218 |
219 // CreateFunctionTemplate creates a v8::FunctionTemplate that will create | 219 // CreateFunctionTemplate creates a v8::FunctionTemplate that will create |
220 // JavaScript functions that execute a provided C++ function or base::Callback. | 220 // JavaScript functions that execute a provided C++ function or base::Callback. |
221 // JavaScript arguments are automatically converted via gin::Converter, as is | 221 // JavaScript arguments are automatically converted via gin::Converter, as is |
222 // the return value of the C++ function, if any. | 222 // the return value of the C++ function, if any. |
| 223 // |
| 224 // NOTE: V8 caches FunctionTemplates for a lifetime of a web page for its own |
| 225 // internal reasons, thus it is generally a good idea to cache the template |
| 226 // returned by this function. Otherwise, repeated method invocations from JS |
| 227 // will create substantial memory leaks. See http://crbug.com/463487. |
223 template<typename Sig> | 228 template<typename Sig> |
224 v8::Local<v8::FunctionTemplate> CreateFunctionTemplate( | 229 v8::Local<v8::FunctionTemplate> CreateFunctionTemplate( |
225 v8::Isolate* isolate, const base::Callback<Sig> callback, | 230 v8::Isolate* isolate, const base::Callback<Sig> callback, |
226 int callback_flags = 0) { | 231 int callback_flags = 0) { |
227 typedef internal::CallbackHolder<Sig> HolderT; | 232 typedef internal::CallbackHolder<Sig> HolderT; |
228 HolderT* holder = new HolderT(isolate, callback, callback_flags); | 233 HolderT* holder = new HolderT(isolate, callback, callback_flags); |
229 | 234 |
230 return v8::FunctionTemplate::New( | 235 return v8::FunctionTemplate::New( |
231 isolate, | 236 isolate, |
232 &internal::Dispatcher<Sig>::DispatchToCallback, | 237 &internal::Dispatcher<Sig>::DispatchToCallback, |
(...skipping 11 matching lines...) Expand all Loading... |
244 typedef internal::CallbackHolder<Sig> HolderT; | 249 typedef internal::CallbackHolder<Sig> HolderT; |
245 HolderT* holder = new HolderT(isolate, callback, callback_flags); | 250 HolderT* holder = new HolderT(isolate, callback, callback_flags); |
246 tmpl->SetCallAsFunctionHandler(&internal::Dispatcher<Sig>::DispatchToCallback, | 251 tmpl->SetCallAsFunctionHandler(&internal::Dispatcher<Sig>::DispatchToCallback, |
247 ConvertToV8<v8::Handle<v8::External> >( | 252 ConvertToV8<v8::Handle<v8::External> >( |
248 isolate, holder->GetHandle(isolate))); | 253 isolate, holder->GetHandle(isolate))); |
249 } | 254 } |
250 | 255 |
251 } // namespace gin | 256 } // namespace gin |
252 | 257 |
253 #endif // GIN_FUNCTION_TEMPLATE_H_ | 258 #endif // GIN_FUNCTION_TEMPLATE_H_ |
OLD | NEW |