tor-browser

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

bound-inlinable-native-1.js (3172B)


      1 // Test inlining bound function to inlinable natives when no additional bound
      2 // arguments are used.
      3 
      4 function testMathMin() {
      5  // Bind the inlinable native |Math.min|.
      6  var MathMin = Math.min.bind();
      7 
      8  for (var i = 0; i < 100; ++i) {
      9    assertEq(MathMin(i, 50), Math.min(i, 50));
     10  }
     11 }
     12 testMathMin();
     13 
     14 function testMathMinMax() {
     15  // Bind the inlinable natives |Math.min| and |Math.max|.
     16  var MathMinMax = [
     17    Math.min.bind(),
     18    Math.max.bind(),
     19  ];
     20 
     21  // Compare against the non-bound |Math.min| and |Math.max|.
     22  var minmax = [
     23    Math.min,
     24    Math.max,
     25  ];
     26 
     27  for (var i = 0; i < 200; ++i) {
     28    assertEq(MathMinMax[i & 1](i, 50), minmax[i & 1](i, 50));
     29  }
     30 }
     31 testMathMinMax();
     32 
     33 function testMathMinBoundAndNonBound() {
     34  // Use bound and non-bound |Math.min|.
     35  var MathMin = [
     36    Math.min.bind(),
     37    Math.min,
     38  ];
     39 
     40  for (var i = 0; i < 200; ++i) {
     41    assertEq(MathMin[i & 1](i, 50), Math.min(i, 50));
     42  }
     43 }
     44 testMathMinBoundAndNonBound();
     45 
     46 function testStringCharCodeAt() {
     47  // Bind the inlinable native |String.prototype.charCodeAt| to
     48  // cover reading the |this| value.
     49  var str = "abcdefgh";
     50  var CharCodeAt = String.prototype.charCodeAt.bind(str);
     51 
     52  for (var i = 0; i < 100; ++i) {
     53    assertEq(CharCodeAt(i & 7), str.charCodeAt(i & 7));
     54  }
     55 }
     56 testStringCharCodeAt();
     57 
     58 function testArrayConstructor() {
     59  // Bind the Array constructor function.
     60  var A = Array.bind();
     61 
     62  for (var i = 0; i < 100; ++i) {
     63    var a = new A(i);
     64    assertEq(a.length, i);
     65    assertEq(Object.getPrototypeOf(a), Array.prototype);
     66  }
     67 }
     68 testArrayConstructor();
     69 
     70 function testArrayConstructorSubclass() {
     71  // Bind the Array constructor function.
     72  var BoundArray = Array.bind();
     73 
     74  // Bound functions don't have a "prototype" property.
     75  assertEq("prototype" in BoundArray, false);
     76 
     77  // Add "prototype" so we can subclass from |BoundArray|.
     78  BoundArray.prototype = Array.prototype;
     79  
     80  // Class whose super-class is a bound function to the Array function.
     81  // This case isn't optimised, because the callee and |new.target| don't match. 
     82  class A extends BoundArray {}
     83 
     84  for (var i = 0; i < 100; ++i) {
     85    var a = new A(i);
     86    assertEq(a.length, i);
     87    assertEq(Object.getPrototypeOf(a), A.prototype);
     88  }
     89 }
     90 testArrayConstructorSubclass();
     91 
     92 function testMathMaxSpread() {
     93  // Bind the inlinable native |Math.max|.
     94  var MathMax = Math.max.bind();
     95 
     96  for (var i = 0; i < 100; ++i) {
     97    var args = [i - 1, i, i + 1];
     98    assertEq(MathMax(...args), i + 1);
     99  }
    100 }
    101 testMathMaxSpread();
    102 
    103 function testFunctionBind() {
    104  // Bound function whose target is |Function.prototype.bind| and whose
    105  // this-value is |Array.prototype.push|.
    106  var FunBind = Function.prototype.bind.bind(Array.prototype.push);
    107 
    108  for (var i = 0; i < 100; ++i) {
    109    var array = [];
    110 
    111    // Create a new bound function. This call is equivalent to calling
    112    // |Array.prototype.push.bind(array).
    113    //
    114    // This call to the bound target |Function.prototype.bind| can be inlined,
    115    // because |FunBind| has no additional bound arguments.
    116    var push = FunBind(array);
    117 
    118    assertEq(array.length, 0);
    119    push(1, 2, 3);
    120    assertEq(array.length, 3);
    121  }
    122 }
    123 testFunctionBind();