refactor: promise_util promise creation (#16401)

* refactor: promise_util creation

* enter correct contexts on resolve/reject

* return Local in helper

* set context correctly

* forgot one
This commit is contained in:
Shelley Vohr 2019-01-15 09:54:59 -08:00 committed by GitHub
parent 05755ba202
commit 8e2ab8b20b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 32 deletions

View file

@ -2,10 +2,11 @@
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/common/promise_util.h"
#include <string>
#include "atom/common/api/locker.h"
#include "atom/common/promise_util.h"
namespace atom {
namespace util {
@ -14,12 +15,20 @@ Promise::Promise(v8::Isolate* isolate) {
auto context = isolate->GetCurrentContext();
auto resolver = v8::Promise::Resolver::New(context).ToLocalChecked();
isolate_ = isolate;
context_.Reset(isolate, context);
resolver_.Reset(isolate, resolver);
}
Promise::~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::Local<v8::Value> error = v8::Exception::Error(error_message);