tor-browser

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

ticks-with-async-iter-resolved-promise-and-constructor-lookup.js (2053B)


      1 // |reftest| async
      2 // Copyright (C) 2019 André Bargull. All rights reserved.
      3 // This code is governed by the BSD license found in the LICENSE file.
      4 
      5 /*---
      6 esid: sec-runtime-semantics-forin-div-ofbodyevaluation-lhs-stmt-iterator-lhskind-labelset
      7 description: >
      8  Ensure the number of ticks and Promise constructor lookups is correct with custom async iterator.
      9 info: |
     10  13.7.5.13 Runtime Semantics: ForIn/OfBodyEvaluation ( lhs, stmt, iteratorRecord, iterationKind,
     11                                                        lhsKind, labelSet [ , iteratorKind ] )
     12  6.2.3.1 Await
     13 
     14 includes: [compareArray.js]
     15 flags: [async]
     16 features: [async-iteration]
     17 ---*/
     18 
     19 // The expected event log.
     20 var expected = [
     21  // Before entering loop.
     22  "pre",
     23 
     24  // Await promise resolved.
     25  "tick 1",
     26 
     27  // In loop body.
     28  "loop",
     29 
     30  // Await promise resolved
     31  "tick 2",
     32 
     33  // After exiting loop.
     34  "post",
     35 ];
     36 
     37 // The actual event log.
     38 var actual = [];
     39 
     40 // Custom async iterator directly using a synchronous iterator.
     41 function toAsyncIterator(iterable) {
     42  return {
     43    [Symbol.asyncIterator]() {
     44      return iterable[Symbol.iterator]();
     45    }
     46  };
     47 }
     48 
     49 // Test function using for-await with a single, already resolved Promise.
     50 async function f() {
     51  var p = Promise.resolve(0);
     52  actual.push("pre");
     53  for await (var x of toAsyncIterator([p])) {
     54    actual.push("loop");
     55  }
     56  actual.push("post");
     57 }
     58 
     59 // Count the number of ticks needed to complete the loop and compare the actual log.
     60 Promise.resolve(0)
     61  .then(() => actual.push("tick 1"))
     62  .then(() => actual.push("tick 2"))
     63  .then(() => {
     64    assert.compareArray(actual, expected, "Ticks and constructor lookups");
     65 }).then($DONE, $DONE);
     66 
     67 // Redefine `Promise.constructor` in order to intercept "constructor" lookups from PromiseResolve.
     68 // (Perform last so that the lookups from SpeciesConstructor in `then` aren't logged.)
     69 Object.defineProperty(Promise.prototype, "constructor", {
     70  get() {
     71    actual.push("constructor");
     72    return Promise;
     73  },
     74  configurable: true,
     75 });
     76 
     77 // Start the asynchronous function.
     78 f();