• tzik's avatar
    Use non-primary promise handler as a source of fallback microtask context · 39bfa157
    tzik authored
    A microtask requires a non-detached Context to trigger, and the Context
    is usually pulled from the primary handler.
    On an example below, |on_rejected| is primary, as the attached promise
    is rejected and |on_rejected| will be called as the reaction.
    
      Promise.reject().then(on_fulfilled, on_rejected);
    
    If the primary handler is undefined or invalid, we used to use the
    promise's context as the fallback. E.g. the primary handler is undefined
    on the examlpe below, and the context of |promise| was used.
    
      let promise = Promise.reject();
      promise.then(on_fulfilled);
    
    However, that causes a non-intuitive behavior around a detached
    context:
    
      let DeadPromise = iframe.contentWindow.Promise;
      iframe.src = "http://example.com"; // navigate away.
      // DeadPromise's Context is detached state now.
    
      let p = DeadPromise.reject();
    
      // |on_rejected| is called, as the context is pulled from |on_rejected|.
      p.then(on_fulfilled, on_rejected);
    
      // |on_rejected| was NOT called, as a microtask to settle |q| does not
      // run due to the detached context.
      let q = p.then(on_fulfilled);
      q.catch(on_rejected);
    
    After this CL, we use non-primary handler as a source of fallback context.
    On the last example above, the Context is pulled from |on_fullfilled|,
    so that |q| is settled using that context.
    
    Bug: chromium:941271
    Change-Id: Iff71acf7c3617f3493d100abcd2c5c36bd1bbfd1
    Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1535916Reviewed-by: 's avatarBenedikt Meurer <bmeurer@chromium.org>
    Commit-Queue: Taiju Tsuiki <tzik@chromium.org>
    Cr-Commit-Position: refs/heads/master@{#60499}
    39bfa157
microtask-queue-unittest.cc 18.7 KB