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
30function testOne(receiver, key, result) {
31  for(var i = 0; i != 10; i++ ) {
32    assertEquals(result, receiver[key]());
33  }
34}
35
36function 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
44var toStringNonSymbol = 'to';
45toStringNonSymbol += 'String';
46
47function TypeOfThis() { return typeof this; }
48
49Number.prototype.square = function() { return this * this; }
50Number.prototype.power4 = function() { return this.square().square(); }
51
52Number.prototype.type = TypeOfThis;
53String.prototype.type = TypeOfThis;
54Boolean.prototype.type = TypeOfThis;
55
56// Use a non-symbol key to force inline cache to generic case.
57testOne(0, toStringNonSymbol, '0');
58
59testOne(1, 'toString', '1');
60testOne('1', 'toString', '1');
61testOne(1.0, 'toString', '1');
62
63testOne(1, 'type', 'object');
64testOne(2.3, 'type', 'object');
65testOne('x', 'type', 'object');
66testOne(true, 'type', 'object');
67testOne(false, 'type', 'object');
68
69testOne(2, 'square', 4);
70testOne(2, 'power4', 16);
71
72function zero  () { return 0; }
73function one   () { return 1; }
74function two   () { return 2; }
75
76var fixed_array = [zero, one, two];
77
78var dict_array = [ zero, one, two ];
79dict_array[100000] = 1;
80
81var fast_prop = { zero: zero, one: one, two: two };
82
83var normal_prop = { zero: zero, one: one, two: two };
84normal_prop.x = 0;
85delete normal_prop.x;
86
87var first3num = [0, 1, 2];
88var first3str = ['zero', 'one', 'two'];
89
90// Use a non-symbol key to force inline cache to generic case.
91testMany('123', [toStringNonSymbol, 'charAt', 'charCodeAt'], ['123', '1', 49]);
92
93testMany(fixed_array, first3num, first3num);
94testMany(dict_array, first3num, first3num);
95testMany(fast_prop, first3str, first3num);
96testMany(normal_prop, first3str, first3num);
97
98
99function 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
113testException([zero, one, /* hole */ ], [0, 1, 2], [false, false, true]);
114