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

@ -717,6 +717,32 @@ def getJSToNativeConversionInfo(type, descriptorProvider, failureCode=None,
return handleOptional(conversionCode, CGGeneric(declType), default)
if type.isUSVString():
assert not isEnforceRange and not isClamp
conversionCode = (
"match FromJSValConvertible::from_jsval(cx, ${val}, ()) {\n"
" Ok(strval) => strval,\n"
" Err(_) => { %s },\n"
"}" % exceptionCode)
if defaultValue is None:
default = None
elif isinstance(defaultValue, IDLNullValue):
assert type.nullable()
default = "None"
else:
assert defaultValue.type.tag() in (IDLType.Tags.domstring, IDLType.Tags.usvstring)
default = 'USVString("%s".to_owned())' % defaultValue.value
if type.nullable():
default = "Some(%s)" % default
declType = "USVString"
if type.nullable():
declType = "Option<%s>" % declType
return handleOptional(conversionCode, CGGeneric(declType), default)
if type.isByteString():
assert not isEnforceRange and not isClamp
@ -1086,6 +1112,11 @@ def getRetvalDeclarationForType(returnType, descriptorProvider):
if returnType.nullable():
result = CGWrapper(result, pre="Option<", post=">")
return result
if returnType.isUSVString():
result = CGGeneric("USVString")
if returnType.nullable():
result = CGWrapper(result, pre="Option<", post=">")
return result
if returnType.isByteString():
result = CGGeneric("ByteString")
if returnType.nullable():
@ -4647,6 +4678,7 @@ class CGBindingRoot(CGThing):
'dom::bindings::proxyhandler::{fill_property_descriptor, get_expando_object}',
'dom::bindings::proxyhandler::{get_property_descriptor}',
'dom::bindings::str::ByteString',
'dom::bindings::str::USVString',
'libc',
'util::str::DOMString',
'std::borrow::ToOwned',

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 {

View file

@ -157,3 +157,7 @@ impl FromStr for ByteString {
Ok(ByteString::new(s.to_owned().into_bytes()))
}
}
/// A string that is constructed from a UCS-2 buffer by replacing invalid code
/// points with the replacement character.
pub struct USVString(pub String);