-
Notifications
You must be signed in to change notification settings - Fork 30.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
util: use V8 C++ API for inspecting Promises #12254
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. might it be better to use Object and name the properties? might be easier if we want to extend with additional metadata, and would allow the JavaScript to be de-coupled from the order of items in the array. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Besides the fact that this is simpler to implement, it is also the approach taken by the existing |
||
|
||
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); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also fyi @Fishrock123, this seems like a cleaner way to inspect Promise states than what the abort-on-rejected PR currently does…
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Errr, this allows you to get the actual result object e.g. an error without going through the Debug API? I was told that was not possible to do synchronously.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Fishrock123 this set of APIs is relatively new, having been added in V8 5.7 in v8/v8@2843258. That might be why.