add converters for base::BindOnce and base::BindRepeating

This commit is contained in:
Shelley Vohr 2018-04-17 18:47:12 -04:00
parent 7a8a0f6b4b
commit ca406637af
No known key found for this signature in database
GPG key ID: F13993A75599653C

View file

@ -133,24 +133,38 @@ struct NativeFunctionInvoker<ReturnType(ArgTypes...)> {
} // namespace internal } // namespace internal
template<typename Sig> template <typename Sig>
struct Converter<base::Callback<Sig>> { struct Converter<base::OnceCallback<Sig>> {
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
base::OnceCallback<Sig>* out) {
if (!val->IsFunction())
return false;
*out = base::BindOnce(&internal::V8FunctionInvoker<Sig>::Go, isolate,
internal::SafeV8Function(isolate, val));
return true;
}
};
template <typename Sig>
struct Converter<base::RepeatingCallback<Sig>> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const base::Callback<Sig>& val) { const base::RepeatingCallback<Sig>& val) {
// We don't use CreateFunctionTemplate here because it creates a new // We don't use CreateFunctionTemplate here because it creates a new
// FunctionTemplate everytime, which is cached by V8 and causes leaks. // FunctionTemplate everytime, which is cached by V8 and causes leaks.
internal::Translater translater = base::Bind( internal::Translater translater =
&internal::NativeFunctionInvoker<Sig>::Go, val); base::BindRepeating(&internal::NativeFunctionInvoker<Sig>::Go, val);
return internal::CreateFunctionFromTranslater(isolate, translater); return internal::CreateFunctionFromTranslater(isolate, translater);
} }
static bool FromV8(v8::Isolate* isolate, static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val, v8::Local<v8::Value> val,
base::Callback<Sig>* out) { base::RepeatingCallback<Sig>* out) {
if (!val->IsFunction()) if (!val->IsFunction())
return false; return false;
*out = base::Bind(&internal::V8FunctionInvoker<Sig>::Go, *out = base::BindRepeating(&internal::V8FunctionInvoker<Sig>::Go, isolate,
isolate, internal::SafeV8Function(isolate, val)); internal::SafeV8Function(isolate, val));
return true; return true;
} }
}; };