auto merge of #4496 : yodalee/servo/issue4484-ToJSValConvertible-for-str, r=jdm

#4484 
Add ToJSValConvertible trait to str type.
This commit is contained in:
bors-servo 2015-01-01 03:00:44 -07:00
commit fda38cf673
3 changed files with 11 additions and 5 deletions

View file

@ -2781,7 +2781,7 @@ pub const strings: &'static [&'static str] = &[
impl ToJSValConvertible for super::%s { impl ToJSValConvertible for super::%s {
fn to_jsval(&self, cx: *mut JSContext) -> JSVal { fn to_jsval(&self, cx: *mut JSContext) -> JSVal {
strings[*self as uint].into_string().to_jsval(cx) strings[*self as uint].to_jsval(cx)
} }
} }
""" % (",\n ".join(['"%s"' % val for val in enum.values()]), enum.identifier.name) """ % (",\n ".join(['"%s"' % val for val in enum.values()]), enum.identifier.name)

View file

@ -44,7 +44,7 @@ pub trait IDLInterface {
} }
/// A trait to convert Rust types to `JSVal`s. /// A trait to convert Rust types to `JSVal`s.
pub trait ToJSValConvertible { pub trait ToJSValConvertible for Sized? {
/// Convert `self` to a `JSVal`. JSAPI failure causes a task failure. /// Convert `self` to a `JSVal`. JSAPI failure causes a task failure.
fn to_jsval(&self, cx: *mut JSContext) -> JSVal; fn to_jsval(&self, cx: *mut JSContext) -> JSVal;
} }
@ -232,10 +232,10 @@ impl FromJSValConvertible<()> for f64 {
} }
} }
impl ToJSValConvertible for DOMString { impl ToJSValConvertible for str {
fn to_jsval(&self, cx: *mut JSContext) -> JSVal { fn to_jsval(&self, cx: *mut JSContext) -> JSVal {
unsafe { unsafe {
let string_utf16: Vec<u16> = self.as_slice().utf16_units().collect(); let string_utf16: Vec<u16> = self.utf16_units().collect();
let jsstr = JS_NewUCStringCopyN(cx, string_utf16.as_ptr(), string_utf16.len() as libc::size_t); let jsstr = JS_NewUCStringCopyN(cx, string_utf16.as_ptr(), string_utf16.len() as libc::size_t);
if jsstr.is_null() { if jsstr.is_null() {
panic!("JS_NewUCStringCopyN failed"); panic!("JS_NewUCStringCopyN failed");
@ -245,6 +245,12 @@ impl ToJSValConvertible for DOMString {
} }
} }
impl ToJSValConvertible for DOMString {
fn to_jsval(&self, cx: *mut JSContext) -> JSVal {
self.as_slice().to_jsval(cx)
}
}
/// Behavior for stringification of `JSVal`s. /// Behavior for stringification of `JSVal`s.
#[deriving(PartialEq)] #[deriving(PartialEq)]
pub enum StringificationBehavior { pub enum StringificationBehavior {

View file

@ -695,7 +695,7 @@ impl<'a> XMLHttpRequestMethods for JSRef<'a, XMLHttpRequest> {
if ready_state == XMLHttpRequestState::XHRDone || ready_state == XMLHttpRequestState::Loading { if ready_state == XMLHttpRequestState::XHRDone || ready_state == XMLHttpRequestState::Loading {
self.text_response().to_jsval(cx) self.text_response().to_jsval(cx)
} else { } else {
"".into_string().to_jsval(cx) "".to_jsval(cx)
} }
}, },
_ if self.ready_state.get() != XMLHttpRequestState::XHRDone => NullValue(), _ if self.ready_state.get() != XMLHttpRequestState::XHRDone => NullValue(),