For AI agents: the complete documentation index is available at /tc39-atlas/en/llms.txt, the full documentation bundle is available at /tc39-atlas/en/llms-full.txt, and this page is available as Markdown at /tc39-atlas/en/proposals/proposal-jobcallback-module.md.
  • English
  • Propagate active ScriptOrModule with JobCallback Record S2

    Proposal details
    Proposal overview

    This proposal aims to align the active ScriptOrModule propagation so that it is determined at the time promise.then is called, rather than at HostEnqueuePromiseJob time, avoiding exposure of the internal [[PromiseState]]. It mandates propagation through promise jobs and FinalizationRegistry callbacks, matching HTML's existing HostMakeJobCallback behavior.

    Note

    The README below comes from the upstream repository and may contain outdated stage or status metadata. Use the proposal details above as the current source of truth.

    ecma262#3195

    This is the repo for the nomative change of https://github.com/tc39/ecma262/pull/3195.

    Status: Stage 2

    Champions: Chengzhong Wu (@legendecas)

    Motivation

    The goal is avoid revealing internal slot [[PromiseState]] with promise.then(onFulfill, onRejection) to the promise handlers by host hook requirement.

    ECMA262 HostEnqueuePromiseJob defines that a host must implement it as:

    The timing of HostEnqueuePromiseJob depends on internal Promise [[PromiseState]]. The motivation instead expect the ActiveScriptOrModule been determined AT the time promise.then() is invoked.

    // module A
    const {promise, resolve} = Promise.withResolvers();
    promise.then(handler); // (1)
    
    // module B
    resolve(); // HostEnqueuPromiseJob for (1)
    promise.then(handler); // (2) HostEnqueuPromiseJob

    That is, in the above example, the ActiveScriptOrModule for the handler at call site (1) should be "module A", and at call site (2) should be "module B".

    HTML integration

    HTML HostEnqueuePromiseJob doesn't follow the requirements of ecma262.

    However, HTML spec defines that the active ScriptOrModule is saved at the time of HostMakeJobCallback is invoked rather than at the time of HostEnqueuePromiseJob is invoked. The two host hooks are invoked with the same active script or module consecutively.

    This change also mandates that the active scripts are propagated through promise jobs and FinalizationRegistry cleanup callbacks.

    For example, the following example uses HTML APIs that depends on the original script's incumbent document URL.

    const frame = frames[0];
    const setLocationHref = Object
      .getOwnPropertyDescriptor(frame.location, "href")
      .set
      .bind(frame.location);
    
    framePromise.resolve("./page-1").then(setLocationHref);

    This proposed behavior exists in the HTML spec since HTML HostMakeJobCallback already propagates the active script.

    Proposed change

    Checkout https://github.com/tc39/ecma262/pull/3195

    The reality of the implementation status is:

    .resolved.then(f)resolve.then(v => f(v))pending.then(f)pending.then(v => f(v))
    HTMLouterouterouterouter
    ECMA-262outerouter☹️ inner☹️ inner
    Chrome/Firefox☹️ innerouter☹️ innerouter
    Safariouterouter☹️ inner☹️ inner
    • pending is resolve in inner iframe.
    • f is iframe setLocationHref.
    • outer: url is resolved to outer page.
    • inner: url is resolved to inner iframe.

    Conclusion:

    • ECMA-262 and Safari reveal the [[PromiseState]].
    • Chrome/Firefox don't respect .then(f) equals to .then(v => f(v))
    • HTML and the proposed behavior have neither of those problems

    The proposed change also fixes the inconsistent requirement on ecma262 HostEnqueuePromiseJob and HTML HostMakeJobCallback.