Blame toolkit/modules/debug.js

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