tor-browser

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

async-implicit.js (1691B)


      1 // Test AutoSetAsyncStackForNewCalls's IMPLICIT kind.
      2 
      3 // Given a SavedFrame stack, return a string listing the frame's function names
      4 // and their async causes, if any.
      5 function stackFunctions(stack) {
      6  const frames = [];
      7  for (; stack; stack = stack.parent || stack.asyncParent) {
      8    if (!stack.functionDisplayName) {
      9      frames.push('(top level)');
     10    } else if (stack.asyncCause) {
     11      frames.push(`${stack.asyncCause}*${stack.functionDisplayName}`);
     12    } else {
     13      frames.push(stack.functionDisplayName);
     14    }
     15  }
     16  return frames.join(', ');
     17 }
     18 
     19 let fakeStack = (function fake1() {
     20  function fake2() {
     21    return saveStack();
     22  }
     23  return fake2();
     24 })();
     25 
     26 function bindAndExpect(options, expected) {
     27  function bindee() {
     28    assertEq(stackFunctions(saveStack()), expected);
     29  }
     30 
     31  return bindToAsyncStack(bindee, options);
     32 }
     33 
     34 function caller(f) {
     35  return f();
     36 }
     37 
     38 // An explicit async stack always overrides the actual callers of the bindee.
     39 // An implicit async stack never overrides callers; it is only attached when
     40 // the stack is otherwise empty.
     41 caller(bindAndExpect({ stack: fakeStack, cause: 'ano', explicit: false },
     42                     "bindee, caller, (top level)"));
     43 
     44 caller(bindAndExpect({ stack: fakeStack, cause: 'hi', explicit: true },
     45                     "bindee, hi*fake2, fake1, (top level)"));
     46 
     47 Promise.resolve()
     48  .then(function () {
     49    bindAndExpect({ stack: fakeStack, cause: 'hana', explicit: true },
     50                  "bindee, hana*fake2, fake1, (top level)");
     51  });
     52 Promise.resolve()
     53  .then(function () {
     54    bindAndExpect({ stack: fakeStack, cause: 'hana', explicit: true },
     55                  "bindee, hana*fake2, fake1, (top level)");
     56  });