blob: 76e200f36e5bd45b8540c0c0d0fb009221eaebb1 [file] [log] [blame]
Leon Clarkee46be812010-01-19 14:06:41 +00001// 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
Leon Clarked91b9f72010-01-27 17:25:45 +000031// 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.
Leon Clarkee46be812010-01-19 14:06:41 +000034function 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 }
Ben Murdoch589d6972011-11-30 16:04:58 +000042
Leon Clarkee46be812010-01-19 14:06:41 +000043 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 }
Ben Murdoch589d6972011-11-30 16:04:58 +000050
Leon Clarkee46be812010-01-19 14:06:41 +000051 function CallTypeOfThis(obj) {
52 assertEquals('object', obj.TypeOfThis());
53 }
Ben Murdoch589d6972011-11-30 16:04:58 +000054
Leon Clarkee46be812010-01-19 14:06:41 +000055 for (var i = 0; i < 10; i++) {
56 CallTypeOfThis('xxx');
57 CallTypeOfThis(true);
58 CallTypeOfThis(false);
59 CallTypeOfThis(42);
60 CallTypeOfThis(3.14);
61 }
Ben Murdoch589d6972011-11-30 16:04:58 +000062
Leon Clarkee46be812010-01-19 14:06:41 +000063 function TestWithWith(obj) {
64 with (obj) {
65 for (var i = 0; i < 10; i++) {
66 assertEquals('object', TypeOfThis());
67 }
68 }
69 }
Ben Murdoch589d6972011-11-30 16:04:58 +000070
Leon Clarkee46be812010-01-19 14:06:41 +000071 TestWithWith('xxx');
72 TestWithWith(true);
73 TestWithWith(false);
74 TestWithWith(42);
75 TestWithWith(3.14);
Ben Murdoch589d6972011-11-30 16:04:58 +000076
Leon Clarkee46be812010-01-19 14:06:41 +000077 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 }
Leon Clarked91b9f72010-01-27 17:25:45 +000083
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 }
Leon Clarkee46be812010-01-19 14:06:41 +000099}
100
101function TypeOfThis() { return typeof this; }
102
Ben Murdoch589d6972011-11-30 16:04:58 +0000103// Test with normal setup of prototype.
Leon Clarkee46be812010-01-19 14:06:41 +0000104String.prototype.TypeOfThis = TypeOfThis;
105Boolean.prototype.TypeOfThis = TypeOfThis;
106Number.prototype.TypeOfThis = TypeOfThis;
107Boolean.prototype[7] = TypeOfThis;
108Number.prototype[7] = TypeOfThis;
Leon Clarked91b9f72010-01-27 17:25:45 +0000109
110String.prototype.ObjectValueOf = Object.prototype.valueOf;
111Boolean.prototype.ObjectValueOf = Object.prototype.valueOf;
112Number.prototype.ObjectValueOf = Object.prototype.valueOf;
113
114String.prototype.ObjectToString = Object.prototype.toString;
115Boolean.prototype.ObjectToString = Object.prototype.toString;
116Number.prototype.ObjectToString = Object.prototype.toString;
Leon Clarkee46be812010-01-19 14:06:41 +0000117
118RunTests();
119
120// Run test after properties have been set to a different value.
121String.prototype.TypeOfThis = 'x';
122Boolean.prototype.TypeOfThis = 'x';
123Number.prototype.TypeOfThis = 'x';
124Boolean.prototype[7] = 'x';
125Number.prototype[7] = 'x';
126
127String.prototype.TypeOfThis = TypeOfThis;
128Boolean.prototype.TypeOfThis = TypeOfThis;
129Number.prototype.TypeOfThis = TypeOfThis;
130Boolean.prototype[7] = TypeOfThis;
131Number.prototype[7] = TypeOfThis;
132
133RunTests();
134
135// Force the prototype into slow case and run the test again.
136delete String.prototype.TypeOfThis;
137delete Boolean.prototype.TypeOfThis;
138delete Number.prototype.TypeOfThis;
139Boolean.prototype[7];
140Number.prototype[7];
141
142String.prototype.TypeOfThis = TypeOfThis;
143Boolean.prototype.TypeOfThis = TypeOfThis;
144Number.prototype.TypeOfThis = TypeOfThis;
145Boolean.prototype[7] = TypeOfThis;
146Number.prototype[7] = TypeOfThis;
147
148RunTests();
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.
152assertEquals('object', TypeOfThis.apply('xxx', []));
153assertEquals('object', TypeOfThis.apply(true, []));
154assertEquals('object', TypeOfThis.apply(false, []));
155assertEquals('object', TypeOfThis.apply(42, []));
156assertEquals('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.
160assertEquals('object', TypeOfThis.call('xxx'));
161assertEquals('object', TypeOfThis.call(true));
162assertEquals('object', TypeOfThis.call(false));
163assertEquals('object', TypeOfThis.call(42));
164assertEquals('object', TypeOfThis.call(3.14));