Issue #3020 - Clear currentScript after running microtasks

Per HTML spec:

`document.currentScript` Returns null if the Document is not currently
executing a script or SVG script element (e.g., because the running
script is an event handler, or a timeout), or if the currently executing
script or SVG script element represents a module script.

---

It's ambiguous about whether, for micro tasks, it should return null.
(This section is labelled as "non-normative", as well.)
However, mainstream browsers handle this differently and don't return
null after running microtasks, so we need to clear this and not assume
null persistently, and run the auto-updater for it, regardless if
it's a module or not.
This commit is contained in:
Moonchild committed 2026-03-24 20:44:13 +01:00
1 parent 18595b01b6
commit 3a4dc8de41
1 file changed
+7 -4
+7 -4
View File
@@ -2315,8 +2315,14 @@ ScriptLoader::EvaluateScript(ScriptLoadRequest* aRequest)
return NS_ERROR_FAILURE;
}
// Update our current script.
// This must be destroyed after destroying nsAutoMicroTask.
nsIScriptElement* currentScript =
aRequest->IsModuleRequest() ? nullptr : aRequest->Element();
AutoCurrentScriptUpdater scriptUpdater(this, currentScript);
// New script entry point required, due to the "Create a script" sub-step of
// http://www.whatwg.org/specs/web-apps/current-work/#execute-the-script-block
// http://html.spec.whatwg.org/multipage/#execute-the-script-block
nsAutoMicroTask mt;
AutoEntryScript aes(globalObject, "<script> element", true);
JSContext* cx = aes.cx();
@@ -2374,9 +2380,6 @@ ScriptLoader::EvaluateScript(ScriptLoadRequest* aRequest)
FinishDynamicImport(cx, request, rv);
}
} else {
// Update our current script.
AutoCurrentScriptUpdater scriptUpdater(this, aRequest->Element());
JS::CompileOptions options(cx);
rv = FillCompileOptionsForRequest(aes, aRequest, global, &options);