KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > bsf > engines > javascript > RhinoContextProxy


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 /**
59  * Insert the type's description here.
60  * Creation date: (8/23/2001 4:09:36 PM)
61  * @author: Administrator
62  */

63
64 import java.util.Hashtable JavaDoc;
65
66 import org.apache.bsf.*;
67 import org.apache.bsf.debug.jsdi.*;
68 import org.mozilla.javascript.*;
69 import org.mozilla.javascript.debug.*;
70
71 import java.rmi.RemoteException JavaDoc;
72
73 public class RhinoContextProxy {
74
75     RhinoEngineDebugger m_reDbg;
76     Context m_context;
77     JsContextStub m_contextStub;
78
79     DebuggableEngine m_engine;
80
81     boolean m_atBreakpoint;
82     int m_frameCount;
83     JsContextStub m_frames[];
84
85     private static final int NO_STEP = 0, STEP_IN = 1, STEP_OVER = 2,
86         STEP_OUT = 3, STOP_ENGINE = 4, RUNNING = 5;
87
88     private int m_stepCmd, m_stepDepth;
89
90     RhinoContextProxy(RhinoEngineDebugger reDbg, Context cx) {
91         m_reDbg = reDbg;
92         m_context = cx;
93         m_engine = cx.getDebuggableEngine();
94     }
95
96     public void cancelStepping() {
97         m_stepCmd = NO_STEP;
98         m_stepDepth = -1;
99         m_engine.setBreakNextLine(false);
100     }
101
102     public JsContextStub getContext(int depth) {
103         return m_frames[depth];
104     }
105
106     public int getContextCount() {
107         return m_frameCount;
108     }
109
110     public JsContextStub getFrame(int no) {
111         if (no < 0 || no > m_frameCount)
112             return null;
113         if (no == m_frameCount)
114             return m_contextStub;
115         else
116             return m_frames[no];
117     }
118
119     public int getLineNumber() {
120         DebugFrame frame = m_engine.getFrame(0);
121
122         return frame.getLineNumber();
123     }
124
125     public RhinoEngineDebugger getRhinoEngineDebugger() {
126         return m_reDbg;
127     }
128
129     String JavaDoc getSourceName() {
130         DebugFrame frame = m_engine.getFrame(0);
131
132         return frame.getSourceName();
133     }
134
135
136     // We hit a known breakpoint.
137
// We need to update the stack.
138
// Also, cancel any pending stepping operation.
139
public JsContextStub hitBreakpoint() throws RemoteException JavaDoc {
140         cancelStepping();
141         updateStack();
142         return m_frames[0];
143     }
144
145
146     public JsContextStub exceptionThrown() throws RemoteException JavaDoc {
147         cancelStepping();
148         updateStack();
149         return m_frames[0];
150     }
151
152     public void resumed() {
153         JsContextStub stub;
154         DebugFrame frame;
155
156         m_atBreakpoint = false;
157
158         for (int f = 0; f < m_frameCount; f++) {
159             stub = m_frames[f];
160             stub.atBreakpoint(false);
161         }
162     }
163
164     public void run() {
165         m_engine.setBreakNextLine(false);
166         m_stepCmd = RUNNING;
167         m_stepDepth = -1;
168
169     }
170
171     public void stepIn() {
172         m_engine.setBreakNextLine(true);
173         m_stepCmd = STEP_IN;
174         m_stepDepth = m_frameCount;
175     }
176
177     public void stepOut() {
178         m_engine.setBreakNextLine(true);
179         m_stepCmd = STEP_OUT;
180         m_stepDepth = m_frameCount;
181
182     }
183
184     public void stepOver() {
185         m_engine.setBreakNextLine(true);
186         m_stepCmd = STEP_OVER;
187         m_stepDepth = m_frameCount;
188     }
189
190     public JsContextStub entry_exit_mode() throws RemoteException JavaDoc {
191         cancelStepping();
192         updateStack();
193         return m_frames[0];
194     }
195
196     public JsContextStub stepping() {
197         // Did we hit a known breakpoint?
198

199         int frameCount = m_engine.getFrameCount();
200
201         try {
202             switch (m_stepCmd) {
203             case NO_STEP :
204                 cancelStepping();
205                 break;
206             case STOP_ENGINE :
207                 updateStack();
208                 cancelStepping();
209                 return m_frames[0];
210             case STEP_IN :
211                 // OG if ((frameCount == m_stepDepth + 1) ||
212
// (frameCount == m_stepDepth)) {
213
// step if we are in the same frame (nothing to step in... :-)
214
// if we are in a called frame...
215
// but also if we stepped out of the current frame...
216
if ((frameCount >= m_stepDepth)
217                     || (frameCount < m_stepDepth)) {
218                     updateStack();
219                     cancelStepping();
220                     return m_frames[0];
221                 }
222                 break;
223             case STEP_OVER :
224                 // OG if (frameCount == m_stepDepth) {
225
// step if we are in the same frame or above...
226
// this basically avoids any children frame but
227
// covers the return of the current frame.
228
if (frameCount <= m_stepDepth) {
229                     updateStack();
230                     cancelStepping();
231                     return m_frames[0];
232                 }
233                 break;
234             case STEP_OUT :
235                 // OG if (frameCount == m_stepDepth - 1) {
236
if (frameCount < m_stepDepth) {
237                     updateStack();
238                     cancelStepping();
239                     return m_frames[0];
240                 }
241                 break;
242             default :
243                 throw new Error JavaDoc("Unknown command.");
244             }
245         } catch (Throwable JavaDoc t) {
246             t.printStackTrace();
247             cancelStepping();
248         }
249         return null;
250     }
251
252     public void stopEngine() {
253         m_engine.setBreakNextLine(true);
254         m_stepCmd = STOP_ENGINE;
255         m_stepDepth = -1;
256     }
257
258     public void updateStack() throws RemoteException JavaDoc {
259         JsContextStub frames[];
260         JsContextStub stub;
261         DebugFrame frame;
262         int nf, of, frameCount;
263
264         m_atBreakpoint = true;
265
266         frameCount = m_engine.getFrameCount();
267         frames = new JsContextStub[frameCount];
268
269         // scan the stacks from the outer frame down
270
// to the inner one of the shortest of the old
271
// and the new known stack.
272
// The goal is to recognize the DebugFrame objects
273
// that are the sames so that we can reuse the
274
// stubs for those.
275
// As soon as a DebugFrame object is found different,
276
// the rest of the stack is different, all the old
277
// stubs can be dropped and invalidated, new ones
278
// must be created.
279

280         for (nf = frameCount - 1, of = m_frameCount - 1;
281              nf >= 0 && of >= 0;
282              nf--, of--) {
283             frame = m_engine.getFrame(nf);
284             if (frame == m_frames[of].m_frame) {
285                 frames[nf] = m_frames[of];
286             } else
287                 break;
288         }
289         // now drop all old frames that diverged.
290
// Also invalidate the frame stubs so to
291
// tracked that they are no longer valid.
292
for (; of >= 0; of--) {
293             m_reDbg.dropStub(m_frames[of].m_frame);
294             m_frames[of].invalidate();
295         }
296         for (; nf >= 0; nf--) {
297             frame = m_engine.getFrame(nf);
298             frames[nf] = new JsContextStub(this, frame, nf);
299         }
300         m_frames = frames;
301         m_frameCount = frameCount;
302     }
303 }
304
Popular Tags