Debian Codegraph @

rhino/src/org/mozilla/javascript/ScriptStackElement.java

raw symbols SCIP
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 package org.mozilla.javascript;
6
7 import java.io.Serializable;
8
9 /**
10 * This class represents an element on the script execution stack.
11 * @see RhinoException#getScriptStack()
12 * @author Hannes Wallnoefer
13 * @since 1.7R3
14 */
15 public final class ScriptStackElement implements Serializable {
16
17 static final long serialVersionUID = -6416688260860477449L;
18
19 public final String fileName;
20 public final String functionName;
21 public final int lineNumber;
22
23 public ScriptStackElement(String fileName, String functionName, int lineNumber) {
24 this.fileName = fileName;
25 this.functionName = functionName;
26 this.lineNumber = lineNumber;
27 }
28
29 @Override
30 public String toString() {
31 StringBuilder sb = new StringBuilder();
32 renderMozillaStyle(sb);
33 return sb.toString();
34 }
35
36 /**
37 * Render stack element in Java-inspired style:
38 * <code> at fileName:lineNumber (functionName)</code>
39 *
40 * @param sb the StringBuilder to append to
41 */
42 public void renderJavaStyle(StringBuilder sb) {
43 sb.append("\tat ").append(fileName);
44 if (lineNumber > -1) {
45 sb.append(':').append(lineNumber);
46 }
47 if (functionName != null) {
48 sb.append(" (").append(functionName).append(')');
49 }
50 }
51
52 /**
53 * Render stack element in Mozilla/Firefox style:
54 * <code>functionName()@fileName:lineNumber</code>
55 *
56 * @param sb the StringBuilder to append to
57 */
58 public void renderMozillaStyle(StringBuilder sb) {
59 if (functionName != null) {
60 sb.append(functionName).append("()");
61 }
62 sb.append('@').append(fileName);
63 if (lineNumber > -1) {
64 sb.append(':').append(lineNumber);
65 }
66 }
67
68 /**
69 * Render stack element in V8 style:
70 * <code> at functionName (fileName:lineNumber:columnNumber)</code>
71 * or:
72 * <code> at fileName:lineNumber:columnNumber</code>
73 *
74 * @param sb the StringBuilder to append to
75 */
76 public void renderV8Style(StringBuilder sb) {
77 sb.append(" at ");
78
79 if ((functionName == null) || "anonymous".equals(functionName) || "undefined"
80 .equals(functionName)) {
81 // Anonymous functions in V8 don't have names in the stack trace
82 appendV8Location(sb);
83
84 } else {
85 sb.append(functionName).append(" (");
86 appendV8Location(sb);
87 sb.append(')');
88 }
89 }
90
91 private void appendV8Location(StringBuilder sb) {
92 sb.append(fileName).append(':');
93 sb.append(lineNumber > -1 ? lineNumber : 0).append(":0");
94 }
95 }