- Use consistent spelling in the API: "Callback" not "CallBack" -- it's a one

word neologism, not two words.
- Use consistent neighboring terseness ("error" rather than "err" in intercaps
  identifiers).
- Don't leave pointers in JSErrorReport to freed memory if bailing on OOM in
  jscntxt.c:js_ExpandErrorArguments.
- Hanging indentation, code fusion via continue, and other misc. cleanups.


git-svn-id: svn://10.0.0.236/trunk@9455 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
brendan%netscape.com 1998-09-06 08:07:35 +00:00
parent 3862b13620
commit f85feaca03
8 changed files with 103 additions and 94 deletions

View File

@ -2499,15 +2499,15 @@ JS_ReportError(JSContext *cx, const char *format, ...)
}
JS_PUBLIC_API(void)
JS_ReportErrorNumber(JSContext *cx, JSErrorCallBack errCallBack, void *userRef,
const uintN errorNumber, ...)
JS_ReportErrorNumber(JSContext *cx, JSErrorCallback errorCallback,
void *userRef, const uintN errorNumber, ...)
{
va_list ap;
CHECK_REQUEST(cx);
va_start(ap, errorNumber);
js_ReportErrorNumberVA(cx, JSREPORT_ERROR,
errCallBack, userRef, errorNumber, ap);
js_ReportErrorNumberVA(cx, JSREPORT_ERROR, errorCallback, userRef,
errorNumber, ap);
va_end(ap);
}

View File

