diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /js/src/jit-test/tests/promise/optimized-promise-already-resolved.js | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'js/src/jit-test/tests/promise/optimized-promise-already-resolved.js')
-rw-r--r-- | js/src/jit-test/tests/promise/optimized-promise-already-resolved.js | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/js/src/jit-test/tests/promise/optimized-promise-already-resolved.js b/js/src/jit-test/tests/promise/optimized-promise-already-resolved.js new file mode 100644 index 0000000000..69e7d95fe0 --- /dev/null +++ b/js/src/jit-test/tests/promise/optimized-promise-already-resolved.js @@ -0,0 +1,73 @@ +// Promise created with default resolution functions (without instances) should +// behave in the same way as normal case. + +class MockPromise { + constructor(...args) { + return new Promise(...args); + } +} + +for (const expected of ["resolve", "reject"]) { + for (const unexpected of ["resolve", "reject"]) { + let resolve, reject; + const p = new Promise((a, b) => { + resolve = a; + reject = b; + }); + + // To prevent fast path in Promise.resolve that returns the passed promise, + // modify constructor property. + p.constructor = MockPromise; + + // Given `p` has custom constructor, Promise.resolve creates a new promise + // instead of returning `p` itself. + // + // Newly created promise `optimized` does not have resolution function + // instances. + const optimized = Promise.resolve(p); + + // Newly created promise `normal` has resolution functions. + const normal = new Promise(r => r(p)); + + // These calls should be ignored because [[AlreadyResolved]] == true, + if (unexpected === "resolve") { + resolvePromise(optimized, "unexpected resolve optimized"); + resolvePromise(normal, "unexpected resolve normal"); + } else { + rejectPromise(optimized, "unexpected reject optimized"); + rejectPromise(normal, "unexpected reject normal"); + } + + if (expected === "resolve") { + resolve("resolve"); + } else { + reject("reject"); + } + + let optimized_resolutionValue, optimized_rejectionValue; + optimized.then( + x => { optimized_resolutionValue = x; }, + x => { optimized_rejectionValue = x; } + ); + + let normal_resolutionValue, normal_rejectionValue; + normal.then( + x => { normal_resolutionValue = x; }, + x => { normal_rejectionValue = x; } + ); + + drainJobQueue(); + + if (expected === "resolve") { + assertEq(optimized_resolutionValue, "resolve", + `${expected} + ${unexpected}`); + assertEq(normal_resolutionValue, "resolve", + `${expected} + ${unexpected}`); + } else { + assertEq(optimized_rejectionValue, "reject", + `${expected} + ${unexpected}`); + assertEq(normal_rejectionValue, "reject", + `${expected} + ${unexpected}`); + } + } +} |