Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // Copyright 2008 the V8 project authors. All rights reserved. |
| 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 | function props(x) { |
| 29 | var array = []; |
| 30 | for (var p in x) array.push(p); |
| 31 | return array.sort(); |
| 32 | } |
| 33 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 34 | assertEquals(0, props({}).length, "olen0"); |
| 35 | assertEquals(1, props({x:1}).length, "olen1"); |
| 36 | assertEquals(2, props({x:1, y:2}).length, "olen2"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 37 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 38 | assertArrayEquals(["x"], props({x:1}), "x"); |
| 39 | assertArrayEquals(["x", "y"], props({x:1, y:2}), "xy"); |
| 40 | assertArrayEquals(["x", "y", "zoom"], props({x:1, y:2, zoom:3}), "xyzoom"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 41 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 42 | assertEquals(0, props([]).length, "alen0"); |
| 43 | assertEquals(1, props([1]).length, "alen1"); |
| 44 | assertEquals(2, props([1,2]).length, "alen2"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 45 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 46 | assertArrayEquals(["0"], props([1]), "0"); |
| 47 | assertArrayEquals(["0", "1"], props([1,2]), "01"); |
| 48 | assertArrayEquals(["0", "1", "2"], props([1,2,3]), "012"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 49 | |
| 50 | var o = {}; |
| 51 | var a = []; |
| 52 | for (var i = 0x0020; i < 0x01ff; i+=2) { |
| 53 | var s = 'char:' + String.fromCharCode(i); |
| 54 | a.push(s); |
| 55 | o[s] = i; |
| 56 | } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 57 | assertArrayEquals(a, props(o), "charcodes"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 58 | |
| 59 | var a = []; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 60 | assertEquals(0, props(a).length, "proplen0"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 61 | a[Math.pow(2,30)-1] = 0; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 62 | assertEquals(1, props(a).length, "proplen1"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 63 | a[Math.pow(2,31)-1] = 0; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 64 | assertEquals(2, props(a).length, "proplen2"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 65 | a[1] = 0; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 66 | assertEquals(3, props(a).length, "proplen3"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 67 | |
| 68 | for (var hest = 'hest' in {}) { } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 69 | assertEquals('hest', hest, "empty-no-override"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 70 | |
| 71 | var result = ''; |
| 72 | for (var p in {a : [0], b : 1}) { result += p; } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 73 | assertEquals('ab', result, "ab"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 74 | |
| 75 | var result = ''; |
| 76 | for (var p in {a : {v:1}, b : 1}) { result += p; } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 77 | assertEquals('ab', result, "ab-nodeep"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 78 | |
| 79 | var result = ''; |
| 80 | for (var p in { get a() {}, b : 1}) { result += p; } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 81 | assertEquals('ab', result, "abget"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 82 | |
| 83 | var result = ''; |
| 84 | for (var p in { get a() {}, set a(x) {}, b : 1}) { result += p; } |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 85 | assertEquals('ab', result, "abgetset"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 86 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 87 | |
| 88 | // Test that for-in in the global scope works with a keyed property as "each". |
| 89 | // Test outside a loop and in a loop for multiple iterations. |
| 90 | a = [1,2,3,4]; |
| 91 | x = {foo:5, bar:6, zip:7, glep:9, 10:11}; |
| 92 | delete x.bar; |
| 93 | y = {} |
| 94 | |
| 95 | for (a[2] in x) { |
| 96 | y[a[2]] = x[a[2]]; |
| 97 | } |
| 98 | |
| 99 | assertEquals(5, y.foo, "y.foo"); |
| 100 | assertEquals("undefined", typeof y.bar, "y.bar"); |
| 101 | assertEquals(7, y.zip, "y.zip"); |
| 102 | assertEquals(9, y.glep, "y.glep"); |
| 103 | assertEquals(11, y[10], "y[10]"); |
| 104 | assertEquals("undefined", typeof y[2], "y[2]"); |
| 105 | assertEquals("undefined", typeof y[0], "y[0]"); |
| 106 | |
| 107 | for (i=0 ; i < 3; ++i) { |
| 108 | y = {} |
| 109 | |
| 110 | for (a[2] in x) { |
| 111 | y[a[2]] = x[a[2]]; |
| 112 | } |
| 113 | |
| 114 | assertEquals(5, y.foo, "y.foo"); |
| 115 | assertEquals("undefined", typeof y.bar, "y.bar"); |
| 116 | assertEquals(7, y.zip, "y.zip"); |
| 117 | assertEquals(9, y.glep, "y.glep"); |
| 118 | assertEquals(11, y[10], "y[10]"); |
| 119 | assertEquals("undefined", typeof y[2], "y[2]"); |
| 120 | assertEquals("undefined", typeof y[0], "y[0]"); |
| 121 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 122 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 123 | (function testLargeElementKeys() { |
| 124 | // Key out of SMI range but well within safe double representaion. |
| 125 | var large_key = 2147483650; |
| 126 | var o = []; |
| 127 | // Trigger dictionary elements with HeapNumber keys. |
| 128 | o[large_key] = 0; |
| 129 | o[large_key+1] = 1; |
| 130 | o[large_key+2] = 2; |
| 131 | o[large_key+3] = 3; |
| 132 | var keys = []; |
| 133 | for (var k in o) { |
| 134 | keys.push(k); |
| 135 | } |
| 136 | assertEquals(["2147483650", "2147483651", "2147483652", "2147483653"], keys); |
| 137 | })(); |
| 138 | |
| 139 | (function testLargeElementKeysWithProto() { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 140 | var large_key = 2147483650; |
| 141 | var o = {__proto__: {}}; |
| 142 | o[large_key] = 1; |
| 143 | o.__proto__[large_key] = 1; |
| 144 | var keys = []; |
| 145 | for (var k in o) { |
| 146 | keys.push(k); |
| 147 | } |
| 148 | assertEquals(["2147483650"], keys); |
| 149 | })(); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 150 | |
| 151 | (function testNonEnumerableArgumentsIndex() { |
| 152 | Object.defineProperty(arguments, 0, {enumerable:false}); |
| 153 | for (var k in arguments) { |
| 154 | assertUnreachable(); |
| 155 | } |
| 156 | })(); |
| 157 | |
| 158 | (function testNonEnumerableSloppyArgumentsIndex(a) { |
| 159 | Object.defineProperty(arguments, 0, {enumerable:false}); |
| 160 | for (var k in arguments) { |
| 161 | assertUnreachable(); |
| 162 | } |
| 163 | })(true); |