Home | History | Annotate | Download | only in mjsunit
      1 // Copyright 2008 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 function props(x) {
     29   var array = [];
     30   for (var p in x) array.push(p);
     31   return array.sort();
     32 }
     33 
     34 function f1() {
     35   this.x = 1;
     36 }
     37 
     38 function f2(x) {
     39   this.x = x;
     40 }
     41 
     42 function f3(x) {
     43   this.x = x;
     44   this.y = 1;
     45   this.z = f1;
     46 }
     47 
     48 function f4(x) {
     49   this.x = x;
     50   this.y = 1;
     51   if (x == 1) return;
     52   this.z = f1;
     53 }
     54 
     55 o1_1 = new f1();
     56 assertEquals(1, o1_1.x, "1");
     57 o1_2 = new f1();
     58 assertEquals(1, o1_1.x, "2");
     59 assertArrayEquals(["x"], props(o1_1), "3");
     60 assertArrayEquals(["x"], props(o1_2), "4");
     61 
     62 o2_1 = new f2(0);
     63 o2_2 = new f2(0);
     64 assertArrayEquals(["x"], props(o2_1));
     65 assertArrayEquals(["x"], props(o2_2));
     66 
     67 o3_1 = new f3(0);
     68 o3_2 = new f3(0);
     69 assertArrayEquals(["x", "y", "z"], props(o3_1));
     70 assertArrayEquals(["x", "y", "z"], props(o3_2));
     71 
     72 o4_0_1 = new f4(0);
     73 o4_0_2 = new f4(0);
     74 assertArrayEquals(["x", "y", "z"], props(o4_0_1));
     75 assertArrayEquals(["x", "y", "z"], props(o4_0_2));
     76 
     77 o4_1_1 = new f4(1);
     78 o4_1_2 = new f4(1);
     79 assertArrayEquals(["x", "y"], props(o4_1_1));
     80 assertArrayEquals(["x", "y"], props(o4_1_2));
     81 
     82 function f5(x, y) {
     83   this.x = x;
     84   this.y = y;
     85 }
     86 
     87 function f6(x, y) {
     88   this.y = y;
     89   this.x = x;
     90 }
     91 
     92 function f7(x, y, z) {
     93   this.x = x;
     94   this.y = y;
     95 }
     96 
     97 function testArgs(fun) {
     98   obj = new fun();
     99   assertArrayEquals(["x", "y"], props(obj));
    100   assertEquals(void 0, obj.x);
    101   assertEquals(void 0, obj.y);
    102 
    103   obj = new fun("x");
    104   assertArrayEquals(["x", "y"], props(obj));
    105   assertEquals("x", obj.x);
    106   assertEquals(void 0, obj.y);
    107 
    108   obj = new fun("x", "y");
    109   assertArrayEquals(["x", "y"], props(obj));
    110   assertEquals("x", obj.x);
    111   assertEquals("y", obj.y);
    112 
    113   obj = new fun("x", "y", "z");
    114   assertArrayEquals(["x", "y"], props(obj));
    115   assertEquals("x", obj.x);
    116   assertEquals("y", obj.y);
    117 }
    118 
    119 for (var i = 0; i < 10; i++) {
    120   testArgs(f5);
    121   testArgs(f6);
    122   testArgs(f7);
    123 }
    124 
    125 function g(){
    126   this.x=1
    127 }
    128 
    129 o = new g();
    130 assertEquals(1, o.x);
    131 o = new g();
    132 assertEquals(1, o.x);
    133 g.prototype = {y:2}
    134 o = new g();
    135 assertEquals(1, o.x);
    136 assertEquals(2, o.y);
    137 o = new g();
    138 assertEquals(1, o.x);
    139 assertEquals(2, o.y);
    140 
    141