diff options
author | Anna Henningsen <anna@addaleax.net> | 2017-04-14 18:27:46 +0200 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2017-05-09 14:55:18 +0200 |
commit | 059f2960503eec1418c32226646f5a2af6ae85f0 (patch) | |
tree | 1b948c7fd85ff17ccdde9a61132fa06edfc937fe /src/node_util.cc | |
parent | f72376d3230be7c968e392d59680bd01f8c20360 (diff) | |
download | node-new-059f2960503eec1418c32226646f5a2af6ae85f0.tar.gz |
util: add internal bindings for promise handling
Add methods for creating, resolving and rejecting promises
using the V8 C++ API that does not require creation of extra
`resolve` and `reject` functions to `process.binding('util')`.
PR-URL: https://github.com/nodejs/node/pull/12442
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: William Kapke <william.kapke@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Teddy Katz <teddy.katz@gmail.com>
Diffstat (limited to 'src/node_util.cc')
-rw-r--r-- | src/node_util.cc | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/node_util.cc b/src/node_util.cc index 5f3de643d4..3424739cc9 100644 --- a/src/node_util.cc +++ b/src/node_util.cc @@ -12,6 +12,7 @@ using v8::Context; using v8::FunctionCallbackInfo; using v8::Integer; using v8::Local; +using v8::Maybe; using v8::Object; using v8::Private; using v8::Promise; @@ -147,6 +148,36 @@ void WatchdogHasPendingSigint(const FunctionCallbackInfo<Value>& args) { } +void CreatePromise(const FunctionCallbackInfo<Value>& args) { + Local<Context> context = args.GetIsolate()->GetCurrentContext(); + auto maybe_resolver = Promise::Resolver::New(context); + if (!maybe_resolver.IsEmpty()) + args.GetReturnValue().Set(maybe_resolver.ToLocalChecked()); +} + + +void PromiseResolve(const FunctionCallbackInfo<Value>& args) { + Local<Context> context = args.GetIsolate()->GetCurrentContext(); + Local<Value> promise = args[0]; + CHECK(promise->IsPromise()); + if (promise.As<Promise>()->State() != Promise::kPending) return; + Local<Promise::Resolver> resolver = promise.As<Promise::Resolver>(); // sic + Maybe<bool> ret = resolver->Resolve(context, args[1]); + args.GetReturnValue().Set(ret.FromMaybe(false)); +} + + +void PromiseReject(const FunctionCallbackInfo<Value>& args) { + Local<Context> context = args.GetIsolate()->GetCurrentContext(); + Local<Value> promise = args[0]; + CHECK(promise->IsPromise()); + if (promise.As<Promise>()->State() != Promise::kPending) return; + Local<Promise::Resolver> resolver = promise.As<Promise::Resolver>(); // sic + Maybe<bool> ret = resolver->Reject(context, args[1]); + args.GetReturnValue().Set(ret.FromMaybe(false)); +} + + void Initialize(Local<Object> target, Local<Value> unused, Local<Context> context) { @@ -192,6 +223,10 @@ void Initialize(Local<Object> target, env->SetMethod(target, "startSigintWatchdog", StartSigintWatchdog); env->SetMethod(target, "stopSigintWatchdog", StopSigintWatchdog); env->SetMethod(target, "watchdogHasPendingSigint", WatchdogHasPendingSigint); + + env->SetMethod(target, "createPromise", CreatePromise); + env->SetMethod(target, "promiseResolve", PromiseResolve); + env->SetMethod(target, "promiseReject", PromiseReject); } } // namespace util |