block-let-crankshaft-sloppy.js 8.71 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
// Copyright 2011 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

28
// Flags: --allow-natives-syntax --opt
29 30 31 32 33 34 35 36

// Check that the following functions are optimizable.
var functions = [ f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12, f13, f14,
                  f15, f16, f17, f18, f19, f20, f21, f22, f23, f24, f25, f26,
                  f27, f28, f29, f30, f31, f32, f33];

for (var i = 0; i < functions.length; ++i) {
  var func = functions[i];
37
  %PrepareFunctionForOptimization(func);
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
  print("Testing:");
  print(func);
  for (var j = 0; j < 10; ++j) {
    func(12);
  }
  %OptimizeFunctionOnNextCall(func);
  func(12);
  assertOptimized(func);
}

function f1() { }

function f2(x) { }

function f3() {
  let x;
}

function f4() {
  function foo() {
  }
}

function f5() {
  let x = 1;
}

function f6() {
  const x = 1;
}

function f7(x) {
  return x;
}

function f8() {
  let x;
  return x;
}

function f9() {
  function x() {
  }
  return x;
}

function f10(x) {
  x = 1;
}

function f11() {
  let x;
  x = 1;
}

function f12() {
  function x() {};
  x = 1;
}

function f13(x) {
  (function() { x; });
}

function f14() {
  let x;
  (function() { x; });
}

function f15() {
  function x() {
  }
  (function() { x; });
}

function f16() {
  let x = 1;
  (function() { x; });
}

function f17() {
  const x = 1;
  (function() { x; });
}

function f18(x) {
  return x;
  (function() { x; });
}

function f19() {
  let x;
  return x;
  (function() { x; });
}

function f20() {
  function x() {
  }
  return x;
  (function() { x; });
}

function f21(x) {
  x = 1;
  (function() { x; });
}

function f22() {
  let x;
  x = 1;
  (function() { x; });
}

function f23() {
  function x() { }
  x = 1;
  (function() { x; });
}

function f24() {
  let x = 1;
  {
    let x = 2;
    {
      let x = 3;
      assertEquals(3, x);
    }
    assertEquals(2, x);
  }
  assertEquals(1, x);
}

function f25() {
  {
    let x = 2;
    L: {
      let x = 3;
      assertEquals(3, x);
      break L;
      assertTrue(false);
    }
    assertEquals(2, x);
  }
  assertTrue(true);
}

function f26() {
  {
    let x = 1;
    L: {
      let x = 2;
      {
        let x = 3;
        assertEquals(3, x);
        break L;
        assertTrue(false);
      }
      assertTrue(false);
    }
    assertEquals(1, x);
  }
}


function f27() {
  do {
    let x = 4;
    assertEquals(4,x);
    {
      let x = 5;
      assertEquals(5, x);
      continue;
      assertTrue(false);
    }
  } while (false);
}

function f28() {
  label: for (var i = 0; i < 10; ++i) {
    let x = 'middle' + i;
    for (var j = 0; j < 10; ++j) {
      let x = 'inner' + j;
      continue label;
    }
  }
}

function f29() {
  // Verify that the context is correctly set in the stack frame after exiting
  // from with.

  let x = 'outer';
  label: {
    let x = 'inner';
    break label;
  }
  f();  // The context could be restored from the stack after the call.
  assertEquals('outer', x);

  function f() {
    assertEquals('outer', x);
  };
}

function f30() {
  let x = 'outer';
  for (var i = 0; i < 10; ++i) {
    let x = 'inner';
    continue;
  }
  f();
  assertEquals('outer', x);

  function f() {
    assertEquals('outer', x);
  };
}

function f31() {
  {
    let x = 'outer';
    label: for (var i = 0; assertEquals('outer', x), i < 10; ++i) {
      let x = 'middle' + i;
      {
        let x = 'inner' + j;
        continue label;
      }
    }
    assertEquals('outer', x);
  }
}

var c = true;

function f32() {
  {
    let x = 'outer';
    L: {
      {
        let x = 'inner';
        if (c) {
          break L;
        }
      }
      foo();
    }
  }

  function foo() {
    return 'bar';
  }
}

function f33() {
  {
    let x = 'outer';
    L: {
      {
        let x = 'inner';
        if (c) {
          break L;
        }
        foo();
      }
    }
  }

  function foo() {
    return 'bar';
  }
}

