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:
parent
7be1905023
commit
2c23e44ed9
61 changed files with 515 additions and 323 deletions
|
@ -7,11 +7,11 @@
|
|||
#include <vector>
|
||||
|
||||
#include "native_mate/arguments.h"
|
||||
#include "native_mate/dictionary.h"
|
||||
#include "native_mate/object_template_builder.h"
|
||||
#include "native_mate/wrappable.h"
|
||||
#include "shell/common/asar/archive.h"
|
||||
#include "shell/common/native_mate_converters/callback_converter_deprecated.h"
|
||||
#include "shell/common/gin_converters/callback_converter.h"
|
||||
#include "shell/common/gin_helper/dictionary.h"
|
||||
#include "shell/common/native_mate_converters/file_path_converter.h"
|
||||
#include "shell/common/node_includes.h"
|
||||
#include "third_party/electron_node/src/node_native_module_env.h"
|
||||
|
@ -56,7 +56,7 @@ class Archive : public mate::Wrappable<Archive> {
|
|||
asar::Archive::FileInfo info;
|
||||
if (!archive_ || !archive_->GetFileInfo(path, &info))
|
||||
return v8::False(isolate);
|
||||
mate::Dictionary dict(isolate, v8::Object::New(isolate));
|
||||
gin_helper::Dictionary dict(isolate, v8::Object::New(isolate));
|
||||
dict.Set("size", info.size);
|
||||
dict.Set("unpacked", info.unpacked);
|
||||
dict.Set("offset", info.offset);
|
||||
|
@ -68,7 +68,7 @@ class Archive : public mate::Wrappable<Archive> {
|
|||
asar::Archive::Stats stats;
|
||||
if (!archive_ || !archive_->Stat(path, &stats))
|
||||
return v8::False(isolate);
|
||||
mate::Dictionary dict(isolate, v8::Object::New(isolate));
|
||||
gin_helper::Dictionary dict(isolate, v8::Object::New(isolate));
|
||||
dict.Set("size", stats.size);
|
||||
dict.Set("offset", stats.offset);
|
||||
dict.Set("isFile", stats.is_file);
|
||||
|
@ -131,7 +131,7 @@ void Initialize(v8::Local<v8::Object> exports,
|
|||
v8::Local<v8::Value> unused,
|
||||
v8::Local<v8::Context> context,
|
||||
void* priv) {
|
||||
mate::Dictionary dict(context->GetIsolate(), exports);
|
||||
gin_helper::Dictionary dict(context->GetIsolate(), exports);
|
||||
dict.SetMethod("createArchive", &Archive::Create);
|
||||
dict.SetMethod("initAsarSupport", &InitAsarSupport);
|
||||
}
|
||||
|
|
|
@ -4,9 +4,10 @@
|
|||
|
||||
#include <string>
|
||||
|
||||
#include "native_mate/dictionary.h"
|
||||
#include "shell/common/native_mate_converters/callback_converter_deprecated.h"
|
||||
#include "shell/common/native_mate_converters/file_path_converter.h"
|
||||
#include "shell/common/gin_converters/callback_converter.h"
|
||||
#include "shell/common/gin_converters/file_path_converter.h"
|
||||
#include "shell/common/gin_helper/dictionary.h"
|
||||
#include "shell/common/gin_helper/error_thrower.h"
|
||||
#include "shell/common/native_mate_converters/gurl_converter.h"
|
||||
#include "shell/common/native_mate_converters/string16_converter.h"
|
||||
#include "shell/common/node_includes.h"
|
||||
|
@ -17,7 +18,7 @@
|
|||
#include "base/win/scoped_com_initializer.h"
|
||||
#include "base/win/shortcut.h"
|
||||
|
||||
namespace mate {
|
||||
namespace gin {
|
||||
|
||||
template <>
|
||||
struct Converter<base::win::ShortcutOperation> {
|
||||
|
@ -39,7 +40,7 @@ struct Converter<base::win::ShortcutOperation> {
|
|||
}
|
||||
};
|
||||
|
||||
} // namespace mate
|
||||
} // namespace gin
|
||||
#endif
|
||||
|
||||
namespace {
|
||||
|
@ -52,13 +53,13 @@ void OnOpenExternalFinished(electron::util::Promise<void*> promise,
|
|||
promise.RejectWithErrorMessage(error.c_str());
|
||||
}
|
||||
|
||||
v8::Local<v8::Promise> OpenExternal(const GURL& url, mate::Arguments* args) {
|
||||
v8::Local<v8::Promise> OpenExternal(const GURL& url, gin::Arguments* args) {
|
||||
electron::util::Promise<void*> promise(args->isolate());
|
||||
v8::Local<v8::Promise> handle = promise.GetHandle();
|
||||
|
||||
platform_util::OpenExternalOptions options;
|
||||
if (args->Length() >= 2) {
|
||||
mate::Dictionary obj;
|
||||
gin::Dictionary obj(nullptr);
|
||||
if (args->GetNext(&obj)) {
|
||||
obj.Get("activate", &options.activate);
|
||||
obj.Get("workingDirectory", &options.working_dir);
|
||||
|
@ -71,7 +72,7 @@ v8::Local<v8::Promise> OpenExternal(const GURL& url, mate::Arguments* args) {
|
|||
return handle;
|
||||
}
|
||||
|
||||
bool MoveItemToTrash(mate::Arguments* args) {
|
||||
bool MoveItemToTrash(gin::Arguments* args) {
|
||||
base::FilePath full_path;
|
||||
args->GetNext(&full_path);
|
||||
|
||||
|
@ -83,10 +84,11 @@ bool MoveItemToTrash(mate::Arguments* args) {
|
|||
|
||||
#if defined(OS_WIN)
|
||||
bool WriteShortcutLink(const base::FilePath& shortcut_path,
|
||||
mate::Arguments* args) {
|
||||
gin::Arguments* args) {
|
||||
base::win::ShortcutOperation operation = base::win::SHORTCUT_CREATE_ALWAYS;
|
||||
args->GetNext(&operation);
|
||||
mate::Dictionary options = mate::Dictionary::CreateEmpty(args->isolate());
|
||||
if (gin::ConvertFromV8(args->isolate(), args->PeekNext(), &operation))
|
||||
args->Skip();
|
||||
gin::Dictionary options = gin::Dictionary::CreateEmpty(args->isolate());
|
||||
if (!args->GetNext(&options)) {
|
||||
args->ThrowError();
|
||||
return false;
|
||||
|
@ -114,16 +116,16 @@ bool WriteShortcutLink(const base::FilePath& shortcut_path,
|
|||
operation);
|
||||
}
|
||||
|
||||
v8::Local<v8::Value> ReadShortcutLink(mate::Arguments* args,
|
||||
v8::Local<v8::Value> ReadShortcutLink(gin_helper::ErrorThrower thrower,
|
||||
const base::FilePath& path) {
|
||||
using base::win::ShortcutProperties;
|
||||
mate::Dictionary options = mate::Dictionary::CreateEmpty(args->isolate());
|
||||
gin::Dictionary options = gin::Dictionary::CreateEmpty(thrower.isolate());
|
||||
base::win::ScopedCOMInitializer com_initializer;
|
||||
base::win::ShortcutProperties properties;
|
||||
if (!base::win::ResolveShortcutProperties(
|
||||
path, ShortcutProperties::PROPERTIES_ALL, &properties)) {
|
||||
args->ThrowError("Failed to read shortcut link");
|
||||
return v8::Null(args->isolate());
|
||||
thrower.ThrowError("Failed to read shortcut link");
|
||||
return v8::Null(thrower.isolate());
|
||||
}
|
||||
options.Set("target", properties.target);
|
||||
options.Set("cwd", properties.working_dir);
|
||||
|
@ -132,7 +134,7 @@ v8::Local<v8::Value> ReadShortcutLink(mate::Arguments* args,
|
|||
options.Set("icon", properties.icon);
|
||||
options.Set("iconIndex", properties.icon_index);
|
||||
options.Set("appUserModelId", properties.app_id);
|
||||
return options.GetHandle();
|
||||
return gin::ConvertToV8(thrower.isolate(), options);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
@ -140,7 +142,7 @@ void Initialize(v8::Local<v8::Object> exports,
|
|||
v8::Local<v8::Value> unused,
|
||||
v8::Local<v8::Context> context,
|
||||
void* priv) {
|
||||
mate::Dictionary dict(context->GetIsolate(), exports);
|
||||
gin_helper::Dictionary dict(context->GetIsolate(), exports);
|
||||
dict.SetMethod("showItemInFolder", &platform_util::ShowItemInFolder);
|
||||
dict.SetMethod("openItem", &platform_util::OpenItem);
|
||||
dict.SetMethod("openExternal", &OpenExternal);
|
||||
|
|
38
shell/common/api/event_emitter_caller_deprecated.cc
Normal file
38
shell/common/api/event_emitter_caller_deprecated.cc
Normal file
|
@ -0,0 +1,38 @@
|
|||
// Copyright (c) 2015 GitHub, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "shell/common/api/event_emitter_caller_deprecated.h"
|
||||
|
||||
#include "shell/common/api/locker.h"
|
||||
#include "shell/common/node_includes.h"
|
||||
|
||||
namespace mate {
|
||||
|
||||
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 mate
|
73
shell/common/api/event_emitter_caller_deprecated.h
Normal file
73
shell/common/api/event_emitter_caller_deprecated.h
Normal file
|
@ -0,0 +1,73 @@
|
|||
// Copyright (c) 2015 GitHub, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef SHELL_COMMON_API_EVENT_EMITTER_CALLER_DEPRECATED_H_
|
||||
#define SHELL_COMMON_API_EVENT_EMITTER_CALLER_DEPRECATED_H_
|
||||
|
||||
// =============================== NOTICE ===============================
|
||||
// Do not add code here, native_mate is being removed. Any new code
|
||||
// should use gin_helper version instead.
|
||||
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "native_mate/converter.h"
|
||||
#include "shell/common/native_mate_converters/string16_converter.h"
|
||||
|
||||
namespace mate {
|
||||
|
||||
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 = {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 = {
|
||||
StringToV8(isolate, name),
|
||||
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 = {
|
||||
ConvertToV8(isolate, std::forward<Args>(args))...,
|
||||
};
|
||||
return internal::CallMethodWithArgs(isolate, object, custom_emit,
|
||||
&converted_args);
|
||||
}
|
||||
|
||||
} // namespace mate
|
||||
|
||||
#endif // SHELL_COMMON_API_EVENT_EMITTER_CALLER_DEPRECATED_H_
|
|
@ -4,17 +4,15 @@
|
|||
|
||||
#include "shell/common/deprecate_util.h"
|
||||
|
||||
#include "base/callback.h"
|
||||
#include "native_mate/converter.h"
|
||||
#include "native_mate/dictionary.h"
|
||||
#include "shell/common/native_mate_converters/callback_converter_deprecated.h"
|
||||
#include "gin/dictionary.h"
|
||||
#include "shell/common/gin_converters/callback_converter.h"
|
||||
|
||||
namespace electron {
|
||||
|
||||
void EmitDeprecationWarning(node::Environment* env,
|
||||
const std::string& warning_msg,
|
||||
const std::string& warning_type) {
|
||||
mate::Dictionary process(env->isolate(), env->process_object());
|
||||
gin::Dictionary process(env->isolate(), env->process_object());
|
||||
|
||||
base::RepeatingCallback<void(base::StringPiece, base::StringPiece,
|
||||
base::StringPiece)>
|
||||
|
|
|
@ -1,47 +0,0 @@
|
|||
// 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_ERROR_UTIL_H_
|
||||
#define SHELL_COMMON_ERROR_UTIL_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "native_mate/converter.h"
|
||||
|
||||
namespace electron {
|
||||
|
||||
namespace util {
|
||||
|
||||
class ErrorThrower {
|
||||
public:
|
||||
explicit ErrorThrower(v8::Isolate* isolate);
|
||||
ErrorThrower();
|
||||
|
||||
~ErrorThrower();
|
||||
|
||||
void ThrowError(const std::string& err_msg);
|
||||
void ThrowTypeError(const std::string& err_msg);
|
||||
void ThrowRangeError(const std::string& err_msg);
|
||||
void ThrowReferenceError(const std::string& err_msg);
|
||||
void ThrowSyntaxError(const std::string& err_msg);
|
||||
|
||||
private:
|
||||
v8::Isolate* isolate() const { return isolate_; }
|
||||
|
||||
using ErrorGenerator =
|
||||
v8::Local<v8::Value> (*)(v8::Local<v8::String> err_msg);
|
||||
void Throw(ErrorGenerator gen, const std::string& err_msg) {
|
||||
v8::Local<v8::Value> exception = gen(mate::StringToV8(isolate_, err_msg));
|
||||
if (!isolate_->IsExecutionTerminating())
|
||||
isolate_->ThrowException(exception);
|
||||
}
|
||||
|
||||
v8::Isolate* isolate_;
|
||||
};
|
||||
|
||||
} // namespace util
|
||||
|
||||
} // namespace electron
|
||||
|
||||
#endif // SHELL_COMMON_ERROR_UTIL_H_
|
|
@ -6,11 +6,30 @@
|
|||
#define SHELL_COMMON_GIN_CONVERTERS_STD_CONVERTER_H_
|
||||
|
||||
#include <set>
|
||||
#include <utility>
|
||||
|
||||
#include "gin/converter.h"
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
#if !defined(OS_LINUX) && !defined(OS_FREEBSD)
|
||||
template <>
|
||||
struct Converter<unsigned long> { // NOLINT(runtime/int)
|
||||
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
||||
unsigned long val); // NOLINT(runtime/int)
|
||||
static bool FromV8(v8::Isolate* isolate,
|
||||
v8::Local<v8::Value> val,
|
||||
unsigned long* out); // NOLINT(runtime/int)
|
||||
};
|
||||
#endif
|
||||
|
||||
template <>
|
||||
struct Converter<v8::Local<v8::Array>> {
|
||||
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
||||
|
|
|
@ -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());
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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))
|
||||
|
|
|
@ -2,14 +2,11 @@
|
|||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include <string>
|
||||
#include "shell/common/gin_helper/error_thrower.h"
|
||||
|
||||
#include "native_mate/converter.h"
|
||||
#include "shell/common/error_util.h"
|
||||
#include "gin/converter.h"
|
||||
|
||||
namespace electron {
|
||||
|
||||
namespace util {
|
||||
namespace gin_helper {
|
||||
|
||||
ErrorThrower::ErrorThrower(v8::Isolate* isolate) : isolate_(isolate) {}
|
||||
|
||||
|
@ -20,26 +17,30 @@ ErrorThrower::ErrorThrower() : isolate_(v8::Isolate::GetCurrent()) {}
|
|||
|
||||
ErrorThrower::~ErrorThrower() = default;
|
||||
|
||||
void ErrorThrower::ThrowError(const std::string& err_msg) {
|
||||
void ErrorThrower::ThrowError(base::StringPiece err_msg) {
|
||||
Throw(v8::Exception::Error, err_msg);
|
||||
}
|
||||
|
||||
void ErrorThrower::ThrowTypeError(const std::string& err_msg) {
|
||||
void ErrorThrower::ThrowTypeError(base::StringPiece err_msg) {
|
||||
Throw(v8::Exception::TypeError, err_msg);
|
||||
}
|
||||
|
||||
void ErrorThrower::ThrowRangeError(const std::string& err_msg) {
|
||||
void ErrorThrower::ThrowRangeError(base::StringPiece err_msg) {
|
||||
Throw(v8::Exception::RangeError, err_msg);
|
||||
}
|
||||
|
||||
void ErrorThrower::ThrowReferenceError(const std::string& err_msg) {
|
||||
void ErrorThrower::ThrowReferenceError(base::StringPiece err_msg) {
|
||||
Throw(v8::Exception::ReferenceError, err_msg);
|
||||
}
|
||||
|
||||
void ErrorThrower::ThrowSyntaxError(const std::string& err_msg) {
|
||||
void ErrorThrower::ThrowSyntaxError(base::StringPiece err_msg) {
|
||||
Throw(v8::Exception::SyntaxError, err_msg);
|
||||
}
|
||||
|
||||
} // namespace util
|
||||
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 electron
|
||||
} // namespace gin_helper
|
38
shell/common/gin_helper/error_thrower.h
Normal file
38
shell/common/gin_helper/error_thrower.h
Normal 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_
|
|
@ -1,13 +1,13 @@
|
|||
// Copyright (c) 2015 GitHub, Inc.
|
||||
// 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/api/event_emitter_caller.h"
|
||||
#include "shell/common/gin_helper/event_emitter_caller.h"
|
||||
|
||||
#include "shell/common/api/locker.h"
|
||||
#include "shell/common/node_includes.h"
|
||||
|
||||
namespace mate {
|
||||
namespace gin_helper {
|
||||
|
||||
namespace internal {
|
||||
|
||||
|
@ -35,4 +35,4 @@ v8::Local<v8::Value> CallMethodWithArgs(v8::Isolate* isolate,
|
|||
|
||||
} // namespace internal
|
||||
|
||||
} // namespace mate
|
||||
} // namespace gin_helper
|
|
@ -1,17 +1,16 @@
|
|||
// Copyright (c) 2015 GitHub, Inc.
|
||||
// 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_API_EVENT_EMITTER_CALLER_H_
|
||||
#define SHELL_COMMON_API_EVENT_EMITTER_CALLER_H_
|
||||
#ifndef SHELL_COMMON_GIN_HELPER_EVENT_EMITTER_CALLER_H_
|
||||
#define SHELL_COMMON_GIN_HELPER_EVENT_EMITTER_CALLER_H_
|
||||
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "native_mate/converter.h"
|
||||
#include "shell/common/native_mate_converters/string16_converter.h"
|
||||
#include "gin/converter.h"
|
||||
|
||||
namespace mate {
|
||||
namespace gin_helper {
|
||||
|
||||
namespace internal {
|
||||
|
||||
|
@ -31,7 +30,7 @@ v8::Local<v8::Value> EmitEvent(v8::Isolate* isolate,
|
|||
v8::Local<v8::Object> obj,
|
||||
const StringType& name,
|
||||
const internal::ValueVector& args) {
|
||||
internal::ValueVector concatenated_args = {StringToV8(isolate, name)};
|
||||
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);
|
||||
|
@ -45,8 +44,8 @@ v8::Local<v8::Value> EmitEvent(v8::Isolate* isolate,
|
|||
const StringType& name,
|
||||
Args&&... args) {
|
||||
internal::ValueVector converted_args = {
|
||||
StringToV8(isolate, name),
|
||||
ConvertToV8(isolate, std::forward<Args>(args))...,
|
||||
gin::StringToV8(isolate, name),
|
||||
gin::ConvertToV8(isolate, std::forward<Args>(args))...,
|
||||
};
|
||||
return internal::CallMethodWithArgs(isolate, obj, "emit", &converted_args);
|
||||
}
|
||||
|
@ -58,12 +57,12 @@ v8::Local<v8::Value> CustomEmit(v8::Isolate* isolate,
|
|||
const char* custom_emit,
|
||||
Args&&... args) {
|
||||
internal::ValueVector converted_args = {
|
||||
ConvertToV8(isolate, std::forward<Args>(args))...,
|
||||
gin::ConvertToV8(isolate, std::forward<Args>(args))...,
|
||||
};
|
||||
return internal::CallMethodWithArgs(isolate, object, custom_emit,
|
||||
&converted_args);
|
||||
}
|
||||
|
||||
} // namespace mate
|
||||
} // namespace gin_helper
|
||||
|
||||
#endif // SHELL_COMMON_API_EVENT_EMITTER_CALLER_H_
|
||||
#endif // SHELL_COMMON_GIN_HELPER_EVENT_EMITTER_CALLER_H_
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -22,12 +22,12 @@
|
|||
#include "content/public/browser/browser_thread.h"
|
||||
#include "content/public/common/content_paths.h"
|
||||
#include "electron/buildflags/buildflags.h"
|
||||
#include "native_mate/dictionary.h"
|
||||
#include "shell/common/api/event_emitter_caller.h"
|
||||
#include "shell/common/api/locker.h"
|
||||
#include "shell/common/atom_command_line.h"
|
||||
#include "shell/common/gin_converters/file_path_converter.h"
|
||||
#include "shell/common/gin_helper/dictionary.h"
|
||||
#include "shell/common/gin_helper/event_emitter_caller.h"
|
||||
#include "shell/common/mac/main_application_bundle.h"
|
||||
#include "shell/common/native_mate_converters/file_path_converter.h"
|
||||
#include "shell/common/node_includes.h"
|
||||
|
||||
#define ELECTRON_BUILTIN_MODULES(V) \
|
||||
|
@ -314,7 +314,7 @@ node::Environment* NodeBindings::CreateEnvironment(
|
|||
break;
|
||||
}
|
||||
|
||||
mate::Dictionary global(context->GetIsolate(), context->Global());
|
||||
gin_helper::Dictionary global(context->GetIsolate(), context->Global());
|
||||
// Do not set DOM globals for renderer process.
|
||||
// We must set this before the node bootstrapper which is run inside
|
||||
// CreateEnvironment
|
||||
|
@ -351,7 +351,7 @@ node::Environment* NodeBindings::CreateEnvironment(
|
|||
context->GetIsolate()->SetMicrotasksPolicy(v8::MicrotasksPolicy::kScoped);
|
||||
}
|
||||
|
||||
mate::Dictionary process(context->GetIsolate(), env->process_object());
|
||||
gin_helper::Dictionary process(context->GetIsolate(), env->process_object());
|
||||
process.SetReadOnly("type", process_type);
|
||||
process.Set("resourcesPath", resources_path);
|
||||
// The path to helper app.
|
||||
|
@ -364,7 +364,7 @@ node::Environment* NodeBindings::CreateEnvironment(
|
|||
|
||||
void NodeBindings::LoadEnvironment(node::Environment* env) {
|
||||
node::LoadEnvironment(env);
|
||||
mate::EmitEvent(env->isolate(), env->process_object(), "loaded");
|
||||
gin_helper::EmitEvent(env->isolate(), env->process_object(), "loaded");
|
||||
}
|
||||
|
||||
void NodeBindings::PrepareMessageLoop() {
|
||||
|
|
|
@ -14,9 +14,7 @@
|
|||
#include "content/public/browser/browser_task_traits.h"
|
||||
#include "content/public/browser/browser_thread.h"
|
||||
#include "native_mate/converter.h"
|
||||
#include "shell/common/api/locker.h"
|
||||
#include "shell/common/native_mate_converters/callback_converter_deprecated.h"
|
||||
#include "shell/common/native_mate_converters/once_callback.h"
|
||||
#include "shell/common/gin_converters/std_converter.h"
|
||||
|
||||
namespace electron {
|
||||
|
||||
|
@ -142,7 +140,7 @@ class Promise {
|
|||
v8::Context::Scope context_scope(
|
||||
v8::Local<v8::Context>::New(isolate(), GetContext()));
|
||||
|
||||
v8::Local<v8::Value> value = mate::ConvertToV8(isolate(), cb);
|
||||
v8::Local<v8::Value> value = gin::ConvertToV8(isolate(), std::move(cb));
|
||||
v8::Local<v8::Function> handler = v8::Local<v8::Function>::Cast(value);
|
||||
|
||||
return GetHandle()->Then(GetContext(), handler);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue