auto merge of #4632 : jdm/servo/reporterr, r=Ms2ger

Rebased version of #4399 that addresses points 2 & 5 from https://critic.hoppipolla.co.uk/showcomment?chain=9848 . Introduction of an AutoJSAPI equivalent and setting the error reporter to null will come later, as they are much larger changes and we're currently blocking any further updates to rust-mozjs that are unrelated to error reporting.
This commit is contained in:
bors-servo 2015-01-14 12:30:49 -07:00
commit 4bb5822f91
7 changed files with 34 additions and 18 deletions

View file

@ -9,19 +9,20 @@
use dom::bindings::global::global_object_for_js_object; use dom::bindings::global::global_object_for_js_object;
use dom::bindings::js::JSRef; use dom::bindings::js::JSRef;
use dom::bindings::utils::Reflectable; use dom::bindings::utils::Reflectable;
use js::jsapi::{JSContext, JSObject, JS_WrapObject, JS_ObjectIsCallable}; use js::jsapi::{JSContext, JSObject, JS_WrapObject, JS_ObjectIsCallable, JS_GetGlobalObject};
use js::jsapi::JS_GetProperty; use js::jsapi::{JS_GetProperty, JS_IsExceptionPending, JS_ReportPendingException};
use js::jsval::{JSVal, UndefinedValue}; use js::jsval::{JSVal, UndefinedValue};
use js::rust::with_compartment;
use std::ptr; use std::ptr;
/// The exception handling used for a call. /// The exception handling used for a call.
#[deriving(Copy)] #[deriving(Copy, PartialEq)]
pub enum ExceptionHandling { pub enum ExceptionHandling {
/// Report any exception and don't throw it to the caller code. /// Report any exception and don't throw it to the caller code.
ReportExceptions, Report,
/// Throw any exception to the caller code. /// Throw any exception to the caller code.
RethrowExceptions Rethrow
} }
/// A common base class for representing IDL callback function types. /// A common base class for representing IDL callback function types.
@ -135,7 +136,7 @@ pub struct CallSetup {
/// The `JSContext` used for the call. /// The `JSContext` used for the call.
cx: *mut JSContext, cx: *mut JSContext,
/// The exception handling used for the call. /// The exception handling used for the call.
_handling: ExceptionHandling _handling: ExceptionHandling,
} }
impl CallSetup { impl CallSetup {
@ -145,9 +146,10 @@ impl CallSetup {
let global = global_object_for_js_object(callback.callback()); let global = global_object_for_js_object(callback.callback());
let global = global.root(); let global = global.root();
let cx = global.r().get_cx(); let cx = global.r().get_cx();
CallSetup { CallSetup {
cx: cx, cx: cx,
_handling: handling _handling: handling,
} }
} }
@ -156,3 +158,17 @@ impl CallSetup {
self.cx self.cx
} }
} }
impl Drop for CallSetup {
fn drop(&mut self) {
let need_to_deal_with_exception = unsafe { JS_IsExceptionPending(self.cx) } != 0;
if need_to_deal_with_exception {
unsafe {
let old_global = JS_GetGlobalObject(self.cx);
with_compartment(self.cx, old_global, || {
JS_ReportPendingException(self.cx)
});
}
}
}
}

View file

