Bug 771636 part 3. Implement default values for WebIDL strings. r=peterv

This commit is contained in:
Boris Zbarsky 2012-07-31 00:22:23 -04:00
Родитель 1b273923c5
Коммит 423774779f
4 изменённых файлов: 31 добавлений и 28 удалений

Просмотреть файл

@ -857,6 +857,7 @@ enum StringificationBehavior {
eNull
};
// pval must not be null and must point to a rooted JS::Value
static inline bool
ConvertJSValueToString(JSContext* cx, const JS::Value& v, JS::Value* pval,
StringificationBehavior nullBehavior,
@ -876,12 +877,7 @@ ConvertJSValueToString(JSContext* cx, const JS::Value& v, JS::Value* pval,
behavior = eStringify;
}
// If pval is null, that means the argument was optional and
// not passed; turn those into void strings if they're
// supposed to be stringified.
if (behavior != eStringify || !pval) {
// Here behavior == eStringify implies !pval, so both eNull and
// eStringify should end up with void strings.
if (behavior != eStringify) {
if (behavior == eEmpty) {
result.Truncate();
} else {

Просмотреть файл

@ -2040,17 +2040,24 @@ for (uint32_t i = 0; i < length; ++i) {
nullBehavior = "eStringify"
undefinedBehavior = "eStringify"
if defaultValue is not None:
if not isinstance(defaultValue, IDLNullValue):
raise TypeError("Can't handle non-null default values for "
"strings yet")
if not type.nullable():
raise TypeError("Null default value for non-nullable string")
val = "(${haveValue}) ? ${val} : JSVAL_NULL"
valPtr = "(${haveValue}) ? ${valPtr} : NULL"
else:
val = "${val}"
valPtr = "${valPtr}"
def getConversionCode(varName):
conversionCode = (
"if (!ConvertJSValueToString(cx, ${val}, ${valPtr}, %s, %s, %s)) {\n"
" return false;\n"
"}" % (nullBehavior, undefinedBehavior, varName))
if defaultValue is None:
return conversionCode
if isinstance(defaultValue, IDLNullValue):
assert(type.nullable())
return handleDefault(conversionCode,
"%s.SetNull()" % varName)
return handleDefault(
conversionCode,
("static const PRUnichar data[] = { %s, 0 };\n"
"%s.SetData(data, ArrayLength(data) - 1)" %
(", ".join("'" + char + "'" for char in defaultValue.value),
varName)))
if isMember:
# We have to make a copy, because our jsval may well not
@ -2059,25 +2066,20 @@ for (uint32_t i = 0; i < length; ++i) {
return (
"{\n"
" FakeDependentString str;\n"
" if (!ConvertJSValueToString(cx, %s, %s, %s, %s, str)) {\n"
" return false;\n"
" }\n"
"%s\n"
" ${declName} = str;\n"
"}\n" %
(val, valPtr, nullBehavior, undefinedBehavior),
declType, None,
isOptional)
"}\n" % CGIndenter(CGGeneric(getConversionCode("str"))).define(),
declType, None, isOptional)
if isOptional:
declType = "Optional<nsAString>"
else:
declType = "NonNull<nsAString>"
return (
"if (!ConvertJSValueToString(cx, %s, %s, %s, %s, ${holderName})) {\n"
" return false;\n"
"}\n"
"%s\n"
"const_cast<%s&>(${declName}) = &${holderName};" %
(val, valPtr, nullBehavior, undefinedBehavior, declType),
(getConversionCode("${holderName}"), declType),
CGGeneric("const " + declType), CGGeneric("FakeDependentString"),
# No need to deal with Optional here; we have handled it already
False)

Просмотреть файл

@ -335,6 +335,7 @@ public:
void PassString(const nsAString&, ErrorResult&);
void PassNullableString(const nsAString&, ErrorResult&);
void PassOptionalString(const Optional<nsAString>&, ErrorResult&);
void PassOptionalStringWithDefaultValue(const nsAString&, ErrorResult&);
void PassOptionalNullableString(const Optional<nsAString>&, ErrorResult&);
void PassOptionalNullableStringWithDefaultValue(const nsAString&, ErrorResult&);
@ -551,6 +552,7 @@ private:
void PassString(nsAString&, ErrorResult&) MOZ_DELETE;
void PassNullableString(nsAString&, ErrorResult&) MOZ_DELETE;
void PassOptionalString(Optional<nsAString>&, ErrorResult&) MOZ_DELETE;
void PassOptionalStringWithDefaultValue(nsAString&, ErrorResult&) MOZ_DELETE;
void PassOptionalNullableString(Optional<nsAString>&, ErrorResult&) MOZ_DELETE;
void PassOptionalNullableStringWithDefaultValue(nsAString&, ErrorResult&) MOZ_DELETE;

Просмотреть файл

@ -233,6 +233,7 @@ interface TestInterface {
void passString(DOMString arg);
void passNullableString(DOMString? arg);
void passOptionalString(optional DOMString arg);
void passOptionalStringWithDefaultValue(optional DOMString arg = "abc");
void passOptionalNullableString(optional DOMString? arg);
void passOptionalNullableStringWithDefaultValue(optional DOMString? arg = null);
@ -357,6 +358,8 @@ dictionary Dict : ParentDict {
long z = 9;
DOMString str;
TestEnum otherEnum = "b";
DOMString otherStr = "def";
DOMString? yetAnotherStr = null;
};
dictionary ParentDict : GrandparentDict {