Use safe JSContext as first argument for throw_dom_exception

This commit is contained in:
marmeladema 2019-07-27 19:36:21 +01:00
parent 6c26518f61
commit 0703a1ad6d
6 changed files with 29 additions and 25 deletions

View file

@ -813,7 +813,7 @@ def getJSToNativeConversionInfo(type, descriptorProvider, failureCode=None,
match Promise::new_resolved(&promiseGlobal, cx, valueToResolve.handle()) { match Promise::new_resolved(&promiseGlobal, cx, valueToResolve.handle()) {
Ok(value) => value, Ok(value) => value,
Err(error) => { Err(error) => {
throw_dom_exception(*cx, &promiseGlobal, error); throw_dom_exception(cx, &promiseGlobal, error);
$*{exceptionCode} $*{exceptionCode}
} }
} }
@ -3372,7 +3372,7 @@ class CGCallGenerator(CGThing):
"let result = match result {\n" "let result = match result {\n"
" Ok(result) => result,\n" " Ok(result) => result,\n"
" Err(e) => {\n" " Err(e) => {\n"
" throw_dom_exception(*cx, %s, e);\n" " throw_dom_exception(cx, %s, e);\n"
" return%s;\n" " return%s;\n"
" },\n" " },\n"
"};" % (glob, errorResult))) "};" % (glob, errorResult)))
@ -5556,12 +5556,12 @@ let global = DomRoot::downcast::<dom::types::%s>(global).unwrap();
// so we can do the spec's object-identity checks. // so we can do the spec's object-identity checks.
rooted!(in(*cx) let new_target = UnwrapObjectDynamic(args.new_target().to_object(), *cx, 1)); rooted!(in(*cx) let new_target = UnwrapObjectDynamic(args.new_target().to_object(), *cx, 1));
if new_target.is_null() { if new_target.is_null() {
throw_dom_exception(*cx, global.upcast::<GlobalScope>(), Error::Type("new.target is null".to_owned())); throw_dom_exception(cx, global.upcast::<GlobalScope>(), Error::Type("new.target is null".to_owned()));
return false; return false;
} }
if args.callee() == new_target.get() { if args.callee() == new_target.get() {
throw_dom_exception(*cx, global.upcast::<GlobalScope>(), throw_dom_exception(cx, global.upcast::<GlobalScope>(),
Error::Type("new.target must not be the active function object".to_owned())); Error::Type("new.target must not be the active function object".to_owned()));
return false; return false;
} }
@ -5602,7 +5602,7 @@ let result: Result<DomRoot<%s>, Error> = html_constructor(&global, &args);
let result = match result { let result = match result {
Ok(result) => result, Ok(result) => result,
Err(e) => { Err(e) => {
throw_dom_exception(*cx, global.upcast::<GlobalScope>(), e); throw_dom_exception(cx, global.upcast::<GlobalScope>(), e);
return false; return false;
}, },
}; };

View file

@ -15,6 +15,7 @@ use crate::dom::bindings::conversions::{
use crate::dom::bindings::str::USVString; use crate::dom::bindings::str::USVString;
use crate::dom::domexception::{DOMErrorName, DOMException}; use crate::dom::domexception::{DOMErrorName, DOMException};
use crate::dom::globalscope::GlobalScope; use crate::dom::globalscope::GlobalScope;
use crate::script_runtime::JSContext as SafeJSContext;
#[cfg(feature = "js_backtrace")] #[cfg(feature = "js_backtrace")]
use backtrace::Backtrace; use backtrace::Backtrace;
use js::error::{throw_range_error, throw_type_error}; use js::error::{throw_range_error, throw_type_error};
@ -104,10 +105,10 @@ pub type Fallible<T> = Result<T, Error>;
pub type ErrorResult = Fallible<()>; pub type ErrorResult = Fallible<()>;
/// Set a pending exception for the given `result` on `cx`. /// Set a pending exception for the given `result` on `cx`.
pub unsafe fn throw_dom_exception(cx: *mut JSContext, global: &GlobalScope, result: Error) { pub fn throw_dom_exception(cx: SafeJSContext, global: &GlobalScope, result: Error) {
#[cfg(feature = "js_backtrace")] #[cfg(feature = "js_backtrace")]
{ {
capture_stack!(in(cx) let stack); capture_stack!(in(*cx) let stack);
let js_stack = stack.and_then(|s| s.as_string(None)); let js_stack = stack.and_then(|s| s.as_string(None));
let rust_stack = Backtrace::new(); let rust_stack = Backtrace::new();
LAST_EXCEPTION_BACKTRACE.with(|backtrace| { LAST_EXCEPTION_BACKTRACE.with(|backtrace| {
@ -139,27 +140,29 @@ pub unsafe fn throw_dom_exception(cx: *mut JSContext, global: &GlobalScope, resu
Error::InvalidModification => DOMErrorName::InvalidModificationError, Error::InvalidModification => DOMErrorName::InvalidModificationError,
Error::NotReadable => DOMErrorName::NotReadableError, Error::NotReadable => DOMErrorName::NotReadableError,
Error::Operation => DOMErrorName::OperationError, Error::Operation => DOMErrorName::OperationError,
Error::Type(message) => { Error::Type(message) => unsafe {
assert!(!JS_IsExceptionPending(cx)); assert!(!JS_IsExceptionPending(*cx));
throw_type_error(cx, &message); throw_type_error(*cx, &message);
return; return;
}, },
Error::Range(message) => { Error::Range(message) => unsafe {
assert!(!JS_IsExceptionPending(cx)); assert!(!JS_IsExceptionPending(*cx));
throw_range_error(cx, &message); throw_range_error(*cx, &message);
return; return;
}, },
Error::JSFailed => { Error::JSFailed => unsafe {
assert!(JS_IsExceptionPending(cx)); assert!(JS_IsExceptionPending(*cx));
return; return;
}, },
}; };
assert!(!JS_IsExceptionPending(cx)); unsafe {
let exception = DOMException::new(global, code); assert!(!JS_IsExceptionPending(*cx));
rooted!(in(cx) let mut thrown = UndefinedValue()); let exception = DOMException::new(global, code);
exception.to_jsval(cx, thrown.handle_mut()); rooted!(in(*cx) let mut thrown = UndefinedValue());
JS_SetPendingException(cx, thrown.handle()); exception.to_jsval(*cx, thrown.handle_mut());
JS_SetPendingException(*cx, thrown.handle());
}
} }
/// A struct encapsulating information about a runtime script error. /// A struct encapsulating information about a runtime script error.
@ -310,7 +313,7 @@ impl Error {
Error::JSFailed => (), Error::JSFailed => (),
_ => assert!(!JS_IsExceptionPending(cx)), _ => assert!(!JS_IsExceptionPending(cx)),
} }
throw_dom_exception(cx, global, self); throw_dom_exception(SafeJSContext::from_ptr(cx), global, self);
assert!(JS_IsExceptionPending(cx)); assert!(JS_IsExceptionPending(cx));
assert!(JS_GetPendingException(cx, rval)); assert!(JS_GetPendingException(cx, rval));
JS_ClearPendingException(cx); JS_ClearPendingException(cx);

View file

@ -158,7 +158,7 @@ fn create_html_element(
unsafe { unsafe {
let _ac = let _ac =
JSAutoRealm::new(*cx, global.reflector().get_jsobject().get()); JSAutoRealm::new(*cx, global.reflector().get_jsobject().get());
throw_dom_exception(*cx, &global, error); throw_dom_exception(cx, &global, error);
report_pending_exception(*cx, true); report_pending_exception(*cx, true);
} }

View file

@ -625,7 +625,7 @@ pub fn upgrade_element(definition: Rc<CustomElementDefinition>, element: &Elemen
let global = GlobalScope::current().expect("No current global"); let global = GlobalScope::current().expect("No current global");
let cx = global.get_cx(); let cx = global.get_cx();
unsafe { unsafe {
throw_dom_exception(*cx, &global, error); throw_dom_exception(cx, &global, error);
report_pending_exception(*cx, true); report_pending_exception(*cx, true);
} }
return; return;

View file

@ -18,6 +18,7 @@ use crate::dom::document::Document;
use crate::dom::element::Element; use crate::dom::element::Element;
use crate::dom::globalscope::GlobalScope; use crate::dom::globalscope::GlobalScope;
use crate::dom::window::Window; use crate::dom::window::Window;
use crate::script_runtime::JSContext as SafeJSContext;
use crate::script_thread::ScriptThread; use crate::script_thread::ScriptThread;
use dom_struct::dom_struct; use dom_struct::dom_struct;
use embedder_traits::EmbedderMsg; use embedder_traits::EmbedderMsg;
@ -959,7 +960,7 @@ pub fn new_window_proxy_handler() -> WindowProxyHandler {
unsafe fn throw_security_error(cx: *mut JSContext) -> bool { unsafe fn throw_security_error(cx: *mut JSContext) -> bool {
if !JS_IsExceptionPending(cx) { if !JS_IsExceptionPending(cx) {
let global = GlobalScope::from_context(cx); let global = GlobalScope::from_context(cx);
throw_dom_exception(cx, &*global, Error::Security); throw_dom_exception(SafeJSContext::from_ptr(cx), &*global, Error::Security);
} }
false false
} }

View file

@ -739,7 +739,7 @@ pub fn handle_get_property(
Err(_) => Ok(WebDriverJSValue::Undefined), Err(_) => Ok(WebDriverJSValue::Undefined),
}, },
Err(error) => { Err(error) => {
unsafe { throw_dom_exception(*cx, &node.reflector().global(), error) }; throw_dom_exception(cx, &node.reflector().global(), error);
Ok(WebDriverJSValue::Undefined) Ok(WebDriverJSValue::Undefined)
}, },
} }