perf: hold Emit()
arg arrays in a std::array
(#43776)
* refactor: CallMethodWithArgs() now takes a span of value handles Co-authored-by: Charles Kerr <charles@charleskerr.com> * perf: use std::array instead of std::vector to hold Emit arg parameter packs Co-authored-by: Charles Kerr <charles@charleskerr.com> * chore: remove unused gin_helper::EmitEvent(iso, obj, name, span<Local>) Co-authored-by: Charles Kerr <charles@charleskerr.com> --------- Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: Charles Kerr <charles@charleskerr.com>
This commit is contained in:
parent
48d4adedaa
commit
948a28fdb8
2 changed files with 13 additions and 26 deletions
|
@ -9,10 +9,11 @@
|
||||||
|
|
||||||
namespace gin_helper::internal {
|
namespace gin_helper::internal {
|
||||||
|
|
||||||
v8::Local<v8::Value> CallMethodWithArgs(v8::Isolate* isolate,
|
v8::Local<v8::Value> CallMethodWithArgs(
|
||||||
|
v8::Isolate* isolate,
|
||||||
v8::Local<v8::Object> obj,
|
v8::Local<v8::Object> obj,
|
||||||
const char* method,
|
const char* method,
|
||||||
ValueVector* args) {
|
const base::span<v8::Local<v8::Value>> args) {
|
||||||
v8::EscapableHandleScope handle_scope{isolate};
|
v8::EscapableHandleScope handle_scope{isolate};
|
||||||
|
|
||||||
// CallbackScope and MakeCallback both require an active node::Environment
|
// CallbackScope and MakeCallback both require an active node::Environment
|
||||||
|
@ -29,7 +30,7 @@ v8::Local<v8::Value> CallMethodWithArgs(v8::Isolate* isolate,
|
||||||
|
|
||||||
// node::MakeCallback will also run pending tasks in Node.js.
|
// node::MakeCallback will also run pending tasks in Node.js.
|
||||||
v8::MaybeLocal<v8::Value> ret = node::MakeCallback(
|
v8::MaybeLocal<v8::Value> ret = node::MakeCallback(
|
||||||
isolate, obj, method, args->size(), args->data(), {0, 0});
|
isolate, obj, method, args.size(), args.data(), {0, 0});
|
||||||
|
|
||||||
// If the JS function throws an exception (doesn't return a value) the result
|
// 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
|
// of MakeCallback will be empty and therefore ToLocal will be false, in this
|
||||||
|
|
|
@ -5,9 +5,10 @@
|
||||||
#ifndef ELECTRON_SHELL_COMMON_GIN_HELPER_EVENT_EMITTER_CALLER_H_
|
#ifndef ELECTRON_SHELL_COMMON_GIN_HELPER_EVENT_EMITTER_CALLER_H_
|
||||||
#define ELECTRON_SHELL_COMMON_GIN_HELPER_EVENT_EMITTER_CALLER_H_
|
#define ELECTRON_SHELL_COMMON_GIN_HELPER_EVENT_EMITTER_CALLER_H_
|
||||||
|
|
||||||
|
#include <array>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
|
#include "base/containers/span.h"
|
||||||
#include "gin/converter.h"
|
#include "gin/converter.h"
|
||||||
#include "gin/wrappable.h"
|
#include "gin/wrappable.h"
|
||||||
|
|
||||||
|
@ -15,28 +16,13 @@ namespace gin_helper {
|
||||||
|
|
||||||
namespace internal {
|
namespace internal {
|
||||||
|
|
||||||
using ValueVector = std::vector<v8::Local<v8::Value>>;
|
|
||||||
|
|
||||||
v8::Local<v8::Value> CallMethodWithArgs(v8::Isolate* isolate,
|
v8::Local<v8::Value> CallMethodWithArgs(v8::Isolate* isolate,
|
||||||
v8::Local<v8::Object> obj,
|
v8::Local<v8::Object> obj,
|
||||||
const char* method,
|
const char* method,
|
||||||
ValueVector* args);
|
base::span<v8::Local<v8::Value>> args);
|
||||||
|
|
||||||
} // namespace internal
|
} // namespace internal
|
||||||
|
|
||||||
// obj.emit.apply(obj, name, args...);
|
|
||||||
// The caller is responsible of allocating a HandleScope.
|
|
||||||
template <typename StringType>
|
|
||||||
v8::Local<v8::Value> EmitEvent(v8::Isolate* isolate,
|
|
||||||
v8::Local<v8::Object> 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...);
|
// obj.emit(name, args...);
|
||||||
// The caller is responsible of allocating a HandleScope.
|
// The caller is responsible of allocating a HandleScope.
|
||||||
template <typename StringType, typename... Args>
|
template <typename StringType, typename... Args>
|
||||||
|
@ -45,12 +31,12 @@ v8::Local<v8::Value> EmitEvent(v8::Isolate* isolate,
|
||||||
const StringType& name,
|
const StringType& name,
|
||||||
Args&&... args) {
|
Args&&... args) {
|
||||||
v8::EscapableHandleScope scope{isolate};
|
v8::EscapableHandleScope scope{isolate};
|
||||||
internal::ValueVector converted_args = {
|
std::array<v8::Local<v8::Value>, 1U + sizeof...(args)> converted_args = {
|
||||||
gin::StringToV8(isolate, name),
|
gin::StringToV8(isolate, name),
|
||||||
gin::ConvertToV8(isolate, std::forward<Args>(args))...,
|
gin::ConvertToV8(isolate, std::forward<Args>(args))...,
|
||||||
};
|
};
|
||||||
return scope.Escape(
|
return scope.Escape(
|
||||||
internal::CallMethodWithArgs(isolate, obj, "emit", &converted_args));
|
internal::CallMethodWithArgs(isolate, obj, "emit", converted_args));
|
||||||
}
|
}
|
||||||
|
|
||||||
// obj.custom_emit(args...)
|
// obj.custom_emit(args...)
|
||||||
|
@ -60,11 +46,11 @@ v8::Local<v8::Value> CustomEmit(v8::Isolate* isolate,
|
||||||
const char* custom_emit,
|
const char* custom_emit,
|
||||||
Args&&... args) {
|
Args&&... args) {
|
||||||
v8::EscapableHandleScope scope{isolate};
|
v8::EscapableHandleScope scope{isolate};
|
||||||
internal::ValueVector converted_args = {
|
std::array<v8::Local<v8::Value>, sizeof...(args)> converted_args = {
|
||||||
gin::ConvertToV8(isolate, std::forward<Args>(args))...,
|
gin::ConvertToV8(isolate, std::forward<Args>(args))...,
|
||||||
};
|
};
|
||||||
return scope.Escape(internal::CallMethodWithArgs(isolate, object, custom_emit,
|
return scope.Escape(internal::CallMethodWithArgs(isolate, object, custom_emit,
|
||||||
&converted_args));
|
converted_args));
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T, typename... Args>
|
template <typename T, typename... Args>
|
||||||
|
|
Loading…
Reference in a new issue