1// Copyright 2013 the V8 project authors. All rights reserved.
2// Copyright (C) 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions
6// are met:
7// 1.  Redistributions of source code must retain the above copyright
8//     notice, this list of conditions and the following disclaimer.
9// 2.  Redistributions in binary form must reproduce the above copyright
10//     notice, this list of conditions and the following disclaimer in the
11//     documentation and/or other materials provided with the distribution.
12//
13// THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16// DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
24description(
25"This page tests for assertion failures in edge cases of property lookup on primitive values."
26);
27
28var didNotCrash = true;
29
30(function () {
31    delete String.prototype.constructor;
32    for (var i = 0; i < 3; ++i)
33        "".replace;
34})();
35
36(function () {
37    String.prototype.__proto__ = { x: 1, y: 1 };
38    delete String.prototype.__proto__.x;
39    for (var i = 0; i < 3; ++i)
40        "".y;
41})();
42
43(function () {
44    function f(x) {
45        x.y;
46    }
47
48    String.prototype.x = 1;
49    String.prototype.y = 1;
50    delete String.prototype.x;
51
52    Number.prototype.x = 1;
53    Number.prototype.y = 1;
54    delete Number.prototype.x;
55
56    for (var i = 0; i < 3; ++i)
57        f("");
58
59    for (var i = 0; i < 3; ++i)
60        f(.5);
61})();
62
63
64var checkOkay;
65
66function checkGet(x, constructor)
67{
68    checkOkay = false;
69    Object.defineProperty(constructor.prototype, "foo", { get: function() { checkOkay = typeof this === 'object'; }, configurable: true });
70    x.foo;
71    delete constructor.prototype.foo;
72    return checkOkay;
73}
74
75function checkSet(x, constructor)
76{
77    checkOkay = false;
78    Object.defineProperty(constructor.prototype, "foo", { set: function() { checkOkay = typeof this === 'object'; }, configurable: true });
79    x.foo = null;
80    delete constructor.prototype.foo;
81    return checkOkay;
82}
83
84function checkGetStrict(x, constructor)
85{
86    checkOkay = false;
87    Object.defineProperty(constructor.prototype, "foo", { get: function() { "use strict"; checkOkay = typeof this !== 'object'; }, configurable: true });
88    x.foo;
89    delete constructor.prototype.foo;
90    return checkOkay;
91}
92
93function checkSetStrict(x, constructor)
94{
95    checkOkay = false;
96    Object.defineProperty(constructor.prototype, "foo", { set: function() { "use strict"; checkOkay = typeof this !== 'object'; }, configurable: true });
97    x.foo = null;
98    delete constructor.prototype.foo;
99    return checkOkay;
100}
101
102shouldBeTrue("checkGet(1, Number)");
103shouldBeTrue("checkGet('hello', String)");
104shouldBeTrue("checkGet(true, Boolean)");
105shouldBeTrue("checkSet(1, Number)");
106shouldBeTrue("checkSet('hello', String)");
107shouldBeTrue("checkSet(true, Boolean)");
108shouldBeTrue("checkGetStrict(1, Number)");
109shouldBeTrue("checkGetStrict('hello', String)");
110shouldBeTrue("checkGetStrict(true, Boolean)");
111shouldBeTrue("checkSetStrict(1, Number)");
112shouldBeTrue("checkSetStrict('hello', String)");
113shouldBeTrue("checkSetStrict(true, Boolean)");
114
115function checkRead(x, constructor)
116{
117    return x.foo === undefined;
118}
119
120function checkWrite(x, constructor)
121{
122    x.foo = null;
123    return x.foo === undefined;
124}
125
126function checkReadStrict(x, constructor)
127{
128    "use strict";
129    return x.foo === undefined;
130}
131
132function checkWriteStrict(x, constructor)
133{
134    "use strict";
135    x.foo = null;
136    return x.foo === undefined;
137}
138
139shouldBeTrue("checkRead(1, Number)");
140shouldBeTrue("checkRead('hello', String)");
141shouldBeTrue("checkRead(true, Boolean)");
142shouldBeTrue("checkWrite(1, Number)");
143shouldBeTrue("checkWrite('hello', String)");
144shouldBeTrue("checkWrite(true, Boolean)");
145shouldBeTrue("checkReadStrict(1, Number)");
146shouldBeTrue("checkReadStrict('hello', String)");
147shouldBeTrue("checkReadStrict(true, Boolean)");
148shouldThrow("checkWriteStrict(1, Number)");
149shouldThrow("checkWriteStrict('hello', String)");
150shouldThrow("checkWriteStrict(true, Boolean)");
151
152function checkNumericGet(x, constructor)
153{
154    checkOkay = false;
155    Object.defineProperty(constructor.prototype, 42, { get: function() { checkOkay = typeof this === 'object'; }, configurable: true });
156    x[42];
157    delete constructor.prototype[42];
158    return checkOkay;
159}
160
161function checkNumericSet(x, constructor)
162{
163    checkOkay = false;
164    Object.defineProperty(constructor.prototype, 42, { set: function() { checkOkay = typeof this === 'object'; }, configurable: true });
165    x[42] = null;
166    delete constructor.prototype[42];
167    return checkOkay;
168}
169
170function checkNumericGetStrict(x, constructor)
171{
172    checkOkay = false;
173    Object.defineProperty(constructor.prototype, 42, { get: function() { "use strict"; checkOkay = typeof this !== 'object'; }, configurable: true });
174    x[42];
175    delete constructor.prototype[42];
176    return checkOkay;
177}
178
179function checkNumericSetStrict(x, constructor)
180{
181    checkOkay = false;
182    Object.defineProperty(constructor.prototype, 42, { set: function() { "use strict"; checkOkay = typeof this !== 'object'; }, configurable: true });
183    x[42] = null;
184    delete constructor.prototype[42];
185    return checkOkay;
186}
187
188shouldBeTrue("checkNumericGet(1, Number)");
189shouldBeTrue("checkNumericGet('hello', String)");
190shouldBeTrue("checkNumericGet(true, Boolean)");
191shouldBeTrue("checkNumericSet(1, Number)");
192shouldBeTrue("checkNumericSet('hello', String)");
193shouldBeTrue("checkNumericSet(true, Boolean)");
194shouldBeTrue("checkNumericGetStrict(1, Number)");
195shouldBeTrue("checkNumericGetStrict('hello', String)");
196shouldBeTrue("checkNumericGetStrict(true, Boolean)");
197shouldBeTrue("checkNumericSetStrict(1, Number)");
198shouldBeTrue("checkNumericSetStrict('hello', String)");
199shouldBeTrue("checkNumericSetStrict(true, Boolean)");
200
201function checkNumericRead(x, constructor)
202{
203    return x[42] === undefined;
204}
205
206function checkNumericWrite(x, constructor)
207{
208    x[42] = null;
209    return x[42] === undefined;
210}
211
212function checkNumericReadStrict(x, constructor)
213{
214    "use strict";
215    return x[42] === undefined;
216}
217
218function checkNumericWriteStrict(x, constructor)
219{
220    "use strict";
221    x[42] = null;
222    return x[42] === undefined;
223}
224
225shouldBeTrue("checkNumericRead(1, Number)");
226shouldBeTrue("checkNumericRead('hello', String)");
227shouldBeTrue("checkNumericRead(true, Boolean)");
228shouldBeTrue("checkNumericWrite(1, Number)");
229shouldBeTrue("checkNumericWrite('hello', String)");
230shouldBeTrue("checkNumericWrite(true, Boolean)");
231shouldBeTrue("checkNumericReadStrict(1, Number)");
232shouldBeTrue("checkNumericReadStrict('hello', String)");
233shouldBeTrue("checkNumericReadStrict(true, Boolean)");
234shouldThrow("checkNumericWriteStrict(1, Number)");
235shouldThrow("checkNumericWriteStrict('hello', String)");
236shouldThrow("checkNumericWriteStrict(true, Boolean)");
237
238shouldBeTrue("didNotCrash");
239