Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | |
|
Michael Starzinger
2013/02/26 20:41:49
The copyright header should say 2013 here.
rossberg
2013/02/27 13:12:23
Done.
| |
| 2 // Redistribution and use in source and binary forms, with or without | |
| 3 // modification, are permitted provided that the following conditions are | |
| 4 // met: | |
| 5 // | |
| 6 // * Redistributions of source code must retain the above copyright | |
| 7 // notice, this list of conditions and the following disclaimer. | |
| 8 // * Redistributions in binary form must reproduce the above | |
| 9 // copyright notice, this list of conditions and the following | |
| 10 // disclaimer in the documentation and/or other materials provided | |
| 11 // with the distribution. | |
| 12 // * Neither the name of Google Inc. nor the names of its | |
| 13 // contributors may be used to endorse or promote products derived | |
| 14 // from this software without specific prior written permission. | |
| 15 // | |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 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. | |
| 27 | |
| 28 // Flags: --harmony-symbols --harmony-collections | |
| 29 // Flags: --expose-gc --allow-natives-syntax | |
| 30 | |
| 31 var symbols = [] | |
| 32 | |
| 33 // Test different forms of constructor calls, all equivalent. | |
| 34 function TestNew() { | |
| 35 function IndirectSymbol() { return new Symbol } | |
| 36 function indirect() { return new IndirectSymbol() } | |
| 37 for (var i = 0; i < 10; ++i) { | |
| 38 symbols.push(new Symbol) | |
| 39 symbols.push(new Symbol()) | |
| 40 symbols.push(Symbol()) | |
| 41 symbols.push(indirect()) | |
| 42 } | |
| 43 %OptimizeFunctionOnNextCall(indirect) | |
| 44 indirect() | |
|
Michael Starzinger
2013/02/26 20:41:49
Could you add a comment like "// Call once before
rossberg
2013/02/27 13:12:23
Done.
| |
| 45 gc(); // Promote, and then allocate some more. | |
| 46 for (var i = 0; i < 10; ++i) { | |
| 47 symbols.push(new Symbol) | |
| 48 symbols.push(new Symbol()) | |
| 49 symbols.push(Symbol()) | |
| 50 symbols.push(indirect()) | |
| 51 } | |
| 52 } | |
| 53 TestNew() | |
| 54 | |
| 55 | |
| 56 function TestType() { | |
| 57 for (var i in symbols) { | |
| 58 assertTrue(%_IsSymbol(symbols[i])) | |
| 59 assertEquals("object", typeof symbols[i]) | |
| 60 assertTrue(typeof symbols[i] === "object") | |
| 61 assertEquals("[object Symbol]", Object.prototype.toString.call(symbols[i])) | |
| 62 } | |
| 63 } | |
| 64 TestType() | |
| 65 | |
| 66 | |
| 67 function TestEquality() { | |
| 68 // Every symbol should equal itself. | |
| 69 for (var i in symbols) { | |
| 70 assertSame(symbols[i], symbols[i]) | |
| 71 assertEquals(symbols[i], symbols[i]) | |
| 72 assertTrue(Object.is(symbols[i], symbols[i])) | |
| 73 assertTrue(symbols[i] === symbols[i]) | |
| 74 assertTrue(symbols[i] == symbols[i]) | |
| 75 } | |
| 76 | |
| 77 // All symbols should be distinct. | |
| 78 for (var i = 0; i < symbols.length; ++i) { | |
| 79 for (var j = i + 1; j < symbols.length; ++j) { | |
| 80 assertFalse(Object.is(symbols[i], symbols[j])) | |
| 81 assertFalse(symbols[i] === symbols[j]) | |
| 82 assertFalse(symbols[i] == symbols[j]) | |
| 83 } | |
| 84 } | |
| 85 } | |
| 86 TestEquality() | |
| 87 | |
| 88 | |
| 89 function TestGet() { | |
| 90 for (var i in symbols) { | |
| 91 assertEquals("[object Symbol]", symbols[i].toString()) | |
| 92 assertEquals(undefined, symbols[i].valueOf) | |
| 93 assertEquals(undefined, symbols[i].a) | |
| 94 assertEquals(undefined, symbols[i]["a" + "b"]) | |
| 95 assertEquals(undefined, symbols[i]["" + "1"]) | |
| 96 assertEquals(undefined, symbols[i][62]) | |
| 97 } | |
| 98 } | |
| 99 TestGet() | |
| 100 | |
| 101 | |
| 102 function TestSet() { | |
| 103 for (var i in symbols) { | |
| 104 symbols[i].toString = 0 | |
| 105 assertEquals("[object Symbol]", symbols[i].toString()) | |
| 106 symbols[i].a = 0 | |
| 107 assertEquals(undefined, symbols[i].a) | |
| 108 symbols[i]["a" + "b"] = 0 | |
| 109 assertEquals(undefined, symbols[i]["a" + "b"]) | |
| 110 symbols[i][62] = 0 | |
| 111 assertEquals(undefined, symbols[i][62]) | |
| 112 } | |
| 113 } | |
| 114 TestSet() | |
| 115 | |
| 116 | |
| 117 function TestMap() { | |
| 118 var map = new Map; | |
| 119 for (var i in symbols) { | |
| 120 map.set(symbols[i], i) | |
| 121 } | |
| 122 for (var i in symbols) { | |
|
Michael Starzinger
2013/02/26 20:41:49
Could you also add a assertTrue(map.has(symbols[i]
rossberg
2013/02/27 13:12:23
Done.
| |
| 123 assertEquals(i, map.get(symbols[i])) | |
| 124 } | |
| 125 } | |
| 126 TestMap() | |
| OLD | NEW |