blob: 3839731e02a6144f834e46a45de22fa4d7f50180 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2011 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
Ben Murdochda12d292016-06-02 14:46:10 +010028// Flags: --allow-natives-syntax
Ben Murdochb8a8cc12014-11-26 15:28:44 +000029
30// Different ways to create an object.
31
32function CreateFromLiteral() {
33 return {};
34}
35
36function CreateFromObject() {
37 return new Object;
38}
39
40function CreateDefault() {
41 return Object.create(Object.prototype);
42}
43
44function CreateFromConstructor(proto) {
45 function C() {}
46 (new C).b = 9; // Make sure that we can have an in-object property.
47 C.prototype = proto;
48 return function() { return new C; }
49}
50
51function CreateFromApi(proto) {
52 return function() { return Object.create(proto); }
53}
54
55function CreateWithProperty(proto) {
56 function C() { this.a = -100; }
57 C.prototype = proto;
58 return function() { return new C; }
59}
60
61var bases = [CreateFromLiteral, CreateFromObject, CreateDefault];
62var inherits = [CreateFromConstructor, CreateFromApi, CreateWithProperty];
63var constructs = [CreateFromConstructor, CreateFromApi];
64
65function TestAllCreates(f) {
66 // The depth of the prototype chain up the.
67 for (var depth = 0; depth < 3; ++depth) {
68 // Introduce readonly-ness this far up the chain.
69 for (var up = 0; up <= depth; ++up) {
70 // Try different construction methods.
71 for (var k = 0; k < constructs.length; ++k) {
72 // Construct a fresh prototype chain from above functions.
73 for (var i = 0; i < bases.length; ++i) {
74 var p = bases[i]();
75 // There may be a preexisting property under the insertion point...
76 for (var j = 0; j < depth - up; ++j) {
77 p = inherits[Math.floor(inherits.length * Math.random())](p)();
78 }
79 // ...but not above it.
80 for (var j = 0; j < up; ++j) {
81 p = constructs[Math.floor(constructs.length * Math.random())](p)();
82 }
83 // Create a fresh constructor.
84 var c = constructs[k](p);
85 f(function() {
86 var o = c();
87 o.up = o;
88 for (var j = 0; j < up; ++j) o.up = Object.getPrototypeOf(o.up);
89 return o;
90 })
91 }
92 }
93 }
94 }
95}
96
97
98// Different ways to make a property read-only.
99
100function ReadonlyByNonwritableDataProperty(o, name) {
101 Object.defineProperty(o, name, {value: -41, writable: false});
102}
103
104function ReadonlyByAccessorPropertyWithoutSetter(o, name) {
105 Object.defineProperty(o, name, {get: function() { return -42; }});
106}
107
108function ReadonlyByGetter(o, name) {
109 o.__defineGetter__("a", function() { return -43; });
110}
111
112function ReadonlyByFreeze(o, name) {
113 o[name] = -44;
114 Object.freeze(o);
115}
116
117function ReadonlyByProto(o, name) {
118 var p = Object.create(o.__proto__);
119 Object.defineProperty(p, name, {value: -45, writable: false});
120 o.__proto__ = p;
121}
122
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000123// TODO(neis,cbruni): Enable once the necessary traps work again.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000124// Allow Proxy to be undefined, so test can run in non-Harmony mode as well.
125var global = this;
126
127function ReadonlyByProxy(o, name) {
128 if (!global.Proxy) return ReadonlyByFreeze(o, name); // Dummy.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000129 var p = new global.Proxy({}, {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000130 getPropertyDescriptor: function() {
131 return {value: -46, writable: false, configurable: true};
132 }
133 });
134 o.__proto__ = p;
135}
136
137var readonlys = [
138 ReadonlyByNonwritableDataProperty, ReadonlyByAccessorPropertyWithoutSetter,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000139 ReadonlyByGetter, ReadonlyByFreeze, ReadonlyByProto // ReadonlyByProxy
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000140]
141
142function TestAllReadonlys(f) {
143 // Provide various methods to making a property read-only.
144 for (var i = 0; i < readonlys.length; ++i) {
145 print(" readonly =", i)
146 f(readonlys[i]);
147 }
148}
149
150
151// Different use scenarios.
152
153function Assign(o, x) {
154 o.a = x;
155}
156
157function AssignStrict(o, x) {
158 "use strict";
159 o.a = x;
160}
161
162function TestAllModes(f) {
163 for (var strict = 0; strict < 2; ++strict) {
164 print(" strict =", strict);
165 f(strict);
166 }
167}
168
169function TestAllScenarios(f) {
170 for (var t = 0; t < 100; t = 2*t + 1) {
171 print("t =", t)
172 f(function(strict, create, readonly) {
173 // Make sure that the assignments are monomorphic.
174 %DeoptimizeFunction(Assign);
175 %DeoptimizeFunction(AssignStrict);
176 %ClearFunctionTypeFeedback(Assign);
177 %ClearFunctionTypeFeedback(AssignStrict);
178 for (var i = 0; i < t; ++i) {
179 var o = create();
180 assertFalse("a" in o && !("a" in o.__proto__));
181 if (strict === 0)
182 Assign(o, i);
183 else
184 AssignStrict(o, i);
185 assertEquals(i, o.a);
186 }
187 %OptimizeFunctionOnNextCall(Assign);
188 %OptimizeFunctionOnNextCall(AssignStrict);
189 var o = create();
190 assertFalse("a" in o && !("a" in o.__proto__));
191 readonly(o.up, "a");
192 assertTrue("a" in o);
193 if (strict === 0)
194 Assign(o, t + 1);
195 else
196 assertThrows(function() { AssignStrict(o, t + 1) }, TypeError);
197 assertTrue(o.a < 0);
198 });
199 }
200}
201
202
203// Runner.
204
205TestAllScenarios(function(scenario) {
206 TestAllModes(function(strict) {
207 TestAllReadonlys(function(readonly) {
208 TestAllCreates(function(create) {
209 scenario(strict, create, readonly);
210 });
211 });
212 });
213});
214
215
216// Extra test forcing bailout.
217
218function Assign2(o, x) { o.a = x }
219
220(function() {
221 var p = CreateFromConstructor(Object.prototype)();
222 var c = CreateFromConstructor(p);
223 for (var i = 0; i < 3; ++i) {
224 var o = c();
225 Assign2(o, i);
226 assertEquals(i, o.a);
227 }
228 %OptimizeFunctionOnNextCall(Assign2);
229 ReadonlyByNonwritableDataProperty(p, "a");
230 var o = c();
231 Assign2(o, 0);
232 assertTrue(o.a < 0);
233})();