Make DOMString a newtype around String, rather than a typedef.

This should make it somewhat easier to experiment with alternative
representations in the future. To reduce churn, this commit leaves the String
field public, though.

Also, this will allow us to use the default String type to represent the IDL
USVString type, which explicitly forbids unpaired surrogates, ans as such is
a better match to the Rust String type.
This commit is contained in:
Ms2ger 2015-11-03 14:16:55 +01:00
parent e6aa976462
commit 6b75078503
83 changed files with 393 additions and 297 deletions

View file

@ -829,7 +829,7 @@ def getJSToNativeConversionInfo(type, descriptorProvider, failureCode=None,
default = "None"
else:
assert defaultValue.type.tag() == IDLType.Tags.domstring
default = '"%s".to_owned()' % defaultValue.value
default = 'DOMString("%s".to_owned())' % defaultValue.value
if type.nullable():
default = "Some(%s)" % default

View file

@ -430,6 +430,13 @@ impl ToJSValConvertible for str {
}
}
//http://heycam.github.io/webidl/#es-DOMString
impl ToJSValConvertible for String {
fn to_jsval(&self, cx: *mut JSContext, rval: MutableHandleValue) {
(**self).to_jsval(cx, rval);
}
}
//http://heycam.github.io/webidl/#es-DOMString
impl ToJSValConvertible for DOMString {
fn to_jsval(&self, cx: *mut JSContext, rval: MutableHandleValue) {
@ -451,7 +458,7 @@ pub enum StringificationBehavior {
pub fn jsstring_to_str(cx: *mut JSContext, s: *mut JSString) -> DOMString {
let mut length = 0;
let latin1 = unsafe { JS_StringHasLatin1Chars(s) };
if latin1 {
DOMString(if latin1 {
let chars = unsafe {
JS_GetLatin1StringCharsAndLength(cx, ptr::null(), s, &mut length)
};
@ -496,7 +503,7 @@ pub fn jsstring_to_str(cx: *mut JSContext, s: *mut JSString) -> DOMString {
}
}
s
}
})
}
/// Convert the given `jsid` to a `DOMString`. Fails if the `jsid` is not a
@ -548,7 +555,7 @@ impl FromJSValConvertible for USVString {
}
let latin1 = unsafe { JS_StringHasLatin1Chars(jsstr) };
if latin1 {
return Ok(USVString(jsstring_to_str(cx, jsstr)));
return Ok(USVString(jsstring_to_str(cx, jsstr).0));
}
unsafe {
let mut length = 0;

View file

@ -18,7 +18,6 @@ use libc;
use std::ffi::CString;
use std::{mem, ptr};
use util::mem::HeapSizeOf;
use util::str::DOMString;
/// DOM exceptions that can be thrown by a native DOM method.
#[derive(Debug, Clone, HeapSizeOf)]
@ -65,9 +64,9 @@ pub enum Error {
TypeMismatch,
/// TypeError JavaScript Error
Type(DOMString),
Type(String),
/// RangeError JavaScript Error
Range(DOMString),
Range(String),
/// A JavaScript exception is already pending.
JSFailed,

View file

@ -83,7 +83,7 @@ use string_cache::{Atom, Namespace};
use style::properties::PropertyDeclarationBlock;
use style::values::specified::Length;
use url::Url;
use util::str::{LengthOrPercentageOrAuto};
use util::str::{DOMString, LengthOrPercentageOrAuto};
/// A trait to allow tracing (only) DOM objects.
@ -285,6 +285,7 @@ no_jsmanaged_fields!(MemProfilerChan);
no_jsmanaged_fields!(PseudoElement);
no_jsmanaged_fields!(Length);
no_jsmanaged_fields!(ElementState);
no_jsmanaged_fields!(DOMString);
impl JSTraceable for Box<ScriptChan + Send> {
#[inline]

View file

@ -452,7 +452,7 @@ pub fn find_enum_string_index(cx: *mut JSContext,
}
let search = jsstring_to_str(cx, jsstr);
Ok(values.iter().position(|value| value == &search))
Ok(values.iter().position(|value| search == *value))
}
/// Returns wether `obj` is a platform object