Home | History | Annotate | Download | only in mjsunit
      1 // Copyright 2009 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 // Testing that optimization of top-level object initialization doesn't
     29 // break V8.
     30 
     31 var x = new Object();
     32 x.a = 7;
     33 x.b = function() { return 42; };
     34 x.c = 88;
     35 x.d = "A Man Called Horse";
     36 
     37 assertEquals(7, x.a);
     38 assertEquals(42, x.b());
     39 assertEquals(88, x.c);
     40 assertEquals("A Man Called Horse", x.d);
     41 
     42 var y = new Object();
     43 y.a = 7;
     44 y.b = function() { return 42; };
     45 y.c = 12 * y.a;
     46 y.d = "A Man Called Horse";
     47 
     48 assertEquals(84, y.c);
     49 
     50 var z = new Object();
     51 z.a = 3;
     52 function forty_two() { return 42; };
     53 z.a += 4;
     54 z.b = forty_two;
     55 z.c = 12;
     56 z.c *= z.a;
     57 z.d = "A Man Called Horse";
     58 
     59 assertEquals(84, z.c);
     60 
     61 var x1 = new Object();
     62 var x2 = new Object();
     63 x1.a = 7;
     64 x1.b = function() { return 42; };
     65 x2.a = 7;
     66 x2.b = function() { return 42; };
     67 x1.c = 88;
     68 x1.d = "A Man Called Horse";
     69 x2.c = 88;
     70 x2.d = "A Man Called Horse";
     71 
     72 assertEquals(7, x1.a);
     73 assertEquals(42, x1.b());
     74 assertEquals(88, x1.c);
     75 assertEquals("A Man Called Horse", x1.d);
     76 
     77 assertEquals(7, x2.a);
     78 assertEquals(42, x2.b());
     79 assertEquals(88, x2.c);
     80 assertEquals("A Man Called Horse", x2.d);
     81 
     82 function Calculator(x, y) {
     83   this.x = x;
     84   this.y = y;
     85 }
     86 
     87 Calculator.prototype.sum = function() { return this.x + this.y; };
     88 Calculator.prototype.difference = function() { return this.x - this.y; };
     89 Calculator.prototype.product = function() { return this.x * this.y; };
     90 Calculator.prototype.quotient = function() { return this.x / this.y; };
     91 
     92 var calc = new Calculator(20, 10);
     93 assertEquals(30, calc.sum());
     94 assertEquals(10, calc.difference());
     95 assertEquals(200, calc.product());
     96 assertEquals(2, calc.quotient());
     97 
     98 var x = new Object();
     99 x.__defineGetter__('a', function() { return 7 });
    100 x.b = function() { return 42; };
    101 x.c = 88;
    102 x.d = "A Man Called Horse";
    103 
    104 assertEquals(7, x.a);
    105 assertEquals(42, x.b());
    106 assertEquals(88, x.c);
    107 assertEquals("A Man Called Horse", x.d);
    108