Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add str ToJSValConvertible for str type
  • Loading branch information
yodalee committed Dec 31, 2014
1 parent 37a97f3 commit 6f569de
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion components/script/dom/bindings/conversions.rs
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 6f569de

Please sign in to comment.