 Chromium Code Reviews
 Chromium Code Reviews Issue 118553003:
  Upgrade Symbol implementation to match current ES6 behavior.  (Closed) 
  Base URL: git://github.com/v8/v8.git@bleeding_edge
    
  
    Issue 118553003:
  Upgrade Symbol implementation to match current ES6 behavior.  (Closed) 
  Base URL: git://github.com/v8/v8.git@bleeding_edge| OLD | NEW | 
|---|---|
| 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 Loading... | |
| 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() } | 
| 
arv (Not doing code reviews)
2014/02/01 01:33:10
Should be lower  case now since it is not a constr
 | |
| 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) | 
| 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 assertThrows(function() { Symbol.prototype.valueOf.call(symbols[i]) }, TypeE rror) | 
| 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 assertThrows(function() { Symbol.prototype.toString.call(symbols[i]) }, Type Error) | 
| 118 assertThrows(function() { Object(symbols[i]).toString() }, TypeError) | 118 | 
| 119 function checkToString(f) { | |
| 120 var stringValue | |
| 121 assertDoesNotThrow(function() { stringValue = f() }) | |
| 122 assertTrue(isValidSymbolString(stringValue)) | |
| 123 } | |
| 124 checkToString(function() { return String(Object(symbols[i])) }) | |
| 125 checkToString(function() { return symbols[i].toString() }) | |
| 126 checkToString(function() { return Object(symbols[i]).toString() }) | |
| 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 Loading... | |
| 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()) | 
| 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 assertEquals(Object(symbols[i]), Object(symbols[i])) | |
| 
arv (Not doing code reviews)
2014/02/01 01:33:10
This looks strange. If symbol[i] is a symbol (not
 
sof
2014/02/01 13:58:10
They're all Symbol values. This is an equals check
 
arv (Not doing code reviews)
2014/02/02 19:49:35
If all elements in symbols are symbol values (not
 
sof
2014/02/02 19:59:57
I know; the object also doesn't have any "keys" in
 | |
| 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() }) | 
| 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() }) | 
| 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("") }) | |
| 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 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 293 | 316 | 
| 294 var names = Object.getOwnPropertyNames(obj) | 317 var names = Object.getOwnPropertyNames(obj) | 
| 295 for (var i in names) { | 318 for (var i in names) { | 
| 296 assertEquals("string", typeof names[i]) | 319 assertEquals("string", typeof names[i]) | 
| 297 } | 320 } | 
| 298 } | 321 } | 
| 299 | 322 | 
| 300 | 323 | 
| 301 function TestKeyDescriptor(obj) { | 324 function TestKeyDescriptor(obj) { | 
| 302 for (var i in symbols) { | 325 for (var i in symbols) { | 
| 303 var desc = Object.getOwnPropertyDescriptor(obj, symbols[i]); | 326 var desc = Object.getOwnPropertyDescriptor(obj, symbols[i]) | 
| 304 assertEquals(i|0, desc.value) | 327 assertEquals(i|0, desc.value) | 
| 305 assertTrue(desc.configurable) | 328 assertTrue(desc.configurable) | 
| 306 assertEquals(i % 2 == 0, desc.writable) | 329 assertEquals(i % 2 == 0, desc.writable) | 
| 307 assertEquals(i % 2 == 0, desc.enumerable) | 330 assertEquals(i % 2 == 0, desc.enumerable) | 
| 308 assertEquals(i % 2 == 0, | 331 assertEquals(i % 2 == 0, | 
| 309 Object.prototype.propertyIsEnumerable.call(obj, symbols[i])) | 332 Object.prototype.propertyIsEnumerable.call(obj, symbols[i])) | 
| 310 } | 333 } | 
| 311 } | 334 } | 
| 312 | 335 | 
| 313 | 336 | 
| (...skipping 29 matching lines...) Expand all Loading... | |
| 343 // but not between scavenges. This must also apply for symbol keys. | 366 // but not between scavenges. This must also apply for symbol keys. | 
| 344 var key = Symbol("key"); | 367 var key = Symbol("key"); | 
| 345 var a = {}; | 368 var a = {}; | 
| 346 a[key] = "abc"; | 369 a[key] = "abc"; | 
| 347 | 370 | 
| 348 for (var i = 0; i < 100000; i++) { | 371 for (var i = 0; i < 100000; i++) { | 
| 349 a[key] += "a"; // Allocations cause a scavenge. | 372 a[key] += "a"; // Allocations cause a scavenge. | 
| 350 } | 373 } | 
| 351 } | 374 } | 
| 352 TestCachedKeyAfterScavenge(); | 375 TestCachedKeyAfterScavenge(); | 
| OLD | NEW |