Auto merge of #12471 - Ms2ger:script-exec, r=nox

Cleanup HTMLScriptElement::execute()

<!-- Reviewable:start -->
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/12471)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2016-07-16 03:47:27 -07:00 committed by GitHub
commit e8fa02a07f

View file

@ -61,7 +61,7 @@ pub struct HTMLScriptElement {
parser_document: JS<Document>, parser_document: JS<Document>,
/// The source this script was loaded from /// The source this script was loaded from
load: DOMRefCell<Option<ScriptOrigin>>, load: DOMRefCell<Option<Result<ScriptOrigin, NetworkError>>>,
} }
impl HTMLScriptElement { impl HTMLScriptElement {
@ -111,9 +111,28 @@ static SCRIPT_JS_MIMES: StaticStringVec = &[
]; ];
#[derive(HeapSizeOf, JSTraceable)] #[derive(HeapSizeOf, JSTraceable)]
pub enum ScriptOrigin { pub struct ScriptOrigin {
Internal(DOMString, Url), text: DOMString,
External(Result<(String, Url), NetworkError>), url: Url,
external: bool,
}
impl ScriptOrigin {
fn internal(text: DOMString, url: Url) -> ScriptOrigin {
ScriptOrigin {
text: text,
url: url,
external: false,
}
}
fn external(text: DOMString, url: Url) -> ScriptOrigin {
ScriptOrigin {
text: text,
url: url,
external: true,
}
}
} }
/// The context required for asynchronously loading an external script source. /// The context required for asynchronously loading an external script source.
@ -172,14 +191,14 @@ impl AsyncResponseListener for ScriptContext {
// Step 7. // Step 7.
let source_text = encoding.decode(&self.data, DecoderTrap::Replace).unwrap(); let source_text = encoding.decode(&self.data, DecoderTrap::Replace).unwrap();
(source_text, metadata.final_url) ScriptOrigin::external(DOMString::from(source_text), metadata.final_url)
}); });
// Step 9. // Step 9.
// https://html.spec.whatwg.org/multipage/#prepare-a-script // https://html.spec.whatwg.org/multipage/#prepare-a-script
// Step 18.6 (When the chosen algorithm asynchronously completes). // Step 18.6 (When the chosen algorithm asynchronously completes).
let elem = self.elem.root(); let elem = self.elem.root();
*elem.load.borrow_mut() = Some(ScriptOrigin::External(load)); *elem.load.borrow_mut() = Some(load);
elem.ready_to_be_parser_executed.set(true); elem.ready_to_be_parser_executed.set(true);
let document = document_from_node(elem.r()); let document = document_from_node(elem.r());
@ -374,13 +393,13 @@ impl HTMLScriptElement {
// TODO: check for script nesting levels. // TODO: check for script nesting levels.
doc.get_script_blocking_stylesheets_count() > 0 { doc.get_script_blocking_stylesheets_count() > 0 {
doc.set_pending_parsing_blocking_script(Some(self)); doc.set_pending_parsing_blocking_script(Some(self));
*self.load.borrow_mut() = Some(ScriptOrigin::Internal(text, base_url)); *self.load.borrow_mut() = Some(Ok(ScriptOrigin::internal(text, base_url)));
self.ready_to_be_parser_executed.set(true); self.ready_to_be_parser_executed.set(true);
// Step 20.f: otherwise. // Step 20.f: otherwise.
} else { } else {
assert!(!text.is_empty()); assert!(!text.is_empty());
self.ready_to_be_parser_executed.set(true); self.ready_to_be_parser_executed.set(true);
*self.load.borrow_mut() = Some(ScriptOrigin::Internal(text, base_url)); *self.load.borrow_mut() = Some(Ok(ScriptOrigin::internal(text, base_url)));
self.execute(); self.execute();
return NextParserState::Continue; return NextParserState::Continue;
} }
@ -409,66 +428,58 @@ impl HTMLScriptElement {
} }
let load = self.load.borrow_mut().take().unwrap(); let load = self.load.borrow_mut().take().unwrap();
let script = match load {
// Step 2. // Step 2.
let (source, external, url) = match load { Err(e) => {
// Step 2.a.
ScriptOrigin::External(Err(e)) => {
warn!("error loading script {:?}", e); warn!("error loading script {:?}", e);
self.dispatch_error_event(); self.dispatch_error_event();
return; return;
} }
// Step 2.b.1.a. Ok(script) => script,
ScriptOrigin::External(Ok((text, url))) => {
debug!("loading external script, url = {}", url);
(DOMString::from(text), true, url)
},
// Step 2.b.1.c.
ScriptOrigin::Internal(text, url) => {
(text, false, url)
}
}; };
// Step 2.b.2. if script.external {
debug!("loading external script, url = {}", script.url);
}
// TODO(#12446): beforescriptexecute.
if !self.dispatch_before_script_execute_event() { if !self.dispatch_before_script_execute_event() {
return; return;
} }
// Step 2.b.3. // Step 3.
// TODO: If the script is from an external file, then increment the // TODO: If the script is from an external file, then increment the
// ignore-destructive-writes counter of the script element's node // ignore-destructive-writes counter of the script element's node
// document. Let neutralised doc be that Document. // document. Let neutralised doc be that Document.
// Step 2.b.4. // Step 4.
let document = document_from_node(self); let document = document_from_node(self);
let document = document.r(); let document = document.r();
let old_script = document.GetCurrentScript(); let old_script = document.GetCurrentScript();
// Step 2.b.5. // Step 5.a.1.
document.set_current_script(Some(self)); document.set_current_script(Some(self));
// Step 2.b.6. // Step 5.a.2.
// TODO: Create a script...
let window = window_from_node(self); let window = window_from_node(self);
rooted!(in(window.get_cx()) let mut rval = UndefinedValue()); rooted!(in(window.get_cx()) let mut rval = UndefinedValue());
window.evaluate_script_on_global_with_result(&*source, window.evaluate_script_on_global_with_result(&script.text,
url.as_str(), script.url.as_str(),
rval.handle_mut()); rval.handle_mut());
// Step 2.b.7. // Step 6.
document.set_current_script(old_script.r()); document.set_current_script(old_script.r());
// Step 2.b.8. // Step 7.
// TODO: Decrement the ignore-destructive-writes counter of neutralised // TODO: Decrement the ignore-destructive-writes counter of neutralised
// doc, if it was incremented in the earlier step. // doc, if it was incremented in the earlier step.
// Step 2.b.9. // TODO(#12446): afterscriptexecute.
self.dispatch_after_script_execute_event(); self.dispatch_after_script_execute_event();
// Step 2.b.10. // Step 8.
if external { if script.external {
self.dispatch_load_event(); self.dispatch_load_event();
} else { } else {
window.dom_manipulation_task_source().queue_simple_event(self.upcast(), atom!("load"), window.r()); window.dom_manipulation_task_source().queue_simple_event(self.upcast(), atom!("load"), window.r());