Bug 1027528 part 3 - Make JS_EncodeString and friends handle Latin1 strings. r=Waldo

This commit is contained in:
Jan de Mooij 2014-06-20 12:39:44 +02:00
parent 2c27694ff3
commit a9a7517ec1
8 changed files with 47 additions and 38 deletions

View File

@ -175,7 +175,8 @@ class ConstTwoByteChars : public mozilla::RangedPtr<const jschar>
* This method cannot trigger GC.
*/
extern Latin1CharsZ
LossyTwoByteCharsToNewLatin1CharsZ(js::ThreadSafeContext *cx, TwoByteChars tbchars);
LossyTwoByteCharsToNewLatin1CharsZ(js::ThreadSafeContext *cx,
const mozilla::Range<const jschar> tbchars);
template <typename CharT>
extern UTF8CharsZ

View File

@ -710,8 +710,9 @@ TokenStream::reportCompileErrorNumberVA(uint32_t offset, unsigned flags, unsigne
err.report.uclinebuf = windowBuf.stealChars();
if (!err.report.uclinebuf)
return false;
TwoByteChars tbchars(err.report.uclinebuf, windowLength);
err.report.linebuf = LossyTwoByteCharsToNewLatin1CharsZ(cx, tbchars).c_str();
mozilla::Range<const jschar> tbchars(err.report.uclinebuf, windowLength);
err.report.linebuf = JS::LossyTwoByteCharsToNewLatin1CharsZ(cx, tbchars).c_str();
if (!err.report.linebuf)
return false;

View File

@ -5,6 +5,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/Range.h"
#include "jsapi.h"
#include "jsstr.h"
@ -38,7 +40,7 @@ END_TEST(testUTF8_bigUTF8)
BEGIN_TEST(testUTF8_badSurrogate)
{
static const jschar badSurrogate[] = { 'A', 'B', 'C', 0xDEEE, 'D', 'E', 0 };
JS::TwoByteChars tbchars(badSurrogate, js_strlen(badSurrogate));
mozilla::Range<const jschar> tbchars(badSurrogate, js_strlen(badSurrogate));
JS::Latin1CharsZ latin1 = JS::LossyTwoByteCharsToNewLatin1CharsZ(cx, tbchars);
CHECK(latin1);
CHECK(latin1[3] == 0x00EE);

View File

@ -5502,17 +5502,34 @@ JS_DecodeBytes(JSContext *cx, const char *src, size_t srclen, jschar *dst, size_
return true;
}
static char *
EncodeLatin1(ExclusiveContext *cx, JSString *str)
{
JSLinearString *linear = str->ensureLinear(cx);
if (!linear)
return nullptr;
JS::AutoCheckCannotGC nogc;
if (linear->hasTwoByteChars())
return JS::LossyTwoByteCharsToNewLatin1CharsZ(cx, linear->twoByteRange(nogc)).c_str();
size_t len = str->length();
Latin1Char *buf = cx->pod_malloc<Latin1Char>(len + 1);
if (!buf)
return nullptr;
mozilla::PodCopy(buf, linear->latin1Chars(nogc), len);
buf[len] = '\0';
return reinterpret_cast<char*>(buf);
}
JS_PUBLIC_API(char *)
JS_EncodeString(JSContext *cx, JSString *str)
{
AssertHeapIsIdle(cx);
CHECK_REQUEST(cx);
JSLinearString *linear = str->ensureLinear(cx);
if (!linear)
return nullptr;
return LossyTwoByteCharsToNewLatin1CharsZ(cx, linear->range()).c_str();
return EncodeLatin1(cx, str);
}
JS_PUBLIC_API(char *)
@ -6477,11 +6494,7 @@ JS::SetAsmJSCacheOps(JSRuntime *rt, const JS::AsmJSCacheOps *ops)
char *
JSAutoByteString::encodeLatin1(ExclusiveContext *cx, JSString *str)
{
JSLinearString *linear = str->ensureLinear(cx);
if (!linear)
return nullptr;
mBytes = LossyTwoByteCharsToNewLatin1CharsZ(cx, linear->range()).c_str();
mBytes = EncodeLatin1(cx, str);
return mBytes;
}

View File

@ -758,10 +758,10 @@ js_ExpandErrorArguments(ExclusiveContext *cx, JSErrorCallback callback,
JS_ASSERT(expandedArgs == argCount);
*out = 0;
js_free(buffer);
TwoByteChars ucmsg(reportp->ucmessage,
PointerRangeSize(static_cast<const jschar *>(reportp->ucmessage),
static_cast<const jschar *>(out)));
*messagep = LossyTwoByteCharsToNewLatin1CharsZ(cx, ucmsg).c_str();
size_t msgLen = PointerRangeSize(static_cast<const jschar *>(reportp->ucmessage),
static_cast<const jschar *>(out));
mozilla::Range<const jschar> ucmsg(reportp->ucmessage, msgLen);
*messagep = JS::LossyTwoByteCharsToNewLatin1CharsZ(cx, ucmsg).c_str();
if (!*messagep)
goto error;
}

View File

@ -1807,11 +1807,7 @@ js::DecompileValueGenerator(JSContext *cx, int spindex, HandleValue v,
return nullptr;
}
RootedLinearString linear(cx, fallback->ensureLinear(cx));
if (!linear)
return nullptr;
TwoByteChars tbchars(linear->chars(), linear->length());
return LossyTwoByteCharsToNewLatin1CharsZ(cx, tbchars).c_str();
return JS_EncodeString(cx, fallback);
}
static bool
@ -1890,14 +1886,12 @@ js::DecompileArgument(JSContext *cx, int formalIndex, HandleValue v)
}
if (v.isUndefined())
return JS_strdup(cx, js_undefined_str); // Prevent users from seeing "(void 0)"
RootedString fallback(cx, ValueToSource(cx, v));
if (!fallback)
return nullptr;
RootedLinearString linear(cx, fallback->ensureLinear(cx));
if (!linear)
return nullptr;
return LossyTwoByteCharsToNewLatin1CharsZ(cx, linear->range()).c_str();
return JS_EncodeString(cx, fallback);
}
bool

