diff spidermonkey-playground.cpp @ 8:e14f496e6e08

added a handleError() function to the TCB, which is called whenever an unhandled exception occurs. Also fixed a bug in the setting of lastException.
author Atul Varma <varmaa@toolness.com>
date Fri, 19 Jun 2009 12:22:52 -0700
parents 500e267ed094
children fcefc3e5a9df
line wrap: on
line diff
--- a/spidermonkey-playground.cpp	Fri Jun 19 09:34:09 2009 -0700
+++ b/spidermonkey-playground.cpp	Fri Jun 19 12:22:52 2009 -0700
@@ -15,16 +15,6 @@
   JSCLASS_NO_OPTIONAL_MEMBERS
 };
 
-/* The error reporter callback. */
-static void reportError(JSContext *cx, const char *message,
-                        JSErrorReport *report)
-{
-  fprintf(stderr, "%s:%u:%s\n",
-          report->filename ? report->filename : "<no filename>",
-          (unsigned int) report->lineno,
-          message);
-}
-
 static JSBool print(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
                     jsval *rval)
 {
@@ -185,9 +175,14 @@
     lastExceptionTraceback = JSVAL_NULL;
   }
 
+  jsval exception = *rval;
+  if (JS_IsExceptionPending(cx))
+    if (!JS_GetPendingException(cx, &exception))
+      printf("Getting exception failed.\n");
+
   if (!JS_SetProperty(cx, tcb_global, "lastExceptionTraceback",
                       &lastExceptionTraceback) ||
-      !JS_SetProperty(cx, tcb_global, "lastException", rval))
+      !JS_SetProperty(cx, tcb_global, "lastException", &exception))
     printf("Setting of exception info failed.");
 
   return JSTRAP_CONTINUE;
@@ -219,7 +214,6 @@
     return 1;
   JS_SetOptions(tcb_cx, JSOPTION_VAROBJFIX);
   JS_SetVersion(tcb_cx, JSVERSION_LATEST);
-  JS_SetErrorReporter(tcb_cx, reportError);
 
   /* Create the TCB's global object. */
   tcb_global = JS_NewObject(tcb_cx, &global_class, NULL, NULL);
@@ -254,7 +248,12 @@
   if (!JS_EvaluateScript(tcb_cx, tcb_global, source,
                          strlen(source), TCB_FILENAME, 1,
                          &rval)) {
-    printf("An error occurred.\n");
+    jsval handleError;
+    if (JS_GetProperty(tcb_cx, tcb_global, "handleError", &handleError) &&
+        JSVAL_IS_OBJECT(handleError) &&
+        JS_ObjectIsFunction(tcb_cx, JSVAL_TO_OBJECT(handleError))) {
+      JS_CallFunctionValue(tcb_cx, tcb_global, handleError, 0, NULL, &rval);
+    }
     return 1;
   }