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 // When calling user-defined functions on strings, booleans or
     29 // numbers, we should create a wrapper object.
     30 
     31 // When running the tests use loops to ensure that the call site moves through
     32 // the different IC states and that both the runtime system and the generated
     33 // IC code is tested.
     34 function RunTests() {
     35   for (var i = 0; i < 10; i++) {
     36     assertEquals('object', 'xxx'.TypeOfThis());
     37     assertEquals('object', true.TypeOfThis(2,3));
     38     assertEquals('object', false.TypeOfThis());
     39     assertEquals('object', (42).TypeOfThis());
     40     assertEquals('object', (3.14).TypeOfThis());
     41   }
     42 
     43   for (var i = 0; i < 10; i++) {
     44     assertEquals('object', 'xxx'['TypeOfThis']());
     45     assertEquals('object', true['TypeOfThis']());
     46     assertEquals('object', false['TypeOfThis']());
     47     assertEquals('object', (42)['TypeOfThis']());
     48     assertEquals('object', (3.14)['TypeOfThis']());
     49   }
     50 
     51   function CallTypeOfThis(obj) {
     52     assertEquals('object', obj.TypeOfThis());
     53   }
     54 
     55   for (var i = 0; i < 10; i++) {
     56     CallTypeOfThis('xxx');
     57     CallTypeOfThis(true);
     58     CallTypeOfThis(false);
     59     CallTypeOfThis(42);
     60     CallTypeOfThis(3.14);
     61   }
     62 
     63   function TestWithWith(obj) {
     64     with (obj) {
     65       for (var i = 0; i < 10; i++) {
     66         assertEquals('object', TypeOfThis());
     67       }
     68     }
     69   }
     70 
     71   TestWithWith('xxx');
     72   TestWithWith(true);
     73   TestWithWith(false);
     74   TestWithWith(42);
     75   TestWithWith(3.14);
     76 
     77   for (var i = 0; i < 10; i++) {
     78     assertEquals('object', true[7]());
     79     assertEquals('object', false[7]());
     80     assertEquals('object', (42)[7]());
     81     assertEquals('object', (3.14)[7]());
     82   }
     83 
     84   for (var i = 0; i < 10; i++) {
     85     assertEquals('object', typeof 'xxx'.ObjectValueOf());
     86     assertEquals('object', typeof true.ObjectValueOf());
     87     assertEquals('object', typeof false.ObjectValueOf());
     88     assertEquals('object', typeof (42).ObjectValueOf());
     89     assertEquals('object', typeof (3.14).ObjectValueOf());
     90   }
     91 
     92   for (var i = 0; i < 10; i++) {
     93     assertEquals('[object String]', 'xxx'.ObjectToString());
     94     assertEquals('[object Boolean]', true.ObjectToString());
     95     assertEquals('[object Boolean]', false.ObjectToString());
     96     assertEquals('[object Number]', (42).ObjectToString());
     97     assertEquals('[object Number]', (3.14).ObjectToString());
     98   }
     99 }
    100 
    101 function TypeOfThis() { return typeof this; }
    102 
    103 // Test with normal setup of prototype.
    104 String.prototype.TypeOfThis = TypeOfThis;
    105 Boolean.prototype.TypeOfThis = TypeOfThis;
    106 Number.prototype.TypeOfThis = TypeOfThis;
    107 Boolean.prototype[7] = TypeOfThis;
    108 Number.prototype[7] = TypeOfThis;
    109 
    110 String.prototype.ObjectValueOf = Object.prototype.valueOf;
    111 Boolean.prototype.ObjectValueOf = Object.prototype.valueOf;
    112 Number.prototype.ObjectValueOf = Object.prototype.valueOf;
    113 
    114 String.prototype.ObjectToString = Object.prototype.toString;
    115 Boolean.prototype.ObjectToString = Object.prototype.toString;
    116 Number.prototype.ObjectToString = Object.prototype.toString;
    117 
    118 RunTests();
    119 
    120 // Run test after properties have been set to a different value.
    121 String.prototype.TypeOfThis = 'x';
    122 Boolean.prototype.TypeOfThis = 'x';
    123 Number.prototype.TypeOfThis = 'x';
    124 Boolean.prototype[7] = 'x';
    125 Number.prototype[7] = 'x';
    126 
    127 String.prototype.TypeOfThis = TypeOfThis;
    128 Boolean.prototype.TypeOfThis = TypeOfThis;
    129 Number.prototype.TypeOfThis = TypeOfThis;
    130 Boolean.prototype[7] = TypeOfThis;
    131 Number.prototype[7] = TypeOfThis;
    132 
    133 RunTests();
    134 
    135 // Force the prototype into slow case and run the test again.
    136 delete String.prototype.TypeOfThis;
    137 delete Boolean.prototype.TypeOfThis;
    138 delete Number.prototype.TypeOfThis;
    139 Boolean.prototype[7];
    140 Number.prototype[7];
    141 
    142 String.prototype.TypeOfThis = TypeOfThis;
    143 Boolean.prototype.TypeOfThis = TypeOfThis;
    144 Number.prototype.TypeOfThis = TypeOfThis;
    145 Boolean.prototype[7] = TypeOfThis;
    146 Number.prototype[7] = TypeOfThis;
    147 
    148 RunTests();
    149 
    150 // According to ES3 15.3.4.3 the this value passed to Function.prototyle.apply
    151 // should wrapped. According to ES5 it should not.
    152 assertEquals('object', TypeOfThis.apply('xxx', []));
    153 assertEquals('object', TypeOfThis.apply(true, []));
    154 assertEquals('object', TypeOfThis.apply(false, []));
    155 assertEquals('object', TypeOfThis.apply(42, []));
    156 assertEquals('object', TypeOfThis.apply(3.14, []));
    157 
    158 // According to ES3 15.3.4.3 the this value passed to Function.prototyle.call
    159 // should wrapped. According to ES5 it should not.
    160 assertEquals('object', TypeOfThis.call('xxx'));
    161 assertEquals('object', TypeOfThis.call(true));
    162 assertEquals('object', TypeOfThis.call(false));
    163 assertEquals('object', TypeOfThis.call(42));
    164 assertEquals('object', TypeOfThis.call(3.14));
    165