summaryrefslogtreecommitdiff
path: root/deps/v8/test/mjsunit/strong/object-delete.js
blob: a655b65c78fa2bfedd08d767bb43760765ce7167 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Flags: --strong-mode --allow-natives-syntax

// TODO(conradw): Track implementation of strong bit for other objects, add
// tests.

function getSloppyObjects() {
  return [(function(){}), ({})];
}

function getStrictObjects() {
  "use strict";
  return [(function(){}), ({})];
}

function getWeakObjects() {
  return getSloppyObjects().concat(getStrictObjects());
}

function getStrongObjects() {
  "use strong";
// Strong functions can't have properties added to them, and will be tested as a
// special case.
  return [({})];
}

function strongFunction() {
  "use strong";
}

function deleteFromObjectSloppy(o) {
  return delete o.foo;
}

function deleteFromObjectKeyedSloppy(o) {
  return delete o["foo"];
}

function deleteFromObjectKeyedVarSloppy(o) {
  var a = "foo";
  return delete o[a];
}

function deleteFromObjectKeyedComputedSloppy(o) {
  var a = "o";
  return delete o["fo" + a];
}

function deleteFromObjectWith(o) {
  with (o) {
    return delete foo;
  }
}

function deleteFromObjectElementSloppy(o) {
  return delete o[0];
}

function deleteFromObjectElementVarSloppy(o) {
  var a = 0;
  return delete o[a];
}

function deleteFromObjectElementSparseSloppy(o) {
  return delete o[100000];
}

function deleteFromObjectElementVarSloppy(o) {
  var a = 0;
  return delete o[a];
}

function deleteFromObjectElementSparseVarSloppy(o) {
  var a = 100000;
  return delete o[a];
}

function deleteFromObjectStrict(o) {
  "use strict";
  return delete o.foo;
}

function deleteFromObjectKeyedStrict(o) {
  "use strict";
  return delete o["foo"];
}

function deleteFromObjectKeyedVarStrict(o) {
  "use strict";
  var a = "foo";
  return delete o[a];
}

function deleteFromObjectKeyedComputedStrict(o) {
  "use strict";
  var a = "o";
  return delete o["fo" + a];
}

function deleteFromObjectElementStrict(o) {
  "use strict";
  return delete o[0];
}

function deleteFromObjectElementSparseStrict(o) {
  "use strict";
  return delete o[100000];
}

function deleteFromObjectElementVarStrict(o) {
  "use strict";
  var a = 0;
  return delete o[a];
}

function deleteFromObjectElementSparseVarStrict(o) {
  "use strict";
  var a = 100000;
  return delete o[a];
}