@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * 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/. */ * 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::Bindings::EventBinding::EventMethods;
use dom::bindings::codegen::InheritTypes::{EventTargetCast, NodeCast, NodeDerived}; use dom::bindings::codegen::InheritTypes::{EventTargetCast, NodeCast, NodeDerived};
use dom::bindings::js::{JS, JSRef, OptionalRootable, Root}; 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()); event.set_current_target(cur_target.r());
for listener in listeners.iter() { for listener in listeners.iter() {
// Explicitly drop any exception on the floor. // 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() { if event.stop_immediate() {
break; break;
@ -74,7 +74,7 @@ pub fn dispatch_event<'a, 'b>(target: JSRef<'a, EventTarget>,
for listeners in opt_listeners.iter() { for listeners in opt_listeners.iter() {
for listener in listeners.iter() { for listener in listeners.iter() {
// Explicitly drop any exception on the floor. // Explicitly drop any exception on the floor.
let _ = listener.HandleEvent_(target, event, ReportExceptions); let _ = listener.HandleEvent_(target, event, Report);
if event.stop_immediate() { if event.stop_immediate() {
break; break;
@ -93,7 +93,7 @@ pub fn dispatch_event<'a, 'b>(target: JSRef<'a, EventTarget>,
event.set_current_target(cur_target.r()); event.set_current_target(cur_target.r());
for listener in listeners.iter() { for listener in listeners.iter() {
// Explicitly drop any exception on the floor. // 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() { if event.stop_immediate() {
break; break;

View file

@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * 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/. */ * 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;
use dom::bindings::codegen::Bindings::TreeWalkerBinding::TreeWalkerMethods; use dom::bindings::codegen::Bindings::TreeWalkerBinding::TreeWalkerMethods;
use dom::bindings::codegen::Bindings::NodeBinding::NodeMethods; use dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
@ -325,7 +325,7 @@ impl<'a> PrivateTreeWalkerHelpers<'a> for JSRef<'a, TreeWalker> {
match self.filter { match self.filter {
Filter::None => Ok(NodeFilterConstants::FILTER_ACCEPT), Filter::None => Ok(NodeFilterConstants::FILTER_ACCEPT),
Filter::Native(f) => Ok((f)(node)), Filter::Native(f) => Ok((f)(node)),
Filter::JS(callback) => callback.AcceptNode_(self, node, RethrowExceptions) Filter::JS(callback) => callback.AcceptNode_(self, node, Rethrow)
} }
} }

View file

@ -3,7 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use dom::bindings::cell::DOMRefCell; use dom::bindings::cell::DOMRefCell;
use dom::bindings::callback::ExceptionHandling::ReportExceptions; use dom::bindings::callback::ExceptionHandling::Report;
use dom::bindings::codegen::Bindings::FunctionBinding::Function; use dom::bindings::codegen::Bindings::FunctionBinding::Function;
use dom::bindings::js::JSRef; use dom::bindings::js::JSRef;
use dom::bindings::utils::Reflectable; use dom::bindings::utils::Reflectable;
@ -189,7 +189,7 @@ impl TimerManager {
// TODO: Must handle rooting of funval and args when movable GC is turned on // TODO: Must handle rooting of funval and args when movable GC is turned on
match data.callback { match data.callback {
TimerCallback::FunctionTimerCallback(function) => { TimerCallback::FunctionTimerCallback(function) => {
let _ = function.Call_(this, data.args, ReportExceptions); let _ = function.Call_(this, data.args, Report);
} }
TimerCallback::StringTimerCallback(code_str) => { TimerCallback::StringTimerCallback(code_str) => {
this.evaluate_js_on_global_with_result(code_str.as_slice()); this.evaluate_js_on_global_with_result(code_str.as_slice());

View file

@ -419,7 +419,7 @@ dependencies = [
[[package]] [[package]]
name = "js" name = "js"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/servo/rust-mozjs#e01a846241bd98ac424878e3f8d3e9b989c79242" source = "git+https://github.com/servo/rust-mozjs#5a69e377d6ab7ea8601f711443994f1c8172c7a8"
dependencies = [ dependencies = [
"mozjs-sys 0.0.0 (git+https://github.com/servo/mozjs)", "mozjs-sys 0.0.0 (git+https://github.com/servo/mozjs)",
] ]

2
ports/cef/Cargo.lock generated
View file

@ -387,7 +387,7 @@ dependencies = [
[[package]] [[package]]
name = "js" name = "js"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/servo/rust-mozjs#e01a846241bd98ac424878e3f8d3e9b989c79242" source = "git+https://github.com/servo/rust-mozjs#5a69e377d6ab7ea8601f711443994f1c8172c7a8"
dependencies = [ dependencies = [
"mozjs-sys 0.0.0 (git+https://github.com/servo/mozjs)", "mozjs-sys 0.0.0 (git+https://github.com/servo/mozjs)",
] ]

2
ports/gonk/Cargo.lock generated
View file

@ -341,7 +341,7 @@ dependencies = [
[[package]] [[package]]
name = "js" name = "js"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/servo/rust-mozjs#e01a846241bd98ac424878e3f8d3e9b989c79242" source = "git+https://github.com/servo/rust-mozjs#5a69e377d6ab7ea8601f711443994f1c8172c7a8"
dependencies = [ dependencies = [
"mozjs-sys 0.0.0 (git+https://github.com/servo/mozjs)", "mozjs-sys 0.0.0 (git+https://github.com/servo/mozjs)",
] ]