Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normative: Propagate active ScriptOrModule with JobCallback Record #3195

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 36 additions & 4 deletions spec.html
Original file line number Diff line number Diff line change
Expand Up @@ -11901,6 +11901,17 @@ <h1>JobCallback Records</h1>
The function to invoke when the Job is invoked.
</td>
</tr>
<tr>
<td>
[[ScriptOrModule]]
</td>
<td>
a Script Record or a Module Record
</td>
<td>
The script or module in which the Job was created.
</td>
</tr>
<tr>
<td>
[[HostDefined]]
Expand All @@ -11926,16 +11937,27 @@ <h1>
</dl>
<p>An implementation of HostMakeJobCallback must conform to the following requirements:</p>
<ul>
<li>It must return a JobCallback Record whose [[Callback]] field is _callback_.</li>
<li>It must return a JobCallback Record whose [[Callback]] field is _callback_, and [[ScriptOrModule]] field is the result of GetActiveScriptOrModule().</li>
</ul>
<p>The default implementation of HostMakeJobCallback performs the following steps when called:</p>
<emu-alg>
1. Return the JobCallback Record { [[Callback]]: _callback_, [[HostDefined]]: ~empty~ }.
1. Let _scriptOrModule_ be GetActiveScriptOrModule().
1. Return the JobCallback Record { [[Callback]]: _callback_, [[ScriptOrModule]]: _scriptOrModule_, [[HostDefined]]: ~empty~ }.
</emu-alg>
<p>ECMAScript hosts that are not web browsers must use the default implementation of HostMakeJobCallback.</p>
<emu-note>
<p>This is called at the time that the callback is passed to the function that is responsible for its being eventually scheduled and run. For example, `promise.then(thenAction)` calls MakeJobCallback on `thenAction` at the time of invoking `Promise.prototype.then`, not at the time of scheduling the reaction Job.</p>
</emu-note>

<emu-note>
<p>An example of when _scriptOrModule_ is not *null*, and saving it in this way is useful, is the following:</p>

<pre><code class="javascript">Promise.resolve('import(`./example.mjs`)').then(eval);</code></pre>

<p>Without this step (and the steps that use it in HostCallJobCallback), the result of GetActiveScriptOrModule() would be *null* when the <emu-xref href="#sec-import-calls">`import()`</emu-xref> call is evaluated, since <emu-xref href="#sec-eval-x">`eval()`</emu-xref> is a built-in function that does not originate from any particular script.</p>

<p>With this step in place, the active _scriptOrModule_ is propagated from the above code into the job, allowing the <emu-xref href="#sec-import-calls">`import()`</emu-xref> call to use the original script's base URL appropriately.</p>
</emu-note>
</emu-clause>

<emu-clause id="sec-hostcalljobcallback" type="host-defined abstract operation">
Expand All @@ -11950,6 +11972,7 @@ <h1>
</dl>
<p>An implementation of HostCallJobCallback must conform to the following requirements:</p>
<ul>
<li>It must perform implementation-defined steps such that _jobCallback_.[[ScriptOrModule]] is the <emu-xref href="#job-activescriptormodule">active script or module</emu-xref> at the time of _job_'s invocation.</li>
<li>It must perform and return the result of Call(_jobCallback_.[[Callback]], _V_, _argumentsList_).</li>
</ul>
<emu-note>
Expand All @@ -11958,7 +11981,17 @@ <h1>
<p>The default implementation of HostCallJobCallback performs the following steps when called:</p>
<emu-alg>
1. Assert: IsCallable(_jobCallback_.[[Callback]]) is *true*.
1. Return ? Call(_jobCallback_.[[Callback]], _V_, _argumentsList_).
1. Let _callerContext_ be the running execution context.
1. If _callerContext_ is not already suspended, suspend _callerContext_.
1. Let _jobContext_ be a new execution context.
1. Let _scriptOrModule_ be _jobCallback_.[[ScriptOrModule]].
1. If _scriptOrModule_ is not *null*, then
1. Set the Realm of _jobContext_ to _scriptOrModule_.[[Realm]].
1. Set the ScriptOrModule of _jobContext_ to _scriptOrModule_.
1. Perform any necessary implementation-defined initialization of _jobContext_.
1. Let _result_ be Completion(Call(_jobCallback_.[[Callback]], _V_, _argumentsList_)).
1. Remove _jobContext_ from the execution context stack and restore _callerContext_ as the running execution context.
1. Return ? _result_.
</emu-alg>
<p>ECMAScript hosts that are not web browsers must use the default implementation of HostCallJobCallback.</p>
</emu-clause>
Expand Down Expand Up @@ -11992,7 +12025,6 @@ <h1>
<p>An implementation of HostEnqueuePromiseJob must conform to the requirements in <emu-xref href="#sec-jobs"></emu-xref> as well as the following:</p>
<ul>
<li>If _realm_ is not *null*, each time _job_ is invoked the implementation must perform implementation-defined steps such that execution is <emu-xref href="#job-preparedtoevaluatecode">prepared to evaluate ECMAScript code</emu-xref> at the time of _job_'s invocation.</li>
<li>Let _scriptOrModule_ be GetActiveScriptOrModule() at the time HostEnqueuePromiseJob is invoked. If _realm_ is not *null*, each time _job_ is invoked the implementation must perform implementation-defined steps such that _scriptOrModule_ is the <emu-xref href="#job-activescriptormodule">active script or module</emu-xref> at the time of _job_'s invocation.</li>
<li>Jobs must run in the same order as the HostEnqueuePromiseJob invocations that scheduled them.</li>
</ul>

Expand Down