diff options
Diffstat (limited to 'components/script/dom/bindings/refcounted.rs')
-rw-r--r-- | components/script/dom/bindings/refcounted.rs | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/components/script/dom/bindings/refcounted.rs b/components/script/dom/bindings/refcounted.rs index dcf11626543..f22074d5fef 100644 --- a/components/script/dom/bindings/refcounted.rs +++ b/components/script/dom/bindings/refcounted.rs @@ -23,12 +23,17 @@ //! as JS roots. use core::nonzero::NonZero; +use dom::bindings::conversions::ToJSValConvertible; +use dom::bindings::error::Error; use dom::bindings::js::Root; use dom::bindings::reflector::{DomObject, Reflector}; use dom::bindings::trace::trace_reflector; use dom::promise::Promise; +use js::jsapi::JSAutoCompartment; use js::jsapi::JSTracer; use libc; +use script_thread::Runnable; +use script_thread::ScriptThread; use std::cell::RefCell; use std::collections::hash_map::Entry::{Occupied, Vacant}; use std::collections::hash_map::HashMap; @@ -115,6 +120,40 @@ impl TrustedPromise { promise }) } + + /// A runnable which will reject the promise. + #[allow(unrooted_must_root)] + pub fn reject_runnable(self, error: Error) -> impl Runnable + Send { + struct RejectPromise(TrustedPromise, Error); + impl Runnable for RejectPromise { + fn main_thread_handler(self: Box<Self>, script_thread: &ScriptThread) { + let this = *self; + let cx = script_thread.get_cx(); + let promise = this.0.root(); + let _ac = JSAutoCompartment::new(cx, promise.reflector().get_jsobject().get()); + promise.reject_error(cx, this.1); + } + } + RejectPromise(self, error) + } + + /// A runnable which will resolve the promise. + #[allow(unrooted_must_root)] + pub fn resolve_runnable<T>(self, value: T) -> impl Runnable + Send where + T: ToJSValConvertible + Send + { + struct ResolvePromise<T>(TrustedPromise, T); + impl<T: ToJSValConvertible> Runnable for ResolvePromise<T> { + fn main_thread_handler(self: Box<Self>, script_thread: &ScriptThread) { + let this = *self; + let cx = script_thread.get_cx(); + let promise = this.0.root(); + let _ac = JSAutoCompartment::new(cx, promise.reflector().get_jsobject().get()); + promise.resolve_native(cx, &this.1); + } + } + ResolvePromise(self, value) + } } /// A safe wrapper around a raw pointer to a DOM object that can be |