Saturday, May 10, 2008

javascript compiling error not reported

in javascript embedding (spidermonkey of Mozilla), there is a way to register an error call back to print out script errors.

Yesterday while I was testing out the new AVBot, a compile time syntex error was not caught. A close inspection revels that in error report, if exception is enabled, an exception was raised instead of calling the error reporter callback.

Since I can't find any code to print out the exceptions on google, I publish my version below (the part getting stack is from mozilla's js code):
jsval val;
if (JS_GetPendingException(cx,&val) && JSVAL_IS_OBJECT(val)) {
JSObject* obj=JSVAL_TO_OBJECT(val);
jsval stack;
string strstack = "";
if (JS_GetProperty(cx, obj, "stack", &stack)) {
strstack = jsValToString(stack);
jsval msg;
string strmsg = "";
if (JS_GetProperty(cx, obj, "message", &msg)) {
strmsg = jsValToString(msg);
string strfileName;
if (JS_GetProperty(cx, obj, "fileName", &msg)) {
strfileName = jsValToString(msg);
string strlineNumber;
if (JS_GetProperty(cx, obj, "lineNumber", &msg)) {
strlineNumber = jsValToString(msg);

So there you go, you have the line number, fileName and stack :-)

No comments: