chore: remove native_mate (Part 10) (#20696)
* refactor: remove direct uses of event_emitter_deprecated.h * refactor: remove event_emitter_deprecated.h in api::App * refactor: use std::move to save a copy * fix: windows and linux builds
This commit is contained in:
parent
77414813b4
commit
be955a9721
24 changed files with 223 additions and 199 deletions
|
@ -24,10 +24,18 @@ v8::Local<v8::Value> ConvertToV8(v8::Isolate* isolate, T&& input) {
|
|||
template <>
|
||||
struct Converter<unsigned long> { // NOLINT(runtime/int)
|
||||
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
||||
unsigned long val); // NOLINT(runtime/int)
|
||||
unsigned long val) { // NOLINT(runtime/int)
|
||||
return v8::Integer::New(isolate, val);
|
||||
}
|
||||
static bool FromV8(v8::Isolate* isolate,
|
||||
v8::Local<v8::Value> val,
|
||||
unsigned long* out); // NOLINT(runtime/int)
|
||||
unsigned long* out) { // NOLINT(runtime/int)
|
||||
auto maybe = val->IntegerValue(isolate->GetCurrentContext());
|
||||
if (maybe.IsNothing())
|
||||
return false;
|
||||
*out = maybe.FromJust();
|
||||
return true;
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
|
|
|
@ -24,7 +24,9 @@ void PreventDefault(gin_helper::Arguments* args) {
|
|||
|
||||
} // namespace
|
||||
|
||||
v8::Local<v8::Object> CreateEventObject(v8::Isolate* isolate) {
|
||||
v8::Local<v8::Object> CreateEvent(v8::Isolate* isolate,
|
||||
v8::Local<v8::Object> sender,
|
||||
v8::Local<v8::Object> custom_event) {
|
||||
if (event_template.IsEmpty()) {
|
||||
event_template.Reset(
|
||||
isolate,
|
||||
|
@ -33,17 +35,15 @@ v8::Local<v8::Object> CreateEventObject(v8::Isolate* isolate) {
|
|||
.Build());
|
||||
}
|
||||
|
||||
return v8::Local<v8::ObjectTemplate>::New(isolate, event_template)
|
||||
->NewInstance(isolate->GetCurrentContext())
|
||||
.ToLocalChecked();
|
||||
}
|
||||
|
||||
v8::Local<v8::Object> CreateCustomEvent(v8::Isolate* isolate,
|
||||
v8::Local<v8::Object> object,
|
||||
v8::Local<v8::Object> custom_event) {
|
||||
v8::Local<v8::Object> event = CreateEventObject(isolate);
|
||||
event->SetPrototype(custom_event->CreationContext(), custom_event).IsJust();
|
||||
Dictionary(isolate, event).Set("sender", object);
|
||||
v8::Local<v8::Context> context = isolate->GetCurrentContext();
|
||||
v8::Local<v8::Object> event =
|
||||
v8::Local<v8::ObjectTemplate>::New(isolate, event_template)
|
||||
->NewInstance(context)
|
||||
.ToLocalChecked();
|
||||
if (!sender.IsEmpty())
|
||||
Dictionary(isolate, event).Set("sender", sender);
|
||||
if (!custom_event.IsEmpty())
|
||||
event->SetPrototype(context, custom_event).IsJust();
|
||||
return event;
|
||||
}
|
||||
|
||||
|
|
|
@ -16,10 +16,10 @@ namespace gin_helper {
|
|||
|
||||
namespace internal {
|
||||
|
||||
v8::Local<v8::Object> CreateEventObject(v8::Isolate* isolate);
|
||||
v8::Local<v8::Object> CreateCustomEvent(v8::Isolate* isolate,
|
||||
v8::Local<v8::Object> object,
|
||||
v8::Local<v8::Object> event);
|
||||
v8::Local<v8::Object> CreateEvent(
|
||||
v8::Isolate* isolate,
|
||||
v8::Local<v8::Object> sender = v8::Local<v8::Object>(),
|
||||
v8::Local<v8::Object> custom_event = v8::Local<v8::Object>());
|
||||
v8::Local<v8::Object> CreateEventFromFlags(v8::Isolate* isolate, int flags);
|
||||
|
||||
} // namespace internal
|
||||
|
@ -45,9 +45,9 @@ class EventEmitter : public Base {
|
|||
bool EmitCustomEvent(base::StringPiece name,
|
||||
v8::Local<v8::Object> event,
|
||||
Args&&... args) {
|
||||
return EmitWithEvent(
|
||||
name, internal::CreateCustomEvent(isolate(), GetWrapper(), event),
|
||||
std::forward<Args>(args)...);
|
||||
return EmitWithEvent(name,
|
||||
internal::CreateEvent(isolate(), GetWrapper(), event),
|
||||
std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
// this.emit(name, new Event(flags), args...);
|
||||
|
@ -67,11 +67,7 @@ class EventEmitter : public Base {
|
|||
if (wrapper.IsEmpty()) {
|
||||
return false;
|
||||
}
|
||||
v8::Local<v8::Object> event = internal::CreateEventObject(isolate());
|
||||
event
|
||||
->Set(isolate()->GetCurrentContext(),
|
||||
gin::StringToV8(isolate(), "sender"), wrapper)
|
||||
.IsJust();
|
||||
v8::Local<v8::Object> event = internal::CreateEvent(isolate(), wrapper);
|
||||
return EmitWithEvent(name, event, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
|
@ -90,7 +86,8 @@ class EventEmitter : public Base {
|
|||
v8::Locker locker(isolate);
|
||||
v8::HandleScope handle_scope(isolate);
|
||||
auto context = isolate->GetCurrentContext();
|
||||
EmitEvent(isolate, GetWrapper(), name, event, std::forward<Args>(args)...);
|
||||
gin_helper::EmitEvent(isolate, GetWrapper(), name, event,
|
||||
std::forward<Args>(args)...);
|
||||
v8::Local<v8::Value> defaultPrevented;
|
||||
if (event->Get(context, gin::StringToV8(isolate, "defaultPrevented"))
|
||||
.ToLocal(&defaultPrevented)) {
|
||||
|
|
|
@ -5,8 +5,11 @@
|
|||
#ifndef SHELL_COMMON_GIN_HELPER_FUNCTION_TEMPLATE_H_
|
||||
#define SHELL_COMMON_GIN_HELPER_FUNCTION_TEMPLATE_H_
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include "base/bind.h"
|
||||
#include "base/callback.h"
|
||||
#include "base/optional.h"
|
||||
#include "gin/arguments.h"
|
||||
#include "shell/common/gin_helper/arguments.h"
|
||||
#include "shell/common/gin_helper/destroyable.h"
|
||||
|
@ -90,6 +93,20 @@ bool GetNextArgument(gin::Arguments* args,
|
|||
}
|
||||
}
|
||||
|
||||
// Support base::Optional as output, which would be empty and do not throw error
|
||||
// when convertion to T fails.
|
||||
template <typename T>
|
||||
bool GetNextArgument(gin::Arguments* args,
|
||||
int create_flags,
|
||||
bool is_first,
|
||||
base::Optional<T>* result) {
|
||||
T converted;
|
||||
// Use gin::Arguments::GetNext which always advances |next| counter.
|
||||
if (args->GetNext(&converted))
|
||||
result->emplace(std::move(converted));
|
||||
return true;
|
||||
}
|
||||
|
||||
// For advanced use cases, we allow callers to request the unparsed Arguments
|
||||
// object and poke around in it directly.
|
||||
inline bool GetNextArgument(gin::Arguments* args,
|
||||
|
|
|
@ -228,4 +228,16 @@ struct Converter<electron::util::Promise<T>> {
|
|||
|
||||
} // namespace mate
|
||||
|
||||
namespace gin {
|
||||
|
||||
template <typename T>
|
||||
struct Converter<electron::util::Promise<T>> {
|
||||
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
||||
const electron::util::Promise<T>& val) {
|
||||
return mate::ConvertToV8(isolate, val);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace gin
|
||||
|
||||
#endif // SHELL_COMMON_PROMISE_UTIL_H_
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue