Bug 878319: Move makeInfallible into its own JSM. r=dcamp
authorJim Blandy <jimb@mozilla.com>
Wed, 05 Jun 2013 09:30:54 -0700
changeset 134121 24e9d3a53f8b761d188c7b8077015af8dc991c9d
parent 134120 4f4a4ae3629dbfc4341b94fcc36c2ead31120d5c
child 134122 48afaae197ab67157194d25dcf0ffe51fb093d24
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersdcamp
bugs878319
milestone24.0a1
Bug 878319: Move makeInfallible into its own JSM. r=dcamp
toolkit/devtools/DevToolsUtils.js
toolkit/devtools/DevToolsUtils.jsm
toolkit/devtools/Makefile.in
toolkit/devtools/client/dbg-client.jsm
toolkit/devtools/server/main.js
toolkit/devtools/server/tests/unit/test_dbgsocket.js
toolkit/devtools/server/transport.js
new file mode 100644
--- /dev/null
+++ b/toolkit/devtools/DevToolsUtils.js
@@ -0,0 +1,70 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+/* General utilities used throughout devtools. */
+
+/* Turn the error e into a string, without fail. */
+function safeErrorString(aError) {
+  try {
+    var s = aError.toString();
+    if (typeof s === "string")
+      return s;
+  } catch (ee) { }
+
+  return "<failed trying to find error description>";
+}
+
+/**
+ * Report that |aWho| threw an exception, |aException|.
+ */
+function reportException(aWho, aException) {
+  let msg = aWho + " threw an exception: " + safeErrorString(aException);
+  if (aException.stack) {
+    msg += "\nCall stack:\n" + aException.stack;
+  }
+
+  dump(msg + "\n");
+
+  if (Components.utils.reportError) {
+    /*
+     * Note that the xpcshell test harness registers an observer for
+     * console messages, so when we're running tests, this will cause
+     * the test to quit.
+     */
+    Components.utils.reportError(msg);
+  }
+}
+
+/**
+ * Given a handler function that may throw, return an infallible handler
+ * function that calls the fallible handler, and logs any exceptions it
+ * throws.
+ *
+ * @param aHandler function
+ *      A handler function, which may throw.
+ * @param aName string
+ *      A name for aHandler, for use in error messages. If omitted, we use
+ *      aHandler.name.
+ *
+ * (SpiderMonkey does generate good names for anonymous functions, but we
+ * don't have a way to get at them from JavaScript at the moment.)
+ */
+function makeInfallible(aHandler, aName) {
+  if (!aName)
+    aName = aHandler.name;
+
+  return function (/* arguments */) {
+    try {
+      return aHandler.apply(this, arguments);
+    } catch (ex) {
+      let who = "Handler function";
+      if (aName) {
+        who += " " + aName;
+      }
+      reportException(who, ex);
+    }
+  }
+}
new file mode 100644
--- /dev/null
+++ b/toolkit/devtools/DevToolsUtils.jsm
@@ -0,0 +1,25 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+/*
+ * General utilities used throughout devtools.
+ *
+ * To support chrome debugging, the debugger server needs to have all its
+ * code in one global, so it must use loadSubScript directly. Everyone else,
+ * though, prefers a JSM.
+ */
+
+this.EXPORTED_SYMBOLS = [ "DevToolsUtils" ];
+
+Components.classes["@mozilla.org/moz/jssubscript-loader;1"]
+  .getService(Components.interfaces.mozIJSSubScriptLoader)
+  .loadSubScript("resource://gre/modules/devtools/DevToolsUtils.js", this);
+
+this.DevToolsUtils = {
+  safeErrorString: safeErrorString,
+  reportException: reportException,
+  makeInfallible: makeInfallible
+};
--- a/toolkit/devtools/Makefile.in
+++ b/toolkit/devtools/Makefile.in
@@ -8,8 +8,9 @@ srcdir    = @srcdir@
 VPATH     = @srcdir@
 
 include $(topsrcdir)/config/config.mk
 
 include $(topsrcdir)/config/rules.mk
 
 libs::
 	$(NSINSTALL) $(IFLAGS1) $(srcdir)/*.jsm $(FINAL_TARGET)/modules/devtools
+	$(NSINSTALL) $(IFLAGS1) $(srcdir)/*.js $(FINAL_TARGET)/modules/devtools
--- a/toolkit/devtools/client/dbg-client.jsm
+++ b/toolkit/devtools/client/dbg-client.jsm
@@ -24,16 +24,19 @@ const { defer, resolve, reject } = Promi
 
 XPCOMUtils.defineLazyServiceGetter(this, "socketTransportService",
                                    "@mozilla.org/network/socket-transport-service;1",
                                    "nsISocketTransportService");
 
 XPCOMUtils.defineLazyModuleGetter(this, "WebConsoleClient",
                                   "resource://gre/modules/devtools/WebConsoleClient.jsm");
 
+Components.utils.import("resource://gre/modules/devtools/DevToolsUtils.jsm");
+var { makeInfallible } = DevToolsUtils;
+
 let wantLogging = Services.prefs.getBoolPref("devtools.debugger.log");
 
 function dumpn(str)
 {
   if (wantLogging) {
     dump("DBG-CLIENT: " + str + "\n");
   }
 }
--- a/toolkit/devtools/server/main.js
+++ b/toolkit/devtools/server/main.js
@@ -24,16 +24,18 @@ let wantLogging = Services.prefs.getBool
 Cu.import("resource://gre/modules/jsdebugger.jsm");
 addDebuggerToGlobal(this);
 
 Cu.import("resource://gre/modules/commonjs/sdk/core/promise.js");
 const { defer, resolve, reject, all } = Promise;
 
 Cu.import("resource://gre/modules/devtools/SourceMap.jsm");
 
+loadSubScript.call(this, "resource://gre/modules/devtools/DevToolsUtils.js");
+
 function dumpn(str) {
   if (wantLogging) {
     dump("DBG-SERVER: " + str + "\n");
   }
 }
 
 function dbg_assert(cond, e) {
   if (!cond) {
--- a/toolkit/devtools/server/tests/unit/test_dbgsocket.js
+++ b/toolkit/devtools/server/tests/unit/test_dbgsocket.js
@@ -1,13 +1,16 @@
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
 
 Cu.import("resource://gre/modules/devtools/dbg-server.jsm");
 Cu.import("resource://gre/modules/devtools/dbg-client.jsm");
+Cu.import("resource://gre/modules/devtools/DevToolsUtils.jsm");
+
+var { safeErrorString } = DevToolsUtils;
 
 let port = 2929;
 
 function run_test()
 {
   do_print("Starting test at " + new Date().toTimeString());
   initTestDebuggerServer();
 
--- a/toolkit/devtools/server/transport.js
+++ b/toolkit/devtools/server/transport.js
@@ -2,72 +2,16 @@
 /* vim: set ft=javascript ts=2 et sw=2 tw=80: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 "use strict";
 Components.utils.import("resource://gre/modules/NetUtil.jsm");
 
-/* Turn the error e into a string, without fail. */
-function safeErrorString(aError) {
-  try {
-    var s = aError.toString();
-    if (typeof s === "string")
-      return s;
-  } catch (ee) { }
-
-  return "<failed trying to find error description>";
-}
-
-/**
- * Given a handler function that may throw, return an infallible handler
- * function that calls the fallible handler, and logs any exceptions it
- * throws.
- *
- * @param aHandler function
- *      A handler function, which may throw.
- * @param aName string
- *      A name for aHandler, for use in error messages. If omitted, we use
- *      aHandler.name.
- *
- * (SpiderMonkey does generate good names for anonymous functions, but we
- * don't have a way to get at them from JavaScript at the moment.)
- */
-function makeInfallible(aHandler, aName) {
-  if (!aName)
-    aName = aHandler.name;
-
-  return function (/* arguments */) {
-    try {
-      return aHandler.apply(this, arguments);
-    } catch (ex) {
-      let msg = "Handler function ";
-      if (aName) {
-        msg += aName + " ";
-      }
-      msg += "threw an exception: " + safeErrorString(ex);
-      if (ex.stack) {
-        msg += "\nCall stack:\n" + ex.stack;
-      }
-
-      dump(msg + "\n");
-
-      if (Cu.reportError) {
-        /*
-         * Note that the xpcshell test harness registers an observer for
-         * console messages, so when we're running tests, this will cause
-         * the test to quit.
-         */
-        Cu.reportError(msg);
-      }
-    }
-  }
-}
-
 /**
  * An adapter that handles data transfers between the debugger client and
  * server. It can work with both nsIPipe and nsIServerSocket transports so
  * long as the properly created input and output streams are specified.
  *
  * @param aInput nsIInputStream
  *        The input stream.
  * @param aOutput nsIAsyncOutputStream