diff options
author | Timothy Gu <timothygu99@gmail.com> | 2017-04-05 22:30:25 -0700 |
---|---|---|
committer | Timothy Gu <timothygu99@gmail.com> | 2017-04-08 11:07:00 -0700 |
commit | a37273c1e4b93ed048e1d45818fe6c525480b121 (patch) | |
tree | a1e86cf5a90921c90a21da4a8fab0b2cafe6b4ac /src/node_util.cc | |
parent | afd5966fa9a874ebb958518633b70b6af4f78b8f (diff) | |
download | node-new-a37273c1e4b93ed048e1d45818fe6c525480b121.tar.gz |
util: use V8 C++ API for inspecting Promises
PR-URL: https://github.com/nodejs/node/pull/12254
Refs: https://github.com/nodejs/node/issues/11875
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Josh Gavant <josh.gavant@outlook.com>
Diffstat (limited to 'src/node_util.cc')
-rw-r--r-- | src/node_util.cc | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/node_util.cc b/src/node_util.cc index 813995de79..fe01717131 100644 --- a/src/node_util.cc +++ b/src/node_util.cc @@ -14,6 +14,7 @@ using v8::Integer; using v8::Local; using v8::Object; using v8::Private; +using v8::Promise; using v8::Proxy; using v8::Value; @@ -43,6 +44,24 @@ using v8::Value; VALUE_METHOD_MAP(V) #undef V +static void GetPromiseDetails(const FunctionCallbackInfo<Value>& args) { + // Return undefined if it's not a Promise. + if (!args[0]->IsPromise()) + return; + + auto isolate = args.GetIsolate(); + + Local<Promise> promise = args[0].As<Promise>(); + Local<Array> ret = Array::New(isolate, 2); + + int state = promise->State(); + ret->Set(0, Integer::New(isolate, state)); + if (state != Promise::PromiseState::kPending) + ret->Set(1, promise->Result()); + + args.GetReturnValue().Set(ret); +} + static void GetProxyDetails(const FunctionCallbackInfo<Value>& args) { // Return undefined if it's not a proxy. if (!args[0]->IsProxy()) @@ -148,8 +167,19 @@ void Initialize(Local<Object> target, Integer::NewFromUnsigned(env->isolate(), NODE_PUSH_VAL_TO_ARRAY_MAX), v8::ReadOnly).FromJust(); +#define V(name) \ + target->Set(context, \ + FIXED_ONE_BYTE_STRING(env->isolate(), #name), \ + Integer::New(env->isolate(), Promise::PromiseState::name)) \ + .FromJust() + V(kPending); + V(kFulfilled); + V(kRejected); +#undef V + env->SetMethod(target, "getHiddenValue", GetHiddenValue); env->SetMethod(target, "setHiddenValue", SetHiddenValue); + env->SetMethod(target, "getPromiseDetails", GetPromiseDetails); env->SetMethod(target, "getProxyDetails", GetProxyDetails); env->SetMethod(target, "startSigintWatchdog", StartSigintWatchdog); |