Upgrade to SM 39

This commit is contained in:
Michael Wu 2015-04-06 19:27:56 -04:00
parent a256f39796
commit 675267b782
205 changed files with 6546 additions and 5340 deletions

View file

@ -6,20 +6,22 @@
use dom::bindings::conversions::ToJSValConvertible;
use dom::bindings::global::GlobalRef;
use dom::bindings::js::Rootable;
use dom::domexception::{DOMException, DOMErrorName};
use util::str::DOMString;
use js::jsapi::{JSContext, JSObject};
use js::jsapi::{JSContext, JSObject, RootedValue};
use js::jsapi::{JS_IsExceptionPending, JS_SetPendingException, JS_ReportPendingException};
use js::jsapi::{JS_ReportErrorNumber, JSErrorFormatString, JSEXN_TYPEERR, JSEXN_RANGEERR};
use js::jsapi::{JS_ReportErrorNumber1, JSErrorFormatString, JSExnType};
use js::jsapi::{JS_SaveFrameChain, JS_RestoreFrameChain};
use js::rust::with_compartment;
use js::jsapi::JSAutoCompartment;
use js::jsval::UndefinedValue;
use js::JSFalse;
use libc;
use std::ffi::CString;
use std::ptr;
use std::mem;
/// DOM exceptions that can be thrown by a native DOM method.
#[derive(Debug, Clone)]
@ -116,10 +118,11 @@ pub fn throw_dom_exception(cx: *mut JSContext, global: GlobalRef,
};
assert!(unsafe { JS_IsExceptionPending(cx) } == 0);
let exception = DOMException::new(global, code).root();
let thrown = exception.to_jsval(cx);
let exception = DOMException::new(global, code);
let mut thrown = RootedValue::new(cx, UndefinedValue());
exception.to_jsval(cx, thrown.handle_mut());
unsafe {
JS_SetPendingException(cx, thrown);
JS_SetPendingException(cx, thrown.handle());
}
}
@ -128,9 +131,10 @@ pub fn report_pending_exception(cx: *mut JSContext, obj: *mut JSObject) {
unsafe {
if JS_IsExceptionPending(cx) != 0 {
let saved = JS_SaveFrameChain(cx);
with_compartment(cx, obj, || {
{
let _ac = JSAutoCompartment::new(cx, obj);
JS_ReportPendingException(cx);
});
}
if saved != 0 {
JS_RestoreFrameChain(cx);
}
@ -158,25 +162,26 @@ static ERROR_FORMAT_STRING_STRING: [libc::c_char; 4] = [
static mut TYPE_ERROR_FORMAT_STRING: JSErrorFormatString = JSErrorFormatString {
format: &ERROR_FORMAT_STRING_STRING as *const libc::c_char,
argCount: 1,
exnType: JSEXN_TYPEERR as i16,
exnType: JSExnType::JSEXN_TYPEERR as i16,
};
/// Format string struct used to throw `RangeError`s.
static mut RANGE_ERROR_FORMAT_STRING: JSErrorFormatString = JSErrorFormatString {
format: &ERROR_FORMAT_STRING_STRING as *const libc::c_char,
argCount: 1,
exnType: JSEXN_RANGEERR as i16,
exnType: JSExnType::JSEXN_RANGEERR as i16,
};
/// Callback used to throw javascript errors.
/// See throw_js_error for info about error_number.
unsafe extern fn get_error_message(_user_ref: *mut libc::c_void,
_locale: *const libc::c_char,
error_number: libc::c_uint) -> *const JSErrorFormatString
error_number: libc::c_uint)
-> *const JSErrorFormatString
{
match error_number as i32 {
JSEXN_TYPEERR => &TYPE_ERROR_FORMAT_STRING as *const JSErrorFormatString,
JSEXN_RANGEERR => &RANGE_ERROR_FORMAT_STRING as *const JSErrorFormatString,
let num: JSExnType = mem::transmute(error_number);
match num {
JSExnType::JSEXN_TYPEERR => &TYPE_ERROR_FORMAT_STRING as *const JSErrorFormatString,
JSExnType::JSEXN_RANGEERR => &RANGE_ERROR_FORMAT_STRING as *const JSErrorFormatString,
_ => panic!("Bad js error number given to get_error_message: {}", error_number)
}
}
@ -188,20 +193,18 @@ unsafe extern fn get_error_message(_user_ref: *mut libc::c_void,
fn throw_js_error(cx: *mut JSContext, error: &str, error_number: u32) {
let error = CString::new(error).unwrap();
unsafe {
JS_ReportErrorNumber(cx,
Some(get_error_message as
unsafe extern "C" fn(*mut libc::c_void, *const libc::c_char,
libc::c_uint) -> *const JSErrorFormatString),
JS_ReportErrorNumber1(cx,
Some(get_error_message),
ptr::null_mut(), error_number, error.as_ptr());
}
}
/// Throw a `TypeError` with the given message.
pub fn throw_type_error(cx: *mut JSContext, error: &str) {
throw_js_error(cx, error, JSEXN_TYPEERR as u32);
throw_js_error(cx, error, JSExnType::JSEXN_TYPEERR as u32);
}
/// Throw a `RangeError` with the given message.
pub fn throw_range_error(cx: *mut JSContext, error: &str) {
throw_js_error(cx, error, JSEXN_RANGEERR as u32);
throw_js_error(cx, error, JSExnType::JSEXN_RANGEERR as u32);
}