servo: Merge #10312 - Report errors from ScriptPort trait methods (from servo:ScriptPort); r=Ms2ger
authorJosh Matthews <josh@joshmatthews.net>
Sat, 02 Apr 2016 00:35:01 +0500
changeset 338382 e7f64c3012ad02c76cb6e42fd2f2cb90475872ba
parent 338381 d7181c86f959796fb4cb4441b6159693976ca0ac
child 338383 fb6b6a657543480dfb6157732bfd6ad2b9a6f599
push id31307
push usergszorc@mozilla.com
push dateSat, 04 Feb 2017 00:59:06 +0000
treeherdermozilla-central@94079d43835f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersMs2ger
servo: Merge #10312 - Report errors from ScriptPort trait methods (from servo:ScriptPort); r=Ms2ger Source-Repo: https://github.com/servo/servo Source-Revision: 6056b16a7714c42e4ff67d2bccb27650d549eae4
servo/components/script/dom/dedicatedworkerglobalscope.rs
servo/components/script/dom/xmlhttprequest.rs
servo/components/script/script_thread.rs
--- a/servo/components/script/dom/dedicatedworkerglobalscope.rs
+++ b/servo/components/script/dom/dedicatedworkerglobalscope.rs
@@ -89,20 +89,21 @@ impl ScriptChan for WorkerThreadWorkerCh
         box WorkerThreadWorkerChan {
             sender: self.sender.clone(),
             worker: self.worker.clone(),
         }
     }
 }
 
 impl ScriptPort for Receiver<(TrustedWorkerAddress, WorkerScriptMsg)> {
-    fn recv(&self) -> CommonScriptMsg {
-        match self.recv().unwrap().1 {
-            WorkerScriptMsg::Common(script_msg) => script_msg,
-            WorkerScriptMsg::DOMMessage(_) => panic!("unexpected worker event message!"),
+    fn recv(&self) -> Result<CommonScriptMsg, ()> {
+        match self.recv().map(|(_, msg)| msg) {
+            Ok(WorkerScriptMsg::Common(script_msg)) => Ok(script_msg),
+            Ok(WorkerScriptMsg::DOMMessage(_)) => panic!("unexpected worker event message!"),
+            Err(_) => Err(()),
         }
     }
 }
 
 /// Set the `worker` field of a related DedicatedWorkerGlobalScope object to a particular
 /// value for the duration of this object's lifetime. This ensures that the related Worker
 /// object only lives as long as necessary (ie. while events are being executed), while
 /// providing a reference that can be cloned freely.
--- a/servo/components/script/dom/xmlhttprequest.rs
+++ b/servo/components/script/dom/xmlhttprequest.rs
@@ -1332,17 +1332,17 @@ impl XMLHttpRequest {
                                        script_chan.clone(), resource_thread);
         } else {
             XMLHttpRequest::initiate_async_xhr(context.clone(), script_chan,
                                                resource_thread, load_data);
         }
 
         if let Some(script_port) = script_port {
             loop {
-                global.process_event(script_port.recv());
+                global.process_event(script_port.recv().unwrap());
                 let context = context.lock().unwrap();
                 let sync_status = context.sync_status.borrow();
                 if let Some(ref status) = *sync_status {
                     return status.clone();
                 }
             }
         }
         Ok(())
--- a/servo/components/script/script_thread.rs
+++ b/servo/components/script/script_thread.rs
@@ -277,45 +277,47 @@ impl OpaqueSender<CommonScriptMsg> for B
         ScriptChan::send(&**self, msg).unwrap();
     }
 }
 
 /// An interface for receiving ScriptMsg values in an event loop. Used for synchronous DOM
 /// APIs that need to abstract over multiple kinds of event loops (worker/main thread) with
 /// different Receiver interfaces.
 pub trait ScriptPort {
-    fn recv(&self) -> CommonScriptMsg;
+    fn recv(&self) -> Result<CommonScriptMsg, ()>;
 }
 
 impl ScriptPort for Receiver<CommonScriptMsg> {
-    fn recv(&self) -> CommonScriptMsg {
-        self.recv().unwrap()
+    fn recv(&self) -> Result<CommonScriptMsg, ()> {
+        self.recv().map_err(|_| ())
     }
 }
 
 impl ScriptPort for Receiver<MainThreadScriptMsg> {
-    fn recv(&self) -> CommonScriptMsg {
-        match self.recv().unwrap() {
-            MainThreadScriptMsg::Common(script_msg) => script_msg,
-            _ => panic!("unexpected main thread event message!")
+    fn recv(&self) -> Result<CommonScriptMsg, ()> {
+        match self.recv() {
+            Ok(MainThreadScriptMsg::Common(script_msg)) => Ok(script_msg),
+            Ok(_) => panic!("unexpected main thread event message!"),
+            _ => Err(()),
         }
     }
 }
 
 impl ScriptPort for Receiver<(TrustedWorkerAddress, CommonScriptMsg)> {
-    fn recv(&self) -> CommonScriptMsg {
-        self.recv().unwrap().1
+    fn recv(&self) -> Result<CommonScriptMsg, ()> {
+        self.recv().map(|(_, msg)| msg).map_err(|_| ())
     }
 }
 
 impl ScriptPort for Receiver<(TrustedWorkerAddress, MainThreadScriptMsg)> {
-    fn recv(&self) -> CommonScriptMsg {
-        match self.recv().unwrap().1 {
-            MainThreadScriptMsg::Common(script_msg) => script_msg,
-            _ => panic!("unexpected main thread event message!")
+    fn recv(&self) -> Result<CommonScriptMsg, ()> {
+        match self.recv().map(|(_, msg)| msg) {
+            Ok(MainThreadScriptMsg::Common(script_msg)) => Ok(script_msg),
+            Ok(_) => panic!("unexpected main thread event message!"),
+            _ => Err(()),
         }
     }
 }
 
 /// Encapsulates internal communication of shared messages within the script thread.
 #[derive(JSTraceable)]
 pub struct SendableMainThreadScriptChan(pub Sender<CommonScriptMsg>);