tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

function-load-name.js (2669B)


      1 // Test transpiling of LoadFunctionNameResult and cover possible bailout conditions.
      2 
      3 function empty() {}
      4 
      5 // Note: Typically won't use LoadFunctionNameResult, because the "name"
      6 // property will be resolved on the first access.
      7 function testGlobalFunction() {
      8  for (var i = 0; i < 200; ++i) {
      9    assertEq(empty.name, "empty");
     10  }
     11 }
     12 testGlobalFunction();
     13 
     14 // Note: Typically won't use LoadFunctionNameResult, because the "name"
     15 // property will be resolved on the first access.
     16 function testInnerFunction() {
     17  function f() {}
     18  for (var i = 0; i < 200; ++i) {
     19    assertEq(f.name, "f");
     20  }
     21 }
     22 testInnerFunction();
     23 
     24 function testPerLoopFunction() {
     25  for (var i = 0; i < 200; ++i) {
     26    assertEq(function f(){}.name, "f");
     27  }
     28 }
     29 testPerLoopFunction();
     30 
     31 // Note: Typically won't use LoadFunctionNameResult, because the "name"
     32 // property will be resolved on the first access.
     33 function testNativeFunction() {
     34  for (var i = 0; i < 200; ++i) {
     35    assertEq(Math.max.name, "max");
     36  }
     37 }
     38 testNativeFunction();
     39 
     40 // Note: Typically won't use LoadFunctionNameResult, because the "name"
     41 // property will be resolved on the first access.
     42 function testSelfHostedFunction() {
     43  for (var i = 0; i < 200; ++i) {
     44    assertEq(Array.prototype.forEach.name, "forEach");
     45  }
     46 }
     47 testSelfHostedFunction();
     48 
     49 // Bailout when the name property is resolved.
     50 function testBailoutResolvedName() {
     51  function f1() {}
     52 
     53  // Ensure the name property of |f1| is resolved.
     54  assertEq(f1.name, "f1");
     55 
     56  var names = ["f", "f1"];
     57 
     58  for (var i = 0; i < 10; ++i) {
     59    var name = names[0 + (i >= 5)];
     60 
     61    for (var j = 0; j < 100; ++j) {
     62      var values = [function f(){}, f1];
     63      var value = values[0 + (i >= 5)];
     64 
     65      assertEq(value.name, name);
     66    }
     67  }
     68 }
     69 testBailoutResolvedName();
     70 
     71 // Bailout when the HAS_BOUND_FUNCTION_NAME_PREFIX isn't set.
     72 function testBailoutBoundName() {
     73  function f1() {}
     74  function f2() {}
     75 
     76  var bound = f1.bind();
     77 
     78  // Ensure the name property of |bound| is resolved. That way new functions
     79  // created through |bound().bind()| will have the HAS_BOUND_FUNCTION_NAME_PREFIX
     80  // flag set.
     81  assertEq(bound.name, "bound f1");
     82 
     83  // |bound1| and |bound2| have the same shape, but different function flags.
     84  var bound1 = bound.bind(); // HAS_BOUND_FUNCTION_NAME_PREFIX
     85  var bound2 = f2.bind(); // ! HAS_BOUND_FUNCTION_NAME_PREFIX
     86 
     87  var values = [bound1, bound2];
     88  var names = ["bound bound bound f1", "bound bound f2"];
     89 
     90  for (var i = 0; i < 10; ++i) {
     91    var value = values[0 + (i >= 5)];
     92    var name = names[0 + (i >= 5)];
     93 
     94    for (var j = 0; j < 100; ++j) {
     95      var f = value.bind();
     96      assertEq(f.name, name);
     97    }
     98  }
     99 }
    100 testBailoutBoundName();