1 // 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
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.
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.
28 // Flags: --allow-natives-syntax --harmony-proxies
30 // Different ways to create an object.
32 function CreateFromLiteral() {
36 function CreateFromObject() {
40 function CreateDefault() {
41 return Object.create(Object.prototype);
44 function CreateFromConstructor(proto) {
46 (new C).b = 9; // Make sure that we can have an in-object property.
48 return function() { return new C; }
51 function CreateFromApi(proto) {
52 return function() { return Object.create(proto); }
55 function CreateWithProperty(proto) {
56 function C() { this.a = -100; }
58 return function() { return new C; }
61 var bases = [CreateFromLiteral, CreateFromObject, CreateDefault];
62 var inherits = [CreateFromConstructor, CreateFromApi, CreateWithProperty];
63 var constructs = [CreateFromConstructor, CreateFromApi];
65 function 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) {
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)();
79 // ...but not above it.
80 for (var j = 0; j < up; ++j) {
81 p = constructs[Math.floor(constructs.length * Math.random())](p)();
83 // Create a fresh constructor.
84 var c = constructs[k](p);
88 for (var j = 0; j < up; ++j) o.up = Object.getPrototypeOf(o.up);
98 // Different ways to make a property read-only.
100 function ReadonlyByNonwritableDataProperty(o, name) {
101 Object.defineProperty(o, name, {value: -41, writable: false});
104 function ReadonlyByAccessorPropertyWithoutSetter(o, name) {
105 Object.defineProperty(o, name, {get: function() { return -42; }});
108 function ReadonlyByGetter(o, name) {
109 o.__defineGetter__("a", function() { return -43; });
112 function ReadonlyByFreeze(o, name) {
117 function ReadonlyByProto(o, name) {
118 var p = Object.create(o.__proto__);
119 Object.defineProperty(p, name, {value: -45, writable: false});
123 // Allow Proxy to be undefined, so test can run in non-Harmony mode as well.
126 function ReadonlyByProxy(o, name) {
127 if (!global.Proxy) return ReadonlyByFreeze(o, name); // Dummy.
128 var p = global.Proxy.create({
129 getPropertyDescriptor: function() {
130 return {value: -46, writable: false, configurable: true};
137 ReadonlyByNonwritableDataProperty, ReadonlyByAccessorPropertyWithoutSetter,
138 ReadonlyByGetter, ReadonlyByFreeze, ReadonlyByProto, ReadonlyByProxy
141 function TestAllReadonlys(f) {
142 // Provide various methods to making a property read-only.
143 for (var i = 0; i < readonlys.length; ++i) {
144 print(" readonly =", i)
150 // Different use scenarios.
152 function Assign(o, x) {
156 function AssignStrict(o, x) {
161 function TestAllModes(f) {
162 for (var strict = 0; strict < 2; ++strict) {
163 print(" strict =", strict);
168 function TestAllScenarios(f) {
169 for (var t = 0; t < 100; t = 2*t + 1) {
171 f(function(strict, create, readonly) {
172 // Make sure that the assignments are monomorphic.
173 %DeoptimizeFunction(Assign);
174 %DeoptimizeFunction(AssignStrict);
175 %ClearFunctionTypeFeedback(Assign);
176 %ClearFunctionTypeFeedback(AssignStrict);
177 for (var i = 0; i < t; ++i) {
179 assertFalse("a" in o && !("a" in o.__proto__));
184 assertEquals(i, o.a);
186 %OptimizeFunctionOnNextCall(Assign);
187 %OptimizeFunctionOnNextCall(AssignStrict);
189 assertFalse("a" in o && !("a" in o.__proto__));
191 assertTrue("a" in o);
195 assertThrows(function() { AssignStrict(o, t + 1) }, TypeError);
204 TestAllScenarios(function(scenario) {
205 TestAllModes(function(strict) {
206 TestAllReadonlys(function(readonly) {
207 TestAllCreates(function(create) {
208 scenario(strict, create, readonly);
215 // Extra test forcing bailout.
217 function Assign2(o, x) { o.a = x }
220 var p = CreateFromConstructor(Object.prototype)();
221 var c = CreateFromConstructor(p);
222 for (var i = 0; i < 3; ++i) {
225 assertEquals(i, o.a);
227 %OptimizeFunctionOnNextCall(Assign2);
228 ReadonlyByNonwritableDataProperty(p, "a");