Bug 1283710 - Part 5: Rename message to toStringResult if it is the result of toString. r=bholley,jwalden,froydnj

This commit is contained in:
Tooru Fujisawa 2016-08-14 20:39:31 +09:00
Родитель 84473915d8
Коммит 8d69b4f507
19 изменённых файлов: 81 добавлений и 53 удалений

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

@ -533,7 +533,7 @@ WarningOnlyErrorReporter(JSContext* aCx, JSErrorReport* aRep)
workers::WorkerPrivate* worker = workers::GetWorkerPrivateFromContext(aCx);
MOZ_ASSERT(worker);
worker->ReportError(aCx, nullptr, aRep);
worker->ReportError(aCx, JS::ConstUTF8CharsZ(), aRep);
return;
}
@ -585,7 +585,7 @@ AutoJSAPI::ReportException()
win = xpc::AddonWindowOrNull(errorGlobal);
}
nsPIDOMWindowInner* inner = win ? win->AsInner() : nullptr;
xpcReport->Init(jsReport.report(), jsReport.message().c_str(),
xpcReport->Init(jsReport.report(), jsReport.toStringResult().c_str(),
nsContentUtils::IsCallerChrome(),
inner ? inner->WindowID() : 0);
if (inner && jsReport.report()->errorNumber != JSMSG_OUT_OF_MEMORY) {
@ -609,7 +609,7 @@ AutoJSAPI::ReportException()
// to get hold of it. After we invoke ReportError, clear the exception on
// cx(), just in case ReportError didn't.
JS_SetPendingException(cx(), exn);
worker->ReportError(cx(), jsReport.message().c_str(), jsReport.report());
worker->ReportError(cx(), jsReport.toStringResult(), jsReport.report());
ClearException();
}
} else {

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

@ -1022,7 +1022,7 @@ Promise::ReportRejectedPromise(JSContext* aCx, JS::HandleObject aPromise)
bool isChrome = isMainThread ? nsContentUtils::IsSystemPrincipal(nsContentUtils::ObjectPrincipal(aPromise))
: GetCurrentThreadWorkerPrivate()->IsChromeWorker();
nsGlobalWindow* win = isMainThread ? xpc::WindowGlobalOrNull(aPromise) : nullptr;
xpcReport->Init(report.report(), report.message().c_str(), isChrome,
xpcReport->Init(report.report(), report.toStringResult().c_str(), isChrome,
win ? win->AsInner()->WindowID() : 0);
// Now post an event to do the real reporting async
@ -2645,7 +2645,8 @@ Promise::MaybeReportRejected()
if (exp) {
xpcReport->Init(cx, exp, isChrome, windowID);
} else {
xpcReport->Init(report.report(), report.message(), isChrome, windowID);
xpcReport->Init(report.report(), report.toStringResult(),
isChrome, windowID);
}
// Now post an event to do the real reporting async

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

@ -2094,7 +2094,7 @@ ScriptExecutorRunnable::LogExceptionToConsole(JSContext* aCx,
}
RefPtr<xpc::ErrorReport> xpcReport = new xpc::ErrorReport();
xpcReport->Init(report.report(), report.message().c_str(),
xpcReport->Init(report.report(), report.toStringResult().c_str(),
aWorkerPrivate->IsChromeWorker(), aWorkerPrivate->WindowID());
RefPtr<AsyncErrorReporter> r = new AsyncErrorReporter(xpcReport);

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

@ -426,7 +426,7 @@ ExtractErrorValues(JSContext* aCx, JS::Handle<JS::Value> aValue,
// this report anywhere.
RefPtr<xpc::ErrorReport> report = new xpc::ErrorReport();
report->Init(err,
"<unknown>", // fallback message
"<unknown>", // toString result
false, // chrome
0); // window ID

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

@ -86,6 +86,7 @@
#include "nsProxyRelease.h"
#include "nsQueryObject.h"
#include "nsSandboxFlags.h"
#include "nsUTF8Utils.h"
#include "prthread.h"
#include "xpcpublic.h"
@ -5811,7 +5812,7 @@ WorkerPrivate::NotifyInternal(JSContext* aCx, Status aStatus)
}
void
WorkerPrivate::ReportError(JSContext* aCx, const char* aFallbackMessage,
WorkerPrivate::ReportError(JSContext* aCx, JS::ConstUTF8CharsZ aToStringResult,
JSErrorReport* aReport)
{
AssertIsOnWorkerThread();
@ -5855,13 +5856,19 @@ WorkerPrivate::ReportError(JSContext* aCx, const char* aFallbackMessage,
flags = nsIScriptError::errorFlag | nsIScriptError::exceptionFlag;
}
if (message.IsEmpty() && aFallbackMessage) {
nsDependentCString fallbackMessage(aFallbackMessage);
if (!AppendUTF8toUTF16(fallbackMessage, message, mozilla::fallible)) {
if (message.IsEmpty() && aToStringResult) {
nsDependentCString toStringResult(aToStringResult.c_str());
if (!AppendUTF8toUTF16(toStringResult, message, mozilla::fallible)) {
// Try again, with only a 1 KB string. Do this infallibly this time.
// If the user doesn't have 1 KB to spare we're done anyways.
nsDependentCString truncatedFallbackMessage(aFallbackMessage, 1024);
AppendUTF8toUTF16(truncatedFallbackMessage, message);
uint32_t index = std::min(uint32_t(1024), toStringResult.Length());
// Drop the last code point that may be cropped.
index = RewindToPriorUTF8Codepoint(toStringResult.BeginReading(), index);
nsDependentCString truncatedToStringResult(aToStringResult.c_str(),
index);
AppendUTF8toUTF16(truncatedToStringResult, message);
}
}

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

@ -9,6 +9,7 @@
#include "Workers.h"
#include "js/CharacterEncoding.h"
#include "nsIContentPolicy.h"
#include "nsIContentSecurityPolicy.h"
#include "nsILoadGroup.h"
@ -1164,7 +1165,8 @@ public:
NotifyInternal(JSContext* aCx, Status aStatus);
void
ReportError(JSContext* aCx, const char* aMessage, JSErrorReport* aReport);
ReportError(JSContext* aCx, JS::ConstUTF8CharsZ aToStringResult,
JSErrorReport* aReport);
static void
ReportErrorToConsole(const char* aMessage);

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

@ -307,7 +307,7 @@ Error(JSContext* cx, const char (&input)[N], uint32_t expectedLine,
CHECK(report.report()->errorNumber == JSMSG_JSON_BAD_PARSE);
const char* lineAndColumnASCII = JS_smprintf("line %d column %d", expectedLine, expectedColumn);
CHECK(strstr(report.message().c_str(), lineAndColumnASCII) != nullptr);
CHECK(strstr(report.toStringResult().c_str(), lineAndColumnASCII) != nullptr);
js_free((void*)lineAndColumnASCII);
/* We do not execute JS, so there should be no exception thrown. */

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

@ -41,11 +41,11 @@ GetSymbolExceptionType(JSContext* cx)
js::ErrorReport report(cx);
MOZ_RELEASE_ASSERT(report.init(cx, exn, js::ErrorReport::WithSideEffects));
if (strcmp(report.message().c_str(), "uncaught exception: Symbol(Symbol.iterator)") == 0)
if (strcmp(report.toStringResult().c_str(), "uncaught exception: Symbol(Symbol.iterator)") == 0)
return SYMBOL_ITERATOR;
if (strcmp(report.message().c_str(), "uncaught exception: Symbol(foo)") == 0)
if (strcmp(report.toStringResult().c_str(), "uncaught exception: Symbol(foo)") == 0)
return SYMBOL_FOO;
if (strcmp(report.message().c_str(), "uncaught exception: Symbol()") == 0)
if (strcmp(report.toStringResult().c_str(), "uncaught exception: Symbol()") == 0)
return SYMBOL_EMPTY;
MOZ_CRASH("Unexpected symbol");
}

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

@ -382,8 +382,8 @@ js::ReportUsageErrorASCII(JSContext* cx, HandleObject callee, const char* msg)
}
bool
js::PrintError(JSContext* cx, FILE* file, const char* message, JSErrorReport* report,
bool reportWarnings)
js::PrintError(JSContext* cx, FILE* file, JS::ConstUTF8CharsZ toStringResult,
JSErrorReport* report, bool reportWarnings)
{
MOZ_ASSERT(report);
@ -407,8 +407,7 @@ js::PrintError(JSContext* cx, FILE* file, const char* message, JSErrorReport* re
JS_free(cx, tmp);
}
if (!message)
message = report->message().c_str();
const char* message = toStringResult ? toStringResult.c_str() : report->message().c_str();
/* embedded newlines -- argh! */
const char* ctmp;

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

@ -631,8 +631,8 @@ ReportUsageErrorASCII(JSContext* cx, HandleObject callee, const char* msg);
* Returns false otherwise.
*/
extern bool
PrintError(JSContext* cx, FILE* file, const char* message, JSErrorReport* report,
bool reportWarnings);
PrintError(JSContext* cx, FILE* file, JS::ConstUTF8CharsZ toStringResult,
JSErrorReport* report, bool reportWarnings);
/*
* Send a JSErrorReport to the warningReporter callback.

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

@ -28,6 +28,7 @@
#include "jswrapper.h"
#include "gc/Marking.h"
#include "js/CharacterEncoding.h"
#include "vm/ErrorObject.h"
#include "vm/GlobalObject.h"
#include "vm/SavedStacks.h"
@ -512,7 +513,7 @@ js::ErrorToException(JSContext* cx, JSErrorReport* reportp,
// we cannot construct the Error constructor without self-hosted code. Just
// print the error to stderr to help debugging.
if (cx->runtime()->isSelfHostingCompartment(cx->compartment())) {
PrintError(cx, stderr, nullptr, reportp, true);
PrintError(cx, stderr, JS::ConstUTF8CharsZ(), reportp, true);
return;
}
@ -852,7 +853,7 @@ ErrorReport::init(JSContext* cx, HandleValue exn,
const char* utf8Message = nullptr;
if (str)
utf8Message = bytesStorage.encodeUtf8(cx, str);
utf8Message = toStringResultBytesStorage.encodeUtf8(cx, str);
if (!utf8Message)
utf8Message = "unknown (can't convert to string)";
@ -870,7 +871,7 @@ ErrorReport::init(JSContext* cx, HandleValue exn,
return false;
}
} else {
message_ = JS::ConstUTF8CharsZ(utf8Message, strlen(utf8Message));
toStringResult_ = JS::ConstUTF8CharsZ(utf8Message, strlen(utf8Message));
/* Flag the error as an exception. */
reportp->flags |= JSREPORT_EXCEPTION;
}
@ -914,7 +915,7 @@ ErrorReport::populateUncaughtExceptionReportUTF8VA(JSContext* cx, va_list ap)
return false;
}
message_ = ownedReport.message();
toStringResult_ = ownedReport.message();
reportp = &ownedReport;
return true;
}

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

@ -1452,9 +1452,9 @@ struct MOZ_STACK_CLASS JS_FRIEND_API(ErrorReport)
return reportp;
}
const JS::ConstUTF8CharsZ message()
const JS::ConstUTF8CharsZ toStringResult()
{
return message_;
return toStringResult_;
}
private:
@ -1473,9 +1473,6 @@ struct MOZ_STACK_CLASS JS_FRIEND_API(ErrorReport)
// We may have a provided JSErrorReport, so need a way to represent that.
JSErrorReport* reportp;
// And we may have a message.
JS::ConstUTF8CharsZ message_;
// Or we may need to synthesize a JSErrorReport one of our own.
JSErrorReport ownedReport;
@ -1489,11 +1486,14 @@ struct MOZ_STACK_CLASS JS_FRIEND_API(ErrorReport)
// And we need to root our exception value.
JS::RootedObject exnObject;
// And possibly some byte storage for our message_.
JSAutoByteString bytesStorage;
// And for our filename.
JSAutoByteString filename;
// We may have a result of error.toString().
// FIXME: We should not call error.toString(), since it could have side
// effect (see bug 633623).
JS::ConstUTF8CharsZ toStringResult_;
JSAutoByteString toStringResultBytesStorage;
};
/* Implemented in vm/StructuredClone.cpp. */

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

@ -6403,7 +6403,7 @@ js::shell::AutoReportException::~AutoReportException()
MOZ_ASSERT(!JSREPORT_IS_WARNING(report.report()->flags));
FILE* fp = ErrorFilePointer();
PrintError(cx, fp, report.message().c_str(), report.report(), reportWarnings);
PrintError(cx, fp, report.toStringResult(), report.report(), reportWarnings);
{
JS::AutoSaveExceptionState savedExc(cx);
@ -6439,7 +6439,7 @@ js::shell::WarningReporter(JSContext* cx, JSErrorReport* report)
}
// Print the warning.
PrintError(cx, fp, nullptr, report, reportWarnings);
PrintError(cx, fp, JS::ConstUTF8CharsZ(), report, reportWarnings);
}
static bool

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

@ -37,6 +37,7 @@
#include "gc/Policy.h"
#include "jit/AtomicOperations.h"
#include "jit/InlinableNatives.h"
#include "js/CharacterEncoding.h"
#include "js/Date.h"
#include "vm/Compression.h"
#include "vm/GeneratorObject.h"
@ -71,7 +72,7 @@ selfHosting_WarningReporter(JSContext* cx, JSErrorReport* report)
MOZ_ASSERT(report);
MOZ_ASSERT(JSREPORT_IS_WARNING(report->flags));
PrintError(cx, stderr, nullptr, report, true);
PrintError(cx, stderr, JS::ConstUTF8CharsZ(), report, true);
}
static bool
@ -2682,7 +2683,7 @@ MaybePrintAndClearPendingException(JSContext* cx, FILE* file)
}
MOZ_ASSERT(!JSREPORT_IS_WARNING(report.report()->flags));
PrintError(cx, file, report.message().c_str(), report.report(), true);
PrintError(cx, file, report.toStringResult(), report.report(), true);
}
class MOZ_STACK_CLASS AutoSelfHostingErrorReporter

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

