1 55 56 package org.apache.bsf.engines.javascript; 57 58 import java.rmi.RemoteException ; 59 import java.io.InputStream ; 60 import java.io.IOException ; 61 import java.lang.reflect.InvocationTargetException ; 62 import java.util.Vector ; 63 64 import org.mozilla.javascript.Script; 65 import org.mozilla.javascript.ClassDefinitionException; 66 import org.mozilla.javascript.Context; 67 import org.mozilla.javascript.EvaluatorException; 68 import org.mozilla.javascript.JavaScriptException; 69 import org.mozilla.javascript.PropertyException; 70 import org.mozilla.javascript.NativeJavaObject; 71 import org.mozilla.javascript.Scriptable; 72 import org.mozilla.javascript.ScriptableObject; 73 import org.mozilla.javascript.ScriptRuntime; 74 import org.mozilla.javascript.WrappedException; 75 import org.mozilla.javascript.Wrapper; 76 import org.mozilla.javascript.ImporterTopLevel; 77 78 import org.mozilla.javascript.debug.*; 79 80 import org.apache.bsf.*; 81 import org.apache.bsf.util.*; 82 83 95 public class JavaScriptEngine extends BSFEngineImpl { 96 100 private Scriptable global; 101 102 private RhinoEngineDebugger m_rhinoDbg; 103 104 public void disconnectedDebuggerNotify() { 105 m_rhinoDbg.disconnectedDebuggerNotify(); 106 } 107 108 BSFDebugManagerImpl getDebugManager() { 109 return dbgmgr; 110 } 111 112 public void placeBreakpointAtLine(int brkptid, String docname, int lineno) 113 throws BSFException { 114 m_rhinoDbg.placeBreakpointAtLine(brkptid, docname, lineno); 115 } 116 117 public void placeBreakpointAtOffset(int brkptid, String docname, 118 int offset) throws BSFException { 119 m_rhinoDbg.placeBreakpointAtOffset(brkptid, docname, offset); 120 } 121 122 public void removeBreakpoint(String docname, int brkptid) 123 throws BSFException { 124 m_rhinoDbg.removeBreakpoint(docname, brkptid); 125 } 126 127 public void setEntryExit(String docname, boolean on) 128 throws BSFException { 129 m_rhinoDbg.setEntryExit(docname, on); 130 } 131 132 140 public Object call(Object object, String method, Object [] args) 141 throws BSFException { 142 Object theReturnValue = null; 143 DebuggableEngine engine; 144 Context cx; 145 try { 146 147 cx = Context.enter(); 148 149 151 Object fun = global.get(method, global); 152 if (fun == Scriptable.NOT_FOUND) { 153 throw new JavaScriptException("function " + method + " not found."); 154 } 155 if (dbgmgr != null) { 156 cx.setGeneratingDebug(true); 159 cx.setGeneratingSource(true); 160 161 cx.setOptimizationLevel(-1); 162 163 engine = cx.getDebuggableEngine(); 164 engine.setDebugger(m_rhinoDbg); 165 166 theReturnValue = ScriptRuntime.call(cx, fun, global, args, 167 null); 168 169 } 170 else { 171 cx.setOptimizationLevel(-1); 172 173 cx.setGeneratingDebug(false); 174 cx.setGeneratingSource(false); 175 176 cx.setOptimizationLevel(0); 177 178 engine = cx.getDebuggableEngine(); 179 engine.setDebugger(null); 180 181 theReturnValue = ScriptRuntime.call(cx, fun, global, args, 182 null); 183 } 184 if (theReturnValue instanceof Wrapper) { 185 theReturnValue = ((Wrapper) theReturnValue).unwrap(); 186 } 187 } catch (Throwable t) { 188 handleError(t); 189 } finally { 190 Context.exit(); 191 } 192 return theReturnValue; 193 } 194 195 public void declareBean(BSFDeclaredBean bean) throws BSFException { 196 Scriptable wrapped = Context.toObject(bean.bean, global); 198 global.put(bean.name, global, wrapped); 199 } 200 201 205 public Object eval(String source, int lineNo, int columnNo, Object oscript) 206 throws BSFException { 207 208 String scriptText = oscript.toString(); 209 Object retval = null; 210 DocumentCell cell; 211 FnOrScript fnOrScript; 212 Script script; 213 DebuggableEngine engine; 214 Context cx; 215 216 try { 217 cx = Context.enter(); 218 219 cell = m_rhinoDbg.loadDocumentNotify(source); 220 fnOrScript = (FnOrScript) cell.registerFnOrScriptLines(scriptText, 221 lineNo, 222 columnNo); 223 224 m_rhinoDbg.setCompilingFnOrScript(fnOrScript); 225 226 if (dbgmgr != null) { 227 cx.setGeneratingDebug(true); 230 cx.setGeneratingSource(true); 231 232 cx.setOptimizationLevel(-1); 233 234 engine = cx.getDebuggableEngine(); 235 engine.setDebugger(m_rhinoDbg); 236 237 if (!engine.getBreakNextLine()) { 239 engine.setBreakNextLine(cell.getEntryExit()); 240 } 241 242 fnOrScript.compile(cx, global); 243 m_rhinoDbg.setCompilingFnOrScript(null); 244 script = fnOrScript.getScript(); 245 246 if (script != null) retval = script.exec(cx, global); 247 else retval = null; 248 } 249 else { 250 cx.setOptimizationLevel(-1); 251 252 cx.setGeneratingDebug(false); 253 cx.setGeneratingSource(false); 254 255 cx.setOptimizationLevel(0); 256 257 engine = cx.getDebuggableEngine(); 258 engine.setDebugger(null); 259 260 retval = cx.evaluateString(global, scriptText, 261 source, lineNo, 262 null); 263 } 264 265 if (retval instanceof NativeJavaObject) 266 retval = ((NativeJavaObject) retval).unwrap(); 267 268 } catch (Throwable t) { handleError(t); 270 } finally { 271 Context.exit(); 272 } 273 return retval; 274 } 275 276 public Object getSpecificDebuggingInterface() { 277 if (m_rhinoDbg != null) return m_rhinoDbg.getDebugInterface(); 278 return null; 279 } 280 281 private void handleError(Throwable t) throws BSFException { 282 if (t instanceof WrappedException) { 283 t = (Throwable ) ((WrappedException) t).unwrap(); 284 } 285 286 String message = null; 287 Throwable target = t; 288 289 if (t instanceof JavaScriptException) { 290 message = t.getLocalizedMessage(); 291 292 Object value = ((JavaScriptException) t).getValue(); 294 if (value instanceof Throwable ) { 295 target = (Throwable ) value; 298 } 299 } 300 else if (t instanceof EvaluatorException || 301 t instanceof SecurityException ) { 302 message = t.getLocalizedMessage(); 303 } 304 else if (t instanceof RuntimeException ) { 305 message = "Internal Error: " + t.toString(); 306 } 307 else if (t instanceof StackOverflowError ) { 308 message = "Stack Overflow"; 309 } 310 311 if (message == null) { 312 message = t.toString(); 313 } 314 315 320 if (t instanceof Error && !(t instanceof StackOverflowError )) { 321 throw (Error ) t; 326 } 327 else { 328 throw new BSFException(BSFException.REASON_OTHER_ERROR, 329 "JavaScript Error: " + message, 330 target); 331 } 332 } 333 334 338 public void initialize(BSFManager mgr, String lang, Vector declaredBeans) 339 throws BSFException { 340 try { 341 m_rhinoDbg = new RhinoEngineDebugger(this); 342 } catch (RemoteException re) { 343 m_rhinoDbg = null; 344 } 345 super.initialize(mgr, lang, declaredBeans); 346 347 try { 349 Context cx = Context.enter(); 350 global = new ImporterTopLevel(cx); 351 Scriptable bsf = cx.toObject(new BSFFunctions(mgr, this), global); 352 global.put("bsf", global, bsf); 353 354 int size = declaredBeans.size(); 355 for (int i = 0; i < size; i++) { 356 declareBean((BSFDeclaredBean) declaredBeans.elementAt(i)); 357 } 358 } catch (Throwable t) { 359 360 } finally { 361 Context.exit(); 362 } 363 } 364 365 public void undeclareBean(BSFDeclaredBean bean) throws BSFException { 366 global.delete(bean.name); 367 } 368 } 369 | Popular Tags |