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

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

Issue 118553003: Upgrade Symbol implementation to match current ES6 behavior. (Closed) Base URL: git://github.com/v8/v8.git@bleeding_edge
Patch Set: EQUALS(): add missing Symbol checks 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
« no previous file with comments | « test/mjsunit/harmony/private.js ('k') | no next file » | 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
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
33 // Test different forms of constructor calls, all equivalent. 43 // Test different forms of constructor calls, all equivalent.
34 function TestNew() { 44 function TestNew() {
35 function IndirectSymbol() { return new Symbol } 45 function indirectSymbol() { return Symbol() }
36 function indirect() { return new IndirectSymbol() } 46 function indirect() { return indirectSymbol() }
37 for (var i = 0; i < 2; ++i) { 47 for (var i = 0; i < 2; ++i) {
38 for (var j = 0; j < 5; ++j) { 48 for (var j = 0; j < 5; ++j) {
39 symbols.push(Symbol()) 49 symbols.push(Symbol())
40 symbols.push(Symbol(undefined)) 50 symbols.push(Symbol(undefined))
41 symbols.push(Symbol("66")) 51 symbols.push(Symbol("66"))
42 symbols.push(Symbol(66)) 52 symbols.push(Symbol(66))
43 symbols.push(Symbol(Symbol())) 53 symbols.push(Symbol().valueOf())
44 symbols.push((new Symbol).valueOf()) 54 symbols.push(indirect())
45 symbols.push((new Symbol()).valueOf()) 55 assertThrows(function () { Symbol(Symbol()) }, TypeError)
arv (Not doing code reviews) 2014/02/14 15:30:11 These two assertThrows can be done once outside th
sof 2014/02/14 15:43:43 Yes. Belongs in TestNew() though, I think, so move
46 symbols.push((new Symbol(Symbol())).valueOf()) 56 assertThrows(function () { new Symbol(66) }, TypeError)
47 symbols.push(Object(Symbol()).valueOf())
48 symbols.push((indirect()).valueOf())
49 } 57 }
50 %OptimizeFunctionOnNextCall(indirect) 58 %OptimizeFunctionOnNextCall(indirect)
51 indirect() // Call once before GC throws away type feedback. 59 indirect() // Call once before GC throws away type feedback.
52 gc() // Promote existing symbols and then allocate some more. 60 gc() // Promote existing symbols and then allocate some more.
53 } 61 }
54 } 62 }
55 TestNew() 63 TestNew()
56 64
57 65
58 function TestType() { 66 function TestType() {
59 for (var i in symbols) { 67 for (var i in symbols) {
60 assertEquals("symbol", typeof symbols[i]) 68 assertEquals("symbol", typeof symbols[i])
61 assertTrue(typeof symbols[i] === "symbol") 69 assertTrue(typeof symbols[i] === "symbol")
62 assertFalse(%SymbolIsPrivate(symbols[i])) 70 assertFalse(%SymbolIsPrivate(symbols[i]))
63 assertEquals(null, %_ClassOf(symbols[i])) 71 assertEquals(null, %_ClassOf(symbols[i]))
64 assertEquals("Symbol", %_ClassOf(new Symbol(symbols[i])))
65 assertEquals("Symbol", %_ClassOf(Object(symbols[i]))) 72 assertEquals("Symbol", %_ClassOf(Object(symbols[i])))
66 } 73 }
67 } 74 }
68 TestType() 75 TestType()
69 76
70 77
71 function TestPrototype() { 78 function TestPrototype() {
72 assertSame(Object.prototype, Symbol.prototype.__proto__) 79 assertSame(Object.prototype, Symbol.prototype.__proto__)
73 assertSame(Symbol.prototype, Symbol().__proto__) 80 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__)
78 assertSame(Symbol.prototype, Object(Symbol()).__proto__) 81 assertSame(Symbol.prototype, Object(Symbol()).__proto__)
79 for (var i in symbols) { 82 for (var i in symbols) {
80 assertSame(Symbol.prototype, symbols[i].__proto__) 83 assertSame(Symbol.prototype, symbols[i].__proto__)
81 } 84 }
82 } 85 }
83 TestPrototype() 86 TestPrototype()
84 87
85 88
86 function TestConstructor() { 89 function TestConstructor() {
90 assertSame(Function.prototype, Symbol.__proto__)
87 assertFalse(Object === Symbol.prototype.constructor) 91 assertFalse(Object === Symbol.prototype.constructor)
88 assertFalse(Symbol === Object.prototype.constructor) 92 assertFalse(Symbol === Object.prototype.constructor)
89 assertSame(Symbol, Symbol.prototype.constructor) 93 assertSame(Symbol, Symbol.prototype.constructor)
90 assertSame(Symbol, Symbol().__proto__.constructor) 94 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 TestName() { 103 function TestValueOf() {
104 for (var i in symbols) { 104 for (var i in symbols) {
105 var name = symbols[i].name 105 assertTrue(symbols[i] === symbols[i].valueOf())
106 assertTrue(name === undefined || name === "66") 106 assertTrue(Symbol.prototype.valueOf.call(symbols[i]) === symbols[i])
107 } 107 }
108 } 108 }
109 TestName() 109 TestValueOf()
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 assertThrows(function() { symbols[i].toString() }, TypeError) 116 assertThrows(function() { (Symbol(symbols[i])).toString() }, TypeError)
arv (Not doing code reviews) 2014/02/14 15:30:11 Doesn't this one throw due to Symbol(symbols[i]) a
sof 2014/02/14 15:43:43 Yes, it's not testing toString(); dropped.
117 assertThrows(function() { (new Symbol(symbols[i])).toString() }, TypeError) 117 assertTrue(isValidSymbolString(String(Object(symbols[i]))))
118 assertThrows(function() { Object(symbols[i]).toString() }, TypeError) 118 assertTrue(isValidSymbolString(symbols[i].toString()))
119 assertEquals("[object Symbol]", Object.prototype.toString.call(symbols[i])) 119 assertTrue(isValidSymbolString(Object(symbols[i]).toString()))
120 assertTrue(
121 isValidSymbolString(Symbol.prototype.toString.call(symbols[i])))
122 assertEquals(
123 "[object Symbol]", Object.prototype.toString.call(symbols[i]))
120 } 124 }
121 } 125 }
122 TestToString() 126 TestToString()
123 127
124 128
125 function TestToBoolean() { 129 function TestToBoolean() {
126 for (var i in symbols) { 130 for (var i in symbols) {
127 assertTrue(Boolean(symbols[i]).valueOf()) 131 assertTrue(Boolean(symbols[i]).valueOf())
128 assertFalse(!symbols[i]) 132 assertFalse(!symbols[i])
129 assertTrue(!!symbols[i]) 133 assertTrue(!!symbols[i])
(...skipping 19 matching lines...) Expand all
149 153
150 154
151 function TestEquality() { 155 function TestEquality() {
152 // Every symbol should equal itself, and non-strictly equal its wrapper. 156 // Every symbol should equal itself, and non-strictly equal its wrapper.
153 for (var i in symbols) { 157 for (var i in symbols) {
154 assertSame(symbols[i], symbols[i]) 158 assertSame(symbols[i], symbols[i])
155 assertEquals(symbols[i], symbols[i]) 159 assertEquals(symbols[i], symbols[i])
156 assertTrue(Object.is(symbols[i], symbols[i])) 160 assertTrue(Object.is(symbols[i], symbols[i]))
157 assertTrue(symbols[i] === symbols[i]) 161 assertTrue(symbols[i] === symbols[i])
158 assertTrue(symbols[i] == symbols[i]) 162 assertTrue(symbols[i] == symbols[i])
159 assertFalse(symbols[i] === new Symbol(symbols[i])) 163 assertFalse(symbols[i] === Object(symbols[i]))
160 assertFalse(new Symbol(symbols[i]) === symbols[i]) 164 assertFalse(Object(symbols[i]) === symbols[i])
161 assertTrue(symbols[i] == new Symbol(symbols[i])) 165 assertFalse(symbols[i] == Object(symbols[i]))
162 assertTrue(new Symbol(symbols[i]) == symbols[i]) 166 assertFalse(Object(symbols[i]) == symbols[i])
167 assertTrue(symbols[i] === symbols[i].valueOf())
168 assertTrue(symbols[i].valueOf() === symbols[i])
169 assertTrue(symbols[i] == symbols[i].valueOf())
170 assertTrue(symbols[i].valueOf() == symbols[i])
171 assertTrue(Object(symbols[i]) !== Object(symbols[i]))
arv (Not doing code reviews) 2014/02/14 15:30:11 assertFalse(Object(symbols[i]) === Object(symbols[
sof 2014/02/14 15:43:43 Done.
172 assertEquals(Object(symbols[i]).valueOf(), Object(symbols[i]).valueOf())
163 } 173 }
164 174
165 // All symbols should be distinct. 175 // All symbols should be distinct.
166 for (var i = 0; i < symbols.length; ++i) { 176 for (var i = 0; i < symbols.length; ++i) {
167 for (var j = i + 1; j < symbols.length; ++j) { 177 for (var j = i + 1; j < symbols.length; ++j) {
168 assertFalse(Object.is(symbols[i], symbols[j])) 178 assertFalse(Object.is(symbols[i], symbols[j]))
169 assertFalse(symbols[i] === symbols[j]) 179 assertFalse(symbols[i] === symbols[j])
170 assertFalse(symbols[i] == symbols[j]) 180 assertFalse(symbols[i] == symbols[j])
171 } 181 }
172 } 182 }
173 183
174 // Symbols should not be equal to any other value (and the test terminates). 184 // Symbols should not be equal to any other value (and the test terminates).
175 var values = [347, 1.275, NaN, "string", null, undefined, {}, function() {}] 185 var values = [347, 1.275, NaN, "string", null, undefined, {}, function() {}]
176 for (var i in symbols) { 186 for (var i in symbols) {
177 for (var j in values) { 187 for (var j in values) {
178 assertFalse(symbols[i] === values[j]) 188 assertFalse(symbols[i] === values[j])
179 assertFalse(values[j] === symbols[i]) 189 assertFalse(values[j] === symbols[i])
180 assertFalse(symbols[i] == values[j]) 190 assertFalse(symbols[i] == values[j])
181 assertFalse(values[j] == symbols[i]) 191 assertFalse(values[j] == symbols[i])
182 } 192 }
183 } 193 }
184 } 194 }
185 TestEquality() 195 TestEquality()
186 196
187 197
188 function TestGet() { 198 function TestGet() {
189 for (var i in symbols) { 199 for (var i in symbols) {
190 assertThrows(function() { symbols[i].toString() }, TypeError) 200 assertTrue(isValidSymbolString(symbols[i].toString()))
191 assertEquals(symbols[i], symbols[i].valueOf()) 201 assertEquals(symbols[i], symbols[i].valueOf())
192 assertEquals(undefined, symbols[i].a) 202 assertEquals(undefined, symbols[i].a)
193 assertEquals(undefined, symbols[i]["a" + "b"]) 203 assertEquals(undefined, symbols[i]["a" + "b"])
194 assertEquals(undefined, symbols[i]["" + "1"]) 204 assertEquals(undefined, symbols[i]["" + "1"])
195 assertEquals(undefined, symbols[i][62]) 205 assertEquals(undefined, symbols[i][62])
196 } 206 }
197 } 207 }
198 TestGet() 208 TestGet()
199 209
200 210
201 function TestSet() { 211 function TestSet() {
202 for (var i in symbols) { 212 for (var i in symbols) {
203 symbols[i].toString = 0 213 symbols[i].toString = 0
204 assertThrows(function() { symbols[i].toString() }, TypeError) 214 assertTrue(isValidSymbolString(symbols[i].toString()))
205 symbols[i].valueOf = 0 215 symbols[i].valueOf = 0
206 assertEquals(symbols[i], symbols[i].valueOf()) 216 assertEquals(symbols[i], symbols[i].valueOf())
207 symbols[i].a = 0 217 symbols[i].a = 0
208 assertEquals(undefined, symbols[i].a) 218 assertEquals(undefined, symbols[i].a)
209 symbols[i]["a" + "b"] = 0 219 symbols[i]["a" + "b"] = 0
210 assertEquals(undefined, symbols[i]["a" + "b"]) 220 assertEquals(undefined, symbols[i]["a" + "b"])
211 symbols[i][62] = 0 221 symbols[i][62] = 0
212 assertEquals(undefined, symbols[i][62]) 222 assertEquals(undefined, symbols[i][62])
213 } 223 }
214 } 224 }
215 TestSet() 225 TestSet()
216 226
217 227
228 // Test Symbol wrapping/boxing over non-builtins.
229 Symbol.prototype.getThisProto = function () { return Object.getPrototypeOf(this) ; }
arv (Not doing code reviews) 2014/02/14 15:30:11 long line
arv (Not doing code reviews) 2014/02/14 15:30:11 too many spaces. But after line wrapping it should
sof 2014/02/14 15:43:43 Done.
230 function TestCall() {
231 for (var i in symbols) {
232 assertTrue(symbols[i].getThisProto() === Symbol.prototype)
233 }
234 }
235 TestCall()
236
237
218 function TestCollections() { 238 function TestCollections() {
219 var set = new Set 239 var set = new Set
220 var map = new Map 240 var map = new Map
221 var weakmap = new WeakMap 241 var weakmap = new WeakMap
222 for (var i in symbols) { 242 for (var i in symbols) {
223 set.add(symbols[i]) 243 set.add(symbols[i])
224 map.set(symbols[i], i) 244 map.set(symbols[i], i)
225 weakmap.set(symbols[i], i) 245 weakmap.set(symbols[i], i)
226 } 246 }
227 assertEquals(symbols.length, set.size) 247 assertEquals(symbols.length, set.size)
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
302 var syms = Object.getOwnPropertySymbols(obj) 322 var syms = Object.getOwnPropertySymbols(obj)
303 assertEquals(syms.length, symbols.length) 323 assertEquals(syms.length, symbols.length)
304 for (var i in syms) { 324 for (var i in syms) {
305 assertEquals("symbol", typeof syms[i]) 325 assertEquals("symbol", typeof syms[i])
306 } 326 }
307 } 327 }
308 328
309 329
310 function TestKeyDescriptor(obj) { 330 function TestKeyDescriptor(obj) {
311 for (var i in symbols) { 331 for (var i in symbols) {
312 var desc = Object.getOwnPropertyDescriptor(obj, symbols[i]); 332 var desc = Object.getOwnPropertyDescriptor(obj, symbols[i])
313 assertEquals(i|0, desc.value) 333 assertEquals(i|0, desc.value)
314 assertTrue(desc.configurable) 334 assertTrue(desc.configurable)
315 assertEquals(i % 2 == 0, desc.writable) 335 assertEquals(i % 2 == 0, desc.writable)
316 assertEquals(i % 2 == 0, desc.enumerable) 336 assertEquals(i % 2 == 0, desc.enumerable)
317 assertEquals(i % 2 == 0, 337 assertEquals(i % 2 == 0,
318 Object.prototype.propertyIsEnumerable.call(obj, symbols[i])) 338 Object.prototype.propertyIsEnumerable.call(obj, symbols[i]))
319 } 339 }
320 } 340 }
321 341
322 342
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
394 var publicSymbol = Symbol() 414 var publicSymbol = Symbol()
395 var publicSymbol2 = Symbol() 415 var publicSymbol2 = Symbol()
396 var obj = {} 416 var obj = {}
397 obj[publicSymbol] = 1 417 obj[publicSymbol] = 1
398 obj[privateSymbol] = 2 418 obj[privateSymbol] = 2
399 obj[publicSymbol2] = 3 419 obj[publicSymbol2] = 3
400 var syms = Object.getOwnPropertySymbols(obj) 420 var syms = Object.getOwnPropertySymbols(obj)
401 assertEquals(syms, [publicSymbol, publicSymbol2]) 421 assertEquals(syms, [publicSymbol, publicSymbol2])
402 } 422 }
403 TestGetOwnPropertySymbolsWithPrivateSymbols() 423 TestGetOwnPropertySymbolsWithPrivateSymbols()
OLDNEW
« no previous file with comments | « test/mjsunit/harmony/private.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698