Bug 1454207 - Part 3: Introduces a new function |ValueToPrintableUtf8| so that we report ReferenceError with readable Unicode characters instead of unreadable Unicode escape sequences r?arai
MozReview-Commit-ID: HtAVxVg54IL
--- a/js/src/vm/JSContext.cpp
+++ b/js/src/vm/JSContext.cpp
@@ -879,19 +879,19 @@ js::ReportErrorNumberUCArray(JSContext*
return warning;
}
void
js::ReportIsNotDefined(JSContext* cx, HandleId id)
{
JSAutoByteString printable;
- if (!ValueToPrintableLatin1(cx, IdToValue(id), &printable))
+ if (!ValueToPrintableUTF8(cx, IdToValue(id), &printable))
return;
- JS_ReportErrorNumberLatin1(cx, GetErrorMessage, nullptr, JSMSG_NOT_DEFINED, printable.ptr());
+ JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, JSMSG_NOT_DEFINED, printable.ptr());
}
void
js::ReportIsNotDefined(JSContext* cx, HandlePropertyName name)
{
RootedId id(cx, NameToId(name));
ReportIsNotDefined(cx, id);
}
--- a/js/src/vm/StringType.cpp
+++ b/js/src/vm/StringType.cpp
@@ -1921,16 +1921,30 @@ js::ValueToPrintableLatin1(JSContext* cx
if (!str)
return nullptr;
str = QuoteString(cx, str, 0);
if (!str)
return nullptr;
return bytes->encodeLatin1(cx, str);
}
+const char*
+js::ValueToPrintableUTF8(JSContext* cx, const Value& vArg, JSAutoByteString* bytes, bool asSource)
+{
+ RootedValue v(cx, vArg);
+ JSString* str;
+ if (asSource)
+ str = ValueToSource(cx, v);
+ else
+ str = ToString<CanGC>(cx, v);
+ if (!str)
+ return nullptr;
+ return bytes->encodeUtf8(cx, RootedString(cx, str));
+}
+
template <AllowGC allowGC>
JSString*
js::ToStringSlow(JSContext* cx, typename MaybeRooted<Value, allowGC>::HandleType arg)
{
/* As with ToObjectSlow, callers must verify that |arg| isn't a string. */
MOZ_ASSERT(!arg.isString());
Value v = arg;
--- a/js/src/vm/StringType.h
+++ b/js/src/vm/StringType.h
@@ -1594,16 +1594,22 @@ SubstringKernel(JSContext* cx, HandleStr
* will be changed to Unicode escape sequences(I.e. \udddd, dddd are 4 hex digits) in the printable
* string.
*/
extern const char*
ValueToPrintableLatin1(JSContext* cx, const Value&, JSAutoByteString* bytes,
bool asSource = false);
/*
+ * Convert a value to a printable C string encoded in UTF-8.
+ */
+extern const char*
+ValueToPrintableUTF8(JSContext* cx, const Value&, JSAutoByteString* bytes, bool asSource = false);
+
+/*
* Convert a non-string value to a string, returning null after reporting an
* error, otherwise returning a new string reference.
*/
template <AllowGC allowGC>
extern JSString*
ToStringSlow(JSContext* cx, typename MaybeRooted<Value, allowGC>::HandleType arg);
/*