11cb0ef41Sopenharmony_ci// Flags: --expose-internals 21cb0ef41Sopenharmony_ci'use strict'; 31cb0ef41Sopenharmony_ci 41cb0ef41Sopenharmony_ciconst common = require('../common'); 51cb0ef41Sopenharmony_ciconst assert = require('assert'); 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ciconst { 81cb0ef41Sopenharmony_ci PromisePrototypeThen, 91cb0ef41Sopenharmony_ci SafePromiseAll, 101cb0ef41Sopenharmony_ci SafePromiseAllReturnArrayLike, 111cb0ef41Sopenharmony_ci SafePromiseAllReturnVoid, 121cb0ef41Sopenharmony_ci SafePromiseAllSettled, 131cb0ef41Sopenharmony_ci SafePromiseAllSettledReturnVoid, 141cb0ef41Sopenharmony_ci SafePromiseAny, 151cb0ef41Sopenharmony_ci SafePromisePrototypeFinally, 161cb0ef41Sopenharmony_ci SafePromiseRace, 171cb0ef41Sopenharmony_ci} = require('internal/test/binding').primordials; 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_ciArray.prototype[Symbol.iterator] = common.mustNotCall('%Array.prototype%[@@iterator]'); 201cb0ef41Sopenharmony_ciPromise.all = common.mustNotCall('%Promise%.all'); 211cb0ef41Sopenharmony_ciPromise.allSettled = common.mustNotCall('%Promise%.allSettled'); 221cb0ef41Sopenharmony_ciPromise.any = common.mustNotCall('%Promise%.any'); 231cb0ef41Sopenharmony_ciPromise.race = common.mustNotCall('%Promise%.race'); 241cb0ef41Sopenharmony_ci 251cb0ef41Sopenharmony_ciObject.defineProperties(Promise.prototype, { 261cb0ef41Sopenharmony_ci catch: { 271cb0ef41Sopenharmony_ci set: common.mustNotCall('set %Promise.prototype%.catch'), 281cb0ef41Sopenharmony_ci get: common.mustNotCall('get %Promise.prototype%.catch'), 291cb0ef41Sopenharmony_ci }, 301cb0ef41Sopenharmony_ci finally: { 311cb0ef41Sopenharmony_ci set: common.mustNotCall('set %Promise.prototype%.finally'), 321cb0ef41Sopenharmony_ci get: common.mustNotCall('get %Promise.prototype%.finally'), 331cb0ef41Sopenharmony_ci }, 341cb0ef41Sopenharmony_ci then: { 351cb0ef41Sopenharmony_ci set: common.mustNotCall('set %Promise.prototype%.then'), 361cb0ef41Sopenharmony_ci get: common.mustNotCall('get %Promise.prototype%.then'), 371cb0ef41Sopenharmony_ci }, 381cb0ef41Sopenharmony_ci}); 391cb0ef41Sopenharmony_ciObject.defineProperties(Array.prototype, { 401cb0ef41Sopenharmony_ci then: { 411cb0ef41Sopenharmony_ci configurable: true, 421cb0ef41Sopenharmony_ci set: common.mustNotCall('set %Array.prototype%.then'), 431cb0ef41Sopenharmony_ci get: common.mustNotCall('get %Array.prototype%.then'), 441cb0ef41Sopenharmony_ci }, 451cb0ef41Sopenharmony_ci}); 461cb0ef41Sopenharmony_ciObject.defineProperties(Object.prototype, { 471cb0ef41Sopenharmony_ci then: { 481cb0ef41Sopenharmony_ci set: common.mustNotCall('set %Object.prototype%.then'), 491cb0ef41Sopenharmony_ci get: common.mustNotCall('get %Object.prototype%.then'), 501cb0ef41Sopenharmony_ci }, 511cb0ef41Sopenharmony_ci}); 521cb0ef41Sopenharmony_ci 531cb0ef41Sopenharmony_ciassertIsPromise(PromisePrototypeThen(test(), common.mustCall())); 541cb0ef41Sopenharmony_ciassertIsPromise(SafePromisePrototypeFinally(test(), common.mustCall())); 551cb0ef41Sopenharmony_ci 561cb0ef41Sopenharmony_ciassertIsPromise(SafePromiseAllReturnArrayLike([test()])); 571cb0ef41Sopenharmony_ciassertIsPromise(SafePromiseAllReturnVoid([test()])); 581cb0ef41Sopenharmony_ciassertIsPromise(SafePromiseAny([test()])); 591cb0ef41Sopenharmony_ciassertIsPromise(SafePromiseRace([test()])); 601cb0ef41Sopenharmony_ci 611cb0ef41Sopenharmony_ciassertIsPromise(SafePromiseAllReturnArrayLike([])); 621cb0ef41Sopenharmony_ciassertIsPromise(SafePromiseAllReturnVoid([])); 631cb0ef41Sopenharmony_ci 641cb0ef41Sopenharmony_ci{ 651cb0ef41Sopenharmony_ci const val1 = Symbol(); 661cb0ef41Sopenharmony_ci const val2 = Symbol(); 671cb0ef41Sopenharmony_ci PromisePrototypeThen( 681cb0ef41Sopenharmony_ci SafePromiseAllReturnArrayLike([Promise.resolve(val1), { then(resolve) { resolve(val2); } }]), 691cb0ef41Sopenharmony_ci common.mustCall((val) => { 701cb0ef41Sopenharmony_ci assert.strictEqual(Array.isArray(val), true); 711cb0ef41Sopenharmony_ci const expected = [val1, val2]; 721cb0ef41Sopenharmony_ci assert.deepStrictEqual(val.length, expected.length); 731cb0ef41Sopenharmony_ci assert.strictEqual(val[0], expected[0]); 741cb0ef41Sopenharmony_ci assert.strictEqual(val[1], expected[1]); 751cb0ef41Sopenharmony_ci }) 761cb0ef41Sopenharmony_ci ); 771cb0ef41Sopenharmony_ci} 781cb0ef41Sopenharmony_ci 791cb0ef41Sopenharmony_ci{ 801cb0ef41Sopenharmony_ci // Never settling promises should not block the resulting promise to be rejected: 811cb0ef41Sopenharmony_ci const error = new Error(); 821cb0ef41Sopenharmony_ci PromisePrototypeThen( 831cb0ef41Sopenharmony_ci SafePromiseAllReturnArrayLike([new Promise(() => {}), Promise.reject(error)]), 841cb0ef41Sopenharmony_ci common.mustNotCall('Should have rejected'), 851cb0ef41Sopenharmony_ci common.mustCall((err) => { 861cb0ef41Sopenharmony_ci assert.strictEqual(err, error); 871cb0ef41Sopenharmony_ci }) 881cb0ef41Sopenharmony_ci ); 891cb0ef41Sopenharmony_ci PromisePrototypeThen( 901cb0ef41Sopenharmony_ci SafePromiseAllReturnVoid([new Promise(() => {}), Promise.reject(error)]), 911cb0ef41Sopenharmony_ci common.mustNotCall('Should have rejected'), 921cb0ef41Sopenharmony_ci common.mustCall((err) => { 931cb0ef41Sopenharmony_ci assert.strictEqual(err, error); 941cb0ef41Sopenharmony_ci }) 951cb0ef41Sopenharmony_ci ); 961cb0ef41Sopenharmony_ci} 971cb0ef41Sopenharmony_ci 981cb0ef41Sopenharmony_ciObject.defineProperties(Array.prototype, { 991cb0ef41Sopenharmony_ci // %Promise.all% and %Promise.allSettled% are depending on the value of 1001cb0ef41Sopenharmony_ci // `%Array.prototype%.then`. 1011cb0ef41Sopenharmony_ci then: { 1021cb0ef41Sopenharmony_ci __proto__: undefined, 1031cb0ef41Sopenharmony_ci value: undefined, 1041cb0ef41Sopenharmony_ci }, 1051cb0ef41Sopenharmony_ci}); 1061cb0ef41Sopenharmony_ci 1071cb0ef41Sopenharmony_ciassertIsPromise(SafePromiseAll([test()])); 1081cb0ef41Sopenharmony_ciassertIsPromise(SafePromiseAllSettled([test()])); 1091cb0ef41Sopenharmony_ciassertIsPromise(SafePromiseAllSettledReturnVoid([test()])); 1101cb0ef41Sopenharmony_ci 1111cb0ef41Sopenharmony_ciassertIsPromise(SafePromiseAll([])); 1121cb0ef41Sopenharmony_ciassertIsPromise(SafePromiseAllSettled([])); 1131cb0ef41Sopenharmony_ciassertIsPromise(SafePromiseAllSettledReturnVoid([])); 1141cb0ef41Sopenharmony_ci 1151cb0ef41Sopenharmony_ciasync function test() { 1161cb0ef41Sopenharmony_ci const catchFn = common.mustCall(); 1171cb0ef41Sopenharmony_ci const finallyFn = common.mustCall(); 1181cb0ef41Sopenharmony_ci 1191cb0ef41Sopenharmony_ci try { 1201cb0ef41Sopenharmony_ci await Promise.reject(); 1211cb0ef41Sopenharmony_ci } catch { 1221cb0ef41Sopenharmony_ci catchFn(); 1231cb0ef41Sopenharmony_ci } finally { 1241cb0ef41Sopenharmony_ci finallyFn(); 1251cb0ef41Sopenharmony_ci } 1261cb0ef41Sopenharmony_ci} 1271cb0ef41Sopenharmony_ci 1281cb0ef41Sopenharmony_cifunction assertIsPromise(promise) { 1291cb0ef41Sopenharmony_ci // Make sure the returned promise is a genuine %Promise% object and not a 1301cb0ef41Sopenharmony_ci // subclass instance. 1311cb0ef41Sopenharmony_ci assert.strictEqual(Object.getPrototypeOf(promise), Promise.prototype); 1321cb0ef41Sopenharmony_ci PromisePrototypeThen(promise, common.mustCall()); 1331cb0ef41Sopenharmony_ci} 134