diff options
author | yodalee <lc85301@gmail.com> | 2014-12-28 22:42:13 +0800 |
---|---|---|
committer | yodalee <lc85301@gmail.com> | 2014-12-31 20:20:44 +0800 |
commit | 6f569dee928e4fb021f24829d34372bf7feba867 (patch) | |
tree | e86667c761d0289537bf1f88d4d7e29150fe1a82 | |
parent | 37a97f3273c442fa59a3f65e8300a2527b004036 (diff) | |
download | servo-6f569dee928e4fb021f24829d34372bf7feba867.tar.gz servo-6f569dee928e4fb021f24829d34372bf7feba867.zip |
add str ToJSValConvertible for str type
-rw-r--r-- | components/script/dom/bindings/conversions.rs | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/components/script/dom/bindings/conversions.rs b/components/script/dom/bindings/conversions.rs index 7cc6ef2cdaf..a08e8dbb7e8 100644 --- a/components/script/dom/bindings/conversions.rs +++ b/components/script/dom/bindings/conversions.rs @@ -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 { |