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

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: Allow valueOf()/toString() to be called over Symbol values. 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
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) {
rossberg 2014/02/14 10:55:30 Please move this down into TestToString.
sof 2014/02/14 14:20:02 I've kept it here, better to have it be next to wh
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()))
rossberg 2014/02/14 10:55:30 Nit: redundant parens
sof 2014/02/14 14:20:02 ok
45 symbols.push((new Symbol()).valueOf()) 55 assertThrows(function () { Symbol(Symbol()) }, TypeError)
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)
117 assertThrows(function() { (new Symbol(symbols[i])).toString() }, TypeError) 117
118 assertThrows(function() { Object(symbols[i]).toString() }, TypeError) 118 function checkToString(f) {
119 var stringValue
120 assertDoesNotThrow(function() { stringValue = f() })
121 assertTrue(isValidSymbolString(stringValue))
rossberg 2014/02/14 10:55:30 I don't think we need the explicit assertDoesNotTh
sof 2014/02/14 14:20:02 Removed
122 }
123 checkToString(function() { return String(Object(symbols[i])) })
124 checkToString(function() { return symbols[i].toString() })
125 checkToString(function() { return Object(symbols[i]).toString() })
126 checkToString(function() { return Symbol.prototype.toString.call(symbols[i]) })
rossberg 2014/02/14 10:55:30 Nit: line length
sof 2014/02/14 14:20:02 Tweaked, but isn't it < 80?
rossberg 2014/02/18 17:05:58 Looked like 83 in the code review...
127
119 assertEquals("[object Symbol]", Object.prototype.toString.call(symbols[i])) 128 assertEquals("[object Symbol]", Object.prototype.toString.call(symbols[i]))
120 } 129 }
121 } 130 }
122 TestToString() 131 TestToString()
123 132
124 133
125 function TestToBoolean() { 134 function TestToBoolean() {
126 for (var i in symbols) { 135 for (var i in symbols) {
127 assertTrue(Boolean(symbols[i]).valueOf()) 136 assertTrue(Boolean(symbols[i]).valueOf())
128 assertFalse(!symbols[i]) 137 assertFalse(!symbols[i])
(...skipping 20 matching lines...) Expand all
149 158
150 159
151 function TestEquality() { 160 function TestEquality() {
152 // Every symbol should equal itself, and non-strictly equal its wrapper. 161 // Every symbol should equal itself, and non-strictly equal its wrapper.
153 for (var i in symbols) { 162 for (var i in symbols) {
154 assertSame(symbols[i], symbols[i]) 163 assertSame(symbols[i], symbols[i])
155 assertEquals(symbols[i], symbols[i]) 164 assertEquals(symbols[i], symbols[i])
156 assertTrue(Object.is(symbols[i], symbols[i])) 165 assertTrue(Object.is(symbols[i], symbols[i]))
157 assertTrue(symbols[i] === symbols[i]) 166 assertTrue(symbols[i] === symbols[i])
158 assertTrue(symbols[i] == symbols[i]) 167 assertTrue(symbols[i] == symbols[i])
159 assertFalse(symbols[i] === new Symbol(symbols[i])) 168 assertTrue(symbols[i] === symbols[i].valueOf())
rossberg 2014/02/14 10:55:30 We should keep the original tests, by replacing "n
sof 2014/02/14 14:20:02 Done.
160 assertFalse(new Symbol(symbols[i]) === symbols[i]) 169 assertTrue(symbols[i].valueOf() === symbols[i])
161 assertTrue(symbols[i] == new Symbol(symbols[i])) 170 assertTrue(symbols[i] == symbols[i].valueOf())
162 assertTrue(new Symbol(symbols[i]) == symbols[i]) 171 assertTrue(symbols[i].valueOf() == symbols[i])
172 assertTrue(Object(symbols[i]) !== Object(symbols[i]))
173 assertEquals(Object(symbols[i]).valueOf(), Object(symbols[i]).valueOf())
163 } 174 }
164 175
165 // All symbols should be distinct. 176 // All symbols should be distinct.
166 for (var i = 0; i < symbols.length; ++i) { 177 for (var i = 0; i < symbols.length; ++i) {
167 for (var j = i + 1; j < symbols.length; ++j) { 178 for (var j = i + 1; j < symbols.length; ++j) {
168 assertFalse(Object.is(symbols[i], symbols[j])) 179 assertFalse(Object.is(symbols[i], symbols[j]))
169 assertFalse(symbols[i] === symbols[j]) 180 assertFalse(symbols[i] === symbols[j])
170 assertFalse(symbols[i] == symbols[j]) 181 assertFalse(symbols[i] == symbols[j])
171 } 182 }
172 } 183 }
173 184
174 // Symbols should not be equal to any other value (and the test terminates). 185 // Symbols should not be equal to any other value (and the test terminates).
175 var values = [347, 1.275, NaN, "string", null, undefined, {}, function() {}] 186 var values = [347, 1.275, NaN, "string", null, undefined, {}, function() {}]
176 for (var i in symbols) { 187 for (var i in symbols) {
177 for (var j in values) { 188 for (var j in values) {
178 assertFalse(symbols[i] === values[j]) 189 assertFalse(symbols[i] === values[j])
179 assertFalse(values[j] === symbols[i]) 190 assertFalse(values[j] === symbols[i])
180 assertFalse(symbols[i] == values[j]) 191 assertFalse(symbols[i] == values[j])
181 assertFalse(values[j] == symbols[i]) 192 assertFalse(values[j] == symbols[i])
182 } 193 }
183 } 194 }
184 } 195 }
185 TestEquality() 196 TestEquality()
186 197
187 198
188 function TestGet() { 199 function TestGet() {
189 for (var i in symbols) { 200 for (var i in symbols) {
190 assertThrows(function() { symbols[i].toString() }, TypeError) 201 assertDoesNotThrow(function() { symbols[i].toString() })
rossberg 2014/02/14 10:55:30 Check result
sof 2014/02/14 14:20:02 Done, using isValidSymbolString().
191 assertEquals(symbols[i], symbols[i].valueOf()) 202 assertEquals(symbols[i], symbols[i].valueOf())
192 assertEquals(undefined, symbols[i].a) 203 assertEquals(undefined, symbols[i].a)
193 assertEquals(undefined, symbols[i]["a" + "b"]) 204 assertEquals(undefined, symbols[i]["a" + "b"])
194 assertEquals(undefined, symbols[i]["" + "1"]) 205 assertEquals(undefined, symbols[i]["" + "1"])
195 assertEquals(undefined, symbols[i][62]) 206 assertEquals(undefined, symbols[i][62])
196 } 207 }
197 } 208 }
198 TestGet() 209 TestGet()
199 210
200 211
201 function TestSet() { 212 function TestSet() {
202 for (var i in symbols) { 213 for (var i in symbols) {
203 symbols[i].toString = 0 214 symbols[i].toString = 0
204 assertThrows(function() { symbols[i].toString() }, TypeError) 215 assertDoesNotThrow(function() { symbols[i].toString() })
rossberg 2014/02/14 10:55:30 Check result
sof 2014/02/14 14:20:02 Done.
205 symbols[i].valueOf = 0 216 symbols[i].valueOf = 0
206 assertEquals(symbols[i], symbols[i].valueOf()) 217 assertEquals(symbols[i], symbols[i].valueOf())
207 symbols[i].a = 0 218 symbols[i].a = 0
208 assertEquals(undefined, symbols[i].a) 219 assertEquals(undefined, symbols[i].a)
209 symbols[i]["a" + "b"] = 0 220 symbols[i]["a" + "b"] = 0
210 assertEquals(undefined, symbols[i]["a" + "b"]) 221 assertEquals(undefined, symbols[i]["a" + "b"])
211 symbols[i][62] = 0 222 symbols[i][62] = 0
212 assertEquals(undefined, symbols[i][62]) 223 assertEquals(undefined, symbols[i][62])
213 } 224 }
214 } 225 }
215 TestSet() 226 TestSet()
216 227
217 228
229 // Test Symbol wrapping/boxing over non-builtins.
230 Symbol.prototype.extra = function (m) { return (m + this.toString()) }
231 function TestCall() {
232 for (var i in symbols) {
233 var stringValue
234 assertDoesNotThrow(function() { stringValue = symbols[i].extra("") })
rossberg 2014/02/14 10:55:30 Like above, you can remove the assertDoesNotThrow
sof 2014/02/14 14:20:02 Done.
235 assertTrue(isValidSymbolString(stringValue))
236 }
237 }
238 TestCall()
239
240
218 function TestCollections() { 241 function TestCollections() {
219 var set = new Set 242 var set = new Set
220 var map = new Map 243 var map = new Map
221 var weakmap = new WeakMap 244 var weakmap = new WeakMap
222 for (var i in symbols) { 245 for (var i in symbols) {
223 set.add(symbols[i]) 246 set.add(symbols[i])
224 map.set(symbols[i], i) 247 map.set(symbols[i], i)
225 weakmap.set(symbols[i], i) 248 weakmap.set(symbols[i], i)
226 } 249 }
227 assertEquals(symbols.length, set.size) 250 assertEquals(symbols.length, set.size)
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
302 var syms = Object.getOwnPropertySymbols(obj) 325 var syms = Object.getOwnPropertySymbols(obj)
303 assertEquals(syms.length, symbols.length) 326 assertEquals(syms.length, symbols.length)
304 for (var i in syms) { 327 for (var i in syms) {
305 assertEquals("symbol", typeof syms[i]) 328 assertEquals("symbol", typeof syms[i])
306 } 329 }
307 } 330 }
308 331
309 332
310 function TestKeyDescriptor(obj) { 333 function TestKeyDescriptor(obj) {
311 for (var i in symbols) { 334 for (var i in symbols) {
312 var desc = Object.getOwnPropertyDescriptor(obj, symbols[i]); 335 var desc = Object.getOwnPropertyDescriptor(obj, symbols[i])
313 assertEquals(i|0, desc.value) 336 assertEquals(i|0, desc.value)
314 assertTrue(desc.configurable) 337 assertTrue(desc.configurable)
315 assertEquals(i % 2 == 0, desc.writable) 338 assertEquals(i % 2 == 0, desc.writable)
316 assertEquals(i % 2 == 0, desc.enumerable) 339 assertEquals(i % 2 == 0, desc.enumerable)
317 assertEquals(i % 2 == 0, 340 assertEquals(i % 2 == 0,
318 Object.prototype.propertyIsEnumerable.call(obj, symbols[i])) 341 Object.prototype.propertyIsEnumerable.call(obj, symbols[i]))
319 } 342 }
320 } 343 }
321 344
322 345
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
394 var publicSymbol = Symbol() 417 var publicSymbol = Symbol()
395 var publicSymbol2 = Symbol() 418 var publicSymbol2 = Symbol()
396 var obj = {} 419 var obj = {}
397 obj[publicSymbol] = 1 420 obj[publicSymbol] = 1
398 obj[privateSymbol] = 2 421 obj[privateSymbol] = 2
399 obj[publicSymbol2] = 3 422 obj[publicSymbol2] = 3
400 var syms = Object.getOwnPropertySymbols(obj) 423 var syms = Object.getOwnPropertySymbols(obj)
401 assertEquals(syms, [publicSymbol, publicSymbol2]) 424 assertEquals(syms, [publicSymbol, publicSymbol2])
402 } 425 }
403 TestGetOwnPropertySymbolsWithPrivateSymbols() 426 TestGetOwnPropertySymbolsWithPrivateSymbols()
OLDNEW
« test/mjsunit/harmony/private.js ('K') | « test/mjsunit/harmony/private.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698