summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/promise/optimized-promise-already-resolved.js
blob: 69e7d95fe06a0bd24e7ef99b677d7aee1df15ae9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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}`);
    }
  }
}