Commit 3d379f51 authored by kouhei's avatar kouhei Committed by Commit bot

[ES6 modules] accessing document.currentScript on module scripts should not crash

Before this CL, accessing document.currentScript caused crash. It dereferenced nullptr
pushed to Document::current_script_stack_ by ScriptLoader::DoExecuteScript() for
script == ScriptType::kModule.

This CL adds null check in Document::currentScriptForBinding so that it is aware that
the item on the stack may be nullptr.

TEST=wpt/html/semantics/scripting-1/the-script-element/module/currentScript-null.html
BUG=716951

Review-Url: https://codereview.chromium.org/2860913002
Cr-Commit-Position: refs/heads/master@{#469435}
parent cb055002
<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script type="module" src="set-currentScript-on-window.js"></script>
<script type="module">
import { currentScriptOnImportedModule } from "./currentscript.js";
test(() => {
assert_equals(document.currentScript, null, "document.currentScript on inline scripts should be null");
assert_equals(currentScriptOnImportedModule, null, "document.currentScript on imported scripts should be null");
assert_equals(window.currentScriptRecorded, null, "document.currentScript on external module scripts should be null");
}, "currentScript on script type=module should be all null");
</script>
......@@ -5289,8 +5289,10 @@ KURL Document::OpenSearchDescriptionURL() {
void Document::currentScriptForBinding(
HTMLScriptElementOrSVGScriptElement& script_element) const {
if (!current_script_stack_.IsEmpty())
current_script_stack_.back()->SetScriptElementForBinding(script_element);
if (!current_script_stack_.IsEmpty()) {
if (ScriptElementBase* script_element_base = current_script_stack_.back())
script_element_base->SetScriptElementForBinding(script_element);
}
}
void Document::PushCurrentScript(ScriptElementBase* new_current_script) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment