chore: remove native_mate (Part 3) (#20131)

* use gin converter in atom_api_menu

* please only put necessary includes in header

Having include in header means they have dependency relationship,
putting arbitrary includes really really really really really makes
refacoring much harder.

* remove some simple uses of callback_converter_deprecated.h

* use gin callback converter in file_dialog code

* use gin in ErrorThrower

* use gin in atom_bundle_mover

* fix mistake in node stream

* deprecate native_mate version of event_emitter_caller

* use gin in node_bindings

* remove usages of native_mate event_emitter_caller.h except for EventEmitter

* fix compilation on Windows

* gin::Arguments behaves differently on GetNext

* just use StringToV8
This commit is contained in:
Cheng Zhao 2019-09-06 14:52:54 +09:00 committed by GitHub
parent 7be1905023
commit 2c23e44ed9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
61 changed files with 515 additions and 323 deletions

View file

@ -41,11 +41,11 @@ void CallTranslater(v8::Local<v8::External> external,
v8::Isolate* isolate = args->isolate();
auto context = isolate->GetCurrentContext();
bool one_time =
state->Has(context, mate::StringToSymbol(isolate, "oneTime")).ToChecked();
state->Has(context, gin::StringToSymbol(isolate, "oneTime")).ToChecked();
// Check if the callback has already been called.
if (one_time) {
auto called_symbol = mate::StringToSymbol(isolate, "called");
auto called_symbol = gin::StringToSymbol(isolate, "called");
if (state->Has(context, called_symbol).ToChecked()) {
args->ThrowTypeError("callback can only be called for once");
return;
@ -145,7 +145,7 @@ v8::Local<v8::Value> BindFunctionWith(v8::Isolate* isolate,
v8::Local<v8::Value> arg1,
v8::Local<v8::Value> arg2) {
v8::MaybeLocal<v8::Value> bind =
func->Get(context, mate::StringToV8(isolate, "bind"));
func->Get(context, gin::StringToV8(isolate, "bind"));
CHECK(!bind.IsEmpty());
v8::Local<v8::Function> bind_func =
v8::Local<v8::Function>::Cast(bind.ToLocalChecked());

View file

@ -10,21 +10,11 @@
#include "base/bind.h"
#include "shell/common/api/locker.h"
#include "shell/common/gin_converters/std_converter.h"
#include "shell/common/gin_helper/function_template.h"
// Implements safe convertions between JS functions and base::Callback.
namespace gin {
// Make it possible to convert move-only types.
template <typename T>
v8::Local<v8::Value> ConvertToV8(v8::Isolate* isolate, T&& input) {
return Converter<typename std::remove_reference<T>::type>::ToV8(
isolate, std::move(input));
}
} // namespace gin
namespace gin_helper {
template <typename T>

View file

@ -69,6 +69,23 @@ class Dictionary : public gin::Dictionary {
.ToChecked();
}
template <typename T>
bool SetReadOnly(base::StringPiece key, const T& val) {
v8::Local<v8::Value> v8_value;
if (!gin::TryConvertToV8(isolate(), val, &v8_value))
return false;
v8::Maybe<bool> result = GetHandle()->DefineOwnProperty(
isolate()->GetCurrentContext(), gin::StringToV8(isolate(), key),
v8_value, v8::ReadOnly);
return !result.IsNothing() && result.FromJust();
}
bool Delete(base::StringPiece key) {
v8::Maybe<bool> result = GetHandle()->Delete(
isolate()->GetCurrentContext(), gin::StringToV8(isolate(), key));
return !result.IsNothing() && result.FromJust();
}
v8::Local<v8::Object> GetHandle() const {
return gin::ConvertToV8(isolate(),
*static_cast<const gin::Dictionary*>(this))

View file

@ -0,0 +1,46 @@
// Copyright (c) 2019 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/common/gin_helper/error_thrower.h"
#include "gin/converter.h"
namespace gin_helper {
ErrorThrower::ErrorThrower(v8::Isolate* isolate) : isolate_(isolate) {}
// This constructor should be rarely if ever used, since
// v8::Isolate::GetCurrent() uses atomic loads and is thus a bit
// costly to invoke
ErrorThrower::ErrorThrower() : isolate_(v8::Isolate::GetCurrent()) {}
ErrorThrower::~ErrorThrower() = default;
void ErrorThrower::ThrowError(base::StringPiece err_msg) {
Throw(v8::Exception::Error, err_msg);
}
void ErrorThrower::ThrowTypeError(base::StringPiece err_msg) {
Throw(v8::Exception::TypeError, err_msg);
}
void ErrorThrower::ThrowRangeError(base::StringPiece err_msg) {
Throw(v8::Exception::RangeError, err_msg);
}
void ErrorThrower::ThrowReferenceError(base::StringPiece err_msg) {
Throw(v8::Exception::ReferenceError, err_msg);
}
void ErrorThrower::ThrowSyntaxError(base::StringPiece err_msg) {
Throw(v8::Exception::SyntaxError, err_msg);
}
void ErrorThrower::Throw(ErrorGenerator gen, base::StringPiece err_msg) {
v8::Local<v8::Value> exception = gen(gin::StringToV8(isolate_, err_msg));
if (!isolate_->IsExecutionTerminating())
isolate_->ThrowException(exception);
}
} // namespace gin_helper

View file

@ -0,0 +1,38 @@
// 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 SHELL_COMMON_GIN_HELPER_ERROR_THROWER_H_
#define SHELL_COMMON_GIN_HELPER_ERROR_THROWER_H_
#include "base/strings/string_piece.h"
#include "v8/include/v8.h"
namespace gin_helper {
class ErrorThrower {
public:
explicit ErrorThrower(v8::Isolate* isolate);
ErrorThrower();
~ErrorThrower();
void ThrowError(base::StringPiece err_msg);
void ThrowTypeError(base::StringPiece err_msg);
void ThrowRangeError(base::StringPiece err_msg);
void ThrowReferenceError(base::StringPiece err_msg);
void ThrowSyntaxError(base::StringPiece err_msg);
v8::Isolate* isolate() const { return isolate_; }
private:
using ErrorGenerator =
v8::Local<v8::Value> (*)(v8::Local<v8::String> err_msg);
void Throw(ErrorGenerator gen, base::StringPiece err_msg);
v8::Isolate* isolate_;
};
} // namespace gin_helper
#endif // SHELL_COMMON_GIN_HELPER_ERROR_THROWER_H_

View file

@ -0,0 +1,38 @@
// Copyright (c) 2019 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/common/gin_helper/event_emitter_caller.h"
#include "shell/common/api/locker.h"
#include "shell/common/node_includes.h"
namespace gin_helper {
namespace internal {
v8::Local<v8::Value> CallMethodWithArgs(v8::Isolate* isolate,
v8::Local<v8::Object> obj,
const char* method,
ValueVector* args) {
// Perform microtask checkpoint after running JavaScript.
v8::MicrotasksScope script_scope(isolate,
v8::MicrotasksScope::kRunMicrotasks);
// Use node::MakeCallback to call the callback, and it will also run pending
// tasks in Node.js.
v8::MaybeLocal<v8::Value> ret = node::MakeCallback(
isolate, obj, method, args->size(), &args->front(), {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
// case we need to return "false" as that indicates that the event emitter did
// not handle the event
v8::Local<v8::Value> localRet;
if (ret.ToLocal(&localRet)) {
return localRet;
}
return v8::Boolean::New(isolate, false);
}
} // namespace internal
} // namespace gin_helper

View file

@ -0,0 +1,68 @@
// 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 SHELL_COMMON_GIN_HELPER_EVENT_EMITTER_CALLER_H_
#define SHELL_COMMON_GIN_HELPER_EVENT_EMITTER_CALLER_H_
#include <utility>
#include <vector>
#include "gin/converter.h"
namespace gin_helper {
namespace internal {
using ValueVector = std::vector<v8::Local<v8::Value>>;
v8::Local<v8::Value> CallMethodWithArgs(v8::Isolate* isolate,
v8::Local<v8::Object> obj,
const char* method,
ValueVector* args);
} // 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...);
// The caller is responsible of allocating a HandleScope.
template <typename StringType, typename... Args>
v8::Local<v8::Value> EmitEvent(v8::Isolate* isolate,
v8::Local<v8::Object> obj,
const StringType& name,
Args&&... args) {
internal::ValueVector converted_args = {
gin::StringToV8(isolate, name),
gin::ConvertToV8(isolate, std::forward<Args>(args))...,
};
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,
Args&&... args) {
internal::ValueVector converted_args = {
gin::ConvertToV8(isolate, std::forward<Args>(args))...,
};
return internal::CallMethodWithArgs(isolate, object, custom_emit,
&converted_args);
}
} // namespace gin_helper
#endif // SHELL_COMMON_GIN_HELPER_EVENT_EMITTER_CALLER_H_

View file

@ -7,8 +7,8 @@
#include "base/callback.h"
#include "gin/arguments.h"
#include "shell/common/error_util.h"
#include "shell/common/gin_helper/destroyable.h"
#include "shell/common/gin_helper/error_thrower.h"
// This file is forked from gin/function_template.h with 2 differences:
// 1. Support for additional types of arguments.
@ -112,8 +112,8 @@ inline bool GetNextArgument(gin::Arguments* args,
inline bool GetNextArgument(gin::Arguments* args,
int create_flags,
bool is_first,
electron::util::ErrorThrower* result) {
*result = electron::util::ErrorThrower(args->isolate());
ErrorThrower* result) {
*result = ErrorThrower(args->isolate());
return true;
}