Bug 1485066 - Part 12: Change the decompiler to return UTF-8 encoded strings. r=Waldo

This commit is contained in:
André Bargull 2018-09-05 01:25:12 -07:00
Родитель ceccb59fda
Коммит f5787fc732
12 изменённых файлов: 54 добавлений и 68 удалений

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

@ -117,7 +117,7 @@ class UTF8CharsZ : public mozilla::RangedPtr<unsigned char>
* A wrapper for a "const char*" that is encoded using UTF-8.
* This class does not manage ownership of the data; that is left
* to others. This differs from UTF8CharsZ in that the chars are
* const and it allows assignment.
* const and it disallows assignment.
*/
class JS_PUBLIC_API(ConstUTF8CharsZ)
{

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

@ -1057,8 +1057,8 @@ js::obj_create(JSContext* cx, unsigned argc, Value* vp)
if (!bytes)
return false;
JS_ReportErrorNumberLatin1(cx, GetErrorMessage, nullptr, JSMSG_UNEXPECTED_TYPE,
bytes.get(), "not an object or null");
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, JSMSG_UNEXPECTED_TYPE,
bytes.get(), "not an object or null");
return false;
}

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

@ -277,8 +277,8 @@ ReportArgTypeError(JSContext* cx, const char* funName, const char* expectedType,
if (!bytes)
return;
JS_ReportErrorNumberLatin1(cx, GetErrorMessage, nullptr, JSMSG_NOT_EXPECTED_TYPE, funName,
expectedType, bytes.get());
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, JSMSG_NOT_EXPECTED_TYPE, funName,
expectedType, bytes.get());
}
static MOZ_MUST_USE bool

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

@ -223,7 +223,7 @@ GetPM(JSContext* cx, JS::HandleValue value, const char* fname)
UniqueChars bytes = DecompileValueGenerator(cx, JSDVG_SEARCH_STACK, value, nullptr);
if (!bytes)
return nullptr;
JS_ReportErrorNumberLatin1(cx, GetErrorMessage, 0, JSMSG_NOT_NONNULL_OBJECT, bytes.get());
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, 0, JSMSG_NOT_NONNULL_OBJECT, bytes.get());
return nullptr;
}
RootedObject obj(cx, &value.toObject());

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

@ -3100,7 +3100,8 @@ DisassembleToString(JSContext* cx, unsigned argc, Value* vp)
if (!DisassembleToSprinter(cx, args.length(), vp, &sprinter))
return false;
JSString* str = JS_NewStringCopyZ(cx, sprinter.string());
JS::ConstUTF8CharsZ utf8chars(sprinter.string(), strlen(sprinter.string()));
JSString* str = JS_NewStringCopyUTF8Z(cx, utf8chars);
if (!str)
return false;
args.rval().setString(str);

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

@ -1147,14 +1147,14 @@ ToDisassemblySource(JSContext* cx, HandleValue v)
JSString* str = JS_DecompileFunction(cx, fun);
if (!str)
return nullptr;
return EncodeLatin1(cx, str);
return StringToNewUTF8CharsZ(cx, *str);
}
if (obj.is<RegExpObject>()) {
JSString* source = obj.as<RegExpObject>().toString(cx);
if (!source)
return nullptr;
return EncodeLatin1(cx, source);
return StringToNewUTF8CharsZ(cx, *source);
}
}
@ -2299,7 +2299,7 @@ js::DecompileValueGenerator(JSContext* cx, int spindex, HandleValue v,
return nullptr;
}
return EncodeLatin1(cx, fallback);
return StringToNewUTF8CharsZ(cx, *fallback);
}
static bool
@ -2379,8 +2379,10 @@ js::DecompileArgument(JSContext* cx, int formalIndex, HandleValue v)
UniqueChars result;
if (!DecompileArgumentFromStack(cx, formalIndex, &result))
return nullptr;
if (result && strcmp(result.get(), "(intermediate value)"))
return NewStringCopyZ<CanGC>(cx, result.get());
if (result && strcmp(result.get(), "(intermediate value)")) {
JS::ConstUTF8CharsZ utf8chars(result.get(), strlen(result.get()));
return NewStringCopyUTF8Z<CanGC>(cx, utf8chars);
}
}
if (v.isUndefined())
return cx->names().undefined; // Prevent users from seeing "(void 0)"
@ -2692,7 +2694,8 @@ GetPCCountJSON(JSContext* cx, const ScriptAndCounts& sac, StringBuffer& buf)
UniqueChars text = ed.getOutput();
if (!text)
return false;
JSString* str = NewLatin1StringZ(cx, std::move(text));
JS::ConstUTF8CharsZ utf8chars(text.get(), strlen(text.get()));
JSString* str = NewStringCopyUTF8Z<CanGC>(cx, utf8chars);
if (!AppendJSONProperty(buf, "text"))
return false;
if (!str || !(str = StringToSource(cx, str)))

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

