1 /*
2 * The Apache Software License, Version 1.1
3 *
4 * Copyright (c) 2002 The Apache Software Foundation. All rights
5 * reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * 3. The end-user documentation included with the redistribution, if
20 * any, must include the following acknowlegement:
21 * "This product includes software developed by the
22 * Apache Software Foundation (http://www.apache.org/)."
23 * Alternately, this acknowlegement may appear in the software itself,
24 * if and wherever such third-party acknowlegements normally appear.
25 *
26 * 4. The names "Apache BSF", "Apache", and "Apache Software Foundation"
27 * must not be used to endorse or promote products derived from
28 * this software without prior written permission. For written
29 * permission, please contact apache@apache.org.
30 *
31 * 5. Products derived from this software may not be called "Apache"
32 * nor may "Apache" appear in their names without prior written
33 * permission of the Apache Group.
34 *
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 * SUCH DAMAGE.
47 * ====================================================================
48 *
49 * This software consists of voluntary contributions made by many individuals
50 * on behalf of the Apache Software Foundation and was originally created by
51 * Sanjiva Weerawarana and others at International Business Machines
52 * Corporation. For more information on the Apache Software Foundation,
53 * please see <http://www.apache.org/>.
54 */
55
56 package org.apache.bsf.engines.javascript;
57
58 import org.apache.bsf;
59 import org.apache.bsf.debug.jsdi;
60 import org.apache.bsf.debug.util.DebugLog;
61 import org.mozilla.javascript;
62 import org.mozilla.javascript.debug;
63
64 import java.util;
65
66 /**
67 * A compilation unit is a Rhino concept.
68 * When a piece of script is provided for eval or
69 * execute to a Rhino engine, it is compiled down
70 * to either JavaScript or Java bytecode.
71 * In debug mode, only the compilation down to JavaScript
72 * bytecode is supported.
73 * During the compilation process, the original piece
74 * of script is sliced into compilation units.
75 * For instance, the script text may contain a function
76 * declaration and an expression to eval. The compilation
77 * will result in two compilation units: the function and
78 * the expression. Each compilation unit will correspond
79 * to a range of the lines of the original script compiled.
80 * All line numbers are global to the document the compiled
81 * script is part of.
82 * It is on compilation units that breakpoints can be set
83 * or removed, more exactly on the DebuggableScript attached
84 * to them. See Rhino for more details.
85 *
86 * @author: Olivier Gruber.
87 */
88 public class CompilationUnit {
89
90 FnOrScript m_fnOrScript;
91 int m_firstLine;
92 int m_lineCount;
93 String m_fnName;
94 DebuggableScript m_dbgScript;
95 int m_validBrkptLines[];
96
97 /**
98 * CompilationUnit constructor comment.
99 */
100 public CompilationUnit(FnOrScript fnOrScript, DebuggableScript dbgScript) {
101
102 int lastLine, lineno;
103
104 m_fnOrScript = fnOrScript;
105 m_dbgScript = dbgScript;
106
107 try {
108 m_validBrkptLines = dbgScript.getLineNumbers();
109 m_firstLine = 99999;
110 lastLine = 0;
111 for (int l = 0; l < m_validBrkptLines.length; l++) {
112 lineno = m_validBrkptLines[l];
113 if (m_firstLine > lineno)
114 m_firstLine = lineno;
115 if (lastLine < lineno)
116 lastLine = lineno;
117 }
118 m_lineCount = lastLine - m_firstLine + 1;
119 } catch (Throwable t) {
120 DebugLog.stderrPrintln("\nWarning: can't get valid line numbers for breakpoints.", DebugLog.BSF_LOG_L2);
121 m_validBrkptLines = null;
122 }
123
124 Scriptable scriptable = dbgScript.getScriptable();
125 if (scriptable instanceof NativeFunction) {
126 NativeFunction f = (NativeFunction) scriptable;
127 String name = f.getFunctionName();
128 if (name.length() > 0 && !name.equals("anonymous")) {
129 m_fnName = name;
130 }
131 }
132 }
133 //----------------------------------------------------------
134 boolean contains(int lineno) {
135 return (m_firstLine <= lineno && lineno < m_firstLine + m_lineCount);
136 }
137 /**
138 * Returns true if the compilation unit contains
139 * the breakpoint.
140 * Notice only breakpoint defined at a line number
141 * are supported here.
142 */
143 boolean contains(BreakPoint bp) {
144 try {
145 return contains(bp.getLineNo());
146 } catch (BSFException ex) {
147 return false;
148 }
149 }
150 /**
151 * Propagates (i.e. set) this breakpoint to the underlying Rhino
152 * engine if Rhino has provided us with the valid lines
153 * information. Otherwise, Rhino crashes with a NullPointerException.
154 */
155 void propagate(int lineno) {
156 if (m_validBrkptLines != null) {
157 m_dbgScript.placeBreakpoint(lineno);
158 }
159 }
160 /**
161 * Unpropagates (i.e. unset) this breakpoint to the underlying Rhino
162 * engine if Rhino has provided us with the valid lines
163 * information. Otherwise, Rhino crashes with a NullPointerException.
164 */
165 void unpropagate(int lineno) {
166 if (m_validBrkptLines != null) {
167 m_dbgScript.removeBreakpoint(lineno);
168 }
169 }
170 }