// Copyright (c) 2019 GitHub, Inc. // Use of this source code is governed by the MIT license that can be // found in the LICENSE file. #ifndef ELECTRON_SHELL_COMMON_GIN_HELPER_EVENT_EMITTER_CALLER_H_ #define ELECTRON_SHELL_COMMON_GIN_HELPER_EVENT_EMITTER_CALLER_H_ #include #include #include "gin/converter.h" #include "gin/wrappable.h" namespace gin_helper { namespace internal { using ValueVector = std::vector>; v8::Local CallMethodWithArgs(v8::Isolate* isolate, v8::Local obj, const char* method, ValueVector* args); } // namespace internal // obj.emit.apply(obj, name, args...); // The caller is responsible of allocating a HandleScope. template v8::Local EmitEvent(v8::Isolate* isolate, v8::Local obj, const StringType& name, const internal::ValueVector& args) { internal::ValueVector concatenated_args = {gin::StringToV8(isolate, name)}; concatenated_args.reserve(1 + args.size()); concatenated_args.insert(concatenated_args.end(), args.begin(), args.end()); return internal::CallMethodWithArgs(isolate, obj, "emit", &concatenated_args); } // obj.emit(name, args...); // The caller is responsible of allocating a HandleScope. template v8::Local EmitEvent(v8::Isolate* isolate, v8::Local obj, const StringType& name, Args&&... args) { internal::ValueVector converted_args = { gin::StringToV8(isolate, name), gin::ConvertToV8(isolate, std::forward(args))..., }; return internal::CallMethodWithArgs(isolate, obj, "emit", &converted_args); } // obj.custom_emit(args...) template v8::Local CustomEmit(v8::Isolate* isolate, v8::Local object, const char* custom_emit, Args&&... args) { internal::ValueVector converted_args = { gin::ConvertToV8(isolate, std::forward(args))..., }; return internal::CallMethodWithArgs(isolate, object, custom_emit, &converted_args); } template v8::Local CallMethod(v8::Isolate* isolate, gin::Wrappable* object, const char* method_name, Args&&... args) { v8::EscapableHandleScope scope(isolate); v8::Local v8_object; if (object->GetWrapper(isolate).ToLocal(&v8_object)) return scope.Escape(CustomEmit(isolate, v8_object, method_name, std::forward(args)...)); else return v8::Local(); } template v8::Local CallMethod(gin::Wrappable* object, const char* method_name, Args&&... args) { v8::Isolate* isolate = v8::Isolate::GetCurrent(); return CallMethod(isolate, object, method_name, std::forward(args)...); } } // namespace gin_helper #endif // ELECTRON_SHELL_COMMON_GIN_HELPER_EVENT_EMITTER_CALLER_H_