refactor: make util::Promise type safe when chaining in native (#19809)

* refactor: make util::Promise type safe when chaining in native

* fixup! refactor: make util::Promise type safe when chaining in native

* chore: remove spare brackets
This commit is contained in:
Samuel Attard 2019-08-22 17:03:28 -07:00 committed by GitHub
parent f7e3e1f97a
commit 6a3922d330
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
39 changed files with 275 additions and 343 deletions

View file

@ -2,71 +2,21 @@
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include <string>
#include "shell/common/api/locker.h"
#include "shell/common/promise_util.h"
namespace electron {
namespace util {
Promise::Promise(v8::Isolate* isolate)
: Promise(isolate,
v8::Promise::Resolver::New(isolate->GetCurrentContext())
.ToLocalChecked()) {}
Promise::Promise(v8::Isolate* isolate, v8::Local<v8::Promise::Resolver> handle)
: isolate_(isolate),
context_(isolate, isolate->GetCurrentContext()),
resolver_(isolate, handle) {}
Promise::~Promise() = default;
Promise::Promise(Promise&&) = default;
Promise& Promise::operator=(Promise&&) = default;
v8::Maybe<bool> Promise::RejectWithErrorMessage(const std::string& string) {
v8::HandleScope handle_scope(isolate());
v8::MicrotasksScope script_scope(isolate(),
v8::MicrotasksScope::kRunMicrotasks);
v8::Context::Scope context_scope(
v8::Local<v8::Context>::New(isolate(), GetContext()));
v8::Local<v8::String> error_message =
v8::String::NewFromUtf8(isolate(), string.c_str(),
v8::NewStringType::kNormal,
static_cast<int>(string.size()))
.ToLocalChecked();
v8::Local<v8::Value> error = v8::Exception::Error(error_message);
return Reject(error);
}
v8::Local<v8::Promise> Promise::GetHandle() const {
return GetInner()->GetPromise();
}
CopyablePromise::CopyablePromise(const Promise& promise)
: isolate_(promise.isolate()), handle_(isolate_, promise.GetInner()) {}
CopyablePromise::CopyablePromise(const CopyablePromise&) = default;
CopyablePromise::~CopyablePromise() = default;
Promise CopyablePromise::GetPromise() const {
return Promise(isolate_,
v8::Local<v8::Promise::Resolver>::New(isolate_, handle_));
}
} // namespace util
} // namespace electron
namespace mate {
v8::Local<v8::Value> mate::Converter<electron::util::Promise>::ToV8(
template <typename T>
v8::Local<v8::Value> mate::Converter<electron::util::Promise<T>>::ToV8(
v8::Isolate*,
const electron::util::Promise& val) {
const electron::util::Promise<T>& val) {
return val.GetHandle();
}
template <>
v8::Local<v8::Value> mate::Converter<electron::util::Promise<void*>>::ToV8(
v8::Isolate*,
const electron::util::Promise<void*>& val) {
return val.GetHandle();
}