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 7 var v = 0; 8 var my_array_proto = {}; 9 my_array_proto.__proto__ = [].__proto__; 10 Object.defineProperty(my_array_proto, "0", { 11 get: function() { return "get " + v; }, 12 set: function(value) { v += value; } 13 }); 14 15 16 // Test that element accessors are called in standard push cases. 17 array = []; 18 array.__proto__ = my_array_proto; 19 20 array[0] = 10; 21 assertEquals(0, array.length); 22 assertEquals(10, v); 23 assertEquals("get 10", array[0]); 24 25 Array.prototype.push.call(array, 100); 26 assertEquals(1, array.length); 27 assertEquals(110, v); 28 assertEquals("get 110", array[0]); 29 30 array = []; 31 array.__proto__ = my_array_proto; 32 33 assertEquals(0, array.length); 34 array.push(110); 35 assertEquals(1, array.length); 36 assertEquals(220, v); 37 assertEquals("get 220", array[0]); 38 39 // Test that elements setters/getters on prototype chain are property detected 40 // and don't lead to overzealous optimization. 41 v = 0; 42 function push_wrapper_1(array, value) { 43 array.push(value); 44 } 45 array = []; 46 array.__proto__ = my_array_proto; 47 push_wrapper_1(array, 100); 48 assertEquals(1, array.length); 49 assertEquals(100, v); 50 push_wrapper_1(array, 100); 51 assertEquals(2, array.length); 52 assertEquals(100, v); 53 assertEquals("get 100", array[0]); 54 %OptimizeFunctionOnNextCall(push_wrapper_1); 55 array = []; 56 array.__proto__ = my_array_proto; 57 push_wrapper_1(array, 100); 58 assertEquals(1, array.length); 59 assertEquals(200, v); 60 assertEquals("get 200", array[0]); 61