tor-browser

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

bind-function-specialized.js (2251B)


      1 load(libdir + "asserts.js");
      2 
      3 function testBasic() {
      4    var g = function foo(a, b, c) { return a - b - c; };
      5    for (var i = 0; i < 100; i++) {
      6        var bound1 = g.bind(null, 1);
      7        assertEq(bound1.length, 2);
      8        assertEq(bound1.name, "bound foo");
      9        var bound2 = bound1.bind(null, 2);
     10        assertEq(bound2.length, 1);
     11        assertEq(bound2.name, "bound bound foo");
     12        assertEq(bound2(9), -10);
     13    }
     14 }
     15 testBasic();
     16 
     17 function testBindNonCtor() {
     18    var g = (a, b, c) => a - b - c;
     19    for (var i = 0; i < 100; i++) {
     20        var bound1 = g.bind(null, 1);
     21        var bound2 = bound1.bind(null, 2);
     22        assertEq(bound1(2, 3), -4);
     23        assertEq(bound2(4), -5);
     24        assertThrowsInstanceOf(() => new bound1(2, 3), TypeError);
     25        assertThrowsInstanceOf(() => new bound2(4), TypeError);
     26        assertEq(bound2.length, 1);
     27        assertEq(bound2.name, "bound bound g");
     28    }
     29 }
     30 testBindNonCtor();
     31 
     32 function testBindSelfHosted() {
     33    var g = Array.prototype.map;
     34    var arr = [1, 2, 3];
     35    for (var i = 0; i < 100; i++) {
     36        var bound1 = g.bind(arr);
     37        var bound2 = bound1.bind(null, x => x + 5);
     38        assertEq(bound1(x => x + 3).toString(), "4,5,6");
     39        assertEq(bound2().toString(), "6,7,8");
     40        assertEq(bound2.length, 0);
     41        assertEq(bound2.name, "bound bound map");
     42    }
     43 }
     44 testBindSelfHosted();
     45 
     46 function testBoundDeletedName() {
     47    var g = function foo(a, b, c) { return a - b - c; };
     48    var bound1 = g.bind(null);
     49    var bound2 = g.bind(null);
     50    delete bound2.name;
     51    for (var i = 0; i < 100; i++) {
     52        var obj = i < 50 ? bound1 : bound2;
     53        var bound3 = obj.bind(null);
     54        assertEq(bound3.length, 3);
     55        assertEq(bound3.name, i < 50 ? "bound bound foo" : "bound ");
     56    }
     57 }
     58 testBoundDeletedName();
     59 
     60 function testWeirdProto() {
     61    var g = function foo() { return 123; };
     62    var proto = {bind: Function.prototype.bind};
     63    Object.setPrototypeOf(g, proto);
     64    for (var i = 0; i < 100; i++) {
     65        var bound1 = g.bind(null);
     66        assertEq(Object.getPrototypeOf(bound1), proto);
     67        var bound2 = bound1.bind(null);
     68        assertEq(Object.getPrototypeOf(bound2), proto);
     69        assertEq(bound2(), 123);
     70    }
     71 }
     72 testWeirdProto();