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 testAdd(mode) {
8  var a = [];
9  Object.defineProperty(a, "length", { writable : false});
10
11  function check(f) {
12    assertThrows(function() { f(a) }, TypeError);
13    assertFalse(0 in a);
14    assertEquals(0, a.length);
15  }
16
17  function push(a) {
18    a.push(3);
19  }
20
21  if (mode == "fast properties") %ToFastProperties(a);
22
23  check(push);
24  check(push);
25  check(push);
26  %OptimizeFunctionOnNextCall(push);
27  check(push);
28
29  function unshift(a) {
30    a.unshift(3);
31  }
32
33  check(unshift);
34  check(unshift);
35  check(unshift);
36  %OptimizeFunctionOnNextCall(unshift);
37  check(unshift);
38
39  function splice(a) {
40    a.splice(0, 0, 3);
41  }
42
43  check(splice);
44  check(splice);
45  check(splice);
46  %OptimizeFunctionOnNextCall(splice);
47  check(splice);
48}
49
50testAdd("fast properties");
51
52testAdd("normalized");
53
54function testRemove(a, mode) {
55  Object.defineProperty(a, "length", { writable : false});
56
57  function check(f) {
58    assertThrows(function() { f(a) }, TypeError);
59    assertEquals(3, a.length);
60  }
61
62  if (mode == "fast properties") %ToFastProperties(a);
63
64  function pop(a) {
65    a.pop();
66  }
67
68  check(pop);
69  check(pop);
70  check(pop);
71  %OptimizeFunctionOnNextCall(pop);
72  check(pop);
73
74  function shift(a) {
75    a.shift();
76  }
77
78  check(shift);
79  check(shift);
80  check(shift);
81  %OptimizeFunctionOnNextCall(shift);
82  check(shift);
83
84  function splice(a) {
85    a.splice(0, 1);
86  }
87
88  check(splice);
89  check(splice);
90  check(splice);
91  %OptimizeFunctionOnNextCall(splice);
92  check(splice);
93
94  %ClearFunctionTypeFeedback(pop);
95  %ClearFunctionTypeFeedback(shift);
96  %ClearFunctionTypeFeedback(splice);
97}
98
99for (var i = 0; i < 3; i++) {
100  var a = [1, 2, 3];
101  if (i == 1) {
102    a = [1, 2, 3.5];
103  } else if (i == 2) {
104    a = [1, 2, "string"];
105  }
106  testRemove(a, "fast properties");
107  testRemove(a, "normalized");
108}
109
110var b = [];
111Object.defineProperty(b.__proto__, "0", {
112  set : function(v) {
113    b.x = v;
114    Object.defineProperty(b, "length", { writable : false });
115  },
116  get: function() {
117    return b.x;
118  }
119});
120
121b = [];
122try {
123  b.push(3, 4, 5);
124} catch(e) { }
125assertFalse(1 in b);
126assertFalse(2 in b);
127assertEquals(0, b.length);
128
129b = [];
130try {
131  b.unshift(3, 4, 5);
132} catch(e) { }
133assertFalse(1 in b);
134assertFalse(2 in b);
135assertEquals(0, b.length);
136
137b = [1, 2];
138try {
139  b.unshift(3, 4, 5);
140} catch(e) { }
141assertEquals(3, b[0]);
142assertEquals(4, b[1]);
143assertEquals(5, b[2]);
144assertEquals(1, b[3]);
145assertEquals(2, b[4]);
146assertEquals(5, b.length);
147
148b = [1, 2];
149
150Object.defineProperty(b.__proto__, "4", {
151  set : function(v) {
152    b.z = v;
153    Object.defineProperty(b, "length", { writable : false });
154  },
155  get: function() {
156    return b.z;
157  }
158});
159
160try {
161  b.unshift(3, 4, 5);
162} catch(e) { }
163
164assertFalse(2 in b);
165assertFalse(3 in b);
166assertEquals(2, b.length);
167