From b180f18b7eaabe066c79b1b60043e3b593a25adb Mon Sep 17 00:00:00 2001 From: Samuel Attard Date: Wed, 28 Mar 2018 11:32:10 +1100 Subject: [PATCH] Add mate::Promise --- native_mate/dictionary.cc | 2 +- native_mate/promise.cc | 45 +++++++++++++++++++++++++++++++ native_mate/promise.h | 57 +++++++++++++++++++++++++++++++++++++++ native_mate_files.gypi | 2 ++ 4 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 native_mate/promise.cc create mode 100644 native_mate/promise.h diff --git a/native_mate/dictionary.cc b/native_mate/dictionary.cc index b63acaf5b77..3caff114597 100644 --- a/native_mate/dictionary.cc +++ b/native_mate/dictionary.cc @@ -20,7 +20,7 @@ Dictionary::~Dictionary() { } Dictionary Dictionary::CreateEmpty(v8::Isolate* isolate) { - return Dictionary(isolate, v8::Object::New(isolate));; + return Dictionary(isolate, v8::Object::New(isolate)); } v8::Local Dictionary::GetHandle() const { diff --git a/native_mate/promise.cc b/native_mate/promise.cc new file mode 100644 index 00000000000..81b79441e72 --- /dev/null +++ b/native_mate/promise.cc @@ -0,0 +1,45 @@ +// 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 "native_mate/promise.h" + +namespace mate { + +Promise::Promise() + : isolate_(NULL) { +} + +Promise::Promise(v8::Isolate* isolate) + : isolate_(isolate) { + resolver_ = v8::Promise::Resolver::New(isolate); +} + +Promise::~Promise() { +} + +Promise Promise::Create(v8::Isolate* isolate) { + return Promise(isolate); +} + +Promise Promise::Create() { + return Promise::Create(v8::Isolate::GetCurrent()); +} + +void Promise::RejectWithErrorMessage(const std::string& string) { + v8::Local error_message = + v8::String::NewFromUtf8(isolate(), string.c_str()); + v8::Local error = v8::Exception::Error(error_message); + resolver_->Reject(mate::ConvertToV8(isolate(), error)); +} + +v8::Local Promise::GetHandle() const { + return resolver_->GetPromise(); +} + +v8::Local Converter::ToV8(v8::Isolate* isolate, + Promise val) { + return val.GetHandle(); +} + +} // namespace mate diff --git a/native_mate/promise.h b/native_mate/promise.h new file mode 100644 index 00000000000..225ac6d048f --- /dev/null +++ b/native_mate/promise.h @@ -0,0 +1,57 @@ +// Copyright (c) 2018 GitHub, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#ifndef NATIVE_MATE_PROMISE_H_ +#define NATIVE_MATE_PROMISE_H_ + +#include "native_mate/converter.h" + +namespace mate { + +class Promise { + public: + Promise(); + Promise(v8::Isolate* isolate); + virtual ~Promise(); + + static Promise Create(v8::Isolate* isolate); + static Promise Create(); + + v8::Isolate* isolate() const { return isolate_; } + + virtual v8::Local GetHandle() const; + + template + void Resolve(T* value) { + resolver_->Resolve(mate::ConvertToV8(isolate(), value)); + } + + template + void Reject(T* value) { + resolver_->Reject(mate::ConvertToV8(isolate(), value)); + } + + void RejectWithErrorMessage(const std::string& error); + + protected: + v8::Isolate* isolate_; + + private: + v8::Local resolver_; +}; + +template<> +struct Converter { + static v8::Local ToV8(v8::Isolate* isolate, + Promise val); + // TODO(MarshallOfSound): Implement FromV8 to allow promise chaining + // in native land + // static bool FromV8(v8::Isolate* isolate, + // v8::Local val, + // Promise* out); +}; + +} // namespace mate + +#endif // NATIVE_MATE_PROMISE_H_ diff --git a/native_mate_files.gypi b/native_mate_files.gypi index 867ec7999c8..bf10470d02a 100644 --- a/native_mate_files.gypi +++ b/native_mate_files.gypi @@ -22,6 +22,8 @@ 'native_mate/wrappable.cc', 'native_mate/wrappable.h', 'native_mate/wrappable_base.h', + 'native_mate/promise.h', + 'native_mate/promise.cc', ], }, }