Merge pull request #3094 from Ms2ger/importScripts

Implement WorkerGlobalScope.importScripts; r=Manishearth
This commit is contained in:
Ms2ger 2014-08-16 12:11:14 +02:00
commit ba592364b7
14 changed files with 44 additions and 54 deletions

View file

@ -18,7 +18,8 @@ interface WorkerGlobalScope : EventTarget {
// http://www.whatwg.org/html/#WorkerGlobalScope-partial
//[Exposed=Worker]
partial interface WorkerGlobalScope { // not obsolete
//void importScripts(DOMString... urls);
[Throws]
void importScripts(DOMString... urls);
readonly attribute WorkerNavigator navigator;
};
//WorkerGlobalScope implements WindowTimers;

View file

@ -3,6 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use dom::bindings::codegen::Bindings::WorkerGlobalScopeBinding::WorkerGlobalScopeMethods;
use dom::bindings::error::{ErrorResult, Syntax, Network, FailureUnknown};
use dom::bindings::trace::Untraceable;
use dom::bindings::global;
use dom::bindings::js::{JS, JSRef, Temporary, OptionalSettable};
@ -13,14 +14,15 @@ use dom::workerlocation::WorkerLocation;
use dom::workernavigator::WorkerNavigator;
use script_task::ScriptChan;
use servo_net::resource_task::ResourceTask;
use servo_net::resource_task::{ResourceTask, load_whole_resource};
use servo_util::str::DOMString;
use js::jsapi::JSContext;
use js::rust::Cx;
use std::cell::Cell;
use std::rc::Rc;
use url::Url;
use url::{Url, UrlParser};
#[deriving(PartialEq,Encodable)]
pub enum WorkerGlobalScopeId {
@ -87,6 +89,38 @@ impl<'a> WorkerGlobalScopeMethods for JSRef<'a, WorkerGlobalScope> {
Temporary::new(self.location.get().get_ref().clone())
}
fn ImportScripts(&self, url_strings: Vec<DOMString>) -> ErrorResult {
let mut urls = Vec::with_capacity(url_strings.len());
for url in url_strings.move_iter() {
let url = UrlParser::new().base_url(&*self.worker_url)
.parse(url.as_slice());
match url {
Ok(url) => urls.push(url),
Err(_) => return Err(Syntax),
};
}
for url in urls.move_iter() {
let (url, source) = match load_whole_resource(&*self.resource_task, url) {
Err(_) => return Err(Network),
Ok((metadata, bytes)) => {
(metadata.final_url, String::from_utf8(bytes).unwrap())
}
};
match self.js_context.evaluate_script(
self.reflector().get_jsobject(), source, url.serialize(), 1) {
Ok(_) => (),
Err(_) => {
println!("evaluate_script failed");
return Err(FailureUnknown);
}
}
}
Ok(())
}
fn Navigator(&self) -> Temporary<WorkerNavigator> {
if self.navigator.get().is_none() {
let navigator = WorkerNavigator::new(self);

View file

@ -1,5 +0,0 @@
[WorkerGlobalScope_importScripts.htm]
type: testharness
[Test Description: WorkerGlobalScope API: importScripts().]
expected: FAIL

View file

@ -1,5 +1,6 @@
[WorkerGlobalScope_importScripts_NetworkErr.htm]
type: testharness
expected: TIMEOUT
[Test Description: importScripts(): Throw NETWORK_ERR exception whenever attempt to fetch script is failed.]
expected: FAIL
expected: NOTRUN

View file

@ -1,5 +0,0 @@
[001.html]
type: testharness
[importScripts no arguments]
expected: FAIL

View file

@ -1,5 +0,0 @@
[002.html]
type: testharness
[importScripts resolving urls]
expected: FAIL

View file

@ -1,5 +0,0 @@
[003.html]
type: testharness
[importScripts running scripts]
expected: FAIL

View file

@ -1,5 +1,6 @@
[004.html]
type: testharness
expected: TIMEOUT
[importScripts broken script]
expected: FAIL
expected: TIMEOUT

View file

@ -1,6 +1,6 @@
[007.html]
[005.html]
type: testharness
expected: TIMEOUT
[postMessage in importScripts]
[importScripts separate scripts]
expected: TIMEOUT

View file

@ -1,6 +0,0 @@
[008.html]
type: testharness
expected: TIMEOUT
[variables and functions crossing importScripts boundary]
expected: TIMEOUT

View file

@ -1,6 +0,0 @@
[009.html]
type: testharness
expected: TIMEOUT
[variables and functions crossing importScripts boundary, take 2]
expected: TIMEOUT

View file

@ -1,5 +0,0 @@
[010.html]
type: testharness
[importScripts(undefined)]
expected: FAIL

View file

@ -1,5 +0,0 @@
[011.html]
type: testharness
[importScripts(null)]
expected: FAIL

View file

@ -1,5 +0,0 @@
[012.html]
type: testharness
[importScripts(1)]
expected: FAIL