2019-09-06 05:52:54 +00:00
|
|
|
// Copyright (c) 2019 GitHub, Inc.
|
2015-06-23 11:46:37 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-09-06 05:52:54 +00:00
|
|
|
#ifndef SHELL_COMMON_GIN_HELPER_EVENT_EMITTER_CALLER_H_
|
|
|
|
#define SHELL_COMMON_GIN_HELPER_EVENT_EMITTER_CALLER_H_
|
2015-06-23 11:46:37 +00:00
|
|
|
|
2019-04-27 04:42:56 +00:00
|
|
|
#include <utility>
|
2015-06-23 11:46:37 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2019-09-06 05:52:54 +00:00
|
|
|
#include "gin/converter.h"
|
2020-03-19 18:35:11 +00:00
|
|
|
#include "gin/wrappable.h"
|
2015-06-23 11:46:37 +00:00
|
|
|
|
2019-09-06 05:52:54 +00:00
|
|
|
namespace gin_helper {
|
2015-06-23 11:46:37 +00:00
|
|
|
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
using ValueVector = std::vector<v8::Local<v8::Value>>;
|
|
|
|
|
2016-10-26 09:10:15 +00:00
|
|
|
v8::Local<v8::Value> CallMethodWithArgs(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Object> obj,
|
|
|
|
const char* method,
|
|
|
|
ValueVector* args);
|
2015-06-23 11:46:37 +00:00
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
|
2015-06-23 12:14:03 +00:00
|
|
|
// obj.emit.apply(obj, name, args...);
|
|
|
|
// The caller is responsible of allocating a HandleScope.
|
2016-10-26 09:10:15 +00:00
|
|
|
template <typename StringType>
|
2015-06-23 12:14:03 +00:00
|
|
|
v8::Local<v8::Value> EmitEvent(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Object> obj,
|
|
|
|
const StringType& name,
|
|
|
|
const internal::ValueVector& args) {
|
2019-09-06 05:52:54 +00:00
|
|
|
internal::ValueVector concatenated_args = {gin::StringToV8(isolate, name)};
|
2015-06-23 12:14:03 +00:00
|
|
|
concatenated_args.reserve(1 + args.size());
|
|
|
|
concatenated_args.insert(concatenated_args.end(), args.begin(), args.end());
|
2016-10-26 09:10:15 +00:00
|
|
|
return internal::CallMethodWithArgs(isolate, obj, "emit", &concatenated_args);
|
2015-06-23 12:14:03 +00:00
|
|
|
}
|
|
|
|
|
2015-06-23 11:46:37 +00:00
|
|
|
// obj.emit(name, args...);
|
|
|
|
// The caller is responsible of allocating a HandleScope.
|
2016-10-26 09:10:15 +00:00
|
|
|
template <typename StringType, typename... Args>
|
2015-06-23 11:46:37 +00:00
|
|
|
v8::Local<v8::Value> EmitEvent(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Object> obj,
|
2015-06-23 12:14:03 +00:00
|
|
|
const StringType& name,
|
2019-04-27 04:42:56 +00:00
|
|
|
Args&&... args) {
|
2015-06-23 11:46:37 +00:00
|
|
|
internal::ValueVector converted_args = {
|
2019-09-06 05:52:54 +00:00
|
|
|
gin::StringToV8(isolate, name),
|
|
|
|
gin::ConvertToV8(isolate, std::forward<Args>(args))...,
|
2016-10-26 09:10:15 +00:00
|
|
|
};
|
|
|
|
return internal::CallMethodWithArgs(isolate, obj, "emit", &converted_args);
|
|
|
|
}
|
|
|
|
|
|
|
|
// obj.custom_emit(args...)
|
|
|
|
template <typename... Args>
|
|
|
|
v8::Local<v8::Value> CustomEmit(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Object> object,
|
|
|
|
const char* custom_emit,
|
2019-04-27 04:42:56 +00:00
|
|
|
Args&&... args) {
|
2016-10-26 09:10:15 +00:00
|
|
|
internal::ValueVector converted_args = {
|
2019-09-06 05:52:54 +00:00
|
|
|
gin::ConvertToV8(isolate, std::forward<Args>(args))...,
|
2015-06-23 11:46:37 +00:00
|
|
|
};
|
2016-10-26 09:10:15 +00:00
|
|
|
return internal::CallMethodWithArgs(isolate, object, custom_emit,
|
|
|
|
&converted_args);
|
2015-06-23 11:46:37 +00:00
|
|
|
}
|
|
|
|
|
2020-03-19 18:35:11 +00:00
|
|
|
template <typename T, typename... Args>
|
|
|
|
v8::Local<v8::Value> CallMethod(v8::Isolate* isolate,
|
|
|
|
gin::Wrappable<T>* object,
|
|
|
|
const char* method_name,
|
|
|
|
Args&&... args) {
|
|
|
|
v8::HandleScope scope(isolate);
|
|
|
|
v8::Local<v8::Object> v8_object;
|
|
|
|
if (object->GetWrapper(isolate).ToLocal(&v8_object))
|
|
|
|
return CustomEmit(isolate, v8_object, method_name,
|
|
|
|
std::forward<Args>(args)...);
|
|
|
|
else
|
|
|
|
return v8::Local<v8::Value>();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename... Args>
|
|
|
|
v8::Local<v8::Value> CallMethod(gin::Wrappable<T>* object,
|
|
|
|
const char* method_name,
|
|
|
|
Args&&... args) {
|
|
|
|
v8::Isolate* isolate = v8::Isolate::GetCurrent();
|
|
|
|
return CallMethod(isolate, object, method_name, std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
2019-09-06 05:52:54 +00:00
|
|
|
} // namespace gin_helper
|
2015-06-23 11:46:37 +00:00
|
|
|
|
2019-09-06 05:52:54 +00:00
|
|
|
#endif // SHELL_COMMON_GIN_HELPER_EVENT_EMITTER_CALLER_H_
|