function testStrongObjectDelete() {
  "use strict";

  let deletePropertyFuncsSloppy = [
    deleteFromObjectSloppy,
    deleteFromObjectKeyedSloppy,
    deleteFromObjectKeyedVarSloppy,
    deleteFromObjectKeyedComputedSloppy,
    deleteFromObjectWith
  ];
  let deletePropertyFuncsStrict = [
    deleteFromObjectStrict,
    deleteFromObjectKeyedStrict,
    deleteFromObjectKeyedVarStrict,
    deleteFromObjectKeyedComputedStrict
  ];
  let deleteElementFuncsSloppy = [
    deleteFromObjectElementSloppy,
    deleteFromObjectElementVarSloppy
  ];
  let deleteElementSparseFuncsSloppy = [
    deleteFromObjectElementSparseSloppy,
    deleteFromObjectElementSparseVarSloppy
  ];
  let deleteElementFuncsStrict = [
    deleteFromObjectElementStrict,
    deleteFromObjectElementVarStrict
  ];
  let deleteElementSparseFuncsStrict = [
    deleteFromObjectElementSparseStrict,
    deleteFromObjectElementSparseVarStrict
  ];
  let deleteFuncs = deletePropertyFuncsSloppy.concat(
    deletePropertyFuncsStrict, deleteElementFuncsSloppy,
    deleteElementSparseFuncsSloppy, deleteElementFuncsStrict,
    deleteElementSparseFuncsStrict);

  for (let deleteFunc of deleteFuncs) {
    assertTrue(deleteFunc(strongFunction));
  }

  let testCasesSloppy = [
    [deletePropertyFuncsSloppy, "foo"],
    [deleteElementFuncsSloppy, "0"],
    [deleteElementSparseFuncsSloppy, "100000"]
  ];

  let testCasesStrict = [
    [deletePropertyFuncsStrict, "foo"],
    [deleteElementFuncsStrict, "0"],
    [deleteElementSparseFuncsStrict, "100000"]
  ];

  let propDescs = [
     {configurable: true, value: "bar"},
     {configurable: true, value: 1},
     {configurable: true, enumerable: true, writable: true, value: "bar"},
     {configurable: true, enumerable: true, writable: true, value: 1},
     {configurable: true, get: (function(){return 0}), set: (function(x){})}
  ];

  for (let propDesc of propDescs) {
    for (let testCase of testCasesSloppy) {
      let name = testCase[1];
      for (let deleteFunc of testCase[0]) {
        for (let o of getWeakObjects()) {
          Object.defineProperty(o, name, propDesc);
          assertTrue(delete o["bar"]);
          assertTrue(delete o[5000]);
          assertTrue(deleteFunc(o));
          assertFalse(o.hasOwnProperty(name));
          %OptimizeFunctionOnNextCall(deleteFunc);
          Object.defineProperty(o, name, propDesc);
          assertTrue(deleteFunc(o));
          assertFalse(o.hasOwnProperty(name));
          %DeoptimizeFunction(deleteFunc);
          Object.defineProperty(o, name, propDesc);
          assertTrue(deleteFunc(o));
          assertFalse(o.hasOwnProperty(name));
        }
        for (let o of getStrongObjects()) {
          Object.defineProperty(o, name, propDesc);
          assertTrue(delete o["bar"]);
          assertTrue(delete o[5000]);
          assertFalse(deleteFunc(o));
          assertTrue(o.hasOwnProperty(name));
          %OptimizeFunctionOnNextCall(deleteFunc);
          assertFalse(deleteFunc(o));
          assertTrue(o.hasOwnProperty(name));
          %DeoptimizeFunction(deleteFunc);
          assertFalse(deleteFunc(o));
          assertTrue(o.hasOwnProperty(name));
        }
      }
    }
    for (let testCase of testCasesStrict) {
      let name = testCase[1];
      for (let deleteFunc of testCase[0]) {
        for (let o of getWeakObjects()) {
          Object.defineProperty(o, name, propDesc);
          assertTrue(delete o["bar"]);
          assertTrue(delete o[5000]);
          assertTrue(deleteFunc(o));
          assertFalse(o.hasOwnProperty(name));
          %OptimizeFunctionOnNextCall(deleteFunc);
          Object.defineProperty(o, name, propDesc);
          assertTrue(deleteFunc(o));
          assertFalse(o.hasOwnProperty(name));
          %DeoptimizeFunction(deleteFunc);
          Object.defineProperty(o, name, propDesc);
          assertTrue(deleteFunc(o));
          assertFalse(o.hasOwnProperty(name));
        }
        for (let o of getStrongObjects()) {
          Object.defineProperty(o, name, propDesc);
          assertTrue(delete o["bar"]);
          assertTrue(delete o[5000]);
          assertThrows(function(){deleteFunc(o)}, TypeError);
          assertTrue(o.hasOwnProperty(name));
          %OptimizeFunctionOnNextCall(deleteFunc);
          assertThrows(function(){deleteFunc(o)}, TypeError);
          assertTrue(o.hasOwnProperty(name));
          %DeoptimizeFunction(deleteFunc);
          assertThrows(function(){deleteFunc(o)}, TypeError);
          assertTrue(o.hasOwnProperty(name));
        }
      }
    }
  }
}
testStrongObjectDelete();