Bug 657537: clean up jschar inflation/deflation. (r=Waldo)

This commit is contained in:
Chris Leary 2011-05-26 15:50:05 -07:00
Родитель 1087242888
Коммит 3df1dd05de
20 изменённых файлов: 188 добавлений и 189 удалений

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

@ -1842,7 +1842,7 @@ ImplicitConvert(JSContext* cx,
case TYPE_unsigned_char: {
// Convert from UTF-16 to UTF-8.
size_t nbytes =
js_GetDeflatedUTF8StringLength(cx, sourceChars, sourceLength);
GetDeflatedUTF8StringLength(cx, sourceChars, sourceLength);
if (nbytes == (size_t) -1)
return false;
@ -1853,7 +1853,7 @@ ImplicitConvert(JSContext* cx,
return false;
}
ASSERT_OK(js_DeflateStringToUTF8Buffer(cx, sourceChars, sourceLength,
ASSERT_OK(DeflateStringToUTF8Buffer(cx, sourceChars, sourceLength,
*charBuffer, &nbytes));
(*charBuffer)[nbytes] = 0;
*freePointer = true;
@ -1899,7 +1899,7 @@ ImplicitConvert(JSContext* cx,
case TYPE_unsigned_char: {
// Convert from UTF-16 to UTF-8.
size_t nbytes =
js_GetDeflatedUTF8StringLength(cx, sourceChars, sourceLength);
GetDeflatedUTF8StringLength(cx, sourceChars, sourceLength);
if (nbytes == (size_t) -1)
return false;
@ -1909,7 +1909,7 @@ ImplicitConvert(JSContext* cx,
}
char* charBuffer = static_cast<char*>(buffer);
ASSERT_OK(js_DeflateStringToUTF8Buffer(cx, sourceChars, sourceLength,
ASSERT_OK(DeflateStringToUTF8Buffer(cx, sourceChars, sourceLength,
charBuffer, &nbytes));
if (targetLength > nbytes)
@ -3584,7 +3584,7 @@ ArrayType::ConstructData(JSContext* cx,
case TYPE_signed_char:
case TYPE_unsigned_char: {
// Determine the UTF-8 length.
length = js_GetDeflatedUTF8StringLength(cx, sourceChars, sourceLength);
length = GetDeflatedUTF8StringLength(cx, sourceChars, sourceLength);
if (length == (size_t) -1)
return false;
@ -5766,7 +5766,7 @@ CData::ReadString(JSContext* cx, uintN argc, jsval* vp)
// Determine the length.
size_t dstlen;
if (!js_InflateUTF8StringToBuffer(cx, bytes, length, NULL, &dstlen))
if (!InflateUTF8StringToBuffer(cx, bytes, length, NULL, &dstlen))
return JS_FALSE;
jschar* dst =
@ -5774,7 +5774,7 @@ CData::ReadString(JSContext* cx, uintN argc, jsval* vp)
if (!dst)
return JS_FALSE;
ASSERT_OK(js_InflateUTF8StringToBuffer(cx, bytes, length, dst, &dstlen));
ASSERT_OK(InflateUTF8StringToBuffer(cx, bytes, length, dst, &dstlen));
dst[dstlen] = 0;
result = JS_NewUCString(cx, dst, dstlen);

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

@ -159,7 +159,7 @@ Library::Create(JSContext* cx, jsval path, JSCTypesCallbacks* callbacks)
// Fallback: assume the platform native charset is UTF-8. This is true
// for Mac OS X, Android, and probably Linux.
size_t nbytes =
js_GetDeflatedUTF8StringLength(cx, pathStr->chars(), pathStr->length());
GetDeflatedUTF8StringLength(cx, pathStr->chars(), pathStr->length());
if (nbytes == (size_t) -1)
return NULL;
@ -167,7 +167,7 @@ Library::Create(JSContext* cx, jsval path, JSCTypesCallbacks* callbacks)
if (!pathBytes)
return NULL;
ASSERT_OK(js_DeflateStringToUTF8Buffer(cx, pathStr->chars(),
ASSERT_OK(DeflateStringToUTF8Buffer(cx, pathStr->chars(),
pathStr->length(), pathBytes, &nbytes));
pathBytes[nbytes] = 0;
}

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

@ -8,6 +8,8 @@
#include "tests.h"
#include "jsstr.h"
using namespace js;
class AutoInflatedString {
JSContext * const cx;
jschar *chars_;
@ -21,7 +23,7 @@ class AutoInflatedString {
template<size_t N> void operator=(const char (&str)[N]) {
length_ = N - 1;
chars_ = js_InflateString(cx, str, &length_);
chars_ = InflateString(cx, str, &length_);
if (!chars_)
abort();
}

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

@ -4519,7 +4519,7 @@ JS_CompileScriptForPrincipals(JSContext *cx, JSObject *obj,
JS_THREADSAFE_ASSERT(cx->compartment != cx->runtime->atomsCompartment);
CHECK_REQUEST(cx);
jschar *chars = js_InflateString(cx, bytes, &length);
jschar *chars = InflateString(cx, bytes, &length);
if (!chars)
return NULL;
JSObject *scriptObj =
@ -4547,9 +4547,9 @@ JS_BufferIsCompilableUnit(JSContext *cx, JSBool bytes_are_utf8, JSObject *obj, c
CHECK_REQUEST(cx);
assertSameCompartment(cx, obj);
if (bytes_are_utf8)
chars = js_InflateString(cx, bytes, &length, JS_TRUE);
chars = InflateString(cx, bytes, &length, CESU8Encoding);
else
chars = js_InflateString(cx, bytes, &length);
chars = InflateString(cx, bytes, &length);
if (!chars)
return JS_TRUE;
@ -4845,7 +4845,7 @@ JS_CompileFunctionForPrincipals(JSContext *cx, JSObject *obj,
const char *filename, uintN lineno)
{
JS_THREADSAFE_ASSERT(cx->compartment != cx->runtime->atomsCompartment);
jschar *chars = js_InflateString(cx, bytes, &length);
jschar *chars = InflateString(cx, bytes, &length);
if (!chars)
return NULL;
JSFunction *fun = JS_CompileUCFunctionForPrincipals(cx, obj, principals, name,
@ -5009,7 +5009,7 @@ JS_EvaluateScriptForPrincipals(JSContext *cx, JSObject *obj, JSPrincipals *princ
{
JS_THREADSAFE_ASSERT(cx->compartment != cx->runtime->atomsCompartment);
size_t length = nbytes;
jschar *chars = js_InflateString(cx, bytes, &length);
jschar *chars = InflateString(cx, bytes, &length);
if (!chars)
return JS_FALSE;
JSBool ok = JS_EvaluateUCScriptForPrincipals(cx, obj, principals, chars, length,
@ -5234,7 +5234,7 @@ JS_NewStringCopyZ(JSContext *cx, const char *s)
if (!s)
return cx->runtime->emptyString;
n = strlen(s);
js = js_InflateString(cx, s, &n);
js = InflateString(cx, s, &n);
if (!js)
return NULL;
str = js_NewString(cx, js, n);
@ -5454,7 +5454,7 @@ JS_EncodeCharacters(JSContext *cx, const jschar *src, size_t srclen, char *dst,
{
size_t n;
if (!dst) {
n = js_GetDeflatedStringLength(cx, src, srclen);
n = GetDeflatedStringLength(cx, src, srclen);
if (n == (size_t)-1) {
*dstlenp = 0;
return JS_FALSE;
@ -5463,20 +5463,20 @@ JS_EncodeCharacters(JSContext *cx, const jschar *src, size_t srclen, char *dst,
return JS_TRUE;
}
return js_DeflateStringToBuffer(cx, src, srclen, dst, dstlenp);
return DeflateStringToBuffer(cx, src, srclen, dst, dstlenp);
}
JS_PUBLIC_API(JSBool)
JS_DecodeBytes(JSContext *cx, const char *src, size_t srclen, jschar *dst, size_t *dstlenp)
{
return js_InflateStringToBuffer(cx, src, srclen, dst, dstlenp);
return InflateStringToBuffer(cx, src, srclen, dst, dstlenp);
}
JS_PUBLIC_API(JSBool)
JS_DecodeUTF8(JSContext *cx, const char *src, size_t srclen, jschar *dst,
size_t *dstlenp)
{
return js_InflateUTF8StringToBuffer(cx, src, srclen, dst, dstlenp);
return InflateUTF8StringToBuffer(cx, src, srclen, dst, dstlenp);
}
JS_PUBLIC_API(char *)
@ -5485,7 +5485,7 @@ JS_EncodeString(JSContext *cx, JSString *str)
const jschar *chars = str->getChars(cx);
if (!chars)
return NULL;
return js_DeflateString(cx, chars, str->length());
return DeflateString(cx, chars, str->length());
}
JS_PUBLIC_API(size_t)
@ -5494,14 +5494,14 @@ JS_GetStringEncodingLength(JSContext *cx, JSString *str)
const jschar *chars = str->getChars(cx);
if (!chars)
return size_t(-1);
return js_GetDeflatedStringLength(cx, chars, str->length());
return GetDeflatedStringLength(cx, chars, str->length());
}
JS_PUBLIC_API(size_t)
JS_EncodeStringToBuffer(JSString *str, char *buffer, size_t length)
{
/*
* FIXME bug 612141 - fix js_DeflateStringToBuffer interface so the result
* FIXME bug 612141 - fix DeflateStringToBuffer interface so the result
* would allow to distinguish between insufficient buffer and encoding
* error.
*/
@ -5509,12 +5509,12 @@ JS_EncodeStringToBuffer(JSString *str, char *buffer, size_t length)
const jschar *chars = str->getChars(NULL);
if (!chars)
return size_t(-1);
if (js_DeflateStringToBuffer(NULL, chars, str->length(), buffer, &writtenLength)) {
if (DeflateStringToBuffer(NULL, chars, str->length(), buffer, &writtenLength)) {
JS_ASSERT(writtenLength <= length);
return writtenLength;
}
JS_ASSERT(writtenLength <= length);
size_t necessaryLength = js_GetDeflatedStringLength(NULL, chars, str->length());
size_t necessaryLength = GetDeflatedStringLength(NULL, chars, str->length());
if (necessaryLength == size_t(-1))
return size_t(-1);
if (writtenLength != length) {
@ -5796,7 +5796,7 @@ JS_PUBLIC_API(JSObject *)
JS_NewRegExpObject(JSContext *cx, JSObject *obj, char *bytes, size_t length, uintN flags)
{
CHECK_REQUEST(cx);
jschar *chars = js_InflateString(cx, bytes, &length);
jschar *chars = InflateString(cx, bytes, &length);
if (!chars)
return NULL;
RegExpStatics *res = RegExpStatics::extractFrom(obj->asGlobal());
@ -5853,7 +5853,7 @@ JS_PUBLIC_API(JSObject *)
JS_NewRegExpObjectNoStatics(JSContext *cx, char *bytes, size_t length, uintN flags)
{
CHECK_REQUEST(cx);
jschar *chars = js_InflateString(cx, bytes, &length);
jschar *chars = InflateString(cx, bytes, &length);
if (!chars)
return NULL;
JSObject *obj = RegExp::createObjectNoStatics(cx, chars, length, flags);

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

@ -546,7 +546,7 @@ js_AtomizeString(JSContext *cx, JSString *str, InternBehavior ib)
}
JSAtom *
js_Atomize(JSContext *cx, const char *bytes, size_t length, InternBehavior ib, bool useCESU8)
js_Atomize(JSContext *cx, const char *bytes, size_t length, InternBehavior ib, FlationCoding fc)
{
CHECK_REQUEST(cx);
@ -554,7 +554,7 @@ js_Atomize(JSContext *cx, const char *bytes, size_t length, InternBehavior ib, b
return NULL;
/*
* Avoiding the malloc in js_InflateString on shorter strings saves us
* Avoiding the malloc in InflateString on shorter strings saves us
* over 20,000 malloc calls on mozilla browser startup. This compares to
* only 131 calls where the string is longer than a 31 char (net) buffer.
* The vast majority of atomized strings are already in the hashtable. So
@ -567,15 +567,15 @@ js_Atomize(JSContext *cx, const char *bytes, size_t length, InternBehavior ib, b
const jschar *chars;
OwnCharsBehavior ocb = CopyChars;
if (length < ATOMIZE_BUF_MAX) {
if (useCESU8)
js_InflateUTF8StringToBuffer(cx, bytes, length, inflated, &inflatedLength, true);
if (fc == CESU8Encoding)
InflateUTF8StringToBuffer(cx, bytes, length, inflated, &inflatedLength, fc);
else
js_InflateStringToBuffer(cx, bytes, length, inflated, &inflatedLength);
InflateStringToBuffer(cx, bytes, length, inflated, &inflatedLength);
inflated[inflatedLength] = 0;
chars = inflated;
} else {
inflatedLength = length;
chars = js_InflateString(cx, bytes, &inflatedLength, useCESU8);
chars = InflateString(cx, bytes, &inflatedLength, fc);
if (!chars)
return NULL;
ocb = TakeCharOwnership;

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

@ -647,7 +647,8 @@ js_AtomizeString(JSContext *cx, JSString *str, js::InternBehavior ib = js::DoNot
extern JSAtom *
js_Atomize(JSContext *cx, const char *bytes, size_t length,
js::InternBehavior ib = js::DoNotInternAtom, bool useCESU8 = false);
js::InternBehavior ib = js::DoNotInternAtom,
js::FlationCoding fc = js::NormalEncoding);
extern JSAtom *
js_AtomizeChars(JSContext *cx, const jschar *chars, size_t length,

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

@ -902,7 +902,7 @@ js_ReportErrorVA(JSContext *cx, uintN flags, const char *format, va_list ap)
PodZero(&report);
report.flags = flags;
report.errorNumber = JSMSG_USER_DEFINED_ERROR;
report.ucmessage = ucmessage = js_InflateString(cx, message, &messagelen);
report.ucmessage = ucmessage = InflateString(cx, message, &messagelen);
PopulateReportBlame(cx, &report);
warning = JSREPORT_IS_WARNING(report.flags);
@ -962,8 +962,7 @@ js_ExpandErrorArguments(JSContext *cx, JSErrorCallback callback,
if (charArgs) {
char *charArg = va_arg(ap, char *);
size_t charArgLength = strlen(charArg);
reportp->messageArgs[i]
= js_InflateString(cx, charArg, &charArgLength);
reportp->messageArgs[i] = InflateString(cx, charArg, &charArgLength);
if (!reportp->messageArgs[i])
goto error;
} else {
@ -986,7 +985,7 @@ js_ExpandErrorArguments(JSContext *cx, JSErrorCallback callback,
size_t expandedLength;
size_t len = strlen(efs->format);
buffer = fmt = js_InflateString (cx, efs->format, &len);
buffer = fmt = InflateString(cx, efs->format, &len);
if (!buffer)
goto error;
expandedLength = len
@ -1021,9 +1020,8 @@ js_ExpandErrorArguments(JSContext *cx, JSErrorCallback callback,
JS_ASSERT(expandedArgs == argCount);
*out = 0;
cx->free_(buffer);
*messagep =
js_DeflateString(cx, reportp->ucmessage,
(size_t)(out - reportp->ucmessage));
*messagep = DeflateString(cx, reportp->ucmessage,
size_t(out - reportp->ucmessage));
if (!*messagep)
goto error;
}
@ -1038,7 +1036,7 @@ js_ExpandErrorArguments(JSContext *cx, JSErrorCallback callback,
if (!*messagep)
goto error;
len = strlen(*messagep);
reportp->ucmessage = js_InflateString(cx, *messagep, &len);
reportp->ucmessage = InflateString(cx, *messagep, &len);
if (!reportp->ucmessage)
goto error;
}

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

@ -1710,7 +1710,7 @@ JS_EvaluateInStackFrame(JSContext *cx, JSStackFrame *fp,
if (!CheckDebugMode(cx))
return JS_FALSE;
chars = js_InflateString(cx, bytes, &len);
chars = InflateString(cx, bytes, &len);
if (!chars)
return JS_FALSE;
length = (uintN) len;
@ -2244,7 +2244,7 @@ js_StartVtune(JSContext *cx, uintN argc, jsval *vp)
jsval *argv = JS_ARGV(cx, vp);
if (argc > 0 && JSVAL_IS_STRING(argv[0])) {
str = JSVAL_TO_STRING(argv[0]);
params.tb5Filename = js_DeflateString(cx, str->chars(), str->length());
params.tb5Filename = DeflateString(cx, str->chars(), str->length());
}
status = VTStartSampling(&params);
@ -2582,7 +2582,7 @@ ethogram_addScript(JSContext *cx, uintN argc, jsval *vp)
}
if (JSVAL_IS_STRING(argv[0])) {
str = JSVAL_TO_STRING(argv[0]);
filename = js_DeflateString(cx, str->chars(), str->length());
filename = DeflateString(cx, str->chars(), str->length());
if (!filename)
return false;
}

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

@ -374,7 +374,7 @@ js_EnterSharpObject(JSContext *cx, JSObject *obj, JSIdArray **idap,
len = JS_snprintf(buf, sizeof buf, "#%u%c",
sharpid >> SHARP_ID_SHIFT,
(sharpid & SHARP_BIT) ? '#' : '=');
*sp = js_InflateString(cx, buf, &len);
*sp = InflateString(cx, buf, &len);
if (!*sp) {
if (ida)
JS_DestroyIdArray(cx, ida);

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

@ -687,14 +687,13 @@ SprintString(Sprinter *sp, JSString *str)
if (!chars)
return -1;
size_t size = js_GetDeflatedStringLength(sp->context, chars, length);
size_t size = GetDeflatedStringLength(sp->context, chars, length);
if (size == (size_t)-1 || !SprintEnsureBuffer(sp, size))
return -1;
ptrdiff_t offset = sp->offset;
sp->offset += size;
js_DeflateStringToBuffer(sp->context, chars, length, sp->base + offset,
&size);
DeflateStringToBuffer(sp->context, chars, length, sp->base + offset, &size);
sp->base[sp->offset] = 0;
return offset;
}
@ -5220,7 +5219,7 @@ js_DecompileValueGenerator(JSContext *cx, intN spindex, jsval v_in,
const jschar *chars = fallback->getChars(cx);
if (!chars)
return NULL;
return js_DeflateString(cx, chars, length);
return DeflateString(cx, chars, length);
}
static char *

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

@ -410,7 +410,7 @@ static int cvt_ws(SprintfState *ss, const jschar *ws, int width, int prec,
*/
if (ws) {
int slen = js_strlen(ws);
char *s = js_DeflateString(NULL, ws, slen);
char *s = DeflateString(NULL, ws, slen);
if (!s)
return -1; /* JSStuffFunc error indicator. */
result = cvt_s(ss, s, width, prec, flags);

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

@ -3234,7 +3234,7 @@ reflect_parse(JSContext *cx, uint32 argc, jsval *vp)
if (!chars)
return JS_FALSE;
filename = js_DeflateString(cx, chars, length);
filename = DeflateString(cx, chars, length);
if (!filename)
return JS_FALSE;
filenamep.reset(filename);

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

@ -485,7 +485,7 @@ TokenStream::reportCompileErrorNumberVA(JSParseNode *pn, uintN flags, uintN erro
}
memcpy(linechars, linebase, linelength * sizeof(jschar));
linechars[linelength] = 0;
linebytes = js_DeflateString(cx, linechars, linelength);
linebytes = DeflateString(cx, linechars, linelength);
if (!linebytes) {
warning = false;
goto out;
@ -706,7 +706,7 @@ TokenStream::getXMLEntity()
bad:
/* No match: throw a TypeError per ECMA-357 10.3.2.1 step 8(a). */
JS_ASSERT((tb.end() - bp) >= 1);
bytes = js_DeflateString(cx, bp + 1, (tb.end() - bp) - 1);
bytes = DeflateString(cx, bp + 1, (tb.end() - bp) - 1);
if (bytes) {
ReportCompileErrorNumber(cx, this, NULL, JSREPORT_ERROR, msg, bytes);
cx->free_(bytes);

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

@ -3644,7 +3644,7 @@ NewShortString(JSContext *cx, const char *chars, size_t length)
#ifdef DEBUG
size_t oldLength = length;
#endif
if (!js_InflateUTF8StringToBuffer(cx, chars, length, storage, &length))
if (!InflateUTF8StringToBuffer(cx, chars, length, storage, &length))
return NULL;
JS_ASSERT(length <= oldLength);
storage[length] = 0;
@ -3793,7 +3793,7 @@ js_NewStringCopyN(JSContext *cx, const char *s, size_t n)
if (JSShortString::lengthFits(n))
return NewShortString(cx, s, n);
jschar *chars = js_InflateString(cx, s, &n);
jschar *chars = InflateString(cx, s, &n);
if (!chars)
return NULL;
JSFixedString *str = js_NewString(cx, chars, n);
@ -4076,35 +4076,28 @@ js_strchr_limit(const jschar *s, jschar c, const jschar *limit)
return NULL;
}
jschar *
js_InflateString(JSContext *cx, const char *bytes, size_t *lengthp, bool useCESU8)
{
size_t nbytes, nchars, i;
jschar *chars;
#ifdef DEBUG
JSBool ok;
#endif
namespace js {
nbytes = *lengthp;
if (js_CStringsAreUTF8 || useCESU8) {
if (!js_InflateUTF8StringToBuffer(cx, bytes, nbytes, NULL, &nchars,
useCESU8))
jschar *
InflateString(JSContext *cx, const char *bytes, size_t *lengthp, FlationCoding fc)
{
size_t nchars;
jschar *chars;
size_t nbytes = *lengthp;
if (js_CStringsAreUTF8 || fc == CESU8Encoding) {
if (!InflateUTF8StringToBuffer(cx, bytes, nbytes, NULL, &nchars, fc))
goto bad;
chars = (jschar *) cx->malloc_((nchars + 1) * sizeof (jschar));
if (!chars)
goto bad;
#ifdef DEBUG
ok =
#endif
js_InflateUTF8StringToBuffer(cx, bytes, nbytes, chars, &nchars,
useCESU8);
JS_ASSERT(ok);
JS_ALWAYS_TRUE(InflateUTF8StringToBuffer(cx, bytes, nbytes, chars, &nchars, fc));
} else {
nchars = nbytes;
chars = (jschar *) cx->malloc_((nchars + 1) * sizeof(jschar));
if (!chars)
goto bad;
for (i = 0; i < nchars; i++)
for (size_t i = 0; i < nchars; i++)
chars[i] = (unsigned char) bytes[i];
}
*lengthp = nchars;
@ -4124,26 +4117,19 @@ js_InflateString(JSContext *cx, const char *bytes, size_t *lengthp, bool useCESU
* May be called with null cx.
*/
char *
js_DeflateString(JSContext *cx, const jschar *chars, size_t nchars)
DeflateString(JSContext *cx, const jschar *chars, size_t nchars)
{
size_t nbytes, i;
char *bytes;
#ifdef DEBUG
JSBool ok;
#endif
if (js_CStringsAreUTF8) {
nbytes = js_GetDeflatedStringLength(cx, chars, nchars);
nbytes = GetDeflatedStringLength(cx, chars, nchars);
if (nbytes == (size_t) -1)
return NULL;
bytes = (char *) (cx ? cx->malloc_(nbytes + 1) : OffTheBooks::malloc_(nbytes + 1));
if (!bytes)
return NULL;
#ifdef DEBUG
ok =
#endif
js_DeflateStringToBuffer(cx, chars, nchars, bytes, &nbytes);
JS_ASSERT(ok);
JS_ALWAYS_TRUE(DeflateStringToBuffer(cx, chars, nchars, bytes, &nbytes));
} else {
nbytes = nchars;
bytes = (char *) (cx ? cx->malloc_(nbytes + 1) : OffTheBooks::malloc_(nbytes + 1));
@ -4157,25 +4143,26 @@ js_DeflateString(JSContext *cx, const jschar *chars, size_t nchars)
}
size_t
js_GetDeflatedStringLength(JSContext *cx, const jschar *chars, size_t nchars)
GetDeflatedStringLength(JSContext *cx, const jschar *chars, size_t nchars)
{
if (!js_CStringsAreUTF8)
return nchars;
return js_GetDeflatedUTF8StringLength(cx, chars, nchars);
return GetDeflatedUTF8StringLength(cx, chars, nchars);
}
/*
* May be called with null cx through public API, see below.
*/
size_t
js_GetDeflatedUTF8StringLength(JSContext *cx, const jschar *chars,
size_t nchars, bool useCESU8)
GetDeflatedUTF8StringLength(JSContext *cx, const jschar *chars,
size_t nchars, FlationCoding fc)
{
size_t nbytes;
const jschar *end;
uintN c, c2;
char buffer[10];
bool useCESU8 = fc == CESU8Encoding;
nbytes = nchars;
for (end = chars + nchars; chars != end; chars++) {
@ -4213,9 +4200,9 @@ js_GetDeflatedUTF8StringLength(JSContext *cx, const jschar *chars,
return (size_t) -1;
}
JSBool
js_DeflateStringToBuffer(JSContext *cx, const jschar *src, size_t srclen,
char *dst, size_t *dstlenp)
bool
DeflateStringToBuffer(JSContext *cx, const jschar *src, size_t srclen,
char *dst, size_t *dstlenp)
{
size_t dstlen, i;
@ -4236,20 +4223,22 @@ js_DeflateStringToBuffer(JSContext *cx, const jschar *src, size_t srclen,
return JS_TRUE;
}
return js_DeflateStringToUTF8Buffer(cx, src, srclen, dst, dstlenp);
return DeflateStringToUTF8Buffer(cx, src, srclen, dst, dstlenp);
}
JSBool
js_DeflateStringToUTF8Buffer(JSContext *cx, const jschar *src, size_t srclen,
char *dst, size_t *dstlenp, bool useCESU8)
bool
DeflateStringToUTF8Buffer(JSContext *cx, const jschar *src, size_t srclen,
char *dst, size_t *dstlenp, FlationCoding fc)
{
size_t dstlen, i, origDstlen, utf8Len;
size_t i, utf8Len;
jschar c, c2;
uint32 v;
uint8 utf8buf[6];
dstlen = *dstlenp;
origDstlen = dstlen;
bool useCESU8 = fc == CESU8Encoding;
size_t dstlen = *dstlenp;
size_t origDstlen = dstlen;
while (srclen) {
c = *src++;
srclen--;
@ -4289,7 +4278,7 @@ badSurrogate:
*dstlenp = (origDstlen - dstlen);
/* Delegate error reporting to the measurement function. */
if (cx)
js_GetDeflatedStringLength(cx, src - 1, srclen + 1);
GetDeflatedStringLength(cx, src - 1, srclen + 1);
return JS_FALSE;
bufferTooSmall:
@ -4301,37 +4290,36 @@ bufferTooSmall:
return JS_FALSE;
}
JSBool
js_InflateStringToBuffer(JSContext *cx, const char *src, size_t srclen,
jschar *dst, size_t *dstlenp)
bool
InflateStringToBuffer(JSContext *cx, const char *src, size_t srclen,
jschar *dst, size_t *dstlenp)
{
size_t dstlen, i;
if (!js_CStringsAreUTF8) {
if (dst) {
dstlen = *dstlenp;
if (srclen > dstlen) {
for (i = 0; i < dstlen; i++)
dst[i] = (unsigned char) src[i];
if (cx) {
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
JSMSG_BUFFER_TOO_SMALL);
}
return JS_FALSE;
}
for (i = 0; i < srclen; i++)
dst[i] = (unsigned char) src[i];
}
*dstlenp = srclen;
return JS_TRUE;
}
if (js_CStringsAreUTF8)
return InflateUTF8StringToBuffer(cx, src, srclen, dst, dstlenp);
return js_InflateUTF8StringToBuffer(cx, src, srclen, dst, dstlenp);
if (dst) {
dstlen = *dstlenp;
if (srclen > dstlen) {
for (i = 0; i < dstlen; i++)
dst[i] = (unsigned char) src[i];
if (cx) {
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
JSMSG_BUFFER_TOO_SMALL);
}
return JS_FALSE;
}
for (i = 0; i < srclen; i++)
dst[i] = (unsigned char) src[i];
}
*dstlenp = srclen;
return JS_TRUE;
}
JSBool
js_InflateUTF8StringToBuffer(JSContext *cx, const char *src, size_t srclen,
jschar *dst, size_t *dstlenp, bool useCESU8)
bool
InflateUTF8StringToBuffer(JSContext *cx, const char *src, size_t srclen,
jschar *dst, size_t *dstlenp, FlationCoding fc)
{
size_t dstlen, origDstlen, offset, j, n;
uint32 v;
@ -4339,6 +4327,7 @@ js_InflateUTF8StringToBuffer(JSContext *cx, const char *src, size_t srclen,
dstlen = dst ? *dstlenp : (size_t) -1;
origDstlen = dstlen;
offset = 0;
bool useCESU8 = fc == CESU8Encoding;
while (srclen) {
v = (uint8) *src;
@ -4410,6 +4399,8 @@ bufferTooSmall:
return JS_FALSE;
}
} /* namepsace js */
/*
* From java.lang.Character.java:
*

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

@ -1108,60 +1108,68 @@ js_SkipWhiteSpace(const jschar *s, const jschar *end)
return s;
}
namespace js {
/*
* Some string functions have an optional bool useCESU8 argument.
* CESU-8 (Compatibility Encoding Scheme for UTF-16: 8-bit) is a
* variant of UTF-8 that allows us to store any wide character
* string as a narrow character string. For strings containing
* mostly ascii, it saves space.
* On encodings:
*
* - Some string functions have an optional FlationCoding argument that allow
* the caller to force CESU-8 encoding handling.
* - Functions that don't take a FlationCoding base their NormalEncoding
* behavior on the js_CStringsAreUTF8 value. NormalEncoding is either raw
* (simple zero-extension) or UTF-8 depending on js_CStringsAreUTF8.
* - Functions that explicitly state their encoding do not use the
* js_CStringsAreUTF8 value.
*
* CESU-8 (Compatibility Encoding Scheme for UTF-16: 8-bit) is a variant of
* UTF-8 that allows us to store any wide character string as a narrow
* character string. For strings containing mostly ascii, it saves space.
* http://www.unicode.org/reports/tr26/
*/
enum FlationCoding
{
NormalEncoding,
CESU8Encoding
};
/*
* Inflate bytes to JS chars and vice versa. Report out of memory via cx and
* return null on error, otherwise return the jschar or byte vector that was
* JS_malloc'ed. length is updated to the length of the new string in jschars.
* Using useCESU8 = true treats 'bytes' as CESU-8.
* Inflate bytes to jschars. Return null on error, otherwise return the jschar
* or byte vector that was malloc'ed. length is updated to the length of the
* new string (in jschars).
*/
extern jschar *
js_InflateString(JSContext *cx, const char *bytes, size_t *length, bool useCESU8 = false);
InflateString(JSContext *cx, const char *bytes, size_t *length,
FlationCoding fc = NormalEncoding);
extern char *
js_DeflateString(JSContext *cx, const jschar *chars, size_t length);
DeflateString(JSContext *cx, const jschar *chars, size_t length);
/*
* Inflate bytes to JS chars into a buffer. 'chars' must be large enough for
* 'length' jschars. The buffer is NOT null-terminated. The destination length
* must be be initialized with the buffer size and will contain on return the
* number of copied chars. Conversion behavior depends on js_CStringsAreUTF8.
* Inflate bytes to JS chars in an existing buffer. 'chars' must be large
* enough for 'length' jschars. The buffer is NOT null-terminated.
*
* charsLength must be be initialized with the destination buffer size and, on
* return, will contain on return the number of copied chars.
*/
extern JSBool
js_InflateStringToBuffer(JSContext *cx, const char *bytes, size_t length,
jschar *chars, size_t *charsLength);
extern bool
InflateStringToBuffer(JSContext *cx, const char *bytes, size_t length,
jschar *chars, size_t *charsLength);
/*
* Same as js_InflateStringToBuffer, but treats 'bytes' as UTF-8 or CESU-8.
*/
extern JSBool
js_InflateUTF8StringToBuffer(JSContext *cx, const char *bytes, size_t length,
jschar *chars, size_t *charsLength,
bool useCESU8 = false);
extern bool
InflateUTF8StringToBuffer(JSContext *cx, const char *bytes, size_t length,
jschar *chars, size_t *charsLength,
FlationCoding fc = NormalEncoding);
/*
* Get number of bytes in the deflated sequence of characters. Behavior depends
* on js_CStringsAreUTF8.
*/
/* Get number of bytes in the deflated sequence of characters. */
extern size_t
js_GetDeflatedStringLength(JSContext *cx, const jschar *chars,
size_t charsLength);
GetDeflatedStringLength(JSContext *cx, const jschar *chars, size_t charsLength);
/*
* Same as js_GetDeflatedStringLength, but treats the result as UTF-8 or CESU-8.
* This function will never fail (return -1) in CESU-8 mode.
*/
/* This function will never fail (return -1) in CESU-8 mode. */
extern size_t
js_GetDeflatedUTF8StringLength(JSContext *cx, const jschar *chars,
size_t charsLength, bool useCESU8 = false);
GetDeflatedUTF8StringLength(JSContext *cx, const jschar *chars,
size_t charsLength,
FlationCoding fc = NormalEncoding);
/*
* Deflate JS chars to bytes into a buffer. 'bytes' must be large enough for
@ -1169,17 +1177,19 @@ js_GetDeflatedUTF8StringLength(JSContext *cx, const jschar *chars,
* must to be initialized with the buffer size and will contain on return the
* number of copied bytes. Conversion behavior depends on js_CStringsAreUTF8.
*/
extern JSBool
js_DeflateStringToBuffer(JSContext *cx, const jschar *chars,
size_t charsLength, char *bytes, size_t *length);
extern bool
DeflateStringToBuffer(JSContext *cx, const jschar *chars,
size_t charsLength, char *bytes, size_t *length);
/*
* Same as js_DeflateStringToBuffer, but treats 'bytes' as UTF-8 or CESU-8.
* Same as DeflateStringToBuffer, but treats 'bytes' as UTF-8 or CESU-8.
*/
extern JSBool
js_DeflateStringToUTF8Buffer(JSContext *cx, const jschar *chars,
size_t charsLength, char *bytes, size_t *length,
bool useCESU8 = false);
extern bool
DeflateStringToUTF8Buffer(JSContext *cx, const jschar *chars,
size_t charsLength, char *bytes, size_t *length,
FlationCoding fc = NormalEncoding);
} /* namespace js */
/* Export a few natives and a helper to other files in SpiderMonkey. */
extern JSBool

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

@ -224,7 +224,7 @@ StringBuffer::appendInflated(const char *cstr, size_t cstrlen)
size_t oldcstrlen = cstrlen;
bool ok =
#endif
js_InflateStringToBuffer(context(), cstr, cstrlen, begin() + lengthBefore, &cstrlen);
InflateStringToBuffer(context(), cstr, cstrlen, begin() + lengthBefore, &cstrlen);
JS_ASSERT(ok && oldcstrlen == cstrlen);
return true;
}

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

@ -16629,7 +16629,7 @@ StartTraceVisNative(JSContext *cx, uintN argc, jsval *vp)
if (argc > 0 && JSVAL_IS_STRING(JS_ARGV(cx, vp)[0])) {
JSString *str = JSVAL_TO_STRING(JS_ARGV(cx, vp)[0]);
char *filename = js_DeflateString(cx, str->chars(), str->length());
char *filename = DeflateString(cx, str->chars(), str->length());
if (!filename)
goto error;
ok = StartTraceVis(filename);

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

@ -424,11 +424,11 @@ JS_XDRString(JSXDRState *xdr, JSString **strp)
size_t len;
if (xdr->mode == JSXDR_ENCODE) {
len = js_GetDeflatedUTF8StringLength(xdr->cx,
(*strp)->getChars(xdr->cx),
(*strp)->length(),
true);
/* js_GetDeflatedUTF8StringLength never fails in CESU8 mode */
len = GetDeflatedUTF8StringLength(xdr->cx,
(*strp)->getChars(xdr->cx),
(*strp)->length(),
CESU8Encoding);
/* GetDeflatedUTF8StringLength never fails in CESU8 mode */
JS_ASSERT(len != (size_t) -1);
JS_ASSERT(size_t(uint32(len)) == len);
/* ensure MAX_LENGTH strings can always fit when CESU8 encoded */
@ -446,12 +446,12 @@ JS_XDRString(JSXDRState *xdr, JSString **strp)
len = (uint32)nchars;
if (xdr->mode == JSXDR_ENCODE) {
JS_ALWAYS_TRUE(js_DeflateStringToUTF8Buffer(xdr->cx,
(*strp)->getChars(xdr->cx),
(*strp)->length(), buf,
&len, true));
JS_ALWAYS_TRUE(DeflateStringToUTF8Buffer(xdr->cx,
(*strp)->getChars(xdr->cx),
(*strp)->length(), buf,
&len, CESU8Encoding));
} else {
jschar *chars = js_InflateString(xdr->cx, buf, &len, true);
jschar *chars = InflateString(xdr->cx, buf, &len, CESU8Encoding);
if (!chars)
return false;
@ -658,7 +658,7 @@ js_XDRAtom(JSXDRState *xdr, JSAtom **atomp)
if (!buf)
return false;
JSAtom *atom = js_Atomize(xdr->cx, buf, len, DoNotInternAtom, true);
JSAtom *atom = js_Atomize(xdr->cx, buf, len, DoNotInternAtom, CESU8Encoding);
if (!atom)
return false;

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

@ -1716,13 +1716,12 @@ ParseXMLSource(JSContext *cx, JSString *src)
return NULL;
dstlen = length;
js_InflateStringToBuffer(cx, prefix, constrlen(prefix), chars, &dstlen);
InflateStringToBuffer(cx, prefix, constrlen(prefix), chars, &dstlen);
offset = dstlen;
js_strncpy(chars + offset, uri->chars(), urilen);
offset += urilen;
dstlen = length - offset + 1;
js_InflateStringToBuffer(cx, middle, constrlen(middle), chars + offset,
&dstlen);
InflateStringToBuffer(cx, middle, constrlen(middle), chars + offset, &dstlen);
offset += dstlen;
srcp = src->getChars(cx);
if (!srcp) {
@ -1732,8 +1731,7 @@ ParseXMLSource(JSContext *cx, JSString *src)
js_strncpy(chars + offset, srcp, srclen);
offset += srclen;
dstlen = length - offset + 1;
js_InflateStringToBuffer(cx, suffix, constrlen(suffix), chars + offset,
&dstlen);
InflateStringToBuffer(cx, suffix, constrlen(suffix), chars + offset, &dstlen);
chars [offset + dstlen] = 0;
LeaveTrace(cx);

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

@ -2137,7 +2137,7 @@ GetElementIC::attachGetProp(JSContext *cx, JSObject *obj, const Value &v, jsid i
CodeLocationLabel cs = buffer.finalize();
#if DEBUG
char *chars = js_DeflateString(cx, v.toString()->getChars(cx), v.toString()->length());
char *chars = DeflateString(cx, v.toString()->getChars(cx), v.toString()->length());
JaegerSpew(JSpew_PICs, "generated %s stub at %p for atom 0x%x (\"%s\") shape 0x%x (%s: %d)\n",
js_CodeName[op], cs.executableAddress(), id, chars, holder->shape(),
cx->fp()->script()->filename, js_FramePCToLineNumber(cx, cx->fp()));