tor-browser

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

mapped-define.js (3189B)


      1 load(libdir + "asserts.js");
      2 
      3 function testMapped(a) {
      4    assertEq(arguments[0], 1);
      5 
      6    Object.defineProperty(arguments, 0, {value: 23, writable: true, configurable: true});
      7    assertEq(arguments[0], 23);
      8    assertEq(a, 23);
      9 
     10    a = 12;
     11    assertEq(a, 12);
     12    assertEq(arguments[0], 12);
     13 
     14    Object.defineProperty(arguments, 0, {value: 9, writable: false, configurable: false});
     15    assertEq(arguments[0], 9);
     16    assertEq(a, 9);
     17 
     18    a = 4;
     19    assertEq(arguments[0], 9);
     20    assertEq(a, 4);
     21 }
     22 for (var i = 0; i < 5; i++) {
     23    testMapped(1);
     24 }
     25 
     26 function testAttributes(x) {
     27    Object.defineProperty(arguments, 0, {enumerable:true,
     28                                         writable:true,
     29                                         configurable:true,
     30                                         value: 4});
     31 
     32    // Should inherit enumerable/configurable attributes.
     33    Object.defineProperty(arguments, 0, {writable:true,
     34                                         value: 8});
     35    assertEq(JSON.stringify(Object.getOwnPropertyDescriptor(arguments, 0)),
     36             '{"value":8,"writable":true,"enumerable":true,"configurable":true}');
     37    assertEq(x, 8);
     38 
     39    // Property becomes non-configurable.
     40    Object.defineProperty(arguments, 0, {writable:true,
     41                                         configurable:false,
     42                                         value: 6});
     43    assertEq(JSON.stringify(Object.getOwnPropertyDescriptor(arguments, 0)),
     44             '{"value":6,"writable":true,"enumerable":true,"configurable":false}');
     45    assertEq(x, 6);
     46 
     47    // Can no longer make it non-enumerable.
     48    assertThrowsInstanceOf(() => Object.defineProperty(arguments, 0, {writable:true,
     49                                                                      configurable:false,
     50                                                                      enumerable:false,
     51                                                                      value: 6}),
     52                           TypeError);
     53 
     54    // Can no longer make it configurable.
     55    assertThrowsInstanceOf(() => Object.defineProperty(arguments, 0, {writable:true,
     56                                                                      configurable:true,
     57                                                                      value: 6}),
     58                           TypeError);
     59 
     60    // Can still make it non-writable.
     61    Object.defineProperty(arguments, 0, {writable:false,
     62                                         enumerable:true,
     63                                         configurable:false,
     64                                         value: 3});
     65    assertEq(x, 3);
     66 
     67    // No longer a mapped property.
     68    x = 5;
     69    assertEq(arguments[0], 3);
     70 
     71    // Can no longer make it writable.
     72    assertThrowsInstanceOf(() => Object.defineProperty(arguments, 0, {writable:true,
     73                                                                      enumerable:true,
     74                                                                      configurable:false,
     75                                                                      value: 5}),
     76                           TypeError);
     77    assertEq(x, 5);
     78 }
     79 for (var i = 0; i < 5; i++) {
     80    testAttributes(i);
     81 }