View File

@ -3254,13 +3254,7 @@ reflect_parse(JSContext *cx, uint32_t argc, jsval *vp)
if (!str)
return false;
size_t length = str->length();
const jschar *chars = str->getChars(cx);
if (!chars)
return false;
TwoByteChars tbchars(chars, length);
filename = LossyTwoByteCharsToNewLatin1CharsZ(cx, tbchars).c_str();
filename = JS_EncodeString(cx, str);
if (!filename)
return false;
}

View File

@ -6,13 +6,17 @@
#include "js/CharacterEncoding.h"
#include "mozilla/Range.h"
#include "jscntxt.h"
#include "jsprf.h"
using namespace JS;
using mozilla::Range;
Latin1CharsZ
JS::LossyTwoByteCharsToNewLatin1CharsZ(js::ThreadSafeContext *cx, TwoByteChars tbchars)
JS::LossyTwoByteCharsToNewLatin1CharsZ(js::ThreadSafeContext *cx, const Range<const jschar> tbchars)
{
JS_ASSERT(cx);
size_t len = tbchars.length();
@ -140,7 +144,7 @@ bufferTooSmall:
template <typename CharT>
UTF8CharsZ
JS::CharsToNewUTF8CharsZ(js::ThreadSafeContext *cx, const mozilla::Range<const CharT> chars)
JS::CharsToNewUTF8CharsZ(js::ThreadSafeContext *cx, const Range<const CharT> chars)
{
JS_ASSERT(cx);
@ -161,10 +165,10 @@ JS::CharsToNewUTF8CharsZ(js::ThreadSafeContext *cx, const mozilla::Range<const C
}
template UTF8CharsZ
JS::CharsToNewUTF8CharsZ(js::ThreadSafeContext *cx, mozilla::Range<const Latin1Char> chars);
JS::CharsToNewUTF8CharsZ(js::ThreadSafeContext *cx, const Range<const Latin1Char> chars);
template UTF8CharsZ
JS::CharsToNewUTF8CharsZ(js::ThreadSafeContext *cx, mozilla::Range<const jschar> chars);
JS::CharsToNewUTF8CharsZ(js::ThreadSafeContext *cx, const Range<const jschar> chars);
static const uint32_t INVALID_UTF8 = UINT32_MAX;