Bug 889911 - Introduce xpc::SystemErrorReporter, roughly based on mozJSComponentLoader's error reporter. r=mrbkap
☠☠ backed out by b3d0c2498b42 ☠ ☠
authorBobby Holley <bobbyholley@gmail.com>
Fri, 26 Jul 2013 18:56:51 -0700
changeset 148080 a6b7104a9a82d7d9ad7a88a4302c8384645b92c5
parent 148079 d1e156bb38059cc3291713c4badfd7ebe3c9e26a
child 148081 b156c252aac0c9f39564700dd33d2c9b118df898
push id2697
push userbbajaj@mozilla.com
push dateMon, 05 Aug 2013 18:49:53 +0000
treeherdermozilla-beta@dfec938c7b63 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmrbkap
bugs889911
milestone24.0a2
Bug 889911 - Introduce xpc::SystemErrorReporter, roughly based on mozJSComponentLoader's error reporter. r=mrbkap
js/xpconnect/src/nsXPConnect.cpp
js/xpconnect/src/xpcpublic.h
--- a/js/xpconnect/src/nsXPConnect.cpp
+++ b/js/xpconnect/src/nsXPConnect.cpp
@@ -216,18 +216,80 @@ JSBool
 nsXPConnect::IsISupportsDescendant(nsIInterfaceInfo* info)
 {
     bool found = false;
     if (info)
         info->HasAncestor(&NS_GET_IID(nsISupports), &found);
     return found;
 }
 
+void
+xpc::SystemErrorReporter(JSContext *cx, const char *message, JSErrorReport *rep)
+{
+    // It would be nice to assert !JS_DescribeScriptedCaller here, to be sure
+    // that there isn't any script running that could catch the exception. But
+    // the JS engine invokes the error reporter directly if someone reports an
+    // ErrorReport that it doesn't know how to turn into an exception. Arguably
+    // it should just learn how to throw everything. But either way, if the
+    // exception is ending here, it's not going to get propagated to a caller,
+    // so it's up to us to make it known.
+
+    nsresult rv;
+
+    /* Use the console service to register the error. */
+    nsCOMPtr<nsIConsoleService> consoleService =
+        do_GetService(NS_CONSOLESERVICE_CONTRACTID);
+
+    /*
+     * Make an nsIScriptError, populate it with information from this
+     * error, then log it with the console service.
+     */
+    nsCOMPtr<nsIScriptError> errorObject =
+        do_CreateInstance(NS_SCRIPTERROR_CONTRACTID);
+
+    if (consoleService && errorObject) {
+        uint32_t column = rep->uctokenptr - rep->uclinebuf;
+
+        const PRUnichar* ucmessage =
+            static_cast<const PRUnichar*>(rep->ucmessage);
+        const PRUnichar* uclinebuf =
+            static_cast<const PRUnichar*>(rep->uclinebuf);
+
+        rv = errorObject->Init(
+              ucmessage ? nsDependentString(ucmessage) : EmptyString(),
+              NS_ConvertASCIItoUTF16(rep->filename),
+              uclinebuf ? nsDependentString(uclinebuf) : EmptyString(),
+              rep->lineno, column, rep->flags,
+              "system javascript");
+        if (NS_SUCCEEDED(rv))
+            consoleService->LogMessage(errorObject);
+    }
+
+    /* Log to stderr in debug builds. */
+#ifdef DEBUG
+    fprintf(stderr, "System JS : %s %s:%d\n"
+            "                     %s\n",
+            JSREPORT_IS_WARNING(rep->flags) ? "WARNING" : "ERROR",
+            rep->filename, rep->lineno,
+            message ? message : "<no message>");
+#endif
+
+}
+
+NS_EXPORT_(void)
+xpc::SystemErrorReporterExternal(JSContext *cx, const char *message,
+                                 JSErrorReport *rep)
+{
+    return SystemErrorReporter(cx, message, rep);
+}
+
+
 /***************************************************************************/
 
+
 nsresult
 nsXPConnect::GetInfoForIID(const nsIID * aIID, nsIInterfaceInfo** info)
 {
   return XPTInterfaceInfoManager::GetSingleton()->GetInfoForIID(aIID, info);
 }
 
 nsresult
 nsXPConnect::GetInfoForName(const char * name, nsIInterfaceInfo** info)
--- a/js/xpconnect/src/xpcpublic.h
+++ b/js/xpconnect/src/xpcpublic.h
@@ -417,16 +417,30 @@ JSObject *
 GetJunkScope();
 
 /**
  * Returns the native global of the junk scope. See comment of GetJunkScope
  * about the conditions of using it.
  */
 nsIGlobalObject *
 GetJunkScopeGlobal();
+
+// Error reporter used when there is no associated DOM window on to which to
+// report errors and warnings.
+void
+SystemErrorReporter(JSContext *cx, const char *message, JSErrorReport *rep);
+
+// We have a separate version that's exported with external linkage for use by
+// xpcshell, since external linkage on windows changes the signature to make it
+// incompatible with the JSErrorReporter type, causing JS_SetErrorReporter calls
+// to fail to compile.
+NS_EXPORT_(void)
+SystemErrorReporterExternal(JSContext *cx, const char *message,
+                            JSErrorReport *rep);
+
 } // namespace xpc
 
 namespace mozilla {
 namespace dom {
 
 extern int HandlerFamily;
 inline void* ProxyFamily() { return &HandlerFamily; }