summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/cacheir/dataview-non-number-value-set.js
blob: 38c7a998b50a29c894ea6e2af5efbaf13357567c (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
const types = [
  "Int8",
  "Int16",
  "Int32",
  "Uint8",
  "Uint16",
  "Uint32",
  "Float32",
  "Float64",
];

function convert(type, value) {
  let num = Number(value);
  switch (type) {
    case "Int8":
      return ((num | 0) << 24) >> 24;
    case "Int16":
      return ((num | 0) << 16) >> 16;
    case "Int32":
      return (num | 0);
    case "Uint8":
      return (num >>> 0) & 0xff;
    case "Uint16":
      return (num >>> 0) & 0xffff;
    case "Uint32":
      return (num >>> 0);
    case "Uint8Clamped": {
      if (Number.isNaN(num)) {
        return 0;
      }
      let clamped = Math.max(0, Math.min(num, 255));
      let f = Math.floor(clamped);
      if (clamped < f + 0.5) {
        return f;
      }
      if (clamped > f + 0.5) {
        return f + 1;
      }
      return f + (f & 1);
    }
    case "Float32":
      return Math.fround(num);
    case "Float64":
      return num;
  }
  throw new Error();
}


function runTest(type, initial, values) {
  let expected = values.map(v => convert(type, v));
  assertEq(
    expected.some(e => Object.is(e, initial)),
    false,
    "initial must be different from the expected values"
  );

  // Create a fresh function to ensure ICs are specific to a single DataView function.
  let test = Function("initial, values, expected", `
    let ab = new ArrayBuffer(16);
    let dv = new DataView(ab);
    for (let i = 0; i < 200; ++i) {
      dv.set${type}(0, initial);
      dv.set${type}(0, values[i % values.length]);
      assertEq(dv.get${type}(0), expected[i % expected.length]);
    }
  `);
  test(initial, values, expected);
}

const tests = [
  // |null| is coerced to zero.
  {
    initial: 1,
    values: [null],
  },

  // |undefined| is coerced to zero or NaN.
  {
    initial: 1,
    values: [undefined],
  },

  // |false| is coerced to zero and |true| is coerced to one.
  {
    initial: 2,
    values: [false, true],
  },
  
  // Strings without a fractional part.
  {
    initial: 42,
    values: [
      "0", "1", "10", "111", "128", "256", "0x7fffffff", "0xffffffff",
    ],
  },

  // Strings without a fractional part, but a leading minus sign.
  {
    initial: 42,
    values: [
      "-0", "-1", "-10", "-111", "-128", "-256", "-2147483647", "-4294967295",
    ],
  },
  
  // Strings with a fractional number part.
  {
    initial: 42,
    values: [
      "0.1", "1.2", "10.8", "111.9",
      "-0.1", "-1.2", "-10.8", "-111.9",
    ],
  },

  // Special values and strings not parseable as a number.
  {
    initial: 42,
    values: ["Infinity", "-Infinity", "NaN", "foobar"],
  },
];

for (let type of types) {
  for (let {initial, values} of tests) {
    runTest(type, initial, values);
  }
}