tor-browser

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

get-bound-name.js (2297B)


      1 // Ensure JSOp::GetBoundName executes [[Has]], [[Get]], and [[Set]] the correct
      2 // number of times.
      3 
      4 function testInc() {
      5  function outer() {
      6    with (env) {
      7      // The inner function can be Warp-compiled, but has a with-environment
      8      // on its scope chain.
      9      return function() {
     10        // The increment operator is compiled to JSOp::GetBoundName.
     11        return ++prop;
     12      }
     13    }
     14  }
     15 
     16  var count_get = 0;
     17  var count_has = 0;
     18  var count_set = 0;
     19 
     20  function proxify(obj) {
     21    return new Proxy(obj, {
     22      get(t, pk, r) {
     23        count_get++;
     24        return Reflect.get(t, pk, r);
     25      },
     26      has(t, pk) {
     27        count_has++;
     28        return Reflect.has(t, pk);
     29      },
     30      set(t, pk, v, r) {
     31        count_set++;
     32        return Reflect.set(t, pk, v, r);
     33      },
     34    });
     35  }
     36 
     37  var count_unscopables = 0;
     38 
     39  var env = {
     40    get [Symbol.unscopables]() {
     41      count_unscopables++;
     42    },
     43    prop: 0,
     44  };
     45  env = proxify(env);
     46 
     47  var inner = outer();
     48  for (let i = 0; i < 200; ++i) {
     49    assertEq(inner(), i + 1);
     50  }
     51 
     52  assertEq(count_unscopables, 200);
     53  assertEq(count_has, 400);
     54  assertEq(count_get, 400);
     55  assertEq(count_set, 200);
     56 }
     57 testInc();
     58 
     59 function testCompoundAssign() {
     60  function outer() {
     61    with (env) {
     62      // The inner function can be Warp-compiled, but has a with-environment
     63      // on its scope chain.
     64      return function() {
     65        // The compound assignment operator is compiled to JSOp::GetBoundName.
     66        return prop += 1;
     67      }
     68    }
     69  }
     70 
     71  var count_get = 0;
     72  var count_has = 0;
     73  var count_set = 0;
     74 
     75  function proxify(obj) {
     76    return new Proxy(obj, {
     77      get(t, pk, r) {
     78        count_get++;
     79        return Reflect.get(t, pk, r);
     80      },
     81      has(t, pk) {
     82        count_has++;
     83        return Reflect.has(t, pk);
     84      },
     85      set(t, pk, v, r) {
     86        count_set++;
     87        return Reflect.set(t, pk, v, r);
     88      },
     89    });
     90  }
     91 
     92  var count_unscopables = 0;
     93 
     94  var env = {
     95    get [Symbol.unscopables]() {
     96      count_unscopables++;
     97    },
     98    prop: 0,
     99  };
    100  env = proxify(env);
    101 
    102  var inner = outer();
    103  for (let i = 0; i < 200; ++i) {
    104    assertEq(inner(), i + 1);
    105  }
    106 
    107  assertEq(count_unscopables, 200);
    108  assertEq(count_has, 400);
    109  assertEq(count_get, 400);
    110  assertEq(count_set, 200);
    111 }
    112 testCompoundAssign();