Bug 1032238 - Make some API functions handle Latin1 strings. r=luke

This commit is contained in:
Jan de Mooij 2014-07-02 13:04:12 +02:00
parent fe1c2b0ae7
commit aca5a16c0a
5 changed files with 47 additions and 19 deletions

View File

@ -5544,8 +5544,7 @@ JS_GetStringEncodingLength(JSContext *cx, JSString *str)
AssertHeapIsIdle(cx);
CHECK_REQUEST(cx);
const jschar *chars = str->getChars(cx);
if (!chars)
if (!str->ensureLinear(cx))
return size_t(-1);
return str->length();
}
@ -5562,10 +5561,21 @@ JS_EncodeStringToBuffer(JSContext *cx, JSString *str, char *buffer, size_t lengt
* error.
*/
size_t writtenLength = length;
const jschar *chars = str->getChars(nullptr);
if (!chars)
return size_t(-1);
if (DeflateStringToBuffer(nullptr, chars, str->length(), buffer, &writtenLength)) {
JSLinearString *linear = str->ensureLinear(cx);
if (!linear)
return size_t(-1);
bool res;
if (linear->hasLatin1Chars()) {
JS::AutoCheckCannotGC nogc;
res = DeflateStringToBuffer(nullptr, linear->latin1Chars(nogc), linear->length(), buffer,
&writtenLength);
} else {
JS::AutoCheckCannotGC nogc;
res = DeflateStringToBuffer(nullptr, linear->twoByteChars(nogc), linear->length(), buffer,
&writtenLength);
}
if (res) {
JS_ASSERT(writtenLength <= length);
return writtenLength;
}
@ -5624,12 +5634,12 @@ JS_Stringify(JSContext *cx, MutableHandleValue vp, HandleObject replacer,
CHECK_REQUEST(cx);
assertSameCompartment(cx, replacer, space);
StringBuffer sb(cx);
if (!sb.ensureTwoByteChars())
return false;
if (!js_Stringify(cx, vp, replacer, space, sb))
return false;
if (sb.empty()) {
HandlePropertyName null = cx->names().null;
return callback(null->chars(), null->length(), data);
}
if (sb.empty() && !sb.append(cx->names().null))
return false;
return callback(sb.rawTwoByteBegin(), sb.length(), data);
}

View File

@ -38,9 +38,13 @@ js::AutoEnterPolicy::reportErrorIfExceptionIsNotPending(JSContext *cx, jsid id)
JSMSG_OBJECT_ACCESS_DENIED);
} else {
JSString *str = IdToString(cx, id);
const jschar *prop = str ? str->getCharsZ(cx) : nullptr;
JS_ReportErrorNumberUC(cx, js_GetErrorMessage, nullptr,
JSMSG_PROPERTY_ACCESS_DENIED, prop);
AutoStableStringChars chars(cx);
const jschar *prop = nullptr;
if (str->ensureFlat(cx) && chars.initTwoByte(cx, str))
prop = chars.twoByteChars();
JS_ReportErrorNumberUC(cx, js_GetErrorMessage, nullptr, JSMSG_PROPERTY_ACCESS_DENIED,
prop);
}
}

View File

@ -4888,14 +4888,15 @@ js::InflateString(ThreadSafeContext *cx, const char *bytes, size_t *lengthp)
return nullptr;
}
template <typename CharT>
bool
js::DeflateStringToBuffer(JSContext *maybecx, const jschar *src, size_t srclen,
js::DeflateStringToBuffer(JSContext *maybecx, const CharT *src, size_t srclen,
char *dst, size_t *dstlenp)
{
size_t dstlen = *dstlenp;
if (srclen > dstlen) {
for (size_t i = 0; i < dstlen; i++)
dst[i] = (char) src[i];
dst[i] = char(src[i]);
if (maybecx) {
AutoSuppressGC suppress(maybecx);
JS_ReportErrorNumber(maybecx, js_GetErrorMessage, nullptr,
@ -4904,11 +4905,19 @@ js::DeflateStringToBuffer(JSContext *maybecx, const jschar *src, size_t srclen,
return false;
}
for (size_t i = 0; i < srclen; i++)
dst[i] = (char) src[i];
dst[i] = char(src[i]);
*dstlenp = srclen;
return true;
}
template bool
js::DeflateStringToBuffer(JSContext *maybecx, const Latin1Char *src, size_t srclen,
char *dst, size_t *dstlenp);
template bool
js::DeflateStringToBuffer(JSContext *maybecx, const jschar *src, size_t srclen,
char *dst, size_t *dstlenp);
#define ____ false
/*

View File

@ -319,8 +319,9 @@ CopyAndInflateChars(jschar *dst, const JS::Latin1Char *src, size_t srclen)
* must to be initialized with the buffer size and will contain on return the
* number of copied bytes.
*/
template <typename CharT>
extern bool
DeflateStringToBuffer(JSContext *maybecx, const jschar *chars,
DeflateStringToBuffer(JSContext *maybecx, const CharT *chars,
size_t charsLength, char *bytes, size_t *length);
/*

View File

@ -44,7 +44,9 @@ JSString::sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf)
// JSExtensibleString: count the full capacity, not just the used space.
if (isExtensible()) {
JSExtensibleString &extensible = asExtensible();
return mallocSizeOf(extensible.nonInlineChars());
return extensible.hasLatin1Chars()
? mallocSizeOf(extensible.rawLatin1Chars())
: mallocSizeOf(extensible.rawTwoByteChars());
}
// JSExternalString: don't count, the chars could be stored anywhere.
@ -59,7 +61,9 @@ JSString::sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf)
// JSUndependedString, there is no need to count the base string, for the
// same reason as JSDependentString above.
JSFlatString &flat = asFlat();
return mallocSizeOf(flat.chars());
return flat.hasLatin1Chars()
? mallocSizeOf(flat.rawLatin1Chars())
: mallocSizeOf(flat.rawTwoByteChars());
}
#ifdef DEBUG