skipping-inner-functions.js 9.19 KB
Newer Older
1 2 3 4
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
// Flags: --preparser-scope-analysis --enable-slow-asserts
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

(function TestBasicSkipping() {
  var result = 0;

  function lazy(ctxt_alloc_param) {
    var ctxt_alloc_var = 10;
    function skip_me() {
      result = ctxt_alloc_param + ctxt_alloc_var;
    }
    return skip_me;
  }
  // Test that parameters and variables of the outer function get context
  // allocated even if we skip the inner function.
  lazy(9)();
  assertEquals(19, result);
})();

(function TestSkippingFunctionWithEval() {
  var result = 0;

  function lazy(ctxt_alloc_param) {
    var ctxt_alloc_var = 10;
    function skip_me() {
      eval('result = ctxt_alloc_param + ctxt_alloc_var');
    }
    return skip_me;
  }
  // Test that parameters and variables of the outer function get context
  // allocated even if we skip the inner function.
  lazy(9)();
  assertEquals(19, result);
})();
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

(function TestCtxtAllocatingNonSimpleParams1() {
  var result = 0;

  function lazy([other_param1, ctxt_alloc_param, other_param2]) {
    function skip_me() {
      result = ctxt_alloc_param;
    }
    return skip_me;
  }
  // Test that parameters and variables of the outer function get context
  // allocated even if we skip the inner function.
  lazy([30, 29, 28])();
  assertEquals(29, result);
})();

(function TestCtxtAllocatingNonSimpleParams2() {
  var result = 0;

  function lazy({a: other_param1, b: ctxt_alloc_param, c: other_param2}) {
    function skip_me() {
      result = ctxt_alloc_param;
    }
    return skip_me;
  }
  // Test that parameters and variables of the outer function get context
  // allocated even if we skip the inner function.
  lazy({a: 31, b: 32, c: 33})();
  assertEquals(32, result);
})();

(function TestCtxtAllocatingNonSimpleParams3() {
  var result = 0;

  function lazy(...ctxt_alloc_param) {
    function skip_me() {
      result = ctxt_alloc_param;
    }
    return skip_me;
  }
  // Test that parameters and variables of the outer function get context
  // allocated even if we skip the inner function.
  lazy(34, 35)();
  assertEquals([34, 35], result);
})();

// Skippable top level functions.
85
var result = 0;
86 87 88 89 90 91 92 93 94 95
function lazy_top_level(ctxt_alloc_param) {
  let ctxt_alloc_var = 24;
  function skip_me() {
    result = ctxt_alloc_param + ctxt_alloc_var;
  }
  skip_me();
}

lazy_top_level(10);
assertEquals(34, result);
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115

// Tests for using a function name in an inner function.
var TestUsingNamedExpressionName1 = function this_is_the_name() {
  function inner() {
    this_is_the_name;
  }
  inner();
}
TestUsingNamedExpressionName1();

function TestUsingNamedExpressionName2() {
  let f = function this_is_the_name() {
    function inner() {
      this_is_the_name;
    }
    inner();
  }
  f();
}
TestUsingNamedExpressionName2();
116 117 118 119 120 121 122 123 124

function TestSkippedFunctionInsideLoopInitializer() {
  let saved_func;
  for (let i = 0, f = function() { return i }; i < 1; ++i) {
    saved_func = f;
  }
  assertEquals(0, saved_func());
}
TestSkippedFunctionInsideLoopInitializer();
125 126 127 128 129 130 131 132 133 134 135 136 137 138

(function TestSkippedFunctionWithParameters() {
  var result = 0;

  function lazy(ctxt_alloc_param) {
    var ctxt_alloc_var = 10;
    function skip_me(param1, param2) {
      result = ctxt_alloc_param + ctxt_alloc_var + param1 + param2;
    }
    return skip_me;
  }
  lazy(9)(8, 7);
  assertEquals(34, result);
})();
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

function TestSkippingDeeperLazyFunctions() {
  let result = 0;
  function inner_lazy(ctxt_alloc_param) {
    let ctxt_alloc_var = 13;
    function skip_me() {
      result = ctxt_alloc_param + ctxt_alloc_var;
    }
    return skip_me;
  }
  let f = inner_lazy(12);
  f();
  assertEquals(25, result);
}

TestSkippingDeeperLazyFunctions();

function TestEagerFunctionsBetweenLazyFunctions() {
  let result = 0;
  // We produce one data set for TestEagerFunctionsBetweenLazyFunctions and
  // another one for inner. The variable data for eager belongs to the former
  // data set.
  let ctxt_allocated1 = 3;
  (function eager() {
    let ctxt_allocated2 = 4;
    function inner() {
      result = ctxt_allocated1 + ctxt_allocated2;
    }
    return inner;
  })()();
  assertEquals(7, result);
}

TestEagerFunctionsBetweenLazyFunctions();

function TestEagerNotIifeFunctionsBetweenLazyFunctions() {
  let result = 0;
  // We produce one data set for TestEagerFunctionsBetweenLazyFunctions and
  // another one for inner. The variable data for eager belongs to the former
  // data set.
  let ctxt_allocated1 = 3;
  (function eager_not_iife() {
    let ctxt_allocated2 = 4;
    function inner() {
      result = ctxt_allocated1 + ctxt_allocated2;
    }
    return inner;
  }); // Function not called; not an iife.
  // This is just a regression test. We cannot test that the context allocation
  // was done correctly (since there's no way to call eager_not_iife), but code
  // like this used to trigger some DCHECKs.
}

