1// Copyright 2014 the V8 project authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5// Flags: --allow-natives-syntax 6 7function test(mode) { 8 var a = []; 9 Object.defineProperty(a, "length", { writable : false}); 10 11 function check(f) { 12 try { 13 f(a); 14 } catch(e) { } 15 assertFalse(0 in a); 16 assertEquals(0, a.length); 17 } 18 19 function push(a) { 20 a.push(3); 21 } 22 23 if (mode == "fast properties") %ToFastProperties(a); 24 25 check(push); 26 check(push); 27 check(push); 28 %OptimizeFunctionOnNextCall(push); 29 check(push); 30 31 function unshift(a) { 32 a.unshift(3); 33 } 34 35 check(unshift); 36 check(unshift); 37 check(unshift); 38 %OptimizeFunctionOnNextCall(unshift); 39 check(unshift); 40} 41 42test("fast properties"); 43 44test("normalized"); 45 46var b = []; 47Object.defineProperty(b.__proto__, "0", { 48 set : function(v) { 49 b.x = v; 50 Object.defineProperty(b, "length", { writable : false }); 51 }, 52 get: function() { 53 return b.x; 54 } 55}); 56 57b = []; 58try { 59 b.push(3, 4, 5); 60} catch(e) { } 61assertFalse(1 in b); 62assertFalse(2 in b); 63assertEquals(0, b.length); 64 65b = []; 66try { 67 b.unshift(3, 4, 5); 68} catch(e) { } 69assertFalse(1 in b); 70assertFalse(2 in b); 71assertEquals(0, b.length); 72 73b = [1, 2]; 74try { 75 b.unshift(3, 4, 5); 76} catch(e) { } 77assertEquals(3, b[0]); 78assertEquals(4, b[1]); 79assertEquals(5, b[2]); 80assertEquals(1, b[3]); 81assertEquals(2, b[4]); 82assertEquals(5, b.length); 83 84b = [1, 2]; 85 86Object.defineProperty(b.__proto__, "4", { 87 set : function(v) { 88 b.z = v; 89 Object.defineProperty(b, "length", { writable : false }); 90 }, 91 get: function() { 92 return b.z; 93 } 94}); 95 96try { 97 b.unshift(3, 4, 5); 98} catch(e) { } 99 100// TODO(ulan): According to the ECMA-262 unshift should throw an exception 101// when moving b[0] to b[3] (see 15.4.4.13 step 6.d.ii). This is difficult 102// to do with our current implementation of SmartMove() in src/array.js and 103// it will regress performance. Uncomment the following line once acceptable 104// solution is found: 105// assertFalse(2 in b); 106// assertFalse(3 in b); 107// assertEquals(2, b.length); 108