Home | History | Annotate | Download | only in mjsunit
      1 // Copyright 2010 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 // A test for keyed call ICs with a mix of smi and string keys.
     29 
     30 function testOne(receiver, key, result) {
     31   for(var i = 0; i != 10; i++ ) {
     32     assertEquals(result, receiver[key]());
     33   }
     34 }
     35 
     36 function testMany(receiver, keys, results) {
     37   for (var i = 0; i != 10; i++) {
     38     for (var k = 0; k != keys.length; k++) {
     39       assertEquals(results[k], receiver[keys[k]]());
     40     }
     41   }
     42 }
     43 
     44 var toStringNonSymbol = 'to';
     45 toStringNonSymbol += 'String';
     46 
     47 function TypeOfThis() { return typeof this; }
     48 
     49 Number.prototype.square = function() { return this * this; }
     50 Number.prototype.power4 = function() { return this.square().square(); }
     51 
     52 Number.prototype.type = TypeOfThis;
     53 String.prototype.type = TypeOfThis;
     54 Boolean.prototype.type = TypeOfThis;
     55 
     56 // Use a non-symbol key to force inline cache to generic case.
     57 testOne(0, toStringNonSymbol, '0');
     58 
     59 testOne(1, 'toString', '1');
     60 testOne('1', 'toString', '1');
     61 testOne(1.0, 'toString', '1');
     62 
     63 testOne(1, 'type', 'object');
     64 testOne(2.3, 'type', 'object');
     65 testOne('x', 'type', 'object');
     66 testOne(true, 'type', 'object');
     67 testOne(false, 'type', 'object');
     68 
     69 testOne(2, 'square', 4);
     70 testOne(2, 'power4', 16);
     71 
     72 function zero  () { return 0; }
     73 function one   () { return 1; }
     74 function two   () { return 2; }
     75 
     76 var fixed_array = [zero, one, two];
     77 
     78 var dict_array = [ zero, one, two ];
     79 dict_array[100000] = 1;
     80 
     81 var fast_prop = { zero: zero, one: one, two: two };
     82 
     83 var normal_prop = { zero: zero, one: one, two: two };
     84 normal_prop.x = 0;
     85 delete normal_prop.x;
     86 
     87 var first3num = [0, 1, 2];
     88 var first3str = ['zero', 'one', 'two'];
     89 
     90 // Use a non-symbol key to force inline cache to generic case.
     91 testMany('123', [toStringNonSymbol, 'charAt', 'charCodeAt'], ['123', '1', 49]);
     92 
     93 testMany(fixed_array, first3num, first3num);
     94 testMany(dict_array, first3num, first3num);
     95 testMany(fast_prop, first3str, first3num);
     96 testMany(normal_prop, first3str, first3num);
     97 
     98 
     99 function testException(receiver, keys, exceptions) {
    100   for (var i = 0; i != 10; i++) {
    101     for (var k = 0; k != keys.length; k++) {
    102       var thrown = false;
    103       try {
    104         var result = receiver[keys[k]]();
    105       } catch (e) {
    106         thrown = true;
    107       }
    108       assertEquals(exceptions[k], thrown);
    109     }
    110   }
    111 }
    112 
    113 testException([zero, one, /* hole */ ], [0, 1, 2], [false, false, true]);
    114