1 // Copyright 2011 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 // Flags: --allow-natives-syntax
32 function get_y() { return this.y; }
33 function strict_get_y() { "use strict"; return this.y; }
35 // Test calls to strict mode function as methods.
36 for (var i = 0; i < 10; i++) assertEquals(3, strict_get_y.call(this));
38 for (var i = 0; i < 10; i++) assertEquals(42, strict_get_y.call(o));
40 // Test calls to strict mode function with implicit receiver.
42 var exception = false;
43 try { strict_get_y(); } catch(e) { exception = true; }
44 assertTrue(exception);
46 for (var i = 0; i < 3; i++) g();
48 // Test calls to local strict mode function with implicit receiver.
49 function local_function_test() {
50 function get_y() { return this.y; }
51 function strict_get_y() { "use strict"; return this.y; }
52 assertEquals(3, get_y());
53 assertEquals(3, get_y(23));
54 var exception = false;
60 assertTrue(exception);
63 for (var i = 0; i < 10; i++) {
64 local_function_test();
67 // Test call to catch variable strict-mode function. Implicit
69 var exception = false;
78 assertTrue(exception);
82 // Test calls to strict-mode function with the object from a with
83 // statement as the receiver.
85 assertEquals(3, strict_get_y());
86 assertEquals(3, strict_get_y(32));
92 assertEquals(27, f());
93 assertEquals(27, f(23));
97 // Check calls to eval within a function with 'undefined' as receiver.
98 function implicit_receiver_eval() {
103 assertEquals(void 0, implicit_receiver_eval());
104 assertEquals(void 0, implicit_receiver_eval(32));
107 // Strict mode function to get inlined.
108 function strict_return_receiver() {
113 // Inline with implicit receiver.
115 return strict_return_receiver();
118 for (var i = 0; i < 5; i++) {
119 assertEquals(void 0, g());
120 assertEquals(void 0, g(42));
122 %OptimizeFunctionOnNextCall(g);
123 assertEquals(void 0, g(42));
124 assertEquals(void 0, g());
126 // Inline with explicit receiver.
129 o.f = strict_return_receiver;
133 for (var i = 0; i < 5; i++) {
134 assertTrue(typeof g2() == "object");
135 assertTrue(typeof g2(42) == "object");
137 %OptimizeFunctionOnNextCall(g2);
138 assertTrue(typeof g2() == "object");
139 assertTrue(typeof g2(42) == "object");
141 // Test calls of aliased eval.
142 function outer_eval_receiver() {
143 var eval = function() { return this; }
144 function inner_strict() {
146 assertEquals('object', typeof eval());
150 outer_eval_receiver();
152 function outer_eval_conversion3(eval, expected) {
153 function inner_strict() {
155 var x = eval("this");
156 assertEquals(expected, typeof x);
161 function strict_return_this() { "use strict"; return this; }
162 function return_this() { return this; }
163 function strict_eval(s) { "use strict"; return eval(s); }
164 function non_strict_eval(s) { return eval(s); }
166 outer_eval_conversion3(strict_return_this, 'undefined');
167 outer_eval_conversion3(return_this, 'object');
168 outer_eval_conversion3(strict_eval, 'undefined');
169 outer_eval_conversion3(non_strict_eval, 'object');
171 outer_eval_conversion3(eval, 'undefined');
173 function test_constant_function() {
174 var o = { f: function() { "use strict"; return this; } };
176 for (var i = 0; i < 10; i++) assertEquals(void 0, f());
178 test_constant_function();
180 function test_field() {
183 o.f = function() { "use strict"; return this; };
185 for (var i = 0; i < 10; i++) assertEquals(void 0, f());