GitHub - tc39/proposal-jobcallback-module (original) (raw)

ecma262#3195

This is the repo for the nomative change of tc39/ecma262#3195.

Status: Stage 2

Champions: Chengzhong Wu (@legendecas)

Motivation

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

ECMA262 HostEnqueuePromiseJobdefines that a host must implement it as:

The timing of HostEnqueuePromiseJob depends on internal Promise[[PromiseState]]. The motivation instead expect the ActiveScriptOrModulebeen 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 HostEnqueuePromiseJobdoesn't follow the requirements of ecma262.

However, HTML spec defines that the active ScriptOrModule is saved at the time ofHostMakeJobCallbackis 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 HTMLHostMakeJobCallback already propagates the active script.

Proposed change

Checkout tc39/ecma262#3195

The reality of the implementation status is:

. resolved.then(f) resolve.then(v => f(v)) pending.then(f) pending.then(v => f(v))
HTML outer outer outer outer
ECMA-262 outer outer ☹️ inner ☹️ inner
Chrome/Firefox ☹️ inner outer ☹️ inner outer
Safari outer outer ☹️ inner ☹️ inner

Conclusion:

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