refactor: remove unnecessary template type in EmitEvent() (#48778)

refactor: remove unnecessary template type in EmitEvent()

refactor: CallMethodWithArgs() takes a std::string_view
This commit is contained in:
Charles Kerr
2025-11-05 17:28:33 -06:00
committed by GitHub
parent 1f78d2258c
commit c460992407
2 changed files with 13 additions and 11 deletions

View File

@@ -11,7 +11,7 @@ namespace gin_helper::internal {
v8::Local<v8::Value> CallMethodWithArgs(
v8::Isolate* isolate,
v8::Local<v8::Object> obj,
const char* method,
const std::string_view method,
const base::span<v8::Local<v8::Value>> args) {
v8::EscapableHandleScope handle_scope{isolate};
@@ -27,8 +27,9 @@ v8::Local<v8::Value> CallMethodWithArgs(
v8::MicrotasksScope::kRunMicrotasks);
// node::MakeCallback will also run pending tasks in Node.js.
v8::MaybeLocal<v8::Value> ret = node::MakeCallback(
isolate, obj, method, args.size(), args.data(), {0, 0});
v8::MaybeLocal<v8::Value> ret =
node::MakeCallback(isolate, obj, gin::StringToV8(isolate, method),
args.size(), args.data(), {0, 0});
// If the JS function throws an exception (doesn't return a value) the result
// of MakeCallback will be empty and therefore ToLocal will be false, in this

View File

@@ -6,6 +6,7 @@
#define ELECTRON_SHELL_COMMON_GIN_HELPER_EVENT_EMITTER_CALLER_H_
#include <array>
#include <string_view>
#include <utility>
#include "base/containers/span.h"
@@ -20,17 +21,17 @@ namespace internal {
v8::Local<v8::Value> CallMethodWithArgs(v8::Isolate* isolate,
v8::Local<v8::Object> obj,
const char* method,
std::string_view method,
base::span<v8::Local<v8::Value>> args);
} // namespace internal
// obj.emit(name, args...);
// The caller is responsible of allocating a HandleScope.
template <typename StringType, typename... Args>
template <typename... Args>
v8::Local<v8::Value> EmitEvent(v8::Isolate* isolate,
v8::Local<v8::Object> obj,
const StringType& name,
const std::string_view name,
Args&&... args) {
v8::EscapableHandleScope scope{isolate};
std::array<v8::Local<v8::Value>, 1U + sizeof...(args)> converted_args = {
@@ -45,7 +46,7 @@ v8::Local<v8::Value> EmitEvent(v8::Isolate* isolate,
template <typename... Args>
v8::Local<v8::Value> CustomEmit(v8::Isolate* isolate,
v8::Local<v8::Object> object,
const char* custom_emit,
const std::string_view custom_emit,
Args&&... args) {
v8::EscapableHandleScope scope{isolate};
std::array<v8::Local<v8::Value>, sizeof...(args)> converted_args = {
@@ -58,7 +59,7 @@ v8::Local<v8::Value> CustomEmit(v8::Isolate* isolate,
template <typename T, typename... Args>
v8::Local<v8::Value> CallMethod(v8::Isolate* isolate,
gin_helper::DeprecatedWrappable<T>* object,
const char* method_name,
const std::string_view method_name,
Args&&... args) {
v8::EscapableHandleScope scope(isolate);
v8::Local<v8::Object> v8_object;
@@ -71,7 +72,7 @@ v8::Local<v8::Value> CallMethod(v8::Isolate* isolate,
template <typename T, typename... Args>
v8::Local<v8::Value> CallMethod(gin_helper::DeprecatedWrappable<T>* object,
const char* method_name,
const std::string_view method_name,
Args&&... args) {
v8::Isolate* isolate = v8::Isolate::GetCurrent();
return CallMethod(isolate, object, method_name, std::forward<Args>(args)...);
@@ -80,7 +81,7 @@ v8::Local<v8::Value> CallMethod(gin_helper::DeprecatedWrappable<T>* object,
template <typename T, typename... Args>
v8::Local<v8::Value> CallMethod(v8::Isolate* isolate,
gin::Wrappable<T>* object,
const char* method_name,
const std::string_view method_name,
Args&&... args) {
v8::EscapableHandleScope scope(isolate);
v8::Local<v8::Object> v8_object;
@@ -93,7 +94,7 @@ v8::Local<v8::Value> CallMethod(v8::Isolate* isolate,
template <typename T, typename... Args>
v8::Local<v8::Value> CallMethod(gin::Wrappable<T>* object,
const char* method_name,
const std::string_view method_name,
Args&&... args) {
v8::Isolate* isolate = v8::Isolate::GetCurrent();
return CallMethod(isolate, object, method_name, std::forward<Args>(args)...);