@ -67,7 +67,7 @@ ForOfIterator::init(HandleValue iterable, NonIterableBehavior nonIterableBehavio
UniqueChars bytes = DecompileValueGenerator(cx, JSDVG_SEARCH_STACK, iterable, nullptr);
if (!bytes)
return false;
JS_ReportErrorNumberLatin1(cx, GetErrorMessage, nullptr, JSMSG_NOT_ITERABLE, bytes.get());
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, JSMSG_NOT_ITERABLE, bytes.get());
return false;
}

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

@ -919,43 +919,31 @@ js::ReportIsNullOrUndefinedForPropertyAccess(JSContext* cx, HandleValue v, bool
return;
if (strcmp(bytes.get(), js_undefined_str) == 0 || strcmp(bytes.get(), js_null_str) == 0) {
JS_ReportErrorNumberLatin1(cx, GetErrorMessage, nullptr, JSMSG_NO_PROPERTIES,
bytes.get());
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, JSMSG_NO_PROPERTIES, bytes.get());
} else if (v.isUndefined()) {
JS_ReportErrorNumberLatin1(cx, GetErrorMessage, nullptr, JSMSG_UNEXPECTED_TYPE,
bytes.get(), js_undefined_str);
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, JSMSG_UNEXPECTED_TYPE, bytes.get(),
js_undefined_str);
} else {
MOZ_ASSERT(v.isNull());
JS_ReportErrorNumberLatin1(cx, GetErrorMessage, nullptr, JSMSG_UNEXPECTED_TYPE,
bytes.get(), js_null_str);
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, JSMSG_UNEXPECTED_TYPE, bytes.get(),
js_null_str);
}
}
static UniqueChars
EncodeIdAsLatin1(JSContext* cx, HandleId id)
{
RootedValue idVal(cx, IdToValue(id));
JSString* idStr = ValueToSource(cx, idVal);
if (!idStr)
return nullptr;
return EncodeLatin1(cx, idStr);
}
void
js::ReportIsNullOrUndefinedForPropertyAccess(JSContext* cx, HandleValue v, HandleId key,
bool reportScanStack)
{
MOZ_ASSERT(v.isNullOrUndefined());
UniqueChars keyBytes = EncodeIdAsLatin1(cx, key);
UniqueChars keyBytes = IdToPrintableUTF8(cx, key, IdToPrintableBehavior::IdIsPropertyKey);
if (!keyBytes)
return;
if (!reportScanStack) {
JS_ReportErrorNumberLatin1(cx, GetErrorMessage, nullptr, JSMSG_PROPERTY_FAIL,
keyBytes.get(),
v.isUndefined() ? js_undefined_str : js_null_str);
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, JSMSG_PROPERTY_FAIL,
keyBytes.get(),
v.isUndefined() ? js_undefined_str : js_null_str);
return;
}
@ -964,15 +952,15 @@ js::ReportIsNullOrUndefinedForPropertyAccess(JSContext* cx, HandleValue v, Handl
return;
if (strcmp(bytes.get(), js_undefined_str) == 0 || strcmp(bytes.get(), js_null_str) == 0) {
JS_ReportErrorNumberLatin1(cx, GetErrorMessage, nullptr, JSMSG_PROPERTY_FAIL,
keyBytes.get(), bytes.get());
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, JSMSG_PROPERTY_FAIL,
keyBytes.get(), bytes.get());
} else if (v.isUndefined()) {
JS_ReportErrorNumberLatin1(cx, GetErrorMessage, nullptr, JSMSG_PROPERTY_FAIL_EXPR,
bytes.get(), js_undefined_str, keyBytes.get());
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, JSMSG_PROPERTY_FAIL_EXPR,
bytes.get(), js_undefined_str, keyBytes.get());
} else {
MOZ_ASSERT(v.isNull());
JS_ReportErrorNumberLatin1(cx, GetErrorMessage, nullptr, JSMSG_PROPERTY_FAIL_EXPR,
bytes.get(), js_null_str, keyBytes.get());
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, JSMSG_PROPERTY_FAIL_EXPR,
bytes.get(), js_null_str, keyBytes.get());
}
}
@ -989,9 +977,8 @@ js::ReportMissingArg(JSContext* cx, HandleValue v, unsigned arg)
if (!bytes)
return;
}
JS_ReportErrorNumberLatin1(cx, GetErrorMessage, nullptr,
JSMSG_MISSING_FUN_ARG,
argbuf, bytes ? bytes.get() : "");
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, JSMSG_MISSING_FUN_ARG,
argbuf, bytes ? bytes.get() : "");
}
bool
@ -1005,8 +992,8 @@ js::ReportValueErrorFlags(JSContext* cx, unsigned flags, const unsigned errorNum
if (!bytes)
return false;
return JS_ReportErrorFlagsAndNumberLatin1(cx, flags, GetErrorMessage, nullptr, errorNumber,
bytes.get(), arg1, arg2);
return JS_ReportErrorFlagsAndNumberUTF8(cx, flags, GetErrorMessage, nullptr, errorNumber,
bytes.get(), arg1, arg2);
}
JSObject*

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

