tor-browser

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

inlinable-native-accessor-8.js (4104B)


      1 // Same as inlinable-native-accessor-7.js, but now without forcing Ion ICs.
      2 
      3 // Ignore unhandled rejections when calling Promise and AsyncFunction methods.
      4 ignoreUnhandledRejections();
      5 
      6 // Function cloned for each test.
      7 function test(v) {
      8  for (var i = 0; i < 100; ++i) {
      9    // |v.key| is a getter calling a built-in method or accessor.
     10    v.key;
     11  }
     12 }
     13 
     14 // Add |fn| as a method on |holder| and then call it using |thisValue| as the this-value.
     15 function runTest(holder, thisValue, key, fn) {
     16  assertEq(typeof fn, "function");
     17  assertEq(
     18    holder === thisValue || Object.prototype.isPrototypeOf.call(holder, Object(thisValue)),
     19    true,
     20    `${String(key)} can be found on thisValue when stored in holder`
     21  );
     22 
     23  // Add a prefix so we don't overwrite built-in methods.
     24  var safeKey = "__" + String(key);
     25 
     26  Object.defineProperty(holder, safeKey, {
     27    get: fn,
     28    configurable: true,
     29  });
     30 
     31  try {
     32    var t = Function(`return ${test.toString().replaceAll("key", safeKey)}`)();
     33    t(thisValue);
     34  } catch {
     35    // Intentionally ignore any errors.
     36  }
     37 
     38  // Also test wrappers of primitive values.
     39  if (Object(thisValue) !== thisValue) {
     40    try {
     41      var t = Function(`return ${test.toString().replaceAll("key", safeKey)}`)();
     42      t(Object(thisValue));
     43    } catch {
     44      // Intentionally ignore any errors.
     45    }
     46  }
     47 }
     48 
     49 // Test all methods and accessors of |object|.
     50 function testForEach(object, holder, thisValue) {
     51  for (var key of Reflect.ownKeys(object)) {
     52    var desc = Reflect.getOwnPropertyDescriptor(object, key);
     53    if (typeof desc.value === "function")
     54      runTest(holder, thisValue, key, desc.value);
     55    if (typeof desc.get === "function")
     56      runTest(holder, thisValue, key, desc.get);
     57    if (typeof desc.set === "function")
     58      runTest(holder, thisValue, key, desc.set);
     59  }
     60 }
     61 
     62 var seenProto = new Set();
     63 
     64 // Test along the prototype chain of |objectOrPrimitive|.
     65 function testProto(objectOrPrimitive) {
     66  var proto = Object.getPrototypeOf(objectOrPrimitive);
     67 
     68  while (proto) {
     69    // Install methods on |proto| and then call wih |obj| as the this-value.
     70    testForEach(proto, proto, objectOrPrimitive);
     71 
     72    // Cover all objects on the prototype chain.
     73    proto = Reflect.getPrototypeOf(proto);
     74 
     75    // But skip already seen prototypes to ensure we don't spend too much time on this test.
     76    if (seenProto.has(proto)) {
     77      break;
     78    }
     79    seenProto.add(proto);
     80  }
     81 }
     82 
     83 // Test constructor of |objectOrPrimitive|.
     84 function testConstructor(objectOrPrimitive) {
     85  // Install constructor methods on the prototype object and then call with |objectOrPrimitive|
     86  // as the this-value.
     87  testForEach(obj.constructor, Object.getPrototypeOf(objectOrPrimitive), objectOrPrimitive);
     88 }
     89 
     90 function testSingleton(singleton) {
     91  var thisValue = {};
     92  testForEach(singleton, thisValue, thisValue);
     93 }
     94 
     95 for (var obj of [
     96  // Fundamental Objects <https://tc39.es/ecma262/#sec-fundamental-objects>.
     97  {},
     98  Function(),
     99  false,
    100  Symbol(),
    101  new Error(),
    102 
    103  // Numbers and Dates <https://tc39.es/ecma262/#sec-numbers-and-dates>
    104  0,
    105  0n,
    106  new Date(0),
    107 
    108  // Text Processing <https://tc39.es/ecma262/#sec-text-processing>
    109  "",
    110  /(?:)/,
    111 
    112  // Indexed Collections <https://tc39.es/ecma262/#sec-indexed-collections>
    113  [],
    114  new Int32Array(1),
    115  new Uint8Array(1),
    116 
    117  // Keyed Collections <https://tc39.es/ecma262/#sec-keyed-collections>
    118  new Map(),
    119  new Set(),
    120  new WeakMap(),
    121  new WeakSet(),
    122  
    123  // Structured Data <https://tc39.es/ecma262/#sec-structured-data>
    124  new ArrayBuffer(1),
    125  new SharedArrayBuffer(1),
    126  new DataView(new ArrayBuffer(8)),
    127 
    128  // Managing Memory <https://tc39.es/ecma262/#sec-managing-memory>
    129  new WeakRef({}),
    130  new FinalizationRegistry(() => {}),
    131 
    132  // Control Abstraction Objects <https://tc39.es/ecma262/#sec-control-abstraction-objects>
    133  new class extends Iterator{},
    134  new Promise(() => {}),
    135  (function*(){}).constructor,
    136  (async function*(){}).constructor,
    137  (async function(){}).constructor,
    138 ]) {
    139  testProto(obj);
    140  testConstructor(obj);
    141 }
    142 
    143 testSingleton(Math);
    144 testSingleton(Atomics);
    145 testSingleton(JSON);
    146 testSingleton(Reflect);