Home | History | Annotate | Download | only in mjsunit
      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
      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 
     28 // Flags: --allow-natives-syntax --es5_readonly
     29 // Flags: --harmony-proxies
     30 
     31 // Different ways to create an object.
     32 
     33 function CreateFromLiteral() {
     34   return {};
     35 }
     36 
     37 function CreateFromObject() {
     38   return new Object;
     39 }
     40 
     41 function CreateDefault() {
     42   return Object.create(Object.prototype);
     43 }
     44 
     45 function CreateFromConstructor(proto) {
     46   function C() {}
     47   (new C).b = 9;  // Make sure that we can have an in-object property.
     48   C.prototype = proto;
     49   return function() { return new C; }
     50 }
     51 
     52 function CreateFromApi(proto) {
     53   return function() { return Object.create(proto); }
     54 }
     55 
     56 function CreateWithProperty(proto) {
     57   function C() { this.a = -100; }
     58   C.prototype = proto;
     59   return function() { return new C; }
     60 }
     61 
     62 var bases = [CreateFromLiteral, CreateFromObject, CreateDefault];
     63 var inherits = [CreateFromConstructor, CreateFromApi, CreateWithProperty];
     64 var constructs = [CreateFromConstructor, CreateFromApi];
     65 
     66 function TestAllCreates(f) {
     67   // The depth of the prototype chain up the.
     68   for (var depth = 0; depth < 3; ++depth) {
     69     // Introduce readonly-ness this far up the chain.
     70     for (var up = 0; up <= depth; ++up) {
     71       // Try different construction methods.
     72       for (var k = 0; k < constructs.length; ++k) {
     73         // Construct a fresh prototype chain from above functions.
     74         for (var i = 0; i < bases.length; ++i) {
     75           var p = bases[i]();
     76           // There may be a preexisting property under the insertion point...
     77           for (var j = 0; j < depth - up; ++j) {
     78             p = inherits[Math.floor(inherits.length * Math.random())](p)();
     79           }
     80           // ...but not above it.
     81           for (var j = 0; j < up; ++j) {
     82             p = constructs[Math.floor(constructs.length * Math.random())](p)();
     83           }
     84           // Create a fresh constructor.
     85           var c = constructs[k](p);
     86           f(function() {
     87             var o = c();
     88             o.up = o;
     89             for (var j = 0; j < up; ++j) o.up = Object.getPrototypeOf(o.up);
     90             return o;
     91           })
     92         }
     93       }
     94     }
     95   }
     96 }
     97 
     98 
     99 // Different ways to make a property read-only.
    100 
    101 function ReadonlyByNonwritableDataProperty(o, name) {
    102   Object.defineProperty(o, name, {value: -41, writable: false});
    103 }
    104 
    105 function ReadonlyByAccessorPropertyWithoutSetter(o, name) {
    106   Object.defineProperty(o, name, {get: function() { return -42; }});
    107 }
    108 
    109 function ReadonlyByGetter(o, name) {
    110   o.__defineGetter__("a", function() { return -43; });
    111 }
    112 
    113 function ReadonlyByFreeze(o, name) {
    114   o[name] = -44;
    115   Object.freeze(o);
    116 }
    117 
    118 function ReadonlyByProto(o, name) {
    119   var p = Object.create(o.__proto__);
    120   Object.defineProperty(p, name, {value: -45, writable: false});
    121   o.__proto__ = p;
    122 }
    123 
    124 // Allow Proxy to be undefined, so test can run in non-Harmony mode as well.
    125 var global = this;
    126 
    127 function ReadonlyByProxy(o, name) {
    128   if (!global.Proxy) return ReadonlyByFreeze(o, name);  // Dummy.
    129   var p = global.Proxy.create({
    130     getPropertyDescriptor: function() {
    131       return {value: -46, writable: false, configurable: true};
    132     }
    133   });
    134   o.__proto__ = p;
    135 }
    136 
    137 var readonlys = [
    138   ReadonlyByNonwritableDataProperty, ReadonlyByAccessorPropertyWithoutSetter,
    139   ReadonlyByGetter, ReadonlyByFreeze, ReadonlyByProto, ReadonlyByProxy
    140 ]
    141 
    142 function 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 
    153 function Assign(o, x) {
    154   o.a = x;
    155 }
    156 
    157 function AssignStrict(o, x) {
    158   "use strict";
    159   o.a = x;
    160 }
    161 
    162 function TestAllModes(f) {
    163   for (var strict = 0; strict < 2; ++strict) {
    164     print(" strict =", strict);
    165     f(strict);
    166   }
    167 }
    168 
    169 function 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 
    205 TestAllScenarios(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 
    218 function 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 })();
    234