Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / v8 / test / mjsunit / value-wrapper.js
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
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
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
33 // IC code is tested.
34
35 function 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   }
43
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']());
50   }
51
52   function CallTypeOfThis(obj) {
53     assertEquals('object', obj.TypeOfThis());
54   }
55
56   for (var i = 0; i < 10; i++) {
57     CallTypeOfThis('xxx');
58     CallTypeOfThis(true);
59     CallTypeOfThis(false);
60     CallTypeOfThis(42);
61     CallTypeOfThis(3.14);
62   }
63
64   function TestWithWith(obj) {
65     with (obj) {
66       for (var i = 0; i < 10; i++) {
67         assertEquals('object', TypeOfThis());
68       }
69     }
70   }
71
72   TestWithWith('xxx');
73   TestWithWith(true);
74   TestWithWith(false);
75   TestWithWith(42);
76   TestWithWith(3.14);
77
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]());
83   }
84
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   }
100 }
101
102 function TypeOfThis() { return typeof this; }
103
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;
110
111 String.prototype.ObjectValueOf = Object.prototype.valueOf;
112 Boolean.prototype.ObjectValueOf = Object.prototype.valueOf;
113 Number.prototype.ObjectValueOf = Object.prototype.valueOf;
114
115 String.prototype.ObjectToString = Object.prototype.toString;
116 Boolean.prototype.ObjectToString = Object.prototype.toString;
117 Number.prototype.ObjectToString = Object.prototype.toString;
118
119 RunTests();
120
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';
127
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;
133
134 RunTests();
135
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];
141 Number.prototype[7];
142
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;
148
149 RunTests();
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.
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, []));
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.
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));