1a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Copyright 2008 the V8 project authors. All rights reserved. 2a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Redistribution and use in source and binary forms, with or without 3a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// modification, are permitted provided that the following conditions are 4a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// met: 5a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// 6a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// * Redistributions of source code must retain the above copyright 7a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// notice, this list of conditions and the following disclaimer. 8a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// * Redistributions in binary form must reproduce the above 9a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// copyright notice, this list of conditions and the following 10a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// disclaimer in the documentation and/or other materials provided 11a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// with the distribution. 12a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// * Neither the name of Google Inc. nor the names of its 13a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// contributors may be used to endorse or promote products derived 14a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// from this software without specific prior written permission. 15a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// 16a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 28537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben Murdoch// Flags: --noharmony-for-in 29537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben Murdoch 30a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockfunction props(x) { 31a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block var array = []; 32a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block for (var p in x) array.push(p); 33a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block return array.sort(); 34a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block} 35a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 363ce2e2076e8e3e60cf1810eec160ea2d8557e9e7Steve BlockassertEquals(0, props({}).length, "olen0"); 373ce2e2076e8e3e60cf1810eec160ea2d8557e9e7Steve BlockassertEquals(1, props({x:1}).length, "olen1"); 383ce2e2076e8e3e60cf1810eec160ea2d8557e9e7Steve BlockassertEquals(2, props({x:1, y:2}).length, "olen2"); 39a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 403ce2e2076e8e3e60cf1810eec160ea2d8557e9e7Steve BlockassertArrayEquals(["x"], props({x:1}), "x"); 413ce2e2076e8e3e60cf1810eec160ea2d8557e9e7Steve BlockassertArrayEquals(["x", "y"], props({x:1, y:2}), "xy"); 423ce2e2076e8e3e60cf1810eec160ea2d8557e9e7Steve BlockassertArrayEquals(["x", "y", "zoom"], props({x:1, y:2, zoom:3}), "xyzoom"); 43a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 443ce2e2076e8e3e60cf1810eec160ea2d8557e9e7Steve BlockassertEquals(0, props([]).length, "alen0"); 453ce2e2076e8e3e60cf1810eec160ea2d8557e9e7Steve BlockassertEquals(1, props([1]).length, "alen1"); 463ce2e2076e8e3e60cf1810eec160ea2d8557e9e7Steve BlockassertEquals(2, props([1,2]).length, "alen2"); 47a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 483ce2e2076e8e3e60cf1810eec160ea2d8557e9e7Steve BlockassertArrayEquals(["0"], props([1]), "0"); 493ce2e2076e8e3e60cf1810eec160ea2d8557e9e7Steve BlockassertArrayEquals(["0", "1"], props([1,2]), "01"); 503ce2e2076e8e3e60cf1810eec160ea2d8557e9e7Steve BlockassertArrayEquals(["0", "1", "2"], props([1,2,3]), "012"); 51a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 52a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvar o = {}; 53a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvar a = []; 54a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockfor (var i = 0x0020; i < 0x01ff; i+=2) { 55a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block var s = 'char:' + String.fromCharCode(i); 56a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block a.push(s); 57a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block o[s] = i; 58a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block} 593ce2e2076e8e3e60cf1810eec160ea2d8557e9e7Steve BlockassertArrayEquals(a, props(o), "charcodes"); 60a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 61a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvar a = []; 623ce2e2076e8e3e60cf1810eec160ea2d8557e9e7Steve BlockassertEquals(0, props(a).length, "proplen0"); 63a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocka[Math.pow(2,30)-1] = 0; 643ce2e2076e8e3e60cf1810eec160ea2d8557e9e7Steve BlockassertEquals(1, props(a).length, "proplen1"); 65a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocka[Math.pow(2,31)-1] = 0; 663ce2e2076e8e3e60cf1810eec160ea2d8557e9e7Steve BlockassertEquals(2, props(a).length, "proplen2"); 67a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocka[1] = 0; 683ce2e2076e8e3e60cf1810eec160ea2d8557e9e7Steve BlockassertEquals(3, props(a).length, "proplen3"); 69a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 70a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockfor (var hest = 'hest' in {}) { } 713ce2e2076e8e3e60cf1810eec160ea2d8557e9e7Steve BlockassertEquals('hest', hest, "empty-no-override"); 72a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 73a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvar result = ''; 74a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockfor (var p in {a : [0], b : 1}) { result += p; } 753ce2e2076e8e3e60cf1810eec160ea2d8557e9e7Steve BlockassertEquals('ab', result, "ab"); 76a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 77a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvar result = ''; 78a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockfor (var p in {a : {v:1}, b : 1}) { result += p; } 793ce2e2076e8e3e60cf1810eec160ea2d8557e9e7Steve BlockassertEquals('ab', result, "ab-nodeep"); 80a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 81a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvar result = ''; 82a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockfor (var p in { get a() {}, b : 1}) { result += p; } 833ce2e2076e8e3e60cf1810eec160ea2d8557e9e7Steve BlockassertEquals('ab', result, "abget"); 84a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 85a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockvar result = ''; 86a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockfor (var p in { get a() {}, set a(x) {}, b : 1}) { result += p; } 873ce2e2076e8e3e60cf1810eec160ea2d8557e9e7Steve BlockassertEquals('ab', result, "abgetset"); 88a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block 898defd9ff6930b4e24729971a61cf7469daf119beSteve Block 908defd9ff6930b4e24729971a61cf7469daf119beSteve Block// Test that for-in in the global scope works with a keyed property as "each". 918defd9ff6930b4e24729971a61cf7469daf119beSteve Block// Test outside a loop and in a loop for multiple iterations. 928defd9ff6930b4e24729971a61cf7469daf119beSteve Blocka = [1,2,3,4]; 938defd9ff6930b4e24729971a61cf7469daf119beSteve Blockx = {foo:5, bar:6, zip:7, glep:9, 10:11}; 948defd9ff6930b4e24729971a61cf7469daf119beSteve Blockdelete x.bar; 958defd9ff6930b4e24729971a61cf7469daf119beSteve Blocky = {} 968defd9ff6930b4e24729971a61cf7469daf119beSteve Block 978defd9ff6930b4e24729971a61cf7469daf119beSteve Blockfor (a[2] in x) { 988defd9ff6930b4e24729971a61cf7469daf119beSteve Block y[a[2]] = x[a[2]]; 998defd9ff6930b4e24729971a61cf7469daf119beSteve Block} 1008defd9ff6930b4e24729971a61cf7469daf119beSteve Block 1018defd9ff6930b4e24729971a61cf7469daf119beSteve BlockassertEquals(5, y.foo, "y.foo"); 1028defd9ff6930b4e24729971a61cf7469daf119beSteve BlockassertEquals("undefined", typeof y.bar, "y.bar"); 1038defd9ff6930b4e24729971a61cf7469daf119beSteve BlockassertEquals(7, y.zip, "y.zip"); 1048defd9ff6930b4e24729971a61cf7469daf119beSteve BlockassertEquals(9, y.glep, "y.glep"); 1058defd9ff6930b4e24729971a61cf7469daf119beSteve BlockassertEquals(11, y[10], "y[10]"); 1068defd9ff6930b4e24729971a61cf7469daf119beSteve BlockassertEquals("undefined", typeof y[2], "y[2]"); 1078defd9ff6930b4e24729971a61cf7469daf119beSteve BlockassertEquals("undefined", typeof y[0], "y[0]"); 1088defd9ff6930b4e24729971a61cf7469daf119beSteve Block 1098defd9ff6930b4e24729971a61cf7469daf119beSteve Blockfor (i=0 ; i < 3; ++i) { 1108defd9ff6930b4e24729971a61cf7469daf119beSteve Block y = {} 1118defd9ff6930b4e24729971a61cf7469daf119beSteve Block 1128defd9ff6930b4e24729971a61cf7469daf119beSteve Block for (a[2] in x) { 1138defd9ff6930b4e24729971a61cf7469daf119beSteve Block y[a[2]] = x[a[2]]; 1148defd9ff6930b4e24729971a61cf7469daf119beSteve Block } 1158defd9ff6930b4e24729971a61cf7469daf119beSteve Block 1168defd9ff6930b4e24729971a61cf7469daf119beSteve Block assertEquals(5, y.foo, "y.foo"); 1178defd9ff6930b4e24729971a61cf7469daf119beSteve Block assertEquals("undefined", typeof y.bar, "y.bar"); 1188defd9ff6930b4e24729971a61cf7469daf119beSteve Block assertEquals(7, y.zip, "y.zip"); 1198defd9ff6930b4e24729971a61cf7469daf119beSteve Block assertEquals(9, y.glep, "y.glep"); 1208defd9ff6930b4e24729971a61cf7469daf119beSteve Block assertEquals(11, y[10], "y[10]"); 1218defd9ff6930b4e24729971a61cf7469daf119beSteve Block assertEquals("undefined", typeof y[2], "y[2]"); 1228defd9ff6930b4e24729971a61cf7469daf119beSteve Block assertEquals("undefined", typeof y[0], "y[0]"); 1238defd9ff6930b4e24729971a61cf7469daf119beSteve Block} 124014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch 1251b268ca467c924004286c97bac133db489cf43d0Ben Murdoch(function testLargeElementKeys() { 1261b268ca467c924004286c97bac133db489cf43d0Ben Murdoch // Key out of SMI range but well within safe double representaion. 1271b268ca467c924004286c97bac133db489cf43d0Ben Murdoch var large_key = 2147483650; 1281b268ca467c924004286c97bac133db489cf43d0Ben Murdoch var o = []; 1291b268ca467c924004286c97bac133db489cf43d0Ben Murdoch // Trigger dictionary elements with HeapNumber keys. 1301b268ca467c924004286c97bac133db489cf43d0Ben Murdoch o[large_key] = 0; 1311b268ca467c924004286c97bac133db489cf43d0Ben Murdoch o[large_key+1] = 1; 1321b268ca467c924004286c97bac133db489cf43d0Ben Murdoch o[large_key+2] = 2; 1331b268ca467c924004286c97bac133db489cf43d0Ben Murdoch o[large_key+3] = 3; 1341b268ca467c924004286c97bac133db489cf43d0Ben Murdoch var keys = []; 1351b268ca467c924004286c97bac133db489cf43d0Ben Murdoch for (var k in o) { 1361b268ca467c924004286c97bac133db489cf43d0Ben Murdoch keys.push(k); 1371b268ca467c924004286c97bac133db489cf43d0Ben Murdoch } 1381b268ca467c924004286c97bac133db489cf43d0Ben Murdoch assertEquals(["2147483650", "2147483651", "2147483652", "2147483653"], keys); 1391b268ca467c924004286c97bac133db489cf43d0Ben Murdoch})(); 1401b268ca467c924004286c97bac133db489cf43d0Ben Murdoch 1411b268ca467c924004286c97bac133db489cf43d0Ben Murdoch(function testLargeElementKeysWithProto() { 142014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch var large_key = 2147483650; 143014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch var o = {__proto__: {}}; 144014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch o[large_key] = 1; 145014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch o.__proto__[large_key] = 1; 146014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch var keys = []; 147014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch for (var k in o) { 148014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch keys.push(k); 149014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch } 150014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch assertEquals(["2147483650"], keys); 151014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch})(); 1521b268ca467c924004286c97bac133db489cf43d0Ben Murdoch 1531b268ca467c924004286c97bac133db489cf43d0Ben Murdoch(function testNonEnumerableArgumentsIndex() { 1541b268ca467c924004286c97bac133db489cf43d0Ben Murdoch Object.defineProperty(arguments, 0, {enumerable:false}); 1551b268ca467c924004286c97bac133db489cf43d0Ben Murdoch for (var k in arguments) { 1561b268ca467c924004286c97bac133db489cf43d0Ben Murdoch assertUnreachable(); 1571b268ca467c924004286c97bac133db489cf43d0Ben Murdoch } 1581b268ca467c924004286c97bac133db489cf43d0Ben Murdoch})(); 1591b268ca467c924004286c97bac133db489cf43d0Ben Murdoch 1601b268ca467c924004286c97bac133db489cf43d0Ben Murdoch(function testNonEnumerableSloppyArgumentsIndex(a) { 1611b268ca467c924004286c97bac133db489cf43d0Ben Murdoch Object.defineProperty(arguments, 0, {enumerable:false}); 1621b268ca467c924004286c97bac133db489cf43d0Ben Murdoch for (var k in arguments) { 1631b268ca467c924004286c97bac133db489cf43d0Ben Murdoch assertUnreachable(); 1641b268ca467c924004286c97bac133db489cf43d0Ben Murdoch } 1651b268ca467c924004286c97bac133db489cf43d0Ben Murdoch})(true); 166