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
authorZhang Junzhi <zjz@zjz.name>
Sun, 15 Apr 2018 13:18:03 +0800
changeset 413985 9e8c23aa94e4ddffc4b3fb6ed502b35ac09494e5
parent 413984 4a202b7736923f1858b92459d8e9d5251df59cbd
child 413986 8b0ba3f7d09995395787369b653c5976dd69fab5
push id33853
push usercbrindusan@mozilla.com
push dateTue, 17 Apr 2018 09:51:13 +0000
treeherdermozilla-central@8b0ba3f7d099 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersarai
bugs1454207
milestone61.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
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
js/src/vm/JSContext.cpp
js/src/vm/StringType.cpp
js/src/vm/StringType.h
--- 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);
 
 /*