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 // Check that the following functions are optimizable.
33 var functions = [ f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12, f13, f14,
34 f15, f16, f17, f18, f19, f20, f21, f22, f23, f24, f25, f26,
35 f27, f28, f29, f30, f31, f32, f33];
37 for (var i = 0; i < functions.length; ++i) {
38 var func = functions[i];
41 for (var j = 0; j < 10; ++j) {
44 %OptimizeFunctionOnNextCall(func);
46 assertOptimized(func);
218 label: for (var i = 0; i < 10; ++i) {
219 let x = 'middle' + i;
220 for (var j = 0; j < 10; ++j) {
228 // Verify that the context is correctly set in the stack frame after exiting
236 f(); // The context could be restored from the stack after the call.
237 assertEquals('outer', x);
240 assertEquals('outer', x);
246 for (var i = 0; i < 10; ++i) {
251 assertEquals('outer', x);
254 assertEquals('outer', x);
261 label: for (var i = 0; assertEquals('outer', x), i < 10; ++i) {
262 let x = 'middle' + i;
268 assertEquals('outer', x);
312 function TestThrow() {
320 for (var i = 0; i < 5; i++) {
324 assertEquals('inner', e);
327 %OptimizeFunctionOnNextCall(f);
331 assertEquals('inner', e);
338 // Test that temporal dead zone semantics for function and block scoped
339 // let bindings are handled by the optimizing compiler.
341 function TestFunctionLocal(s) {
343 var func = eval("(function baz(){" + s + "; })");
346 for (var i = 0; i < 5; ++i) {
351 assertInstanceof(e, ReferenceError);
354 %OptimizeFunctionOnNextCall(func);
359 assertInstanceof(e, ReferenceError);
363 function TestFunctionContext(s) {
365 var func = eval("(function baz(){ " + s + "; (function() { x; }); })");
368 for (var i = 0; i < 5; ++i) {
374 assertInstanceof(e, ReferenceError);
378 %OptimizeFunctionOnNextCall(func);
385 assertInstanceof(e, ReferenceError);
389 function TestBlockLocal(s) {
391 var func = eval("(function baz(){ { " + s + "; } })");
394 for (var i = 0; i < 5; ++i) {
399 assertInstanceof(e, ReferenceError);
402 %OptimizeFunctionOnNextCall(func);
407 assertInstanceof(e, ReferenceError);
411 function TestBlockContext(s) {
413 var func = eval("(function baz(){ { " + s + "; (function() { x; }); } })");
416 for (var i = 0; i < 5; ++i) {
422 assertInstanceof(e, ReferenceError);
426 %OptimizeFunctionOnNextCall(func);
433 assertInstanceof(e, ReferenceError);
437 function TestAll(s) {
438 TestFunctionLocal(s);
439 TestFunctionContext(s);
444 // Use before initialization in declaration statement.
445 TestAll('let x = x + 1');
446 TestAll('let x = x += 1');
447 TestAll('let x = x++');
448 TestAll('let x = ++x');
449 TestAll('const x = x + 1');
451 // Use before initialization in prior statement.
452 TestAll('x + 1; let x;');
453 TestAll('x = 1; let x;');
454 TestAll('x += 1; let x;');
455 TestAll('++x; let x;');
456 TestAll('x++; let x;');
457 TestAll('let y = x; const x = 1;');
472 for (var i=0; i<10; i++) {
477 %OptimizeFunctionOnNextCall(f);
478 %OptimizeFunctionOnNextCall(g);
483 assertTrue(%GetOptimizationStatus(f) != 2);
484 assertTrue(%GetOptimizationStatus(g) != 2);