2018-06-27 21:06:08 +00:00
|
|
|
// Copyright (c) 2018 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
2019-01-15 17:54:59 +00:00
|
|
|
#include "atom/common/api/locker.h"
|
|
|
|
#include "atom/common/promise_util.h"
|
|
|
|
|
2018-06-27 21:06:08 +00:00
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
namespace util {
|
|
|
|
|
2018-06-28 21:20:11 +00:00
|
|
|
Promise::Promise(v8::Isolate* isolate) {
|
2018-11-27 21:42:02 +00:00
|
|
|
auto context = isolate->GetCurrentContext();
|
|
|
|
auto resolver = v8::Promise::Resolver::New(context).ToLocalChecked();
|
2018-06-28 21:20:11 +00:00
|
|
|
isolate_ = isolate;
|
2019-01-15 17:54:59 +00:00
|
|
|
|
|
|
|
context_.Reset(isolate, context);
|
2018-11-27 21:42:02 +00:00
|
|
|
resolver_.Reset(isolate, resolver);
|
2018-06-28 21:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Promise::~Promise() = default;
|
|
|
|
|
2018-06-27 21:06:08 +00:00
|
|
|
v8::Maybe<bool> Promise::RejectWithErrorMessage(const std::string& string) {
|
2019-01-15 17:54:59 +00:00
|
|
|
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()));
|
|
|
|
|
2018-06-27 21:06:08 +00:00
|
|
|
v8::Local<v8::String> error_message =
|
2019-01-21 16:27:11 +00:00
|
|
|
v8::String::NewFromUtf8(isolate(), string.c_str(),
|
|
|
|
v8::NewStringType::kNormal,
|
|
|
|
static_cast<int>(string.size()))
|
|
|
|
.ToLocalChecked();
|
2018-06-27 21:06:08 +00:00
|
|
|
v8::Local<v8::Value> error = v8::Exception::Error(error_message);
|
2018-10-15 15:26:47 +00:00
|
|
|
return Reject(error);
|
2018-06-27 21:06:08 +00:00
|
|
|
}
|
|
|
|
|
2018-09-27 14:59:23 +00:00
|
|
|
v8::Local<v8::Promise> Promise::GetHandle() const {
|
2018-06-27 21:06:08 +00:00
|
|
|
return GetInner()->GetPromise();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace util
|
|
|
|
|
|
|
|
} // namespace atom
|
|
|
|
|
|
|
|
namespace mate {
|
|
|
|
|
|
|
|
v8::Local<v8::Value> mate::Converter<atom::util::Promise*>::ToV8(
|
|
|
|
v8::Isolate*,
|
|
|
|
atom::util::Promise* val) {
|
|
|
|
return val->GetHandle();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mate
|