TestEagerNotIifeFunctionsBetweenLazyFunctions();

// Regression test for functions inside a lazy arrow function. (Only top-level
// arrow functions are lazy, so this cannot be wrapped in a function.)
result = 0;
let f1 = (ctxt_alloc_param) => {
  let ctxt_alloc_var = 10;
  function inner() {
    result = ctxt_alloc_param + ctxt_alloc_var;
  }
  return inner;
}
f1(9)();
assertEquals(19, result);
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

function TestStrictEvalInParams() {
  "use strict";
  var result = 0;

  function lazy(a = function() { return 2; }, b = eval('3')) {
    function skip_me() {
      result = a() + b;
    }
    return skip_me;
  }
  lazy()();
  assertEquals(5, result);

  function not_skippable_either() {}
}

TestStrictEvalInParams();

function TestSloppyEvalInFunctionWithComplexParams() {
  var result = 0;

  function lazy1(ctxt_alloc_param = 2) {
    var ctxt_alloc_var = 3;
    function skip_me() {
      result = ctxt_alloc_param + ctxt_alloc_var;
    }
    eval('');
    return skip_me;
  }
  lazy1()();
  assertEquals(5, result);

  function lazy2(ctxt_alloc_param = 4) {
    var ctxt_alloc_var = 5;
    function skip_me() {
      eval('result = ctxt_alloc_param + ctxt_alloc_var;');
    }
    return skip_me;
  }
  lazy2()();
  assertEquals(9, result);
}

TestSloppyEvalInFunctionWithComplexParams();
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

function TestSkippableFunctionInForOfHeader() {
  var c;
  function inner() {
    for (let [a, b = c = function() { return a; }] of [[10]]) {
    }
  }
  inner();
  var result = c();
  assertEquals(10, result);
}

TestSkippableFunctionInForOfHeader();

function TestSkippableFunctionInForOfBody() {
  var c;
  function inner() {
    for (let [a, b] of [[10, 11]]) {
      c = function f() {
        return a + b;
      }
    }
  }
  inner();
  var result = c();
  assertEquals(21, result);
}

TestSkippableFunctionInForOfBody();


function TestSkippableFunctionInForOfHeaderAndBody() {
  var c1;
  var c2;
  function inner() {
    for (let [a, b = c1 = function() { return a; }] of [[10]]) {
      c2 = function f() {
        return a + 1;
      }
    }
  }
  inner();
  var result = c1() + c2();
  assertEquals(21, result);
}

TestSkippableFunctionInForOfHeaderAndBody();
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316

(function TestSkippableGeneratorInSloppyBlock() {
  var result = 0;

  function lazy(ctxt_alloc_param) {
    var ctxt_alloc_var = 10;
    {
      function *skip_me() {
        result = ctxt_alloc_param + ctxt_alloc_var;
        yield 3;
      }
      return skip_me;
    }
  }
  // Test that parameters and variables of the outer function get context
  // allocated even if we skip the inner function.
  assertEquals(3, lazy(9)().next().value);
  assertEquals(19, result);
})();
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340

(function TestRestoringDataToAsyncArrowFunctionWithNonSimpleParams_1() {
  // Regression test for
  // https://bugs.chromium.org/p/chromium/issues/detail?id=765532
  function lazy() {
    // The arrow function is not skippable, but we need to traverse its scopes
    // and restore data to them.
    async(a=0) => { const d = 0; }
    function skippable() {}
  }
  lazy();
})();

(function TestRestoringDataToAsyncArrowFunctionWithNonSimpleParams_2() {
  // Regression test for
  // https://bugs.chromium.org/p/chromium/issues/detail?id=765532
  function lazy() {
    // The arrow function is not skippable, but we need to traverse its scopes
    // and restore data to them.
    async(...a) => { const d = 0; }
    function skippable() {}
  }
  lazy();
})();
341 342 343 344 345 346 347 348 349 350 351 352 353 354

(function TestSloppyBlockFunctionShadowingCatchVariable() {
  // Regression test for
  // https://bugs.chromium.org/p/chromium/issues/detail?id=771474
  function lazy() {
    try {
    } catch (my_var) {
      if (false) {
        function my_var() { }
      }
    }
  }
  lazy();
})();
355 356 357 358 359 360 361 362 363 364 365 366 367


(function TestLazinessDecisionWithDefaultConstructors() {
  // Regression test for
  // https://bugs.chromium.org/p/chromium/issues/detail?id=773576

  // The problem was that Parser and PreParser treated default constructors
  // differently, and that threw off the "next / previous function is likely
  // called" logic.

  function lazy(p = (function() {}, class {}, function() {}, class { method1() { } })) { }
  lazy();
})();
368 369 370 371 372 373 374 375 376 377 378 379

(function TestOneByteTwoByteMismatch() {
  // Regression test for
  // https://bugs.chromium.org/p/v8/issues/detail?id=7428

  let name = 'weird_string\u2653'.slice(0, 12);
  let o = {};
  o[name] = null;
  var x;
  eval('x = function weird_string() { function skip() {} };');
  x();
})();