michael@0: /* vim:set ts=2 sw=2 sts=2 ci et: */ michael@0: /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: // This file contains functions that are useful for debugging purposes from michael@0: // within JavaScript code. michael@0: michael@0: this.EXPORTED_SYMBOLS = ["NS_ASSERT"]; michael@0: michael@0: var gTraceOnAssert = true; michael@0: michael@0: /** michael@0: * This function provides a simple assertion function for JavaScript. michael@0: * If the condition is true, this function will do nothing. If the michael@0: * condition is false, then the message will be printed to the console michael@0: * and an alert will appear showing a stack trace, so that the (alpha michael@0: * or nightly) user can file a bug containing it. For future enhancements, michael@0: * see bugs 330077 and 330078. michael@0: * michael@0: * To suppress the dialogs, you can run with the environment variable michael@0: * XUL_ASSERT_PROMPT set to 0 (if unset, this defaults to 1). michael@0: * michael@0: * @param condition represents the condition that we're asserting to be michael@0: * true when we call this function--should be michael@0: * something that can be evaluated as a boolean. michael@0: * @param message a string to be displayed upon failure of the assertion michael@0: */ michael@0: michael@0: this.NS_ASSERT = function NS_ASSERT(condition, message) { michael@0: if (condition) michael@0: return; michael@0: michael@0: var releaseBuild = true; michael@0: var defB = Components.classes["@mozilla.org/preferences-service;1"] michael@0: .getService(Components.interfaces.nsIPrefService) michael@0: .getDefaultBranch(null); michael@0: try { michael@0: switch (defB.getCharPref("app.update.channel")) { michael@0: case "nightly": michael@0: case "aurora": michael@0: case "alpha": michael@0: case "beta": michael@0: case "default": michael@0: releaseBuild = false; michael@0: } michael@0: } catch(ex) {} michael@0: michael@0: var caller = arguments.callee.caller; michael@0: var assertionText = "ASSERT: " + message + "\n"; michael@0: michael@0: // Report the error to the console michael@0: Components.utils.reportError(assertionText); michael@0: michael@0: if (releaseBuild) { michael@0: return; michael@0: } michael@0: michael@0: // dump the stack to stdout too in non-release builds michael@0: var stackText = ""; michael@0: if (gTraceOnAssert) { michael@0: stackText = "Stack Trace: \n"; michael@0: var count = 0; michael@0: while (caller) { michael@0: stackText += count++ + ":" + caller.name + "("; michael@0: for (var i = 0; i < caller.arguments.length; ++i) { michael@0: var arg = caller.arguments[i]; michael@0: stackText += arg; michael@0: if (i < caller.arguments.length - 1) michael@0: stackText += ","; michael@0: } michael@0: stackText += ")\n"; michael@0: caller = caller.arguments.callee.caller; michael@0: } michael@0: } michael@0: michael@0: dump(assertionText + stackText); michael@0: }