function TestThrow() {
  function f() {
    let x = 'outer';
    {
      let x = 'inner';
      throw x;
    }
  }
319
  %PrepareFunctionForOptimization(f);
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
  for (var i = 0; i < 5; i++) {
    try {
      f();
    } catch (e) {
      assertEquals('inner', e);
    }
  }
  %OptimizeFunctionOnNextCall(f);
  try {
    f();
  } catch (e) {
    assertEquals('inner', e);
  }
  assertOptimized(f);
}

TestThrow();

// Test that temporal dead zone semantics for function and block scoped
// let bindings are handled by the optimizing compiler.

function TestFunctionLocal(s) {
  'use strict';
  var func = eval("(function baz(){" + s + "; })");
344
  %PrepareFunctionForOptimization(func);
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
  print("Testing:");
  print(func);
  for (var i = 0; i < 5; ++i) {
    try {
      func();
      assertUnreachable();
    } catch (e) {
      assertInstanceof(e, ReferenceError);
    }
  }
  %OptimizeFunctionOnNextCall(func);
  try {
    func();
    assertUnreachable();
  } catch (e) {
    assertInstanceof(e, ReferenceError);
  }
}

function TestFunctionContext(s) {
  'use strict';
  var func = eval("(function baz(){ " + s + "; (function() { x; }); })");
367
  %PrepareFunctionForOptimization(func);
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
  print("Testing:");
  print(func);
  for (var i = 0; i < 5; ++i) {
    print(i);
    try {
      func();
      assertUnreachable();
    } catch (e) {
      assertInstanceof(e, ReferenceError);
    }
  }
  print("optimize");
  %OptimizeFunctionOnNextCall(func);
  try {
    print("call");
    func();
    assertUnreachable();
  } catch (e) {
    print("catch");
    assertInstanceof(e, ReferenceError);
  }
}

function TestBlockLocal(s) {
  'use strict';
  var func = eval("(function baz(){ { " + s + "; } })");
394
  %PrepareFunctionForOptimization(func);
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
  print("Testing:");
  print(func);
  for (var i = 0; i < 5; ++i) {
    try {
      func();
      assertUnreachable();
    } catch (e) {
      assertInstanceof(e, ReferenceError);
    }
  }
  %OptimizeFunctionOnNextCall(func);
  try {
    func();
    assertUnreachable();
  } catch (e) {
    assertInstanceof(e, ReferenceError);
  }
}

function TestBlockContext(s) {
  'use strict';
  var func = eval("(function baz(){ { " + s + "; (function() { x; }); } })");
417
  %PrepareFunctionForOptimization(func);
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
  print("Testing:");
  print(func);
  for (var i = 0; i < 5; ++i) {
    print(i);
    try {
      func();
      assertUnreachable();
    } catch (e) {
      assertInstanceof(e, ReferenceError);
    }
  }
  print("optimize");
  %OptimizeFunctionOnNextCall(func);
  try {
    print("call");
    func();
    assertUnreachable();
  } catch (e) {
    print("catch");
    assertInstanceof(e, ReferenceError);
  }
}

function TestAll(s) {
  TestFunctionLocal(s);
  TestFunctionContext(s);
  TestBlockLocal(s);
  TestBlockContext(s);
}

// Use before initialization in declaration statement.
TestAll('let x = x + 1');
TestAll('let x = x += 1');
TestAll('let x = x++');
TestAll('let x = ++x');
TestAll('const x = x + 1');

// Use before initialization in prior statement.
TestAll('x + 1; let x;');
TestAll('x = 1; let x;');
TestAll('x += 1; let x;');
TestAll('++x; let x;');
TestAll('x++; let x;');
TestAll('let y = x; const x = 1;');


function f(x) {
  let y = x + 42;
  return y;
}

function g(x) {
  {
    let y = x + 42;
    return y;
  }
}

476 477 478
%PrepareFunctionForOptimization(f);
%PrepareFunctionForOptimization(g);

479 480 481 482 483 484 485 486 487 488 489
for (var i=0; i<10; i++) {
  f(i);
  g(i);
}

%OptimizeFunctionOnNextCall(f);
%OptimizeFunctionOnNextCall(g);

f(12);
g(12);

490 491
assertOptimized(f);
assertOptimized(g);