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
parent 044b5a993c
commit 556e7c80df
19 changed files with 81 additions and 53 deletions

View File

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

View File

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

View File

@@ -2094,7 +2094,7 @@ ScriptExecutorRunnable::LogExceptionToConsole(JSContext* aCx,
} }
RefPtr<xpc::ErrorReport> xpcReport = new xpc::ErrorReport(); 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()); aWorkerPrivate->IsChromeWorker(), aWorkerPrivate->WindowID());
RefPtr<AsyncErrorReporter> r = new AsyncErrorReporter(xpcReport); RefPtr<AsyncErrorReporter> r = new AsyncErrorReporter(xpcReport);

View File

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

View File

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

View File

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

View File

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

View File

@@ -41,11 +41,11 @@ GetSymbolExceptionType(JSContext* cx)
js::ErrorReport report(cx); js::ErrorReport report(cx);
MOZ_RELEASE_ASSERT(report.init(cx, exn, js::ErrorReport::WithSideEffects)); 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; 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; 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; return SYMBOL_EMPTY;
MOZ_CRASH("Unexpected symbol"); MOZ_CRASH("Unexpected symbol");
} }

View File

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

View File

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

View File

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

View File

@@ -1452,9 +1452,9 @@ struct MOZ_STACK_CLASS JS_FRIEND_API(ErrorReport)
return reportp; return reportp;
} }
const JS::ConstUTF8CharsZ message() const JS::ConstUTF8CharsZ toStringResult()
{ {
return message_; return toStringResult_;
} }
private: 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. // We may have a provided JSErrorReport, so need a way to represent that.
JSErrorReport* reportp; JSErrorReport* reportp;
// And we may have a message.
JS::ConstUTF8CharsZ message_;
// Or we may need to synthesize a JSErrorReport one of our own. // Or we may need to synthesize a JSErrorReport one of our own.
JSErrorReport ownedReport; JSErrorReport ownedReport;
@@ -1489,11 +1486,14 @@ struct MOZ_STACK_CLASS JS_FRIEND_API(ErrorReport)
// And we need to root our exception value. // And we need to root our exception value.
JS::RootedObject exnObject; JS::RootedObject exnObject;
// And possibly some byte storage for our message_.
JSAutoByteString bytesStorage;
// And for our filename. // And for our filename.
JSAutoByteString 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. */ /* Implemented in vm/StructuredClone.cpp. */

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -13,6 +13,7 @@
#include "nscore.h" #include "nscore.h"
#include "mozilla/Assertions.h" #include "mozilla/Assertions.h"
#include "mozilla/SSE.h" #include "mozilla/SSE.h"
#include "mozilla/TypeTraits.h"
#include "nsCharTraits.h" #include "nsCharTraits.h"
@@ -722,4 +723,20 @@ private:
}; };
#endif // MOZILLA_INTERNAL_API #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_) */ #endif /* !defined(nsUTF8Utils_h_) */