@ -931,8 +931,8 @@ JS_ReportError(JSContext *cx, const char *format, ...);
* As above, but use an errorNumber for the format string
*/
extern JS_PUBLIC_API(void)
JS_ReportErrorNumber(JSContext *cx, JSErrorCallBack errCallBack, void *userRef,
const uintN errorNumber, ...);
JS_ReportErrorNumber(JSContext *cx, JSErrorCallback errorCallback,
void *userRef, const uintN errorNumber, ...);
/*
* As above, but report a warning instead (JSREPORT_IS_WARNING(report->flags)).

View File

@ -196,21 +196,21 @@ js_ReportErrorVA(JSContext *cx, uintN flags, const char *format, va_list ap)
free(last);
}
/*
* The arguments from ap need to be packaged up into an array and stored
* into the report struct.
*
* The format string addressed by the error number may contain operands
* identified by the format {N}, where N is a decimal digit. Each of these
* is to be replaced by the Nth argument from the va_list. The complete
* message is placed into reportp->ucmessage converted to a JSString.
*
* returns true/false if the expansion succeeds (can fail for memory errors)
*/
JSBool
js_ExpandErrorArguments(JSContext *cx, JSErrorCallBack callback,
js_ExpandErrorArguments(JSContext *cx, JSErrorCallback callback,
void *userRef, const uintN errorNumber, char **message,
JSErrorReport *reportp, va_list ap)
/*
The arguments from va_list need to be packaged up into an array and stored
into the report struct.
The format string addressed by the error number may contain operands
identified by the format {N}, where N is a decimal digit. Each of these
is to be replaced by the Nth argument from the va_list. The complete
message is placed into reportp->ucmessage converted to a JSString.
returns true/false if the expansion succeeds (can fail for memory errors)
*/
{
const JSErrorFormatString *fmtData;
int i;
@ -221,70 +221,75 @@ js_ExpandErrorArguments(JSContext *cx, JSErrorCallBack callback,
fmtData = (*callback)(userRef, "Mountain View", errorNumber);
if (fmtData != NULL) {
argCount = fmtData->argCount;
if (argCount > 0) {
/*
gather the arguments into a char * array, the
messageArgs field is supposed to be an array of
JSString's and we'll convert them later.
*/
reportp->messageArgs = (JSString **)malloc(sizeof(char *) * argCount);
if (!reportp->messageArgs) return JS_FALSE;
for (i = 0; i < argCount; i++)
reportp->messageArgs[i] = (JSString *) va_arg(ap, char *);
}
if (argCount > 0) {
/*
* Gather the arguments into a char * array, the
* messageArgs field is supposed to be an array of
* JSString's and we'll convert them later.
*/
reportp->messageArgs = malloc(sizeof(char *) * argCount);
if (!reportp->messageArgs)
return JS_FALSE;
for (i = 0; i < argCount; i++)
reportp->messageArgs[i] = (JSString *) va_arg(ap, char *);
}
/*
parse the error format, substituting the argument X
for {X} in the format
*/
* Parse the error format, substituting the argument X
* for {X} in the format.
*/
if (argCount > 0) {
if (fmtData->format) {
const char *fmt;
const char *fmt, *arg;
char *out;
int expandedArgs = 0;
int expandedLength
= strlen(fmtData->format)
- (3 * argCount); /* exclude the {n} */
for (i = 0; i < argCount; i++)
= strlen(fmtData->format)
- (3 * argCount); /* exclude the {n} */
for (i = 0; i < argCount; i++) {
expandedLength
+= strlen((char *)(reportp->messageArgs[i]));
+= strlen((char *)reportp->messageArgs[i]);
}
*message = out = malloc(expandedLength + 1);
if (!out) {
if (reportp->messageArgs) free(reportp->messageArgs);
return JS_FALSE;
if (reportp->messageArgs) {
free(reportp->messageArgs);
reportp->messageArgs = NULL;
}
return JS_FALSE;
}
fmt = fmtData->format;
while (*fmt) {
if (*fmt == '{') {
if (*fmt == '{') { /* balance} */
if (isdigit(fmt[1])) {
int d = JS7_UNDEC(fmt[1]);
PR_ASSERT(expandedArgs < argCount);
strcpy(out, (char *)(reportp->messageArgs[d]));
out += strlen((char *)(reportp->messageArgs[d]));
arg = (char *)reportp->messageArgs[d];
strcpy(out, arg);
out += strlen(arg);
fmt += 3;
expandedArgs++;
continue;
}
else
*out++ = *fmt++;
}
else
*out++ = *fmt++;
*out++ = *fmt++;
}
PR_ASSERT(expandedArgs == argCount);
*out = '\0';
}
/*
Now convert all the arguments to JSString's
*/
* Now convert all the arguments to JSStrings.
*/
for (i = 0; i < argCount; i++) {
reportp->messageArgs[i] = JS_NewStringCopyZ(cx,
(char *)(reportp->messageArgs[i]));
reportp->messageArgs[i] =
JS_NewStringCopyZ(cx, (char *)reportp->messageArgs[i]);
}
}
else
} else {
*message = JS_strdup(cx, fmtData->format);
}
/*
And finally convert the message
*/
* And finally convert the message.
*/
reportp->ucmessage = JS_NewStringCopyZ(cx, *message);
}
}
@ -299,7 +304,7 @@ js_ExpandErrorArguments(JSContext *cx, JSErrorCallBack callback,
}
void
js_ReportErrorNumberVA(JSContext *cx, uintN flags, JSErrorCallBack callback,
js_ReportErrorNumberVA(JSContext *cx, uintN flags, JSErrorCallback callback,
void *userRef, const uintN errorNumber, va_list ap)
{
JSStackFrame *fp;
@ -344,8 +349,10 @@ js_ReportErrorNumberVA(JSContext *cx, uintN flags, JSErrorCallBack callback,
js_ReportErrorAgain(cx, message, reportp);
if (message) free(message);
if (report.messageArgs) free(report.messageArgs);
if (message)
free(message);
if (report.messageArgs)
free(report.messageArgs);
}
JS_FRIEND_API(void)

View File

@ -159,9 +159,9 @@ struct JSContext {
JSPackedBool gcActive;
jsrefcount requestDepth;
#endif
JSStackFrame *dormantFrameChain; /* dormant frame chains */
JSPackedBool throwing; /* is there a pending exception? */
jsval exception; /* most-recently-thrown exceptin */
JSStackFrame *dormantFrameChain; /* dormant frame chains */
JSPackedBool throwing; /* is there a pending exception? */
jsval exception; /* most-recently-thrown exceptin */
};
typedef struct JSInterpreterHooks {
@ -203,14 +203,14 @@ js_GetErrorMessage(void *userRef, const char *locale, const uintN errorNumber);
extern void
js_ReportErrorVA(JSContext *cx, uintN flags, const char *format, va_list ap);
extern void
js_ReportErrorNumberVA(JSContext *cx, uintN flags, JSErrorCallBack callback,
void *userRef, const uintN errorNumber, va_list ap);
js_ReportErrorNumberVA(JSContext *cx, uintN flags, JSErrorCallback callback,
void *userRef, const uintN errorNumber, va_list ap);
extern JSBool
js_ExpandErrorArguments(JSContext *cx, JSErrorCallBack callback,
void *userRef, const uintN errorNumber,
char **message, JSErrorReport *reportp,
va_list ap);
js_ExpandErrorArguments(JSContext *cx, JSErrorCallback callback,
void *userRef, const uintN errorNumber,
char **message, JSErrorReport *reportp,
va_list ap);
#endif
/*

View File

@ -460,23 +460,23 @@ js_EmitFunctionBody(JSContext *cx, JSCodeGenerator *cg, JSParseNode *body,
static JSBool
FixupFinallyJumps(JSContext *cx, JSCodeGenerator *cg, ptrdiff_t tryStart,
ptrdiff_t finallyIndex)
ptrdiff_t finallyIndex)
{
jsbytecode *pc;
pc = cg->base + tryStart;
BYTECODE_ITER(pc, cg->next, \
if (*pc == JSOP_GOSUB) { \
ptrdiff_t index = GET_JUMP_OFFSET(pc); \
if (index <= 0) { \
if (index == 0) { \
index = finallyIndex - (pc - cg->base); \
} else { \
index++; \
} \
CHECK_AND_SET_JUMP_OFFSET(cx, cg, pc, index); \
} \
} \
);
BYTECODE_ITER(pc, cg->next,
if (*pc == JSOP_GOSUB) {
ptrdiff_t index = GET_JUMP_OFFSET(pc);
if (index <= 0) {
if (index == 0)
index = finallyIndex - (pc - cg->base);
else
index++;
CHECK_AND_SET_JUMP_OFFSET(cx, cg, pc, index);
}
}
);
return JS_TRUE;
}
@ -485,11 +485,12 @@ FixupCatchJumps(JSContext *cx, JSCodeGenerator *cg, ptrdiff_t tryStart,
ptrdiff_t postCatch)
{
jsbytecode *pc;
pc = cg->base + tryStart;
BYTECODE_ITER(pc, cg->next, \
if (*pc == JSOP_GOTO && !GET_JUMP_OFFSET(pc)) { \
CHECK_AND_SET_JUMP_OFFSET(cx, cg, pc, postCatch - (pc - cg->base)); \
} \
BYTECODE_ITER(pc, cg->next,
if (*pc == JSOP_GOTO && !GET_JUMP_OFFSET(pc)) {
CHECK_AND_SET_JUMP_OFFSET(cx, cg, pc, postCatch - (pc - cg->base));
}
);
return JS_TRUE;
}
@ -827,7 +828,7 @@ js_EmitTree(JSContext *cx, JSCodeGenerator *cg, JSParseNode *pn)
if (!js_EmitTree(cx, cg, pn4))
return JS_FALSE;
pn3->pn_offset = pn4->pn_offset;
if (pn3->pn_type == TOK_DEFAULT)
if (pn3->pn_type == TOK_DEFAULT)
off = pn3->pn_offset - top;
}
@ -1180,6 +1181,7 @@ js_EmitTree(JSContext *cx, JSCodeGenerator *cg, JSParseNode *pn)
return js_PopStatementCG(cx, cg);
#if JS_HAS_EXCEPTIONS
case TOK_TRY: {
ptrdiff_t start, end, catchStart, finallyCatch, catchjmp = -1;
JSParseNode *iter = pn;
@ -1371,7 +1373,7 @@ js_EmitTree(JSContext *cx, JSCodeGenerator *cg, JSParseNode *pn)
if (pn->pn_kid3 ||
(catchjmp != -1 && iter->pn_kid1->pn_expr)) {
/*
/*
* Emit another stack fix, because the catch could itself
* throw an exception in an unbalanced state, and the finally
* may need to call functions etc.
@ -2385,7 +2387,7 @@ js_AllocTryNotes(JSContext *cx, JSCodeGenerator *cg)
ptrdiff_t nextOffset = cg->tryNext - cg->tryBase;
size_t oldsize = (char *)cg->tryLimit - (char *)cg->tryBase;
size = oldsize + cg->treeContext.tryCount * sizeof(JSTryNote);
PR_ARENA_GROW(cg->tryBase, &cx->tempPool, oldsize, size);
if (!cg->tryBase)
return JS_FALSE;

View File

@ -159,9 +159,8 @@ static JSClass prop_iterator_class = {
PR_END_MACRO
/*
* This POP variant is called only for bitwise operators and for tableswitch,
* so we don't bother to inline it. The calls in Interpret must therefore
* SAVE_SP first!
* This POP variant is called only for bitwise operators, so we don't bother
* to inline it. The calls in Interpret must therefore SAVE_SP first!
*/
static JSBool
PopInt(JSContext *cx, jsint *ip)

View File

@ -195,17 +195,17 @@ OPDEF(JSOP_INSTANCEOF,112,js_instanceof_str,js_instanceof_str,1,2,1,6,JOF_BYTE)
OPDEF(JSOP_DEBUGGER, 113,"debugger", NULL, 1, 0, 0, 0, JOF_BYTE)
/* gosub/retsub for finally handling */
OPDEF(JSOP_GOSUB, 114,"gosub", NULL, 3, 0, 1, 0, JOF_JUMP)
OPDEF(JSOP_GOSUB, 114,"gosub", NULL, 3, 0, 1, 0, JOF_JUMP)
OPDEF(JSOP_RETSUB, 115,"retsub", NULL, 1, 1, 0, 0, JOF_BYTE)
/* more exception handling ops */
OPDEF(JSOP_EXCEPTION, 116,"exception", NULL, 1, 0, 1, 0, JOF_BYTE)
OPDEF(JSOP_SETSP, 117,"setsp", NULL, 3, 0, 0, 0, JOF_UINT16)
OPDEF(JSOP_SETSP, 117,"setsp", NULL, 3, 0, 0, 0, JOF_UINT16)
/*
* ECMA-compliant switch statement ops.
* "switch" is essentially "nop" and "default" is essentially "pop" + "goto".
*/
OPDEF(JSOP_CONDSWITCH,118,"switch", NULL, -1, 0, 0, 0, JOF_LOOKUPSWITCH)
OPDEF(JSOP_CONDSWITCH,118,"switch", NULL, -1, 0, 0, 0, JOF_LOOKUPSWITCH)
OPDEF(JSOP_CASE, 119,"case", NULL, 3, 1, 0, 0, JOF_JUMP)
OPDEF(JSOP_DEFAULT, 120,"default", NULL, 3, 1, 0, 0, JOF_JUMP)

View File

@ -296,6 +296,7 @@ typedef struct JSErrorFormatString {
} JSErrorFormatString;
typedef const JSErrorFormatString *
(* CRT_CALL JSErrorCallBack)(void *userRef, const char *locale, const uintN errorNumber);
(* CRT_CALL JSErrorCallback)(void *userRef, const char *locale,
const uintN errorNumber);
#endif /* jspubtd_h___ */