tor-browser

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

getter-setter-guards2.js (3347B)


      1 function testRedefineGetter() {
      2    var callGetter = function(o) {
      3        return o.x;
      4    };
      5 
      6    var proto = {get foo() {}, bar: 1};
      7    var obj = Object.create(proto);
      8 
      9    // Add "x" getter on the prototype. Warm up the IC.
     10    var count1 = 0;
     11    Object.defineProperty(proto, "x", {get: function(v) {
     12        count1++;
     13    }, configurable: true});
     14    for (var i = 0; i < 20; i++) {
     15        callGetter(obj);
     16    }
     17    assertEq(count1, 20);
     18 
     19    // Redefine "x" with a different getter. Ensure the new getter is called.
     20    var count2 = 0;
     21    Object.defineProperty(proto, "x", {get: function() {
     22        count2++;
     23    }, configurable: true});
     24    for (var i = 0; i < 20; i++) {
     25        callGetter(obj);
     26    }
     27    assertEq(count1, 20);
     28    assertEq(count2, 20);
     29 }
     30 testRedefineGetter();
     31 
     32 function testRedefineSetter() {
     33    var callSetter = function(o) {
     34        o.x = 1;
     35    };
     36 
     37    var proto = {get foo() {}, bar: 1};
     38    var obj = Object.create(proto);
     39 
     40    // Add "x" setter on the prototype. Warm up the IC.
     41    var count1 = 0;
     42    Object.defineProperty(proto, "x", {set: function(v) {
     43        count1++;
     44    }, configurable: true});
     45    for (var i = 0; i < 20; i++) {
     46        callSetter(obj);
     47    }
     48    assertEq(count1, 20);
     49 
     50    // Redefine "x" with a different setter. Ensure the new setter is called.
     51    var count2 = 0;
     52    Object.defineProperty(proto, "x", {set: function() {
     53        count2++;
     54    }, configurable: true});
     55    for (var i = 0; i < 20; i++) {
     56        callSetter(obj);
     57    }
     58    assertEq(count1, 20);
     59    assertEq(count2, 20);
     60 }
     61 testRedefineSetter();
     62 
     63 function testDeleteAdd() {
     64    var callGetter = function(o) {
     65        return o.x;
     66    };
     67 
     68    var proto = {get foo() {}, bar: 1};
     69    var obj = Object.create(proto);
     70 
     71    // Add "x" getter on the prototype. Warm up the IC.
     72    var count1 = 0;
     73    Object.defineProperty(proto, "x", {get: function() {
     74        count1++;
     75    }, configurable: true});
     76    for (var i = 0; i < 20; i++) {
     77        callGetter(obj);
     78    }
     79    assertEq(count1, 20);
     80 
     81    // Delete the getter.
     82    delete proto.x;
     83 
     84    // Add "x" back with a different getter. Ensure the new getter is called.
     85    var count2 = 0;
     86    Object.defineProperty(proto, "x", {get: function() {
     87        count2++;
     88    }, configurable: true});
     89    for (var i = 0; i < 20; i++) {
     90        callGetter(obj);
     91    }
     92    assertEq(count1, 20);
     93    assertEq(count2, 20);
     94 }
     95 testDeleteAdd();
     96 
     97 function testAccessorToDataAndBack() {
     98    var callGetter = function(o) {
     99        return o.x;
    100    };
    101 
    102    var proto = {get foo() {}, bar: 1};
    103    var obj = Object.create(proto);
    104 
    105    // Add "x" getter on the prototype. Warm up the IC.
    106    var count1 = 0;
    107    Object.defineProperty(proto, "x", {get: function() {
    108        count1++;
    109    }, configurable: true});
    110    for (var i = 0; i < 20; i++) {
    111        callGetter(obj);
    112    }
    113    assertEq(count1, 20);
    114 
    115    // Turn the getter into a data property.
    116    Object.defineProperty(proto, "x", {configurable: true, value: 123});
    117 
    118    // Turn the data property into a (different) getter. Ensure the new getter is
    119    // called.
    120    var count2 = 0;
    121    Object.defineProperty(proto, "x", {get: function() {
    122        count2++;
    123    }, configurable: true});
    124    for (var i = 0; i < 20; i++) {
    125        callGetter(obj);
    126    }
    127    assertEq(count1, 20);
    128    assertEq(count2, 20);
    129 }
    130 testAccessorToDataAndBack();