1 /* vim:set ts=2 sw=2 sts=2 ci et: */
2 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 // This file contains functions that are useful for debugging purposes from
9 // within JavaScript code.
11 this.EXPORTED_SYMBOLS = ["NS_ASSERT"];
13 var gTraceOnAssert = true;
16 * This function provides a simple assertion function for JavaScript.
17 * If the condition is true, this function will do nothing. If the
18 * condition is false, then the message will be printed to the console
19 * and an alert will appear showing a stack trace, so that the (alpha
20 * or nightly) user can file a bug containing it. For future enhancements,
21 * see bugs 330077 and 330078.
23 * To suppress the dialogs, you can run with the environment variable
24 * XUL_ASSERT_PROMPT set to 0 (if unset, this defaults to 1).
26 * @param condition represents the condition that we're asserting to be
27 * true when we call this function--should be
28 * something that can be evaluated as a boolean.
29 * @param message a string to be displayed upon failure of the assertion
32 this.NS_ASSERT = function NS_ASSERT(condition, message) {
36 var releaseBuild = true;
37 var defB = Components.classes["@mozilla.org/preferences-service;1"]
38 .getService(Components.interfaces.nsIPrefService)
39 .getDefaultBranch(null);
41 switch (defB.getCharPref("app.update.channel")) {
49 var caller = arguments.callee.caller;
50 var assertionText = "ASSERT: " + message + "\n";
53 // Just report the error to the console
54 Components.utils.reportError(assertionText);
58 // Otherwise, dump to stdout and launch an assertion failure dialog
63 stackText = "Stack Trace: \n";
66 stackText += count++ + ":" + caller.name + "(";
67 for (var i = 0; i < caller.arguments.length; ++i) {
68 var arg = caller.arguments[i];
70 if (i < caller.arguments.length - 1)
74 caller = caller.arguments.callee.caller;
78 var environment = Components.classes["@mozilla.org/process/environment;1"].
79 getService(Components.interfaces.nsIEnvironment);
80 if (environment.exists("XUL_ASSERT_PROMPT") &&
81 !parseInt(environment.get("XUL_ASSERT_PROMPT")))
87 var ps = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].
88 getService(Components.interfaces.nsIPromptService);
89 ps.alert(source, "Assertion Failed", assertionText + stackText);