Add a simple API to reject promises with DOM error values.

This commit is contained in:
Josh Matthews 2016-09-06 19:58:26 -04:00
parent ae81ab3972
commit 27d44c8d10
5 changed files with 35 additions and 2 deletions

View file

@ -19,6 +19,7 @@ use js::jsapi::JS_ErrorFromException;
use js::jsapi::JS_GetPendingException;
use js::jsapi::JS_IsExceptionPending;
use js::jsapi::JS_SetPendingException;
use js::jsapi::MutableHandleValue;
use js::jsval::UndefinedValue;
use libc::c_uint;
use std::slice::from_raw_parts;
@ -266,3 +267,14 @@ pub unsafe fn throw_invalid_this(cx: *mut JSContext, proto_id: u16) {
proto_id_to_name(proto_id));
throw_type_error(cx, &error);
}
impl Error {
/// Convert this error value to a JS value, consuming it in the process.
pub unsafe fn to_jsval(self, cx: *mut JSContext, global: GlobalRef, rval: MutableHandleValue) {
assert!(!JS_IsExceptionPending(cx));
throw_dom_exception(cx, global, self);
assert!(JS_IsExceptionPending(cx));
assert!(JS_GetPendingException(cx, rval));
JS_ClearPendingException(cx);
}
}

View file

@ -14,7 +14,7 @@
use dom::bindings::callback::CallbackContainer;
use dom::bindings::codegen::Bindings::PromiseBinding::AnyCallback;
use dom::bindings::conversions::root_from_object;
use dom::bindings::error::Fallible;
use dom::bindings::error::{Error, Fallible};
use dom::bindings::global::GlobalRef;
use dom::bindings::js::MutHeapJSVal;
use dom::bindings::reflector::{Reflectable, MutReflectable, Reflector};
@ -156,6 +156,15 @@ impl Promise {
self.maybe_reject(cx, v.handle());
}
#[allow(unsafe_code)]
pub fn maybe_reject_error(&self, cx: *mut JSContext, error: Error) {
rooted!(in(cx) let mut v = UndefinedValue());
unsafe {
error.maybe_to_jsval(cx, self.global().r(), v.handle_mut());
}
self.maybe_reject(cx, v.handle());
}
#[allow(unrooted_must_root, unsafe_code)]
pub fn maybe_reject(&self,
cx: *mut JSContext,

View file

@ -20,7 +20,7 @@ use dom::bindings::codegen::UnionTypes::{EventOrUSVString, HTMLElementOrLong, Lo
use dom::bindings::codegen::UnionTypes::{HTMLElementOrUnsignedLongOrStringOrBoolean, LongSequenceOrBoolean};
use dom::bindings::codegen::UnionTypes::{StringOrLongSequence, StringOrStringSequence, StringSequenceOrUnsignedLong};
use dom::bindings::codegen::UnionTypes::{StringOrUnsignedLong, StringOrBoolean, UnsignedLongOrBoolean};
use dom::bindings::error::Fallible;
use dom::bindings::error::{Error, Fallible};
use dom::bindings::global::{GlobalRef, global_root_from_context};
use dom::bindings::js::Root;
use dom::bindings::mozmap::MozMap;
@ -668,6 +668,10 @@ impl TestBindingMethods for TestBinding {
p.maybe_reject(cx, v);
}
fn PromiseRejectWithTypeError(&self, p: &Promise, s: USVString) {
p.maybe_reject_error(self.global().r().get_cx(), Error::Type(s.0));
}
#[allow(unrooted_must_root)]
fn PromiseNativeHandler(&self,
resolve: Option<Rc<SimpleCallback>>,

View file

@ -518,6 +518,7 @@ interface TestBinding {
Promise<any> promiseNativeHandler(SimpleCallback? resolve, SimpleCallback? reject);
void promiseResolveNative(Promise<any> p, any value);
void promiseRejectNative(Promise<any> p, any value);
void promiseRejectWithTypeError(Promise<any> p, USVString message);
void panic();
};