1 55 56 package org.apache.bsf.engines.javascript; 57 58 63 64 import java.util.Hashtable ; 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 ; 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 getSourceName() { 130 DebugFrame frame = m_engine.getFrame(0); 131 132 return frame.getSourceName(); 133 } 134 135 136 public JsContextStub hitBreakpoint() throws RemoteException { 140 cancelStepping(); 141 updateStack(); 142 return m_frames[0]; 143 } 144 145 146 public JsContextStub exceptionThrown() throws RemoteException { 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 { 191 cancelStepping(); 192 updateStack(); 193 return m_frames[0]; 194 } 195 196 public JsContextStub stepping() { 197 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 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 if (frameCount <= m_stepDepth) { 229 updateStack(); 230 cancelStepping(); 231 return m_frames[0]; 232 } 233 break; 234 case STEP_OUT : 235 if (frameCount < m_stepDepth) { 237 updateStack(); 238 cancelStepping(); 239 return m_frames[0]; 240 } 241 break; 242 default : 243 throw new Error ("Unknown command."); 244 } 245 } catch (Throwable 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 { 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 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 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 |