bug 317476: The error thrown by JS_ReportError is not catchable. r=brendan

This commit is contained in:
mrbkap%gmail.com 2005-11-28 20:42:30 +00:00
parent b51cc879fb
commit 3a77b12867
3 changed files with 21 additions and 5 deletions

View File

@ -1612,6 +1612,13 @@ TestUtf8(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
return !JS_IsExceptionPending (cx);
}
static JSBool
ThrowError(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
JS_ReportError(cx, "This is an error");
return JS_FALSE;
}
static JSFunctionSpec shell_functions[] = {
{"version", Version, 0},
{"options", Options, 0},
@ -1627,6 +1634,7 @@ static JSFunctionSpec shell_functions[] = {
{"pc2line", PCToLine, 0},
{"stringsAreUtf8", StringsAreUtf8, 0},
{"testUtf8", TestUtf8, 1},
{"throwError", ThrowError, 0},
#ifdef DEBUG
{"dis", Disassemble, 1},
{"dissrc", DisassWithSrc, 1},
@ -1668,6 +1676,7 @@ static char *shell_help_messages[] = {
"pc2line(fun[, pc]) Map PC to line number",
"stringsAreUTF8() Check if strings are UTF-8 encoded",
"testUTF8(mode) Perform UTF-8 tests (modes are 1 to 4)",
"throwError() Throw an error from JS_ReportError",
#ifdef DEBUG
"dis([fun]) Disassemble functions into bytecodes",
"dissrc([fun]) Disassemble functions with source lines",

View File

@ -285,3 +285,4 @@ MSG_DEF(JSMSG_BUFFER_TOO_SMALL, 202, 0, JSEXN_INTERNALERR, "buffer too sma
MSG_DEF(JSMSG_BAD_SURROGATE_CHAR, 203, 1, JSEXN_TYPEERR, "bad surrogate character {0}")
MSG_DEF(JSMSG_UTF8_CHAR_TOO_LARGE, 204, 1, JSEXN_TYPEERR, "UTF-8 character {0} too large")
MSG_DEF(JSMSG_MALFORMED_UTF8_CHAR, 205, 1, JSEXN_TYPEERR, "malformed UTF-8 character sequence at offset {0}")
MSG_DEF(JSMSG_USER_DEFINED_ERROR, 206, 0, JSEXN_ERR, "JS_ReportError was called")

View File

@ -758,7 +758,9 @@ js_ReportOutOfMemory(JSContext *cx, JSErrorCallback callback)
JSBool
js_ReportErrorVA(JSContext *cx, uintN flags, const char *format, va_list ap)
{
char *last;
char *message;
jschar *ucmessage;
size_t messagelen;
JSStackFrame *fp;
JSErrorReport report;
JSBool warning;
@ -766,12 +768,15 @@ js_ReportErrorVA(JSContext *cx, uintN flags, const char *format, va_list ap)
if ((flags & JSREPORT_STRICT) && !JS_HAS_STRICT_OPTION(cx))
return JS_TRUE;
last = JS_vsmprintf(format, ap);
if (!last)
message = JS_vsmprintf(format, ap);
if (!message)
return JS_FALSE;
messagelen = strlen(message);
memset(&report, 0, sizeof (struct JSErrorReport));
report.flags = flags;
report.errorNumber = JSMSG_USER_DEFINED_ERROR;
report.ucmessage = ucmessage = js_InflateString(cx, message, &messagelen);
/* Find the top-most active script frame, for best line number blame. */
for (fp = cx->fp; fp; fp = fp->down) {
@ -788,8 +793,9 @@ js_ReportErrorVA(JSContext *cx, uintN flags, const char *format, va_list ap)
warning = JS_FALSE;
}
ReportError(cx, last, &report);
free(last);
ReportError(cx, message, &report);
free(message);
JS_free(cx, ucmessage);
return warning;
}