mirror of
https://github.com/servo/servo.git
synced 2025-08-06 14:10:11 +01:00
Add handling for unreported exceptions when invoking callback objects.
This commit is contained in:
parent
dfad8763a8
commit
159235b3d0
7 changed files with 58 additions and 18 deletions
|
@ -9,19 +9,21 @@
|
|||
use dom::bindings::global::global_object_for_js_object;
|
||||
use dom::bindings::js::JSRef;
|
||||
use dom::bindings::utils::Reflectable;
|
||||
use js::jsapi::{JSContext, JSObject, JS_WrapObject, JS_ObjectIsCallable};
|
||||
use js::jsapi::JS_GetProperty;
|
||||
use js::jsapi::{JSContext, JSObject, JS_WrapObject, JS_ObjectIsCallable, JS_GetGlobalObject};
|
||||
use js::jsapi::{JS_GetProperty, JS_IsExceptionPending, JS_ReportPendingException};
|
||||
use js::jsapi::{JS_GetPendingException};
|
||||
use js::jsval::{JSVal, UndefinedValue};
|
||||
use js::rust::with_compartment;
|
||||
|
||||
use std::ptr;
|
||||
|
||||
/// The exception handling used for a call.
|
||||
#[deriving(Copy)]
|
||||
#[deriving(Copy, PartialEq)]
|
||||
pub enum ExceptionHandling {
|
||||
/// Report any exception and don't throw it to the caller code.
|
||||
ReportExceptions,
|
||||
Report,
|
||||
/// Throw any exception to the caller code.
|
||||
RethrowExceptions
|
||||
Rethrow
|
||||
}
|
||||
|
||||
/// A common base class for representing IDL callback function types.
|
||||
|
@ -135,7 +137,7 @@ pub struct CallSetup {
|
|||
/// The `JSContext` used for the call.
|
||||
cx: *mut JSContext,
|
||||
/// The exception handling used for the call.
|
||||
_handling: ExceptionHandling
|
||||
handling: ExceptionHandling,
|
||||
}
|
||||
|
||||
impl CallSetup {
|
||||
|
@ -145,9 +147,10 @@ impl CallSetup {
|
|||
let global = global_object_for_js_object(callback.callback());
|
||||
let global = global.root();
|
||||
let cx = global.r().get_cx();
|
||||
|
||||
CallSetup {
|
||||
cx: cx,
|
||||
_handling: handling
|
||||
handling: handling,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -156,3 +159,40 @@ impl CallSetup {
|
|||
self.cx
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for CallSetup {
|
||||
fn drop(&mut self) {
|
||||
let mut need_to_deal_with_exception = unsafe { JS_IsExceptionPending(self.cx) } != 0;
|
||||
if self.handling == ExceptionHandling::Rethrow && need_to_deal_with_exception {
|
||||
let mut exn = UndefinedValue();
|
||||
let got_exn = unsafe {
|
||||
JS_GetPendingException(self.cx, &mut exn) != 0
|
||||
};
|
||||
|
||||
if got_exn {
|
||||
//TODO: actually rethrow instead of eagerly reporting.
|
||||
// Gecko stores a mutable reference to an ErrorResult
|
||||
// abstraction that can store a JS exception and
|
||||
// report it at content boundaries. Our return value
|
||||
// wrappers around Result<U,V> make that more difficult.
|
||||
unsafe {
|
||||
JS_ReportPendingException(self.cx);
|
||||
}
|
||||
need_to_deal_with_exception = false;
|
||||
}
|
||||
}
|
||||
|
||||
if need_to_deal_with_exception {
|
||||
// Either we're supposed to report our exceptions, or we're supposed to
|
||||
// re-throw them but we failed to JS_GetPendingException. Either way,
|
||||
// just report the pending exception, if any.
|
||||
|
||||
unsafe {
|
||||
let old_global = JS_GetGlobalObject(self.cx);
|
||||
with_compartment(self.cx, old_global, || {
|
||||
JS_ReportPendingException(self.cx)
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
use dom::bindings::callback::ExceptionHandling::ReportExceptions;
|
||||
use dom::bindings::callback::ExceptionHandling::Report;
|
||||
use dom::bindings::codegen::Bindings::EventBinding::EventMethods;
|
||||
use dom::bindings::codegen::InheritTypes::{EventTargetCast, NodeCast, NodeDerived};
|
||||
use dom::bindings::js::{JS, JSRef, OptionalRootable, Root};
|
||||
|
@ -48,7 +48,7 @@ pub fn dispatch_event<'a, 'b>(target: JSRef<'a, EventTarget>,
|
|||
event.set_current_target(cur_target.r());
|
||||
for listener in listeners.iter() {
|
||||
// Explicitly drop any exception on the floor.
|
||||
let _ = listener.HandleEvent_(cur_target.r(), event, ReportExceptions);
|
||||
let _ = listener.HandleEvent_(cur_target.r(), event, Report);
|
||||
|
||||
if event.stop_immediate() {
|
||||
break;
|
||||
|
@ -74,7 +74,7 @@ pub fn dispatch_event<'a, 'b>(target: JSRef<'a, EventTarget>,
|
|||
for listeners in opt_listeners.iter() {
|
||||
for listener in listeners.iter() {
|
||||
// Explicitly drop any exception on the floor.
|
||||
let _ = listener.HandleEvent_(target, event, ReportExceptions);
|
||||
let _ = listener.HandleEvent_(target, event, Report);
|
||||
|
||||
if event.stop_immediate() {
|
||||
break;
|
||||
|
@ -93,7 +93,7 @@ pub fn dispatch_event<'a, 'b>(target: JSRef<'a, EventTarget>,
|
|||
event.set_current_target(cur_target.r());
|
||||
for listener in listeners.iter() {
|
||||
// Explicitly drop any exception on the floor.
|
||||
let _ = listener.HandleEvent_(cur_target.r(), event, ReportExceptions);
|
||||
let _ = listener.HandleEvent_(cur_target.r(), event, Report);
|
||||
|
||||
if event.stop_immediate() {
|
||||
break;
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
use dom::bindings::callback::ExceptionHandling::RethrowExceptions;
|
||||
use dom::bindings::callback::ExceptionHandling::Rethrow;
|
||||
use dom::bindings::codegen::Bindings::TreeWalkerBinding;
|
||||
use dom::bindings::codegen::Bindings::TreeWalkerBinding::TreeWalkerMethods;
|
||||
use dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
|
||||
|
@ -325,7 +325,7 @@ impl<'a> PrivateTreeWalkerHelpers<'a> for JSRef<'a, TreeWalker> {
|
|||
match self.filter {
|
||||
Filter::None => Ok(NodeFilterConstants::FILTER_ACCEPT),
|
||||
Filter::Native(f) => Ok((f)(node)),
|
||||
Filter::JS(callback) => callback.AcceptNode_(self, node, RethrowExceptions)
|
||||
Filter::JS(callback) => callback.AcceptNode_(self, node, Rethrow)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue