diff options
author | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2018-05-15 10:20:33 +0200 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2018-05-15 10:28:57 +0000 |
commit | d17ea114e5ef69ad5d5d7413280a13e6428098aa (patch) | |
tree | 2c01a75df69f30d27b1432467cfe7c1467a498da /chromium/gin/function_template.h | |
parent | 8c5c43c7b138c9b4b0bf56d946e61d3bbc111bec (diff) | |
download | qtwebengine-chromium-d17ea114e5ef69ad5d5d7413280a13e6428098aa.tar.gz |
BASELINE: Update Chromium to 67.0.3396.47
Change-Id: Idcb1341782e417561a2473eeecc82642dafda5b7
Reviewed-by: Michal Klocek <michal.klocek@qt.io>
Diffstat (limited to 'chromium/gin/function_template.h')
-rw-r--r-- | chromium/gin/function_template.h | 72 |
1 files changed, 44 insertions, 28 deletions
diff --git a/chromium/gin/function_template.h b/chromium/gin/function_template.h index 1e60649a091..d94acc86bee 100644 --- a/chromium/gin/function_template.h +++ b/chromium/gin/function_template.h @@ -10,6 +10,7 @@ #include "base/callback.h" #include "base/logging.h" #include "base/macros.h" +#include "base/strings/strcat.h" #include "gin/arguments.h" #include "gin/converter.h" #include "gin/gin_export.h" @@ -17,8 +18,9 @@ namespace gin { -enum CreateFunctionTemplateFlags { - HolderIsFirstArgument = 1 << 0, +struct InvokerOptions { + bool holder_is_first_argument = false; + const char* holder_type = nullptr; // Null if unknown or not applicable. }; namespace internal { @@ -66,22 +68,26 @@ class CallbackHolder : public CallbackHolderBase { public: CallbackHolder(v8::Isolate* isolate, base::RepeatingCallback<Sig> callback, - int flags) + InvokerOptions invoker_options) : CallbackHolderBase(isolate), callback(std::move(callback)), - flags(flags) {} + invoker_options(std::move(invoker_options)) {} + base::RepeatingCallback<Sig> callback; - int flags; + InvokerOptions invoker_options; + private: virtual ~CallbackHolder() {} DISALLOW_COPY_AND_ASSIGN(CallbackHolder); }; -template<typename T> -bool GetNextArgument(Arguments* args, int create_flags, bool is_first, +template <typename T> +bool GetNextArgument(Arguments* args, + const InvokerOptions& invoker_options, + bool is_first, T* result) { - if (is_first && (create_flags & HolderIsFirstArgument) != 0) { + if (is_first && invoker_options.holder_is_first_argument) { return args->GetHolder(result); } else { return args->GetNext(result); @@ -90,24 +96,35 @@ bool GetNextArgument(Arguments* args, int create_flags, bool is_first, // For advanced use cases, we allow callers to request the unparsed Arguments // object and poke around in it directly. -inline bool GetNextArgument(Arguments* args, int create_flags, bool is_first, +inline bool GetNextArgument(Arguments* args, + const InvokerOptions& invoker_options, + bool is_first, Arguments* result) { *result = *args; return true; } -inline bool GetNextArgument(Arguments* args, int create_flags, bool is_first, +inline bool GetNextArgument(Arguments* args, + const InvokerOptions& invoker_options, + bool is_first, Arguments** result) { *result = args; return true; } // It's common for clients to just need the isolate, so we make that easy. -inline bool GetNextArgument(Arguments* args, int create_flags, - bool is_first, v8::Isolate** result) { +inline bool GetNextArgument(Arguments* args, + const InvokerOptions& invoker_options, + bool is_first, + v8::Isolate** result) { *result = args->isolate(); return true; } +// Throws an error indicating conversion failure. +GIN_EXPORT void ThrowConversionError(Arguments* args, + const InvokerOptions& invoker_options, + size_t index); + // Class template for extracting and storing single argument for callback // at position |index|. template <size_t index, typename ArgType> @@ -117,14 +134,10 @@ struct ArgumentHolder { ArgLocalType value; bool ok; - ArgumentHolder(Arguments* args, int create_flags) - : ok(GetNextArgument(args, create_flags, index == 0, &value)) { - if (!ok) { - // Ideally we would include the expected c++ type in the error - // message which we can access via typeid(ArgType).name() - // however we compile with no-rtti, which disables typeid. - args->ThrowError(); - } + ArgumentHolder(Arguments* args, const InvokerOptions& invoker_options) + : ok(GetNextArgument(args, invoker_options, index == 0, &value)) { + if (!ok) + ThrowConversionError(args, invoker_options, index); } }; @@ -141,9 +154,9 @@ class Invoker<std::index_sequence<indices...>, ArgTypes...> // C++ has always been strict about the class initialization order, // so it is guaranteed ArgumentHolders will be initialized (and thus, will // extract arguments from Arguments) in the right order. - Invoker(Arguments* args, int create_flags) - : ArgumentHolder<indices, ArgTypes>(args, create_flags)..., args_(args) { - } + Invoker(Arguments* args, const InvokerOptions& invoker_options) + : ArgumentHolder<indices, ArgTypes>(args, invoker_options)..., + args_(args) {} bool IsOK() { return And(ArgumentHolder<indices, ArgTypes>::ok...); @@ -191,7 +204,7 @@ struct Dispatcher<ReturnType(ArgTypes...)> { HolderT* holder = static_cast<HolderT*>(holder_base); using Indices = std::index_sequence_for<ArgTypes...>; - Invoker<Indices, ArgTypes...> invoker(&args, holder->flags); + Invoker<Indices, ArgTypes...> invoker(&args, holder->invoker_options); if (invoker.IsOK()) invoker.DispatchToCallback(holder->callback); } @@ -199,11 +212,13 @@ struct Dispatcher<ReturnType(ArgTypes...)> { } // namespace internal - // CreateFunctionTemplate creates a v8::FunctionTemplate that will create // JavaScript functions that execute a provided C++ function or base::Callback. // JavaScript arguments are automatically converted via gin::Converter, as is -// the return value of the C++ function, if any. +// the return value of the C++ function, if any. |invoker_options| contains +// additional parameters. If it contains a holder_type, it will be used to +// provide a useful conversion error if the holder is the first argument. If not +// provided, a generic invocation error will be used. // // NOTE: V8 caches FunctionTemplates for a lifetime of a web page for its own // internal reasons, thus it is generally a good idea to cache the template @@ -213,9 +228,10 @@ template <typename Sig> v8::Local<v8::FunctionTemplate> CreateFunctionTemplate( v8::Isolate* isolate, base::RepeatingCallback<Sig> callback, - int callback_flags = 0) { + InvokerOptions invoker_options = {}) { typedef internal::CallbackHolder<Sig> HolderT; - HolderT* holder = new HolderT(isolate, std::move(callback), callback_flags); + HolderT* holder = + new HolderT(isolate, std::move(callback), std::move(invoker_options)); v8::Local<v8::FunctionTemplate> tmpl = v8::FunctionTemplate::New( isolate, &internal::Dispatcher<Sig>::DispatchToCallback, |