@ -84,8 +84,8 @@ js::ReportNotObject(JSContext* cx, HandleValue v)
MOZ_ASSERT(!v.isObject());
if (UniqueChars bytes = DecompileValueGenerator(cx, JSDVG_SEARCH_STACK, v, nullptr)) {
JS_ReportErrorNumberLatin1(cx, GetErrorMessage, nullptr, JSMSG_NOT_NONNULL_OBJECT,
bytes.get());
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, JSMSG_NOT_NONNULL_OBJECT,
bytes.get());
}
}
@ -228,8 +228,8 @@ js::GetFirstArgumentAsObject(JSContext* cx, const CallArgs& args, const char* me
UniqueChars bytes = DecompileValueGenerator(cx, JSDVG_SEARCH_STACK, v, nullptr);
if (!bytes)
return false;
JS_ReportErrorNumberLatin1(cx, GetErrorMessage, nullptr, JSMSG_UNEXPECTED_TYPE,
bytes.get(), "not an object");
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, JSMSG_UNEXPECTED_TYPE,
bytes.get(), "not an object");
return false;
}

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

@ -8,6 +8,7 @@
#include "mozilla/PodOperations.h"
#include "mozilla/Printf.h"
#include "mozilla/RangedPtr.h"
#include <ctype.h>
#include <stdarg.h>
@ -16,6 +17,7 @@
#include "jsutil.h"
#include "ds/LifoAlloc.h"
#include "js/CharacterEncoding.h"
#include "util/Text.h"
#include "util/Windows.h"
#include "vm/JSContext.h"
@ -223,24 +225,17 @@ Sprinter::putString(JSString* s)
{
InvariantChecker ic(this);
size_t length = s->length();
JSFlatString* flat = s->ensureFlat(context);
if (!flat)
return false;
size_t length = JS::GetDeflatedUTF8StringLength(flat);
char* buffer = reserve(length);
if (!buffer)
return false;
JSLinearString* linear = s->ensureLinear(context);
if (!linear)
return false;
JS::AutoCheckCannotGC nogc;
if (linear->hasLatin1Chars()) {
PodCopy(reinterpret_cast<Latin1Char*>(buffer), linear->latin1Chars(nogc), length);
} else {
const char16_t* src = linear->twoByteChars(nogc);
for (size_t i = 0; i < length; i++)
buffer[i] = char(src[i]);
}
JS::DeflateStringToUTF8Buffer(flat, mozilla::RangedPtr<char>(buffer, length));
buffer[length] = '\0';
return true;

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

@ -299,7 +299,7 @@ ThrowErrorWithType(JSContext* cx, JSExnType type, const CallArgs& args)
JSString* str = ToString<CanGC>(cx, val);
if (!str)
return;
errorArgs[i - 1] = EncodeLatin1(cx, str);
errorArgs[i - 1] = StringToNewUTF8CharsZ(cx, *str);
} else {
errorArgs[i - 1] = DecompileValueGenerator(cx, JSDVG_SEARCH_STACK, val, nullptr);
}
@ -307,8 +307,8 @@ ThrowErrorWithType(JSContext* cx, JSExnType type, const CallArgs& args)
return;
}
JS_ReportErrorNumberLatin1(cx, GetErrorMessage, nullptr, errorNumber,
errorArgs[0].get(), errorArgs[1].get(), errorArgs[2].get());
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, errorNumber,
errorArgs[0].get(), errorArgs[1].get(), errorArgs[2].get());
}
static bool

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

@ -1324,8 +1324,8 @@ TypedArrayObjectTemplate<T>::fromObject(JSContext* cx, HandleObject other, Handl
UniqueChars bytes = DecompileValueGenerator(cx, JSDVG_SEARCH_STACK, otherVal, nullptr);
if (!bytes)
return nullptr;
JS_ReportErrorNumberLatin1(cx, GetErrorMessage, nullptr, JSMSG_NOT_ITERABLE,
bytes.get());
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, JSMSG_NOT_ITERABLE,
bytes.get());
return nullptr;
}