Debian Codegraph @

js/src/tests/test262/built-ins/BigInt/asUintN/bits-toindex-toprimitive.js

raw
1 // Copyright (C) 2017 Josh Wolfe. All rights reserved.
2 // This code is governed by the BSD license found in the LICENSE file.
3 /*---
4 description: BigInt.asUintN type coercion for bits parameter
5 esid: sec-bigint.asuintn
6 info: |
7 BigInt.asUintN ( bits, bigint )
8
9 1. Let bits be ? ToIndex(bits).
10 features: [BigInt, computed-property-names, Symbol, Symbol.toPrimitive]
11 ---*/
12 assert.sameValue(typeof BigInt, 'function');
13 assert.sameValue(typeof BigInt.asUintN, 'function');
14
15 function err() {
16 throw new Test262Error();
17 }
18
19 function MyError() {}
20
21 assert.sameValue(BigInt.asUintN({
22 [Symbol.toPrimitive]: function() {
23 return 1;
24 },
25 valueOf: err,
26 toString: err
27 }, 1n), 1n, "ToPrimitive: @@toPrimitive takes precedence");
28 assert.sameValue(BigInt.asUintN({
29 valueOf: function() {
30 return 1;
31 },
32 toString: err
33 }, 1n), 1n, "ToPrimitive: valueOf takes precedence over toString");
34 assert.sameValue(BigInt.asUintN({
35 toString: function() {
36 return 1;
37 }
38 }, 1n), 1n, "ToPrimitive: toString with no valueOf");
39 assert.sameValue(BigInt.asUintN({
40 [Symbol.toPrimitive]: undefined,
41 valueOf: function() {
42 return 1;
43 }
44 }, 1n), 1n, "ToPrimitive: skip @@toPrimitive when it's undefined");
45 assert.sameValue(BigInt.asUintN({
46 [Symbol.toPrimitive]: null,
47 valueOf: function() {
48 return 1;
49 }
50 }, 1n), 1n, "ToPrimitive: skip @@toPrimitive when it's null");
51 assert.sameValue(BigInt.asUintN({
52 valueOf: null,
53 toString: function() {
54 return 1;
55 }
56 }, 1n), 1n, "ToPrimitive: skip valueOf when it's not callable");
57 assert.sameValue(BigInt.asUintN({
58 valueOf: 1,
59 toString: function() {
60 return 1;
61 }
62 }, 1n), 1n, "ToPrimitive: skip valueOf when it's not callable");
63 assert.sameValue(BigInt.asUintN({
64 valueOf: {},
65 toString: function() {
66 return 1;
67 }
68 }, 1n), 1n, "ToPrimitive: skip valueOf when it's not callable");
69 assert.sameValue(BigInt.asUintN({
70 valueOf: function() {
71 return {};
72 },
73 toString: function() {
74 return 1;
75 }
76 }, 1n), 1n, "ToPrimitive: skip valueOf when it returns an object");
77 assert.sameValue(BigInt.asUintN({
78 valueOf: function() {
79 return Object(12345);
80 },
81 toString: function() {
82 return 1;
83 }
84 }, 1n), 1n, "ToPrimitive: skip valueOf when it returns an object");
85 assert.throws(TypeError, function() {
86 BigInt.asUintN({
87 [Symbol.toPrimitive]: 1
88 }, 0n);
89 }, "ToPrimitive: throw when @@toPrimitive is not callable");
90 assert.throws(TypeError, function() {
91 BigInt.asUintN({
92 [Symbol.toPrimitive]: {}
93 }, 0n);
94 }, "ToPrimitive: throw when @@toPrimitive is not callable");
95 assert.throws(TypeError, function() {
96 BigInt.asUintN({
97 [Symbol.toPrimitive]: function() {
98 return Object(1);
99 }
100 }, 0n);
101 }, "ToPrimitive: throw when @@toPrimitive returns an object");
102 assert.throws(TypeError, function() {
103 BigInt.asUintN({
104 [Symbol.toPrimitive]: function() {
105 return {};
106 }
107 }, 0n);
108 }, "ToPrimitive: throw when @@toPrimitive returns an object");
109 assert.throws(MyError, function() {
110 BigInt.asUintN({
111 [Symbol.toPrimitive]: function() {
112 throw new MyError();
113 }
114 }, 0n);
115 }, "ToPrimitive: propagate errors from @@toPrimitive");
116 assert.throws(MyError, function() {
117 BigInt.asUintN({
118 valueOf: function() {
119 throw new MyError();
120 }
121 }, 0n);
122 }, "ToPrimitive: propagate errors from valueOf");
123 assert.throws(MyError, function() {
124 BigInt.asUintN({
125 toString: function() {
126 throw new MyError();
127 }
128 }, 0n);
129 }, "ToPrimitive: propagate errors from toString");
130 assert.throws(TypeError, function() {
131 BigInt.asUintN({
132 valueOf: null,
133 toString: null
134 }, 0n);
135 }, "ToPrimitive: throw when skipping both valueOf and toString");
136 assert.throws(TypeError, function() {
137 BigInt.asUintN({
138 valueOf: 1,
139 toString: 1
140 }, 0n);
141 }, "ToPrimitive: throw when skipping both valueOf and toString");
142 assert.throws(TypeError, function() {
143 BigInt.asUintN({
144 valueOf: {},
145 toString: {}
146 }, 0n);
147 }, "ToPrimitive: throw when skipping both valueOf and toString");
148 assert.throws(TypeError, function() {
149 BigInt.asUintN({
150 valueOf: function() {
151 return Object(1);
152 },
153 toString: function() {
154 return Object(1);
155 }
156 }, 0n);
157 }, "ToPrimitive: throw when skipping both valueOf and toString");
158 assert.throws(TypeError, function() {
159 BigInt.asUintN({
160 valueOf: function() {
161 return {};
162 },
163 toString: function() {
164 return {};
165 }
166 }, 0n);
167 }, "ToPrimitive: throw when skipping both valueOf and toString");
168
169 reportCompare(0, 0);