blob: 33ef013caf3ba83d91a36ed40b2c34a08e45d797 [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
31function RunTests() {
32 for (var i = 0; i < 10; i++) {
33 assertEquals('object', 'xxx'.TypeOfThis());
34 assertEquals('object', true.TypeOfThis(2,3));
35 assertEquals('object', false.TypeOfThis());
36 assertEquals('object', (42).TypeOfThis());
37 assertEquals('object', (3.14).TypeOfThis());
38 }
39
40 for (var i = 0; i < 10; i++) {
41 assertEquals('object', 'xxx'['TypeOfThis']());
42 assertEquals('object', true['TypeOfThis']());
43 assertEquals('object', false['TypeOfThis']());
44 assertEquals('object', (42)['TypeOfThis']());
45 assertEquals('object', (3.14)['TypeOfThis']());
46 }
47
48 function CallTypeOfThis(obj) {
49 assertEquals('object', obj.TypeOfThis());
50 }
51
52 for (var i = 0; i < 10; i++) {
53 CallTypeOfThis('xxx');
54 CallTypeOfThis(true);
55 CallTypeOfThis(false);
56 CallTypeOfThis(42);
57 CallTypeOfThis(3.14);
58 }
59
60 function TestWithWith(obj) {
61 with (obj) {
62 for (var i = 0; i < 10; i++) {
63 assertEquals('object', TypeOfThis());
64 }
65 }
66 }
67
68 TestWithWith('xxx');
69 TestWithWith(true);
70 TestWithWith(false);
71 TestWithWith(42);
72 TestWithWith(3.14);
73
74 for (var i = 0; i < 10; i++) {
75 assertEquals('object', true[7]());
76 assertEquals('object', false[7]());
77 assertEquals('object', (42)[7]());
78 assertEquals('object', (3.14)[7]());
79 }
80}
81
82function TypeOfThis() { return typeof this; }
83
84// Test with normal setup of prototype.
85String.prototype.TypeOfThis = TypeOfThis;
86Boolean.prototype.TypeOfThis = TypeOfThis;
87Number.prototype.TypeOfThis = TypeOfThis;
88Boolean.prototype[7] = TypeOfThis;
89Number.prototype[7] = TypeOfThis;
Leon Clarkeeab96aa2010-01-27 16:31:12 +000090
Leon Clarkee46be812010-01-19 14:06:41 +000091
92RunTests();
93
94// Run test after properties have been set to a different value.
95String.prototype.TypeOfThis = 'x';
96Boolean.prototype.TypeOfThis = 'x';
97Number.prototype.TypeOfThis = 'x';
98Boolean.prototype[7] = 'x';
99Number.prototype[7] = 'x';
100
101String.prototype.TypeOfThis = TypeOfThis;
102Boolean.prototype.TypeOfThis = TypeOfThis;
103Number.prototype.TypeOfThis = TypeOfThis;
104Boolean.prototype[7] = TypeOfThis;
105Number.prototype[7] = TypeOfThis;
106
107RunTests();
108
109// Force the prototype into slow case and run the test again.
110delete String.prototype.TypeOfThis;
111delete Boolean.prototype.TypeOfThis;
112delete Number.prototype.TypeOfThis;
113Boolean.prototype[7];
114Number.prototype[7];
115
116String.prototype.TypeOfThis = TypeOfThis;
117Boolean.prototype.TypeOfThis = TypeOfThis;
118Number.prototype.TypeOfThis = TypeOfThis;
119Boolean.prototype[7] = TypeOfThis;
120Number.prototype[7] = TypeOfThis;
121
122RunTests();
123
124// According to ES3 15.3.4.3 the this value passed to Function.prototyle.apply
125// should wrapped. According to ES5 it should not.
126assertEquals('object', TypeOfThis.apply('xxx', []));
127assertEquals('object', TypeOfThis.apply(true, []));
128assertEquals('object', TypeOfThis.apply(false, []));
129assertEquals('object', TypeOfThis.apply(42, []));
130assertEquals('object', TypeOfThis.apply(3.14, []));
131
132// According to ES3 15.3.4.3 the this value passed to Function.prototyle.call
133// should wrapped. According to ES5 it should not.
134assertEquals('object', TypeOfThis.call('xxx'));
135assertEquals('object', TypeOfThis.call(true));
136assertEquals('object', TypeOfThis.call(false));
137assertEquals('object', TypeOfThis.call(42));
138assertEquals('object', TypeOfThis.call(3.14));