Add V8 extra utils for promise state

This will allow V8 extra consumers to track promise state without
using a side-table. This is used by streams as of
173f9f67be.

BUG=chromium:658144

Review-Url: https://codereview.chromium.org/2784213002
Cr-Commit-Position: refs/heads/master@{#44287}
This commit is contained in:
domenic 2017-03-30 22:58:15 -07:00 committed by Commit bot
parent 5f41fbee57
commit a6e635d692
3 changed files with 35 additions and 1 deletions

View File

@ -218,6 +218,16 @@ extrasUtils.markPromiseAsHandled = function markPromiseAsHandled(promise) {
%PromiseMarkAsHandled(promise);
};
extrasUtils.promiseState = function promiseState(promise) {
return %PromiseStatus(promise);
};
// [[PromiseState]] values (for extrasUtils.promiseState())
// These values should be kept in sync with PromiseStatus in globals.h
extrasUtils.kPROMISE_PENDING = 0;
extrasUtils.kPROMISE_FULFILLED = 1;
extrasUtils.kPROMISE_REJECTED = 2;
%ToFastProperties(extrasUtils);
})

View File

@ -25486,6 +25486,12 @@ TEST(ExtrasUtilsObject) {
.ToLocalChecked()
.As<v8::Promise>();
CHECK(rejected_but_handled_promise->HasHandler());
auto promise_states = result->Get(env.local(), v8_str("promiseStates"))
.ToLocalChecked()
.As<v8::String>();
String::Utf8Value promise_states_string(promise_states);
CHECK_EQ(0, strcmp(*promise_states_string, "pending fulfilled rejected"));
}

View File

@ -70,12 +70,30 @@
v8.rejectPromise(rejectedButHandledPromise, 4);
v8.markPromiseAsHandled(rejectedButHandledPromise);
function promiseStateToString(promise) {
switch (v8.promiseState(promise)) {
case v8.kPROMISE_PENDING:
return "pending";
case v8.kPROMISE_FULFILLED:
return "fulfilled";
case v8.kPROMISE_REJECTED:
return "rejected";
default:
throw new Error("Unexpected value for promiseState");
}
}
let promiseStates = promiseStateToString(new Promise(() => {})) + ' ' +
promiseStateToString(fulfilledPromise) + ' ' +
promiseStateToString(rejectedPromise);
return {
privateSymbol: v8.createPrivateSymbol('sym'),
fulfilledPromise, // should be fulfilled with 1
fulfilledPromise2, // should be fulfilled with 2
rejectedPromise, // should be rejected with 3
rejectedButHandledPromise // should be rejected but have a handler
rejectedButHandledPromise, // should be rejected but have a handler
promiseStates // should be the string "pending fulfilled rejected"
};
};
})