From 28ea915792a70df12314647ead664eea004ce2aa Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Sat, 28 Jul 2012 16:59:14 -0700 Subject: [PATCH] servo: Use ResourceTask to fetch scripts Source-Repo: https://github.com/servo/servo Source-Revision: 08789e5bc8480002a93b776ef95e3b9be9471578 --- servo/src/servo/parser/html_builder.rs | 40 +++++++++++++++++--------- servo/src/test/test-js.html | 2 +- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/servo/src/servo/parser/html_builder.rs b/servo/src/servo/parser/html_builder.rs index 4c89a49ed1cd..850a0eb09938 100644 --- a/servo/src/servo/parser/html_builder.rs +++ b/servo/src/servo/parser/html_builder.rs @@ -13,8 +13,10 @@ import parser::Token; import dom::style::Stylesheet; import vec::{push, push_all_move, flat_map}; import std::net::url::url; -import resource::resource_task::ResourceTask; +import resource::resource_task::{ResourceTask, Load, Payload, Done}; import dvec::extensions; +import result::{ok, err}; +import to_str::to_str; enum CSSMessage { File(url), @@ -22,7 +24,7 @@ enum CSSMessage { } enum js_message { - js_file(~str), + js_file(url), js_exit } @@ -123,22 +125,33 @@ fn css_link_listener(to_parent : comm::chan, from_parent : comm::por to_parent.send(css_rules); } -fn js_script_listener(to_parent : comm::chan<~[~[u8]]>, from_parent : comm::port) { +fn js_script_listener(to_parent : comm::chan<~[~[u8]]>, from_parent : comm::port, + resource_task: ResourceTask) { let mut result_vec = ~[]; loop { alt from_parent.recv() { - js_file(filename) { + js_file(url) { let result_port = comm::port(); let result_chan = comm::chan(result_port); - let filename = copy filename; do task::spawn { - let filename <- copy filename; - let file_try = io::read_whole_file(filename); - if (file_try.is_ok()) { - result_chan.send(file_try.get()); - } else { - #error("error loading script %s", filename); + let input_port = port(); + resource_task.send(Load(url, input_port.chan())); + + let mut buf = ~[]; + loop { + alt input_port.recv() { + Payload(data) { + buf += data; + } + Done(ok(*)) { + result_chan.send(buf); + break; + } + Done(err(*)) { + #error("error loading script %s", url.to_str()); + } + } } } push(result_vec, result_port); @@ -172,7 +185,7 @@ fn build_dom(scope: NodeScope, stream: comm::port, url: url, let js_port = comm::port(); let child_chan = comm::chan(js_port); let js_chan = task::spawn_listener(|child_port| { - js_script_listener(child_chan, child_port); + js_script_listener(child_chan, child_port, resource_task); }); loop { @@ -228,7 +241,8 @@ fn build_dom(scope: NodeScope, stream: comm::port, url: url, alt elmt.get_attr(~"src") { some(filename) { #debug["Linking to a js script named: %s", filename]; - js_chan.send(js_file(copy filename)); + let new_url = make_url(filename, some(url)); + js_chan.send(js_file(new_url)); } none { /* fall through */ } } diff --git a/servo/src/test/test-js.html b/servo/src/test/test-js.html index 41617b9ca5a1..a810121d91f8 100644 --- a/servo/src/test/test-js.html +++ b/servo/src/test/test-js.html @@ -4,4 +4,4 @@ - +