diff --git a/src/js/prologue.js b/src/js/prologue.js index 5994934465..92d99e0072 100644 --- a/src/js/prologue.js +++ b/src/js/prologue.js @@ -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); }) diff --git a/test/cctest/test-api.cc b/test/cctest/test-api.cc index b97c0cf265..65e6135f8a 100644 --- a/test/cctest/test-api.cc +++ b/test/cctest/test-api.cc @@ -25486,6 +25486,12 @@ TEST(ExtrasUtilsObject) { .ToLocalChecked() .As(); CHECK(rejected_but_handled_promise->HasHandler()); + + auto promise_states = result->Get(env.local(), v8_str("promiseStates")) + .ToLocalChecked() + .As(); + String::Utf8Value promise_states_string(promise_states); + CHECK_EQ(0, strcmp(*promise_states_string, "pending fulfilled rejected")); } diff --git a/test/cctest/test-extra.js b/test/cctest/test-extra.js index 88c5f6e659..69a7e63221 100644 --- a/test/cctest/test-extra.js +++ b/test/cctest/test-extra.js @@ -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" }; }; })