blob: cc81f9555931c63f19538e45854292a0ffc592a6 [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.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000034
Leon Clarkee46be812010-01-19 14:06:41 +000035function RunTests() {
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());
42 }
Ben Murdoch589d6972011-11-30 16:04:58 +000043
Leon Clarkee46be812010-01-19 14:06:41 +000044 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']());
50 }
Ben Murdoch589d6972011-11-30 16:04:58 +000051
Leon Clarkee46be812010-01-19 14:06:41 +000052 function CallTypeOfThis(obj) {
53 assertEquals('object', obj.TypeOfThis());
54 }
Ben Murdoch589d6972011-11-30 16:04:58 +000055
Leon Clarkee46be812010-01-19 14:06:41 +000056 for (var i = 0; i < 10; i++) {
57 CallTypeOfThis('xxx');
58 CallTypeOfThis(true);
59 CallTypeOfThis(false);
60 CallTypeOfThis(42);
61 CallTypeOfThis(3.14);
62 }
Ben Murdoch589d6972011-11-30 16:04:58 +000063
Leon Clarkee46be812010-01-19 14:06:41 +000064 function TestWithWith(obj) {
65 with (obj) {
66 for (var i = 0; i < 10; i++) {
67 assertEquals('object', TypeOfThis());
68 }
69 }
70 }
Ben Murdoch589d6972011-11-30 16:04:58 +000071
Leon Clarkee46be812010-01-19 14:06:41 +000072 TestWithWith('xxx');
73 TestWithWith(true);
74 TestWithWith(false);
75 TestWithWith(42);
76 TestWithWith(3.14);
Ben Murdoch589d6972011-11-30 16:04:58 +000077
Leon Clarkee46be812010-01-19 14:06:41 +000078 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]());
83 }
Leon Clarked91b9f72010-01-27 17:25:45 +000084
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());
91 }
92
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());
99 }
Leon Clarkee46be812010-01-19 14:06:41 +0000100}
101
102function TypeOfThis() { return typeof this; }
103
Ben Murdoch589d6972011-11-30 16:04:58 +0000104// Test with normal setup of prototype.
Leon Clarkee46be812010-01-19 14:06:41 +0000105String.prototype.TypeOfThis = TypeOfThis;
106Boolean.prototype.TypeOfThis = TypeOfThis;
107Number.prototype.TypeOfThis = TypeOfThis;
108Boolean.prototype[7] = TypeOfThis;
109Number.prototype[7] = TypeOfThis;
Leon Clarked91b9f72010-01-27 17:25:45 +0000110
111String.prototype.ObjectValueOf = Object.prototype.valueOf;
112Boolean.prototype.ObjectValueOf = Object.prototype.valueOf;
113Number.prototype.ObjectValueOf = Object.prototype.valueOf;
114
115String.prototype.ObjectToString = Object.prototype.toString;
116Boolean.prototype.ObjectToString = Object.prototype.toString;
117Number.prototype.ObjectToString = Object.prototype.toString;
Leon Clarkee46be812010-01-19 14:06:41 +0000118
119RunTests();
120
121// Run test after properties have been set to a different value.
122String.prototype.TypeOfThis = 'x';
123Boolean.prototype.TypeOfThis = 'x';
124Number.prototype.TypeOfThis = 'x';
125Boolean.prototype[7] = 'x';
126Number.prototype[7] = 'x';
127
128String.prototype.TypeOfThis = TypeOfThis;
129Boolean.prototype.TypeOfThis = TypeOfThis;
130Number.prototype.TypeOfThis = TypeOfThis;
131Boolean.prototype[7] = TypeOfThis;
132Number.prototype[7] = TypeOfThis;
133
134RunTests();
135
136// Force the prototype into slow case and run the test again.
137delete String.prototype.TypeOfThis;
138delete Boolean.prototype.TypeOfThis;
139delete Number.prototype.TypeOfThis;
140Boolean.prototype[7];
141Number.prototype[7];
142
143String.prototype.TypeOfThis = TypeOfThis;
144Boolean.prototype.TypeOfThis = TypeOfThis;
145Number.prototype.TypeOfThis = TypeOfThis;
146Boolean.prototype[7] = TypeOfThis;
147Number.prototype[7] = TypeOfThis;
148
149RunTests();
150
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.
153assertEquals('object', TypeOfThis.apply('xxx', []));
154assertEquals('object', TypeOfThis.apply(true, []));
155assertEquals('object', TypeOfThis.apply(false, []));
156assertEquals('object', TypeOfThis.apply(42, []));
157assertEquals('object', TypeOfThis.apply(3.14, []));
158
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.
161assertEquals('object', TypeOfThis.call('xxx'));
162assertEquals('object', TypeOfThis.call(true));
163assertEquals('object', TypeOfThis.call(false));
164assertEquals('object', TypeOfThis.call(42));
165assertEquals('object', TypeOfThis.call(3.14));