tor-browser

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

shell.js (7494B)


      1 // GENERATED, DO NOT EDIT
      2 // file: asyncHelpers.js
      3 // Copyright (C) 2022 Igalia, S.L. All rights reserved.
      4 // This code is governed by the BSD license found in the LICENSE file.
      5 /*---
      6 description: |
      7    A collection of assertion and wrapper functions for testing asynchronous built-ins.
      8 defines: [asyncTest, assert.throwsAsync]
      9 ---*/
     10 
     11 /**
     12 * Defines the **sole** asynchronous test of a file.
     13 * @see {@link ../docs/rfcs/async-helpers.md} for background.
     14 *
     15 * @param {Function} testFunc a callback whose returned promise indicates test results
     16 *   (fulfillment for success, rejection for failure)
     17 * @returns {void}
     18 */
     19 function asyncTest(testFunc) {
     20  if (!Object.prototype.hasOwnProperty.call(globalThis, "$DONE")) {
     21    throw new Test262Error("asyncTest called without async flag");
     22  }
     23  if (typeof testFunc !== "function") {
     24    $DONE(new Test262Error("asyncTest called with non-function argument"));
     25    return;
     26  }
     27  try {
     28    testFunc().then(
     29      function () {
     30        $DONE();
     31      },
     32      function (error) {
     33        $DONE(error);
     34      }
     35    );
     36  } catch (syncError) {
     37    $DONE(syncError);
     38  }
     39 }
     40 
     41 /**
     42 * Asserts that a callback asynchronously throws an instance of a particular
     43 * error (i.e., returns a promise whose rejection value is an object referencing
     44 * the constructor).
     45 *
     46 * @param {Function} expectedErrorConstructor the expected constructor of the
     47 *   rejection value
     48 * @param {Function} func the callback
     49 * @param {string} [message] the prefix to use for failure messages
     50 * @returns {Promise<void>} fulfills if the expected error is thrown,
     51 *   otherwise rejects
     52 */
     53 assert.throwsAsync = function (expectedErrorConstructor, func, message) {
     54  return new Promise(function (resolve) {
     55    var fail = function (detail) {
     56      if (message === undefined) {
     57        throw new Test262Error(detail);
     58      }
     59      throw new Test262Error(message + " " + detail);
     60    };
     61    if (typeof expectedErrorConstructor !== "function") {
     62      fail("assert.throwsAsync called with an argument that is not an error constructor");
     63    }
     64    if (typeof func !== "function") {
     65      fail("assert.throwsAsync called with an argument that is not a function");
     66    }
     67    var expectedName = expectedErrorConstructor.name;
     68    var expectation = "Expected a " + expectedName + " to be thrown asynchronously";
     69    var res;
     70    try {
     71      res = func();
     72    } catch (thrown) {
     73      fail(expectation + " but the function threw synchronously");
     74    }
     75    if (res === null || typeof res !== "object" || typeof res.then !== "function") {
     76      fail(expectation + " but result was not a thenable");
     77    }
     78    var onResFulfilled, onResRejected;
     79    var resSettlementP = new Promise(function (onFulfilled, onRejected) {
     80      onResFulfilled = onFulfilled;
     81      onResRejected = onRejected;
     82    });
     83    try {
     84      res.then(onResFulfilled, onResRejected)
     85    } catch (thrown) {
     86      fail(expectation + " but .then threw synchronously");
     87    }
     88    resolve(resSettlementP.then(
     89      function () {
     90        fail(expectation + " but no exception was thrown at all");
     91      },
     92      function (thrown) {
     93        var actualName;
     94        if (thrown === null || typeof thrown !== "object") {
     95          fail(expectation + " but thrown value was not an object");
     96        } else if (thrown.constructor !== expectedErrorConstructor) {
     97          actualName = thrown.constructor.name;
     98          if (expectedName === actualName) {
     99            fail(expectation +
    100              " but got a different error constructor with the same name");
    101          }
    102          fail(expectation + " but got a " + actualName);
    103        }
    104      }
    105    ));
    106  });
    107 };
    108 
    109 // file: resizableArrayBufferUtils.js
    110 // Copyright 2023 the V8 project authors. All rights reserved.
    111 // This code is governed by the BSD license found in the LICENSE file.
    112 
    113 /*---
    114 description: |
    115    Collection of helper constants and functions for testing resizable array buffers.
    116 defines:
    117  - floatCtors
    118  - ctors
    119  - MyBigInt64Array
    120  - CreateResizableArrayBuffer
    121  - MayNeedBigInt
    122  - Convert
    123  - ToNumbers
    124  - CreateRabForTest
    125  - CollectValuesAndResize
    126  - TestIterationAndResize
    127 features: [BigInt]
    128 ---*/
    129 // Helper to create subclasses without bombing out when `class` isn't supported
    130 function subClass(type) {
    131  try {
    132    return new Function('return class My' + type + ' extends ' + type + ' {}')();
    133  } catch (e) {}
    134 }
    135 
    136 const MyUint8Array = subClass('Uint8Array');
    137 const MyFloat32Array = subClass('Float32Array');
    138 const MyBigInt64Array = subClass('BigInt64Array');
    139 
    140 const builtinCtors = [
    141  Uint8Array,
    142  Int8Array,
    143  Uint16Array,
    144  Int16Array,
    145  Uint32Array,
    146  Int32Array,
    147  Float32Array,
    148  Float64Array,
    149  Uint8ClampedArray,
    150 ];
    151 
    152 // Big(U)int64Array and Float16Array are newer features adding them above unconditionally
    153 // would cause implementations lacking it to fail every test which uses it.
    154 if (typeof Float16Array !== 'undefined') {
    155  builtinCtors.push(Float16Array);
    156 }
    157 
    158 if (typeof BigUint64Array !== 'undefined') {
    159  builtinCtors.push(BigUint64Array);
    160 }
    161 
    162 if (typeof BigInt64Array !== 'undefined') {
    163  builtinCtors.push(BigInt64Array);
    164 }
    165 
    166 const floatCtors = [
    167  Float32Array,
    168  Float64Array,
    169  MyFloat32Array
    170 ];
    171 
    172 if (typeof Float16Array !== 'undefined') {
    173  floatCtors.push(Float16Array);
    174 }
    175 
    176 const ctors = builtinCtors.concat(MyUint8Array, MyFloat32Array);
    177 
    178 if (typeof MyBigInt64Array !== 'undefined') {
    179    ctors.push(MyBigInt64Array);
    180 }
    181 
    182 function CreateResizableArrayBuffer(byteLength, maxByteLength) {
    183  return new ArrayBuffer(byteLength, { maxByteLength: maxByteLength });
    184 }
    185 
    186 function Convert(item) {
    187  if (typeof item == 'bigint') {
    188    return Number(item);
    189  }
    190  return item;
    191 }
    192 
    193 function ToNumbers(array) {
    194  let result = [];
    195  for (let i = 0; i < array.length; i++) {
    196    let item = array[i];
    197    result.push(Convert(item));
    198  }
    199  return result;
    200 }
    201 
    202 function MayNeedBigInt(ta, n) {
    203  assert.sameValue(typeof n, 'number');
    204  if ((BigInt64Array !== 'undefined' && ta instanceof BigInt64Array)
    205      || (BigUint64Array !== 'undefined' && ta instanceof BigUint64Array)) {
    206    return BigInt(n);
    207  }
    208  return n;
    209 }
    210 
    211 function CreateRabForTest(ctor) {
    212  const rab = CreateResizableArrayBuffer(4 * ctor.BYTES_PER_ELEMENT, 8 * ctor.BYTES_PER_ELEMENT);
    213  // Write some data into the array.
    214  const taWrite = new ctor(rab);
    215  for (let i = 0; i < 4; ++i) {
    216    taWrite[i] = MayNeedBigInt(taWrite, 2 * i);
    217  }
    218  return rab;
    219 }
    220 
    221 function CollectValuesAndResize(n, values, rab, resizeAfter, resizeTo) {
    222  if (typeof n == 'bigint') {
    223    values.push(Number(n));
    224  } else {
    225    values.push(n);
    226  }
    227  if (values.length == resizeAfter) {
    228    rab.resize(resizeTo);
    229  }
    230  return true;
    231 }
    232 
    233 function TestIterationAndResize(iterable, expected, rab, resizeAfter, newByteLength) {
    234  let values = [];
    235  let resized = false;
    236  var arrayValues = false;
    237 
    238  for (let value of iterable) {
    239    if (Array.isArray(value)) {
    240      arrayValues = true;
    241      values.push([
    242        value[0],
    243        Number(value[1])
    244      ]);
    245    } else {
    246      values.push(Number(value));
    247    }
    248    if (!resized && values.length == resizeAfter) {
    249      rab.resize(newByteLength);
    250      resized = true;
    251    }
    252  }
    253  if (!arrayValues) {
    254      assert.compareArray([].concat(values), expected, "TestIterationAndResize: list of iterated values");
    255  } else {
    256    for (let i = 0; i < expected.length; i++) {
    257      assert.compareArray(values[i], expected[i], "TestIterationAndResize: list of iterated lists of values");
    258    }
    259  }
    260  assert(resized, "TestIterationAndResize: resize condition should have been hit");
    261 }