summaryrefslogtreecommitdiffstats
path: root/js/src/tests/test262/built-ins/Set/prototype/union/size-is-a-number.js
blob: a6d35f655f8e7d8cb338abe8357414c2a50dc09f (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
// |reftest| skip -- set-methods is not supported
// Copyright (C) 2023 Anthony Frehner. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-getsetrecord
description: GetSetRecord throws an exception if the Set-like object has a size that is coerced to NaN
info: |
    2. Let rawSize be ? Get(obj, "size").
    3. Let numSize be ? ToNumber(rawSize).
    4. NOTE: If rawSize is undefined, then numSize will be NaN.
    5. If numSize is NaN, throw a TypeError exception.
features: [set-methods]
---*/

const s1 = new Set([1, 2]);
const s2 = {
  size: undefined,
  has: () => {},
  keys: function* keys() {
    yield 2;
    yield 3;
  },
};
assert.throws(
  TypeError,
  function () {
    s1.union(s2);
  },
  "GetSetRecord throws an error when size is undefined"
);

s2.size = NaN;
assert.throws(
  TypeError,
  function () {
    s1.union(s2);
  },
  "GetSetRecord throws an error when size is NaN"
);

let coercionCalls = 0;
s2.size = {
  valueOf: function() {
    ++coercionCalls;
    return NaN;
  },
};
assert.throws(
  TypeError,
  function () {
    s1.union(s2);
  },
  "GetSetRecord throws an error when size coerces to NaN"
);
assert.sameValue(coercionCalls, 1, "GetSetRecord coerces size");

s2.size = 0n;
assert.throws(
  TypeError,
  function () {
    s1.union(s2);
  },
  "GetSetRecord throws an error when size is a BigInt"
);

s2.size = "string";
assert.throws(
  TypeError,
  function () {
    s1.union(s2);
  },
  "GetSetRecord throws an error when size is a non-numeric string"
);

reportCompare(0, 0);