Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: test/mjsunit/harmony/symbols.js

Issue 181453002: Reset trunk to 3.24.35.4 (Closed) Base URL: https://v8.googlecode.com/svn/trunk
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « test/mjsunit/harmony/proxies.js ('k') | test/mjsunit/mjsunit.status » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 the V8 project authors. All rights reserved. 1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 12 matching lines...) Expand all
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 27
28 // Flags: --harmony-symbols --harmony-collections 28 // Flags: --harmony-symbols --harmony-collections
29 // Flags: --expose-gc --allow-natives-syntax 29 // Flags: --expose-gc --allow-natives-syntax
30 30
31 var symbols = [] 31 var symbols = []
32 32
33 33 // Test different forms of constructor calls, all equivalent.
34 // Returns true if the string is a valid
35 // serialization of Symbols added to the 'symbols'
36 // array. Adjust if you extend 'symbols' with other
37 // values.
38 function isValidSymbolString(s) {
39 return ["Symbol(66)", "Symbol()"].indexOf(s) >= 0;
40 }
41
42
43 // Test different forms of constructor calls.
44 function TestNew() { 34 function TestNew() {
45 function indirectSymbol() { return Symbol() } 35 function IndirectSymbol() { return new Symbol }
46 function indirect() { return indirectSymbol() } 36 function indirect() { return new IndirectSymbol() }
47 for (var i = 0; i < 2; ++i) { 37 for (var i = 0; i < 2; ++i) {
48 for (var j = 0; j < 5; ++j) { 38 for (var j = 0; j < 5; ++j) {
49 symbols.push(Symbol()) 39 symbols.push(Symbol())
50 symbols.push(Symbol(undefined)) 40 symbols.push(Symbol(undefined))
51 symbols.push(Symbol("66")) 41 symbols.push(Symbol("66"))
52 symbols.push(Symbol(66)) 42 symbols.push(Symbol(66))
53 symbols.push(Symbol().valueOf()) 43 symbols.push(Symbol(Symbol()))
54 symbols.push(indirect()) 44 symbols.push((new Symbol).valueOf())
45 symbols.push((new Symbol()).valueOf())
46 symbols.push((new Symbol(Symbol())).valueOf())
47 symbols.push(Object(Symbol()).valueOf())
48 symbols.push((indirect()).valueOf())
55 } 49 }
56 %OptimizeFunctionOnNextCall(indirect) 50 %OptimizeFunctionOnNextCall(indirect)
57 indirect() // Call once before GC throws away type feedback. 51 indirect() // Call once before GC throws away type feedback.
58 gc() // Promote existing symbols and then allocate some more. 52 gc() // Promote existing symbols and then allocate some more.
59 } 53 }
60 assertThrows(function () { Symbol(Symbol()) }, TypeError)
61 assertThrows(function () { new Symbol(66) }, TypeError)
62 } 54 }
63 TestNew() 55 TestNew()
64 56
65 57
66 function TestType() { 58 function TestType() {
67 for (var i in symbols) { 59 for (var i in symbols) {
68 assertEquals("symbol", typeof symbols[i]) 60 assertEquals("symbol", typeof symbols[i])
69 assertTrue(typeof symbols[i] === "symbol") 61 assertTrue(typeof symbols[i] === "symbol")
70 assertFalse(%SymbolIsPrivate(symbols[i])) 62 assertFalse(%SymbolIsPrivate(symbols[i]))
71 assertEquals(null, %_ClassOf(symbols[i])) 63 assertEquals(null, %_ClassOf(symbols[i]))
64 assertEquals("Symbol", %_ClassOf(new Symbol(symbols[i])))
72 assertEquals("Symbol", %_ClassOf(Object(symbols[i]))) 65 assertEquals("Symbol", %_ClassOf(Object(symbols[i])))
73 } 66 }
74 } 67 }
75 TestType() 68 TestType()
76 69
77 70
78 function TestPrototype() { 71 function TestPrototype() {
79 assertSame(Object.prototype, Symbol.prototype.__proto__) 72 assertSame(Object.prototype, Symbol.prototype.__proto__)
80 assertSame(Symbol.prototype, Symbol().__proto__) 73 assertSame(Symbol.prototype, Symbol().__proto__)
74 assertSame(Symbol.prototype, Symbol(Symbol()).__proto__)
75 assertSame(Symbol.prototype, (new Symbol).__proto__)
76 assertSame(Symbol.prototype, (new Symbol()).__proto__)
77 assertSame(Symbol.prototype, (new Symbol(Symbol())).__proto__)
81 assertSame(Symbol.prototype, Object(Symbol()).__proto__) 78 assertSame(Symbol.prototype, Object(Symbol()).__proto__)
82 for (var i in symbols) { 79 for (var i in symbols) {
83 assertSame(Symbol.prototype, symbols[i].__proto__) 80 assertSame(Symbol.prototype, symbols[i].__proto__)
84 } 81 }
85 } 82 }
86 TestPrototype() 83 TestPrototype()
87 84
88 85
89 function TestConstructor() { 86 function TestConstructor() {
90 assertSame(Function.prototype, Symbol.__proto__)
91 assertFalse(Object === Symbol.prototype.constructor) 87 assertFalse(Object === Symbol.prototype.constructor)
92 assertFalse(Symbol === Object.prototype.constructor) 88 assertFalse(Symbol === Object.prototype.constructor)
93 assertSame(Symbol, Symbol.prototype.constructor) 89 assertSame(Symbol, Symbol.prototype.constructor)
94 assertSame(Symbol, Symbol().__proto__.constructor) 90 assertSame(Symbol, Symbol().__proto__.constructor)
91 assertSame(Symbol, Symbol(Symbol()).__proto__.constructor)
92 assertSame(Symbol, (new Symbol).__proto__.constructor)
93 assertSame(Symbol, (new Symbol()).__proto__.constructor)
94 assertSame(Symbol, (new Symbol(Symbol())).__proto__.constructor)
95 assertSame(Symbol, Object(Symbol()).__proto__.constructor) 95 assertSame(Symbol, Object(Symbol()).__proto__.constructor)
96 for (var i in symbols) { 96 for (var i in symbols) {
97 assertSame(Symbol, symbols[i].__proto__.constructor) 97 assertSame(Symbol, symbols[i].__proto__.constructor)
98 } 98 }
99 } 99 }
100 TestConstructor() 100 TestConstructor()
101 101
102 102
103 function TestValueOf() { 103 function TestName() {
104 for (var i in symbols) { 104 for (var i in symbols) {
105 assertTrue(symbols[i] === symbols[i].valueOf()) 105 var name = symbols[i].name
106 assertTrue(Symbol.prototype.valueOf.call(symbols[i]) === symbols[i]) 106 assertTrue(name === undefined || name === "66")
107 } 107 }
108 } 108 }
109 TestValueOf() 109 TestName()
110 110
111 111
112 function TestToString() { 112 function TestToString() {
113 for (var i in symbols) { 113 for (var i in symbols) {
114 assertThrows(function() { String(symbols[i]) }, TypeError) 114 assertThrows(function() { String(symbols[i]) }, TypeError)
115 assertThrows(function() { symbols[i] + "" }, TypeError) 115 assertThrows(function() { symbols[i] + "" }, TypeError)
116 assertTrue(isValidSymbolString(String(Object(symbols[i])))) 116 assertThrows(function() { symbols[i].toString() }, TypeError)
117 assertTrue(isValidSymbolString(symbols[i].toString())) 117 assertThrows(function() { (new Symbol(symbols[i])).toString() }, TypeError)
118 assertTrue(isValidSymbolString(Object(symbols[i]).toString())) 118 assertThrows(function() { Object(symbols[i]).toString() }, TypeError)
119 assertTrue( 119 assertEquals("[object Symbol]", Object.prototype.toString.call(symbols[i]))
120 isValidSymbolString(Symbol.prototype.toString.call(symbols[i])))
121 assertEquals(
122 "[object Symbol]", Object.prototype.toString.call(symbols[i]))
123 } 120 }
124 } 121 }
125 TestToString() 122 TestToString()
126 123
127 124
128 function TestToBoolean() { 125 function TestToBoolean() {
129 for (var i in symbols) { 126 for (var i in symbols) {
130 assertTrue(Boolean(symbols[i]).valueOf()) 127 assertTrue(Boolean(symbols[i]).valueOf())
131 assertFalse(!symbols[i]) 128 assertFalse(!symbols[i])
132 assertTrue(!!symbols[i]) 129 assertTrue(!!symbols[i])
(...skipping 19 matching lines...) Expand all
152 149
153 150
154 function TestEquality() { 151 function TestEquality() {
155 // Every symbol should equal itself, and non-strictly equal its wrapper. 152 // Every symbol should equal itself, and non-strictly equal its wrapper.
156 for (var i in symbols) { 153 for (var i in symbols) {
157 assertSame(symbols[i], symbols[i]) 154 assertSame(symbols[i], symbols[i])
158 assertEquals(symbols[i], symbols[i]) 155 assertEquals(symbols[i], symbols[i])
159 assertTrue(Object.is(symbols[i], symbols[i])) 156 assertTrue(Object.is(symbols[i], symbols[i]))
160 assertTrue(symbols[i] === symbols[i]) 157 assertTrue(symbols[i] === symbols[i])
161 assertTrue(symbols[i] == symbols[i]) 158 assertTrue(symbols[i] == symbols[i])
162 assertFalse(symbols[i] === Object(symbols[i])) 159 assertFalse(symbols[i] === new Symbol(symbols[i]))
163 assertFalse(Object(symbols[i]) === symbols[i]) 160 assertFalse(new Symbol(symbols[i]) === symbols[i])
164 assertFalse(symbols[i] == Object(symbols[i])) 161 assertTrue(symbols[i] == new Symbol(symbols[i]))
165 assertFalse(Object(symbols[i]) == symbols[i]) 162 assertTrue(new Symbol(symbols[i]) == symbols[i])
166 assertTrue(symbols[i] === symbols[i].valueOf())
167 assertTrue(symbols[i].valueOf() === symbols[i])
168 assertTrue(symbols[i] == symbols[i].valueOf())
169 assertTrue(symbols[i].valueOf() == symbols[i])
170 assertFalse(Object(symbols[i]) === Object(symbols[i]))
171 assertEquals(Object(symbols[i]).valueOf(), Object(symbols[i]).valueOf())
172 } 163 }
173 164
174 // All symbols should be distinct. 165 // All symbols should be distinct.
175 for (var i = 0; i < symbols.length; ++i) { 166 for (var i = 0; i < symbols.length; ++i) {
176 for (var j = i + 1; j < symbols.length; ++j) { 167 for (var j = i + 1; j < symbols.length; ++j) {
177 assertFalse(Object.is(symbols[i], symbols[j])) 168 assertFalse(Object.is(symbols[i], symbols[j]))
178 assertFalse(symbols[i] === symbols[j]) 169 assertFalse(symbols[i] === symbols[j])
179 assertFalse(symbols[i] == symbols[j]) 170 assertFalse(symbols[i] == symbols[j])
180 } 171 }
181 } 172 }
182 173
183 // Symbols should not be equal to any other value (and the test terminates). 174 // Symbols should not be equal to any other value (and the test terminates).
184 var values = [347, 1.275, NaN, "string", null, undefined, {}, function() {}] 175 var values = [347, 1.275, NaN, "string", null, undefined, {}, function() {}]
185 for (var i in symbols) { 176 for (var i in symbols) {
186 for (var j in values) { 177 for (var j in values) {
187 assertFalse(symbols[i] === values[j]) 178 assertFalse(symbols[i] === values[j])
188 assertFalse(values[j] === symbols[i]) 179 assertFalse(values[j] === symbols[i])
189 assertFalse(symbols[i] == values[j]) 180 assertFalse(symbols[i] == values[j])
190 assertFalse(values[j] == symbols[i]) 181 assertFalse(values[j] == symbols[i])
191 } 182 }
192 } 183 }
193 } 184 }
194 TestEquality() 185 TestEquality()
195 186
196 187
197 function TestGet() { 188 function TestGet() {
198 for (var i in symbols) { 189 for (var i in symbols) {
199 assertTrue(isValidSymbolString(symbols[i].toString())) 190 assertThrows(function() { symbols[i].toString() }, TypeError)
200 assertEquals(symbols[i], symbols[i].valueOf()) 191 assertEquals(symbols[i], symbols[i].valueOf())
201 assertEquals(undefined, symbols[i].a) 192 assertEquals(undefined, symbols[i].a)
202 assertEquals(undefined, symbols[i]["a" + "b"]) 193 assertEquals(undefined, symbols[i]["a" + "b"])
203 assertEquals(undefined, symbols[i]["" + "1"]) 194 assertEquals(undefined, symbols[i]["" + "1"])
204 assertEquals(undefined, symbols[i][62]) 195 assertEquals(undefined, symbols[i][62])
205 } 196 }
206 } 197 }
207 TestGet() 198 TestGet()
208 199
209 200
210 function TestSet() { 201 function TestSet() {
211 for (var i in symbols) { 202 for (var i in symbols) {
212 symbols[i].toString = 0 203 symbols[i].toString = 0
213 assertTrue(isValidSymbolString(symbols[i].toString())) 204 assertThrows(function() { symbols[i].toString() }, TypeError)
214 symbols[i].valueOf = 0 205 symbols[i].valueOf = 0
215 assertEquals(symbols[i], symbols[i].valueOf()) 206 assertEquals(symbols[i], symbols[i].valueOf())
216 symbols[i].a = 0 207 symbols[i].a = 0
217 assertEquals(undefined, symbols[i].a) 208 assertEquals(undefined, symbols[i].a)
218 symbols[i]["a" + "b"] = 0 209 symbols[i]["a" + "b"] = 0
219 assertEquals(undefined, symbols[i]["a" + "b"]) 210 assertEquals(undefined, symbols[i]["a" + "b"])
220 symbols[i][62] = 0 211 symbols[i][62] = 0
221 assertEquals(undefined, symbols[i][62]) 212 assertEquals(undefined, symbols[i][62])
222 } 213 }
223 } 214 }
224 TestSet() 215 TestSet()
225 216
226 217
227 // Test Symbol wrapping/boxing over non-builtins.
228 Symbol.prototype.getThisProto = function () {
229 return Object.getPrototypeOf(this);
230 }
231 function TestCall() {
232 for (var i in symbols) {
233 assertTrue(symbols[i].getThisProto() === Symbol.prototype)
234 }
235 }
236 TestCall()
237
238
239 function TestCollections() { 218 function TestCollections() {
240 var set = new Set 219 var set = new Set
241 var map = new Map 220 var map = new Map
242 var weakmap = new WeakMap 221 var weakmap = new WeakMap
243 for (var i in symbols) { 222 for (var i in symbols) {
244 set.add(symbols[i]) 223 set.add(symbols[i])
245 map.set(symbols[i], i) 224 map.set(symbols[i], i)
246 weakmap.set(symbols[i], i) 225 weakmap.set(symbols[i], i)
247 } 226 }
248 assertEquals(symbols.length, set.size) 227 assertEquals(symbols.length, set.size)
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
323 var syms = Object.getOwnPropertySymbols(obj) 302 var syms = Object.getOwnPropertySymbols(obj)
324 assertEquals(syms.length, symbols.length) 303 assertEquals(syms.length, symbols.length)
325 for (var i in syms) { 304 for (var i in syms) {
326 assertEquals("symbol", typeof syms[i]) 305 assertEquals("symbol", typeof syms[i])
327 } 306 }
328 } 307 }
329 308
330 309
331 function TestKeyDescriptor(obj) { 310 function TestKeyDescriptor(obj) {
332 for (var i in symbols) { 311 for (var i in symbols) {
333 var desc = Object.getOwnPropertyDescriptor(obj, symbols[i]) 312 var desc = Object.getOwnPropertyDescriptor(obj, symbols[i]);
334 assertEquals(i|0, desc.value) 313 assertEquals(i|0, desc.value)
335 assertTrue(desc.configurable) 314 assertTrue(desc.configurable)
336 assertEquals(i % 2 == 0, desc.writable) 315 assertEquals(i % 2 == 0, desc.writable)
337 assertEquals(i % 2 == 0, desc.enumerable) 316 assertEquals(i % 2 == 0, desc.enumerable)
338 assertEquals(i % 2 == 0, 317 assertEquals(i % 2 == 0,
339 Object.prototype.propertyIsEnumerable.call(obj, symbols[i])) 318 Object.prototype.propertyIsEnumerable.call(obj, symbols[i]))
340 } 319 }
341 } 320 }
342 321
343 322
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
415 var publicSymbol = Symbol() 394 var publicSymbol = Symbol()
416 var publicSymbol2 = Symbol() 395 var publicSymbol2 = Symbol()
417 var obj = {} 396 var obj = {}
418 obj[publicSymbol] = 1 397 obj[publicSymbol] = 1
419 obj[privateSymbol] = 2 398 obj[privateSymbol] = 2
420 obj[publicSymbol2] = 3 399 obj[publicSymbol2] = 3
421 var syms = Object.getOwnPropertySymbols(obj) 400 var syms = Object.getOwnPropertySymbols(obj)
422 assertEquals(syms, [publicSymbol, publicSymbol2]) 401 assertEquals(syms, [publicSymbol, publicSymbol2])
423 } 402 }
424 TestGetOwnPropertySymbolsWithPrivateSymbols() 403 TestGetOwnPropertySymbolsWithPrivateSymbols()
OLDNEW
« no previous file with comments | « test/mjsunit/harmony/proxies.js ('k') | test/mjsunit/mjsunit.status » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698