Fix for bug 756257 (Replace xpc_qsDOMString with a conversion function in new DOM bindings). r=bz.

--HG--
extra : rebase_source : 3f5ac89f72fade970f9f3505b93e51ac44ee7525
This commit is contained in:
Peter Van der Beken 2012-05-18 23:25:46 +02:00
parent 6556500061
commit edd0f71b9b
2 changed files with 56 additions and 5 deletions

View File

@ -669,6 +669,58 @@ protected:
#endif
};
enum StringificationBehavior {
eStringify,
eEmpty,
eNull
};
static inline bool
ConvertJSValueToString(JSContext* cx, const JS::Value& v, JS::Value* pval,
StringificationBehavior nullBehavior,
StringificationBehavior undefinedBehavior,
nsDependentString& result)
{
JSString *s;
if (v.isString()) {
s = v.toString();
} else {
StringificationBehavior behavior;
if (v.isNull()) {
behavior = nullBehavior;
} else if (v.isUndefined()) {
behavior = undefinedBehavior;
} else {
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.
result.SetIsVoid(behavior != eEmpty);
return true;
}
s = JS_ValueToString(cx, v);
if (!s) {
return false;
}
pval->setString(s); // Root the new string.
}
size_t len;
const jschar *chars = JS_GetStringCharsZAndLength(cx, s, &len);
if (!chars) {
return false;
}
result.Rebind(chars, len);
return true;
}
} // namespace dom
} // namespace mozilla

View File

@ -1440,12 +1440,11 @@ ${declName}.SwapElements(arr);
undefinedBehavior = "eStringify"
return (
"const xpc_qsDOMString ${declName}(cx, ${val}, ${valPtr},\n"
" xpc_qsDOMString::%s,\n"
" xpc_qsDOMString::%s);\n"
"if (!${declName}.IsValid()) {\n"
"if (!ConvertJSValueToString(cx, ${val}, ${valPtr}, %s, %s, ${holderName})) {\n"
" return false;\n"
"}" % (nullBehavior, undefinedBehavior), None, None)
"}\n"
"${declName} = &${holderName};" % (nullBehavior, undefinedBehavior),
CGGeneric("NonNull<const nsAString>"), CGGeneric("nsDependentString"))
if type.isEnum():
if type.nullable():