]> git.saurik.com Git - apple/javascriptcore.git/blob - tests/stress/unscopables.js
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / tests / stress / unscopables.js
1 function test(actual, expected) {
2 if (actual !== expected)
3 throw new Error('bad value: ' + actual);
4 }
5
6 (function () {
7 var array = [];
8 var unscopables = array[Symbol.unscopables];
9
10 test(typeof unscopables, "object");
11 test(unscopables.__proto__, undefined);
12 test(String(Object.keys(unscopables).sort()), "copyWithin,entries,fill,find,findIndex,keys,values");
13 }());
14
15 (function () {
16 var find = "Cocoa";
17 var forEach = "Hidden";
18 var array = [];
19 test(typeof array.find, "function");
20
21 with (array) {
22 test(typeof find, "string");
23 test(find, "Cocoa");
24 test(typeof forEach, "function");
25 test(__proto__, Array.prototype); // array's unscopables.__proto__ is undefined => false.
26 }
27 }());
28
29 (function () {
30 var object = {
31 [Symbol.unscopables]: {
32 Cocoa: false,
33 Cappuccino: true
34 },
35
36 Cocoa: null,
37 Cappuccino: null
38 };
39
40 var Cocoa = "Cocoa";
41 var Cappuccino = "Cappuccino";
42 var toString = "toString";
43
44 with (object) {
45 test(Cocoa, null);
46 test(Cappuccino, "Cappuccino");
47 test(toString, "toString"); // object.toString is hidden by unscopables.toString.
48 }
49
50 object[Symbol.unscopables].Cocoa = true;
51
52 with (object) {
53 test(Cocoa, "Cocoa");
54 test(Cappuccino, "Cappuccino");
55 }
56 }());
57
58 (function () {
59 var unscopables = Object.create(null);
60 unscopables.Cocoa = false;
61 unscopables.Cappuccino = true;
62
63 var object = {
64 [Symbol.unscopables]: unscopables,
65 Cocoa: null,
66 Cappuccino: null,
67 Matcha: null
68 };
69
70 var Cocoa = "Cocoa";
71 var Cappuccino = "Cappuccino";
72 var Matcha = "Matcha";
73 var toString = "toString";
74
75 with (object) {
76 test(Cocoa, null);
77 test(Cappuccino, "Cappuccino");
78 test(Matcha, null);
79 test(toString, Object.prototype.toString);
80 }
81
82 object[Symbol.unscopables].Cocoa = true;
83 object[Symbol.unscopables].Cappuccino = false;
84 object[Symbol.unscopables].toString = true;
85
86 with (object) {
87 test(Cocoa, "Cocoa");
88 test(Cappuccino, null);
89 test(toString, "toString");
90 test(Matcha, null);
91 }
92 }());
93
94 (function () {
95 var proto = Object.create(null);
96 var unscopables = Object.create(proto);
97 unscopables.Cocoa = false;
98 unscopables.Cappuccino = true;
99
100 var object = {
101 [Symbol.unscopables]: unscopables,
102 Cocoa: null,
103 Cappuccino: null,
104 Matcha: null
105 };
106
107 var Cocoa = "Cocoa";
108 var Cappuccino = "Cappuccino";
109 var Matcha = "Matcha";
110 var toString = "toString";
111
112 with (object) {
113 test(Cocoa, null);
114 test(Cappuccino, "Cappuccino");
115 test(Matcha, null);
116 test(toString, Object.prototype.toString);
117 }
118
119 object[Symbol.unscopables].Cocoa = true;
120 object[Symbol.unscopables].Cappuccino = false;
121 object[Symbol.unscopables].toString = true;
122
123 with (object) {
124 test(Cocoa, "Cocoa");
125 test(Cappuccino, null);
126 test(toString, "toString");
127 test(Matcha, null);
128 }
129
130 proto.Matcha = true;
131
132 with (object) {
133 test(Cocoa, "Cocoa");
134 test(Cappuccino, null);
135 test(toString, "toString");
136 test(Matcha, "Matcha");
137 }
138 }());
139
140 (function () {
141 var object = {
142 get Cocoa() {
143 throw new Error("bad trap");
144 },
145 Cappuccino: null
146 };
147
148 object[Symbol.unscopables] = {
149 Cocoa: true,
150 Cappuccino: true
151 };
152
153 var Cocoa = "Cocoa";
154 var Cappuccino = "Cappuccino";
155 var toString = "toString";
156
157 with (object) {
158 test(Cocoa, "Cocoa");
159 }
160
161 object[Symbol.unscopables].Cocoa = false;
162
163 var error = null;
164 try {
165 with (object) {
166 Cocoa;
167 }
168 } catch (e) {
169 error = e;
170 }
171 test(String(error), "Error: bad trap");
172 }());
173
174 (function () {
175 var object = {
176 Cocoa: null,
177 };
178 Object.defineProperty(object, Symbol.unscopables, {
179 get: function () {
180 throw new Error("unscopables trap");
181 }
182 });
183
184 var Cocoa = "Cocoa";
185 var Cappuccino = "Cappuccino";
186
187 var error = null;
188 try {
189 with (object) {
190 Cocoa
191 }
192 } catch (e) {
193 error = e;
194 }
195 test(String(error), "Error: unscopables trap");
196
197 with (object) {
198 test(Cappuccino, "Cappuccino");
199 }
200 }());
201
202 (function () {
203 var object = {
204 [Symbol.unscopables]: {
205 get Cocoa() {
206 throw new Error("unscopables trap");
207 }
208 },
209 Cocoa: null,
210 };
211 var Cocoa = "Cocoa";
212 var Cappuccino = "Cappuccino";
213
214 var error = null;
215 try {
216 with (object) {
217 Cocoa
218 }
219 } catch (e) {
220 error = e;
221 }
222 test(String(error), "Error: unscopables trap");
223
224 with (object) {
225 test(Cappuccino, "Cappuccino");
226 }
227 }());
228
229 (function () {
230 var object = {
231 [Symbol.unscopables]: 42,
232 Cocoa: "OK",
233 };
234 var Cocoa = "Cocoa";
235
236 with (object) {
237 test(Cocoa, "OK");
238 }
239 }());