|
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 -*- */ |
|
3 |
|
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/. */ |
|
7 |
|
8 // This file contains functions that are useful for debugging purposes from |
|
9 // within JavaScript code. |
|
10 |
|
11 this.EXPORTED_SYMBOLS = ["NS_ASSERT"]; |
|
12 |
|
13 var gTraceOnAssert = true; |
|
14 |
|
15 /** |
|
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. |
|
22 * |
|
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). |
|
25 * |
|
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 |
|
30 */ |
|
31 |
|
32 this.NS_ASSERT = function NS_ASSERT(condition, message) { |
|
33 if (condition) |
|
34 return; |
|
35 |
|
36 var releaseBuild = true; |
|
37 var defB = Components.classes["@mozilla.org/preferences-service;1"] |
|
38 .getService(Components.interfaces.nsIPrefService) |
|
39 .getDefaultBranch(null); |
|
40 try { |
|
41 switch (defB.getCharPref("app.update.channel")) { |
|
42 case "nightly": |
|
43 case "aurora": |
|
44 case "alpha": |
|
45 case "beta": |
|
46 case "default": |
|
47 releaseBuild = false; |
|
48 } |
|
49 } catch(ex) {} |
|
50 |
|
51 var caller = arguments.callee.caller; |
|
52 var assertionText = "ASSERT: " + message + "\n"; |
|
53 |
|
54 // Report the error to the console |
|
55 Components.utils.reportError(assertionText); |
|
56 |
|
57 if (releaseBuild) { |
|
58 return; |
|
59 } |
|
60 |
|
61 // dump the stack to stdout too in non-release builds |
|
62 var stackText = ""; |
|
63 if (gTraceOnAssert) { |
|
64 stackText = "Stack Trace: \n"; |
|
65 var count = 0; |
|
66 while (caller) { |
|
67 stackText += count++ + ":" + caller.name + "("; |
|
68 for (var i = 0; i < caller.arguments.length; ++i) { |
|
69 var arg = caller.arguments[i]; |
|
70 stackText += arg; |
|
71 if (i < caller.arguments.length - 1) |
|
72 stackText += ","; |
|
73 } |
|
74 stackText += ")\n"; |
|
75 caller = caller.arguments.callee.caller; |
|
76 } |
|
77 } |
|
78 |
|
79 dump(assertionText + stackText); |
|
80 } |