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

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

Issue 12957004: ES6 symbols: turn symbols into a proper primitive type (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Addressed comments Created 7 years, 9 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 | « src/x64/lithium-codegen-x64.cc ('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 16 matching lines...) Expand all
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 // Test different forms of constructor calls, all equivalent. 33 // Test different forms of constructor calls, all equivalent.
34 function TestNew() { 34 function TestNew() {
35 function IndirectSymbol() { return new Symbol } 35 function IndirectSymbol() { return new Symbol }
36 function indirect() { return new IndirectSymbol() } 36 function indirect() { return new IndirectSymbol() }
37 for (var i = 0; i < 10; ++i) { 37 for (var i = 0; i < 2; ++i) {
38 symbols.push(new Symbol) 38 for (var j = 0; j < 5; ++j) {
39 symbols.push(new Symbol()) 39 symbols.push(Symbol())
40 symbols.push(Symbol()) 40 symbols.push(Symbol(Symbol()))
41 symbols.push(indirect()) 41 symbols.push((new Symbol).valueOf())
42 } 42 symbols.push((new Symbol()).valueOf())
43 %OptimizeFunctionOnNextCall(indirect) 43 symbols.push((new Symbol(Symbol())).valueOf())
44 indirect() // Call once before GC throws away type feedback. 44 symbols.push(Object(Symbol()).valueOf())
45 gc() // Promote existing symbols and then allocate some more. 45 symbols.push((indirect()).valueOf())
46 for (var i = 0; i < 10; ++i) { 46 }
47 symbols.push(new Symbol) 47 %OptimizeFunctionOnNextCall(indirect)
48 symbols.push(new Symbol()) 48 indirect() // Call once before GC throws away type feedback.
49 symbols.push(Symbol()) 49 gc() // Promote existing symbols and then allocate some more.
50 symbols.push(indirect())
51 } 50 }
52 } 51 }
53 TestNew() 52 TestNew()
54 53
55 54
56 function TestType() { 55 function TestType() {
57 for (var i in symbols) { 56 for (var i in symbols) {
58 assertTrue(%_IsSymbol(symbols[i])) 57 assertEquals("symbol", typeof symbols[i])
59 assertEquals("object", typeof symbols[i]) 58 assertTrue(typeof symbols[i] === "symbol")
60 assertTrue(typeof symbols[i] === "object") 59 assertEquals(null, %_ClassOf(symbols[i]))
61 assertEquals("[object Symbol]", Object.prototype.toString.call(symbols[i])) 60 assertEquals("Symbol", %_ClassOf(new Symbol(symbols[i])))
61 assertEquals("Symbol", %_ClassOf(Object(symbols[i])))
62 } 62 }
63 } 63 }
64 TestType() 64 TestType()
65 65
66 66
67 function TestPrototype() {
68 assertSame(Object.prototype, Symbol.prototype.__proto__)
69 assertSame(Symbol.prototype, Symbol().__proto__)
70 assertSame(Symbol.prototype, Symbol(Symbol()).__proto__)
71 assertSame(Symbol.prototype, (new Symbol).__proto__)
72 assertSame(Symbol.prototype, (new Symbol()).__proto__)
73 assertSame(Symbol.prototype, (new Symbol(Symbol())).__proto__)
74 assertSame(Symbol.prototype, Object(Symbol()).__proto__)
75 for (var i in symbols) {
76 assertSame(Symbol.prototype, symbols[i].__proto__)
77 }
78 }
79 TestPrototype()
80
81
82 function TestToString() {
83 for (var i in symbols) {
84 assertThrows(function() { String(symbols[i]) }, TypeError)
85 assertThrows(function() { symbols[i] + "" }, TypeError)
86 assertThrows(function() { symbols[i].toString() }, TypeError)
87 assertThrows(function() { (new Symbol(symbols[i])).toString() }, TypeError)
88 assertThrows(function() { Object(symbols[i]).toString() }, TypeError)
89 assertEquals("[object Symbol]", Object.prototype.toString.call(symbols[i]))
90 }
91 }
92 TestToString()
93
94
95 function TestToBoolean() {
96 for (var i in symbols) {
97 assertTrue(Boolean(symbols[i]).valueOf())
98 assertFalse(!symbols[i])
99 assertTrue(!!symbols[i])
100 assertTrue(symbols[i] && true)
101 assertFalse(!symbols[i] && false)
102 assertTrue(!symbols[i] || true)
103 assertEquals(1, symbols[i] ? 1 : 2)
104 assertEquals(2, !symbols[i] ? 1 : 2)
105 if (!symbols[i]) assertUnreachable();
106 if (symbols[i]) {} else assertUnreachable();
107 }
108 }
109 TestToBoolean()
110
111
112 function TestToNumber() {
113 for (var i in symbols) {
114 assertSame(NaN, Number(symbols[i]).valueOf())
115 assertSame(NaN, symbols[i] + 0)
116 }
117 }
118 TestToNumber()
119
120
67 function TestEquality() { 121 function TestEquality() {
68 // Every symbol should equal itself. 122 // Every symbol should equal itself.
69 for (var i in symbols) { 123 for (var i in symbols) {
70 assertSame(symbols[i], symbols[i]) 124 assertSame(symbols[i], symbols[i])
71 assertEquals(symbols[i], symbols[i]) 125 assertEquals(symbols[i], symbols[i])
72 assertTrue(Object.is(symbols[i], symbols[i])) 126 assertTrue(Object.is(symbols[i], symbols[i]))
73 assertTrue(symbols[i] === symbols[i]) 127 assertTrue(symbols[i] === symbols[i])
74 assertTrue(symbols[i] == symbols[i]) 128 assertTrue(symbols[i] == symbols[i])
75 } 129 }
76 130
77 // All symbols should be distinct. 131 // All symbols should be distinct.
78 for (var i = 0; i < symbols.length; ++i) { 132 for (var i = 0; i < symbols.length; ++i) {
79 for (var j = i + 1; j < symbols.length; ++j) { 133 for (var j = i + 1; j < symbols.length; ++j) {
80 assertFalse(Object.is(symbols[i], symbols[j])) 134 assertFalse(Object.is(symbols[i], symbols[j]))
81 assertFalse(symbols[i] === symbols[j]) 135 assertFalse(symbols[i] === symbols[j])
82 assertFalse(symbols[i] == symbols[j]) 136 assertFalse(symbols[i] == symbols[j])
83 } 137 }
84 } 138 }
85 } 139 }
86 TestEquality() 140 TestEquality()
87 141
88 142
89 function TestGet() { 143 function TestGet() {
90 for (var i in symbols) { 144 for (var i in symbols) {
91 assertEquals("[object Symbol]", symbols[i].toString()) 145 assertThrows(function() { symbols[i].toString() }, TypeError)
92 assertEquals(undefined, symbols[i].valueOf) 146 assertEquals(symbols[i], symbols[i].valueOf())
93 assertEquals(undefined, symbols[i].a) 147 assertEquals(undefined, symbols[i].a)
94 assertEquals(undefined, symbols[i]["a" + "b"]) 148 assertEquals(undefined, symbols[i]["a" + "b"])
95 assertEquals(undefined, symbols[i]["" + "1"]) 149 assertEquals(undefined, symbols[i]["" + "1"])
96 assertEquals(undefined, symbols[i][62]) 150 assertEquals(undefined, symbols[i][62])
97 } 151 }
98 } 152 }
99 TestGet() 153 TestGet()
100 154
101 155
102 function TestSet() { 156 function TestSet() {
103 for (var i in symbols) { 157 for (var i in symbols) {
104 symbols[i].toString = 0 158 symbols[i].toString = 0
105 assertEquals("[object Symbol]", symbols[i].toString()) 159 assertThrows(function() { symbols[i].toString() }, TypeError)
160 symbols[i].valueOf = 0
161 assertEquals(symbols[i], symbols[i].valueOf())
106 symbols[i].a = 0 162 symbols[i].a = 0
107 assertEquals(undefined, symbols[i].a) 163 assertEquals(undefined, symbols[i].a)
108 symbols[i]["a" + "b"] = 0 164 symbols[i]["a" + "b"] = 0
109 assertEquals(undefined, symbols[i]["a" + "b"]) 165 assertEquals(undefined, symbols[i]["a" + "b"])
110 symbols[i][62] = 0 166 symbols[i][62] = 0
111 assertEquals(undefined, symbols[i][62]) 167 assertEquals(undefined, symbols[i][62])
112 } 168 }
113 } 169 }
114 TestSet() 170 TestSet()
115 171
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 function TestKeyHas() { 228 function TestKeyHas() {
173 for (var i in symbols) { 229 for (var i in symbols) {
174 assertTrue(symbols[i] in obj) 230 assertTrue(symbols[i] in obj)
175 assertTrue(Object.hasOwnProperty.call(obj, symbols[i])) 231 assertTrue(Object.hasOwnProperty.call(obj, symbols[i]))
176 } 232 }
177 } 233 }
178 234
179 235
180 function TestKeyEnum(obj) { 236 function TestKeyEnum(obj) {
181 for (var name in obj) { 237 for (var name in obj) {
182 assertFalse(%_IsSymbol(name)) 238 assertTrue(typeof name !== "symbol")
183 } 239 }
184 } 240 }
185 241
186 242
187 function TestKeyNames(obj) { 243 function TestKeyNames(obj) {
188 assertEquals(0, Object.keys(obj).length) 244 assertEquals(0, Object.keys(obj).length)
189 245
190 var names = Object.getOwnPropertyNames(obj) 246 var names = Object.getOwnPropertyNames(obj)
191 assertTrue(symbols.length <= names.length) 247 assertTrue(symbols.length <= names.length)
192 // TODO(rossberg): once we have iterators, the following would be: 248 // TODO(rossberg): once we have iterators, the following would be:
193 // var expected = new Set(symbols) 249 // var expected = new Set(symbols)
194 var expected = new Set 250 var expected = new Set
195 for (var i = 0; i < symbols.length; ++i) expected.add(symbols[i]) 251 for (var i = 0; i < symbols.length; ++i) expected.add(symbols[i])
196 for (var i = 0; i < names.length; ++i) { 252 for (var i = 0; i < names.length; ++i) {
197 var name = names[i] 253 var name = names[i]
198 var asString = String(name) 254 if (typeof name === 'symbol') {
199 if (asString !== name) {
200 assertEquals("[object Symbol]", asString)
201 assertTrue(expected.has(name)) 255 assertTrue(expected.has(name))
202 expected.delete(name) 256 expected.delete(name)
203 } 257 }
204 } 258 }
205 assertEquals(0, expected.size) 259 assertEquals(0, expected.size)
206 } 260 }
207 261
208 262
209 function TestKeyDescriptor(obj) { 263 function TestKeyDescriptor(obj) {
210 for (var i in symbols) { 264 for (var i in symbols) {
(...skipping 24 matching lines...) Expand all
235 var obj = objs[i] 289 var obj = objs[i]
236 TestKeySet(obj) 290 TestKeySet(obj)
237 TestKeyDefine(obj) 291 TestKeyDefine(obj)
238 TestKeyGet(obj) 292 TestKeyGet(obj)
239 TestKeyHas(obj) 293 TestKeyHas(obj)
240 TestKeyEnum(obj) 294 TestKeyEnum(obj)
241 TestKeyNames(obj) 295 TestKeyNames(obj)
242 TestKeyDescriptor(obj) 296 TestKeyDescriptor(obj)
243 TestKeyDelete(obj) 297 TestKeyDelete(obj)
244 } 298 }
OLDNEW
« no previous file with comments | « src/x64/lithium-codegen-x64.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698