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
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.
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.
28 // When calling user-defined functions on strings, booleans or
29 // numbers, we should create a wrapper object.
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
36 for (var i = 0; i < 10; i++) {
37 assertEquals('object', 'xxx'.TypeOfThis());
38 assertEquals('object', true.TypeOfThis(2,3));
39 assertEquals('object', false.TypeOfThis());
40 assertEquals('object', (42).TypeOfThis());
41 assertEquals('object', (3.14).TypeOfThis());
44 for (var i = 0; i < 10; i++) {
45 assertEquals('object', 'xxx'['TypeOfThis']());
46 assertEquals('object', true['TypeOfThis']());
47 assertEquals('object', false['TypeOfThis']());
48 assertEquals('object', (42)['TypeOfThis']());
49 assertEquals('object', (3.14)['TypeOfThis']());
52 function CallTypeOfThis(obj) {
53 assertEquals('object', obj.TypeOfThis());
56 for (var i = 0; i < 10; i++) {
57 CallTypeOfThis('xxx');
59 CallTypeOfThis(false);
64 function TestWithWith(obj) {
66 for (var i = 0; i < 10; i++) {
67 assertEquals('object', TypeOfThis());
78 for (var i = 0; i < 10; i++) {
79 assertEquals('object', true[7]());
80 assertEquals('object', false[7]());
81 assertEquals('object', (42)[7]());
82 assertEquals('object', (3.14)[7]());
85 for (var i = 0; i < 10; i++) {
86 assertEquals('object', typeof 'xxx'.ObjectValueOf());
87 assertEquals('object', typeof true.ObjectValueOf());
88 assertEquals('object', typeof false.ObjectValueOf());
89 assertEquals('object', typeof (42).ObjectValueOf());
90 assertEquals('object', typeof (3.14).ObjectValueOf());
93 for (var i = 0; i < 10; i++) {
94 assertEquals('[object String]', 'xxx'.ObjectToString());
95 assertEquals('[object Boolean]', true.ObjectToString());
96 assertEquals('[object Boolean]', false.ObjectToString());
97 assertEquals('[object Number]', (42).ObjectToString());
98 assertEquals('[object Number]', (3.14).ObjectToString());
102 function TypeOfThis() { return typeof this; }
104 // Test with normal setup of prototype.
105 String.prototype.TypeOfThis = TypeOfThis;
106 Boolean.prototype.TypeOfThis = TypeOfThis;
107 Number.prototype.TypeOfThis = TypeOfThis;
108 Boolean.prototype[7] = TypeOfThis;
109 Number.prototype[7] = TypeOfThis;
111 String.prototype.ObjectValueOf = Object.prototype.valueOf;
112 Boolean.prototype.ObjectValueOf = Object.prototype.valueOf;
113 Number.prototype.ObjectValueOf = Object.prototype.valueOf;
115 String.prototype.ObjectToString = Object.prototype.toString;
116 Boolean.prototype.ObjectToString = Object.prototype.toString;
117 Number.prototype.ObjectToString = Object.prototype.toString;
121 // Run test after properties have been set to a different value.
122 String.prototype.TypeOfThis = 'x';
123 Boolean.prototype.TypeOfThis = 'x';
124 Number.prototype.TypeOfThis = 'x';
125 Boolean.prototype[7] = 'x';
126 Number.prototype[7] = 'x';
128 String.prototype.TypeOfThis = TypeOfThis;
129 Boolean.prototype.TypeOfThis = TypeOfThis;
130 Number.prototype.TypeOfThis = TypeOfThis;
131 Boolean.prototype[7] = TypeOfThis;
132 Number.prototype[7] = TypeOfThis;
136 // Force the prototype into slow case and run the test again.
137 delete String.prototype.TypeOfThis;
138 delete Boolean.prototype.TypeOfThis;
139 delete Number.prototype.TypeOfThis;
140 Boolean.prototype[7];
143 String.prototype.TypeOfThis = TypeOfThis;
144 Boolean.prototype.TypeOfThis = TypeOfThis;
145 Number.prototype.TypeOfThis = TypeOfThis;
146 Boolean.prototype[7] = TypeOfThis;
147 Number.prototype[7] = TypeOfThis;
151 // According to ES3 15.3.4.3 the this value passed to Function.prototyle.apply
152 // should wrapped. According to ES5 it should not.
153 assertEquals('object', TypeOfThis.apply('xxx', []));
154 assertEquals('object', TypeOfThis.apply(true, []));
155 assertEquals('object', TypeOfThis.apply(false, []));
156 assertEquals('object', TypeOfThis.apply(42, []));
157 assertEquals('object', TypeOfThis.apply(3.14, []));
159 // According to ES3 15.3.4.3 the this value passed to Function.prototyle.call
160 // should wrapped. According to ES5 it should not.
161 assertEquals('object', TypeOfThis.call('xxx'));
162 assertEquals('object', TypeOfThis.call(true));
163 assertEquals('object', TypeOfThis.call(false));
164 assertEquals('object', TypeOfThis.call(42));
165 assertEquals('object', TypeOfThis.call(3.14));