Don't leak TranslaterHolder

This commit is contained in:
Cheng Zhao 2015-10-28 18:32:21 +08:00
parent 9847747736
commit 54dad72d92
2 changed files with 8 additions and 12 deletions

View file

@ -4,21 +4,22 @@
#include "atom/common/native_mate_converters/callback.h"
#include "native_mate/wrappable.h"
namespace mate {
namespace internal {
namespace {
struct TranslaterHolder {
struct TranslaterHolder : public Wrappable {
Translater translater;
};
// Cached JavaScript version of |CallTranslater|.
v8::Persistent<v8::FunctionTemplate> g_call_translater;
void CallTranslater(v8::Local<v8::External> external, mate::Arguments* args) {
TranslaterHolder* holder = static_cast<TranslaterHolder*>(external->Value());
void CallTranslater(TranslaterHolder* holder, mate::Arguments* args) {
holder->translater.Run(args);
}
@ -54,7 +55,7 @@ v8::Local<v8::Value> CreateFunctionFromTranslater(
return BindFunctionWith(isolate,
isolate->GetCurrentContext(),
call_translater->GetFunction(),
v8::External::New(isolate, holder));
holder->GetWrapper(isolate));
}
} // namespace internal

View file

@ -101,21 +101,16 @@ struct NativeFunctionInvoker<ReturnType(ArgTypes...)> {
}
};
// Create a static function that accepts generic callback.
template <typename Sig>
Translater ConvertToTranslater(const base::Callback<Sig>& val) {
return base::Bind(&NativeFunctionInvoker<Sig>::Go, val);
}
} // namespace internal
template<typename Sig>
struct Converter<base::Callback<Sig> > {
struct Converter<base::Callback<Sig>> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const base::Callback<Sig>& val) {
// We don't use CreateFunctionTemplate here because it creates a new
// FunctionTemplate everytime, which is cached by V8 and causes leaks.
internal::Translater translater = internal::ConvertToTranslater(val);
internal::Translater translater = base::Bind(
&internal::NativeFunctionInvoker<Sig>::Go, val);
return internal::CreateFunctionFromTranslater(isolate, translater);
}
static bool FromV8(v8::Isolate* isolate,