1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/modules/debug.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,80 @@ 1.4 +/* vim:set ts=2 sw=2 sts=2 ci et: */ 1.5 +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.6 + 1.7 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.8 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.9 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.10 + 1.11 +// This file contains functions that are useful for debugging purposes from 1.12 +// within JavaScript code. 1.13 + 1.14 +this.EXPORTED_SYMBOLS = ["NS_ASSERT"]; 1.15 + 1.16 +var gTraceOnAssert = true; 1.17 + 1.18 +/** 1.19 + * This function provides a simple assertion function for JavaScript. 1.20 + * If the condition is true, this function will do nothing. If the 1.21 + * condition is false, then the message will be printed to the console 1.22 + * and an alert will appear showing a stack trace, so that the (alpha 1.23 + * or nightly) user can file a bug containing it. For future enhancements, 1.24 + * see bugs 330077 and 330078. 1.25 + * 1.26 + * To suppress the dialogs, you can run with the environment variable 1.27 + * XUL_ASSERT_PROMPT set to 0 (if unset, this defaults to 1). 1.28 + * 1.29 + * @param condition represents the condition that we're asserting to be 1.30 + * true when we call this function--should be 1.31 + * something that can be evaluated as a boolean. 1.32 + * @param message a string to be displayed upon failure of the assertion 1.33 + */ 1.34 + 1.35 +this.NS_ASSERT = function NS_ASSERT(condition, message) { 1.36 + if (condition) 1.37 + return; 1.38 + 1.39 + var releaseBuild = true; 1.40 + var defB = Components.classes["@mozilla.org/preferences-service;1"] 1.41 + .getService(Components.interfaces.nsIPrefService) 1.42 + .getDefaultBranch(null); 1.43 + try { 1.44 + switch (defB.getCharPref("app.update.channel")) { 1.45 + case "nightly": 1.46 + case "aurora": 1.47 + case "alpha": 1.48 + case "beta": 1.49 + case "default": 1.50 + releaseBuild = false; 1.51 + } 1.52 + } catch(ex) {} 1.53 + 1.54 + var caller = arguments.callee.caller; 1.55 + var assertionText = "ASSERT: " + message + "\n"; 1.56 + 1.57 + // Report the error to the console 1.58 + Components.utils.reportError(assertionText); 1.59 + 1.60 + if (releaseBuild) { 1.61 + return; 1.62 + } 1.63 + 1.64 + // dump the stack to stdout too in non-release builds 1.65 + var stackText = ""; 1.66 + if (gTraceOnAssert) { 1.67 + stackText = "Stack Trace: \n"; 1.68 + var count = 0; 1.69 + while (caller) { 1.70 + stackText += count++ + ":" + caller.name + "("; 1.71 + for (var i = 0; i < caller.arguments.length; ++i) { 1.72 + var arg = caller.arguments[i]; 1.73 + stackText += arg; 1.74 + if (i < caller.arguments.length - 1) 1.75 + stackText += ","; 1.76 + } 1.77 + stackText += ")\n"; 1.78 + caller = caller.arguments.callee.caller; 1.79 + } 1.80 + } 1.81 + 1.82 + dump(assertionText + stackText); 1.83 +}