From 89be48b3cc2429d0575bda8b0b7fa7277d43d984 Mon Sep 17 00:00:00 2001 From: Dan Witte Date: Tue, 15 Dec 2009 14:20:48 -0800 Subject: [PATCH] Bug 518621 - JS_ReportErrorNumber ignores exception type for user-generated messages. r=mrbkap --- js/ctypes/ctypes.msg | 7 +------ js/src/jsapi.cpp | 3 ++- js/src/jscntxt.cpp | 13 ++++++++----- js/src/jsexn.cpp | 8 ++++++-- js/src/jsexn.h | 3 ++- js/src/jsscan.cpp | 2 +- 6 files changed, 20 insertions(+), 16 deletions(-) diff --git a/js/ctypes/ctypes.msg b/js/ctypes/ctypes.msg index 798bab7dfa3..479f9de09d6 100644 --- a/js/ctypes/ctypes.msg +++ b/js/ctypes/ctypes.msg @@ -43,10 +43,5 @@ */ MSG_DEF(CTYPESMSG_PLACEHOLDER_0, 0, 0, JSEXN_NONE, NULL) -MSG_DEF(CTYPESMSG_PLACEHOLDER_1, 1, 0, JSEXN_NONE, NULL) -MSG_DEF(CTYPESMSG_PLACEHOLDER_2, 2, 0, JSEXN_NONE, NULL) - -// This error has to be number 3 in order to be a TypeError, -// due to a bug in the js engine. -MSG_DEF(CTYPESMSG_TYPE_ERROR, 3, 2, JSEXN_TYPEERR, "expected type {0}, got {1}") +MSG_DEF(CTYPESMSG_TYPE_ERROR, 1, 2, JSEXN_TYPEERR, "expected type {0}, got {1}") diff --git a/js/src/jsapi.cpp b/js/src/jsapi.cpp index 4f86761630a..59f11f3ef63 100644 --- a/js/src/jsapi.cpp +++ b/js/src/jsapi.cpp @@ -5827,7 +5827,8 @@ JS_PUBLIC_API(JSBool) JS_ThrowReportedError(JSContext *cx, const char *message, JSErrorReport *reportp) { - return JS_IsRunning(cx) && js_ErrorToException(cx, message, reportp); + return JS_IsRunning(cx) && + js_ErrorToException(cx, message, reportp, NULL, NULL); } JS_PUBLIC_API(JSBool) diff --git a/js/src/jscntxt.cpp b/js/src/jscntxt.cpp index ec2793a6b05..af629793673 100644 --- a/js/src/jscntxt.cpp +++ b/js/src/jscntxt.cpp @@ -1281,7 +1281,8 @@ MarkLocalRoots(JSTracer *trc, JSLocalRootStack *lrs) } static void -ReportError(JSContext *cx, const char *message, JSErrorReport *reportp) +ReportError(JSContext *cx, const char *message, JSErrorReport *reportp, + JSErrorCallback callback, void *userRef) { /* * Check the error report, and set a JavaScript-catchable exception @@ -1290,7 +1291,8 @@ ReportError(JSContext *cx, const char *message, JSErrorReport *reportp) * on the error report, and exception-aware hosts should ignore it. */ JS_ASSERT(reportp); - if (reportp->errorNumber == JSMSG_UNCAUGHT_EXCEPTION) + if ((!callback || callback == js_GetErrorMessage) && + reportp->errorNumber == JSMSG_UNCAUGHT_EXCEPTION) reportp->flags |= JSREPORT_EXCEPTION; /* @@ -1301,7 +1303,8 @@ ReportError(JSContext *cx, const char *message, JSErrorReport *reportp) * propagates out of scope. This is needed for compatability * with the old scheme. */ - if (!JS_IsRunning(cx) || !js_ErrorToException(cx, message, reportp)) { + if (!JS_IsRunning(cx) || + !js_ErrorToException(cx, message, reportp, callback, userRef)) { js_ReportErrorAgain(cx, message, reportp); } else if (cx->debugHooks->debugErrorHook && cx->errorReporter) { JSDebugErrorHook hook = cx->debugHooks->debugErrorHook; @@ -1457,7 +1460,7 @@ js_ReportErrorVA(JSContext *cx, uintN flags, const char *format, va_list ap) warning = JSREPORT_IS_WARNING(report.flags); - ReportError(cx, message, &report); + ReportError(cx, message, &report, NULL, NULL); js_free(message); cx->free(ucmessage); return warning; @@ -1651,7 +1654,7 @@ js_ReportErrorNumberVA(JSContext *cx, uintN flags, JSErrorCallback callback, return JS_FALSE; } - ReportError(cx, message, &report); + ReportError(cx, message, &report, callback, userRef); if (message) cx->free(message); diff --git a/js/src/jsexn.cpp b/js/src/jsexn.cpp index e76d0e0c386..443198e1cd9 100644 --- a/js/src/jsexn.cpp +++ b/js/src/jsexn.cpp @@ -1104,7 +1104,8 @@ static struct exnname { char *name; char *exception; } errortoexnname[] = { #endif /* DEBUG */ JSBool -js_ErrorToException(JSContext *cx, const char *message, JSErrorReport *reportp) +js_ErrorToException(JSContext *cx, const char *message, JSErrorReport *reportp, + JSErrorCallback callback, void *userRef) { JSErrNum errorNumber; const JSErrorFormatString *errorString; @@ -1124,7 +1125,10 @@ js_ErrorToException(JSContext *cx, const char *message, JSErrorReport *reportp) /* Find the exception index associated with this error. */ errorNumber = (JSErrNum) reportp->errorNumber; - errorString = js_GetLocalizedErrorMessage(cx, NULL, NULL, errorNumber); + if (!callback || callback == js_GetErrorMessage) + errorString = js_GetLocalizedErrorMessage(cx, NULL, NULL, errorNumber); + else + errorString = callback(userRef, NULL, errorNumber); exn = errorString ? (JSExnType) errorString->exnType : JSEXN_NONE; JS_ASSERT(exn < JSEXN_LIMIT); diff --git a/js/src/jsexn.h b/js/src/jsexn.h index 867586d8394..0e398aa7782 100644 --- a/js/src/jsexn.h +++ b/js/src/jsexn.h @@ -63,7 +63,8 @@ js_InitExceptionClasses(JSContext *cx, JSObject *obj); * found and set, JS_FALSE otherwise. */ extern JSBool -js_ErrorToException(JSContext *cx, const char *message, JSErrorReport *reportp); +js_ErrorToException(JSContext *cx, const char *message, JSErrorReport *reportp, + JSErrorCallback callback, void *userRef); /* * Called if a JS API call to js_Execute or js_InternalCall fails; calls the diff --git a/js/src/jsscan.cpp b/js/src/jsscan.cpp index 3d7ed726deb..a9c974fcf98 100644 --- a/js/src/jsscan.cpp +++ b/js/src/jsscan.cpp @@ -607,7 +607,7 @@ ReportCompileErrorNumberVA(JSContext *cx, JSTokenStream *ts, JSParseNode *pn, * which is likely spurious. */ if (!(ts->flags & TSF_ERROR)) { - if (js_ErrorToException(cx, message, &report)) + if (js_ErrorToException(cx, message, &report, NULL, NULL)) onError = NULL; }