Implement USVString.

This commit is contained in:
Ms2ger 2015-03-13 16:51:22 +01:00
parent 4157a2b02b
commit bbbdb98897
5 changed files with 93 additions and 2 deletions

View file

@ -19,6 +19,7 @@
//! | float | `f32` |
//! | double | `f64` |
//! | DOMString | `DOMString` |
//! | USVString | `USVString` |
//! | ByteString | `ByteString` |
//! | object | `*mut JSObject` |
//! | interface types | `JSRef<T>` | `Temporary<T>` |
@ -31,7 +32,7 @@
use dom::bindings::codegen::PrototypeList;
use dom::bindings::js::{JSRef, Root, Unrooted};
use dom::bindings::str::ByteString;
use dom::bindings::str::{ByteString, USVString};
use dom::bindings::utils::{Reflectable, Reflector, DOMClass};
use util::str::DOMString;
@ -339,6 +340,31 @@ impl FromJSValConvertible for DOMString {
}
}
impl ToJSValConvertible for USVString {
fn to_jsval(&self, cx: *mut JSContext) -> JSVal {
self.0.to_jsval(cx)
}
}
impl FromJSValConvertible for USVString {
type Config = ();
fn from_jsval(cx: *mut JSContext, value: JSVal, _: ())
-> Result<USVString, ()> {
let jsstr = unsafe { JS_ValueToString(cx, value) };
if jsstr.is_null() {
debug!("JS_ValueToString failed");
Err(())
} else {
unsafe {
let mut length = 0;
let chars = JS_GetStringCharsAndLength(cx, jsstr, &mut length);
let char_vec = slice::from_raw_parts(chars, length as usize);
Ok(USVString(String::from_utf16_lossy(char_vec)))
}
}
}
}
impl ToJSValConvertible for ByteString {
fn to_jsval(&self, cx: *mut JSContext) -> JSVal {
unsafe {