add str ToJSValConvertible for str type

This commit is contained in:
yodalee 2014-12-28 22:42:13 +08:00
parent 37a97f3273
commit 6f569dee92

View file

@ -44,7 +44,7 @@ pub trait IDLInterface {
}
/// 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.
fn to_jsval(&self, cx: *mut JSContext) -> JSVal;
}
@ -232,6 +232,19 @@ impl FromJSValConvertible<()> for f64 {
}
}
impl ToJSValConvertible for str {
fn to_jsval(&self, cx: *mut JSContext) -> JSVal {
unsafe {
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);
if jsstr.is_null() {
panic!("JS_NewUCStringCopyN failed");
}
StringValue(&*jsstr)
}
}
}
impl ToJSValConvertible for DOMString {
fn to_jsval(&self, cx: *mut JSContext) -> JSVal {
unsafe {