tor-browser

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

this-constructor.js (1945B)


      1 // |reftest| async
      2 // Copyright (C) 2023 Igalia, S.L. All rights reserved.
      3 // This code is governed by the BSD license found in the LICENSE file.
      4 
      5 /*---
      6 esid: sec-array.fromasync
      7 description: >
      8  Constructs the this-value once if asyncItems is iterable, twice if not, and
      9  length and element properties are set correctly on the result
     10 info: |
     11  3.e. If IsConstructor(_C_) is *true*, then
     12    i. Let _A_ be ? Construct(_C_).
     13  ...
     14  j. If _iteratorRecord_ is not *undefined*, then
     15    ...
     16  k. Else,
     17    ...
     18    iv. If IsConstructor(_C_) is *true*, then
     19      1. Let _A_ be ? Construct(_C_, « 𝔽(_len_) »).
     20 includes: [compareArray.js, asyncHelpers.js]
     21 flags: [async]
     22 features: [Array.fromAsync]
     23 ---*/
     24 
     25 asyncTest(async function () {
     26  const constructorCalls = [];
     27 
     28  function MyArray(...args) {
     29    constructorCalls.push(args);
     30  }
     31 
     32  let result = await Array.fromAsync.call(MyArray, [1, 2]);
     33  assert(result instanceof MyArray, "result is an instance of the constructor this-value");
     34  assert.sameValue(result.length, 2, "length is set on result");
     35  assert.sameValue(result[0], 1, "element 0 is set on result");
     36  assert.sameValue(result[1], 2, "element 1 is set on result");
     37  assert.sameValue(constructorCalls.length, 1, "constructor is called once");
     38  assert.compareArray(constructorCalls[0], [], "constructor is called with no arguments");
     39 
     40  constructorCalls.splice(0);  // reset
     41 
     42  result = await Array.fromAsync.call(MyArray, {
     43    length: 2,
     44    0: 1,
     45    1: 2
     46  });
     47  assert(result instanceof MyArray, "result is an instance of the constructor this-value");
     48  assert.sameValue(result.length, 2, "length is set on result");
     49  assert.sameValue(result[0], 1, "element 0 is set on result");
     50  assert.sameValue(result[1], 2, "element 1 is set on result");
     51  assert.sameValue(constructorCalls.length, 1, "constructor is called once");
     52  assert.compareArray(constructorCalls[0], [2], "constructor is called with a length argument");
     53 });