@ -1090,11 +1090,11 @@ XPCConvert::JSValToXPCException(MutableHandleValue s,
// extract the report and build an xpcexception from that
const JSErrorReport* report;
if (nullptr != (report = JS_ErrorFromException(cx, obj))) {
JSAutoByteString message;
JSString* str;
if (nullptr != (str = ToString(cx, s)))
message.encodeLatin1(cx, str);
return JSErrorToXPCException(message.ptr(), ifaceName,
JSAutoByteString toStringResult;
RootedString str(cx, ToString(cx, s));
if (str)
toStringResult.encodeUtf8(cx, str);
return JSErrorToXPCException(toStringResult.ptr(), ifaceName,
methodName, report, exceptn);
}
@ -1191,7 +1191,7 @@ XPCConvert::JSValToXPCException(MutableHandleValue s,
// static
nsresult
XPCConvert::JSErrorToXPCException(const char* message,
XPCConvert::JSErrorToXPCException(const char* toStringResult,
const char* ifaceName,
const char* methodName,
const JSErrorReport* report,
@ -1204,8 +1204,8 @@ XPCConvert::JSErrorToXPCException(const char* message,
nsAutoString bestMessage;
if (report && report->message()) {
CopyUTF8toUTF16(report->message().c_str(), bestMessage);
} else if (message) {
CopyASCIItoUTF16(message, bestMessage);
} else if (toStringResult) {
CopyUTF8toUTF16(toStringResult, bestMessage);
} else {
bestMessage.AssignLiteral("JavaScript Error");
}

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

@ -169,7 +169,7 @@ nsXPConnect::IsISupportsDescendant(nsIInterfaceInfo* info)
}
void
xpc::ErrorReport::Init(JSErrorReport* aReport, const char* aFallbackMessage,
xpc::ErrorReport::Init(JSErrorReport* aReport, const char* aToStringResult,
bool aIsChrome, uint64_t aWindowID)
{
mCategory = aIsChrome ? NS_LITERAL_CSTRING("chrome javascript")
@ -177,8 +177,8 @@ xpc::ErrorReport::Init(JSErrorReport* aReport, const char* aFallbackMessage,
mWindowID = aWindowID;
ErrorReportToMessageString(aReport, mErrorMsg);
if (mErrorMsg.IsEmpty() && aFallbackMessage) {
mErrorMsg.AssignWithConversion(aFallbackMessage);
if (mErrorMsg.IsEmpty() && aToStringResult) {
AppendUTF8toUTF16(aToStringResult, mErrorMsg);
}
if (!aReport->filename) {

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

@ -2376,7 +2376,7 @@ public:
const char* methodName,
nsIException** exception);
static nsresult JSErrorToXPCException(const char* message,
static nsresult JSErrorToXPCException(const char* toStringResult,
const char* ifaceName,
const char* methodName,
const JSErrorReport* report,

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

@ -516,7 +516,7 @@ class ErrorReport {
, mIsMuted(false)
{}
void Init(JSErrorReport* aReport, const char* aFallbackMessage,
void Init(JSErrorReport* aReport, const char* aToStringResult,
bool aIsChrome, uint64_t aWindowID);
void Init(JSContext* aCx, mozilla::dom::Exception* aException,
bool aIsChrome, uint64_t aWindowID);

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

@ -13,6 +13,7 @@
#include "nscore.h"
#include "mozilla/Assertions.h"
#include "mozilla/SSE.h"
#include "mozilla/TypeTraits.h"
#include "nsCharTraits.h"
@ -722,4 +723,20 @@ private:
};
#endif // MOZILLA_INTERNAL_API
template<typename Char, typename UnsignedT>
inline UnsignedT
RewindToPriorUTF8Codepoint(const Char* utf8Chars, UnsignedT index)
{
static_assert(mozilla::IsSame<Char, char>::value ||
mozilla::IsSame<Char, unsigned char>::value ||
mozilla::IsSame<Char, signed char>::value,
"UTF-8 data must be in 8-bit units");
static_assert(mozilla::IsUnsigned<UnsignedT>::value, "index type must be unsigned");
while (index > 0 && (utf8Chars[index] & 0xC0) == 0x80)
--index;
return index;
}
#endif /* !defined(nsUTF8Utils_h_) */