mirror of
https://github.com/servo/servo.git
synced 2025-08-06 22:15:33 +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::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::jsapi::{JS_GetPendingException};
|
||||||
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 +137,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 +147,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 +159,40 @@ impl CallSetup {
|
||||||
self.cx
|
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
|
* 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;
|
||||||
|
|
|
@ -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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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());
|
||||||
|
|
2
components/servo/Cargo.lock
generated
2
components/servo/Cargo.lock
generated
|
@ -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
2
ports/cef/Cargo.lock
generated
|
@ -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
2
ports/gonk/Cargo.lock
generated
|
@ -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)",
|
||||||
]
|
]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue