updated Makefile.common
[org.ibex.core.git] / src / org / ibex / js / JSExn.java
index 8d1a758..2afcef1 100644 (file)
@@ -2,30 +2,51 @@
 package org.ibex.js; 
 
 import org.ibex.util.*; 
+import java.io.*;
 
 /** An exception which can be thrown and caught by JavaScript code */
 public class JSExn extends Exception { 
     private Vec backtrace = new Vec();
-    private Object js = null; 
-    public JSExn(Object js) { this.js = js; } 
-    public String toString() { return "JSExn: " + js; }
+    private JS js; 
+    public JSExn(String s) { this(JS.S(s)); }
+    public JSExn(JS js) { this(js,null); }
+    public JSExn(JS js, Interpreter cx) { this.js = js; fill(cx); }
+    
+    private void fill(Interpreter cx) {
+        if(cx == null) cx = Interpreter.current();
+        if(cx == null) return;
+        addBacktrace(cx.f.sourceName + ":" + cx.f.line[cx.pc]);
+        cx.stack.backtrace(this);
+    }
+    public void printStackTrace() { printStackTrace(System.err); }
+    public void printStackTrace(PrintWriter pw) {
+        for(int i=0; i<backtrace.size(); i++) pw.println("    at " + (String) backtrace.elementAt(i));
+        super.printStackTrace(pw);
+    }
+    public void printStackTrace(PrintStream ps) {
+        for(int i=0; i<backtrace.size(); i++) ps.println("    at " + (String) backtrace.elementAt(i));
+        super.printStackTrace(ps);
+    }
+    public String toString() { return "JSExn: " + JS.debugToString(js); }
     public String getMessage() { return toString(); }
-    public Object getObject() { return js; } 
-    public void addBacktrace(String sourceName, int lineNo) { backtrace.addElement(sourceName + ":" + lineNo); }
-    public String backtrace() {
-        StringBuffer sb = new StringBuffer(1024);
-        for(int i=0;i<backtrace.size();i++)
-            sb.append("    at " + (String) backtrace.elementAt(i) + "\n");
-        return sb.toString();
+    public JS getObject() { return js; } 
+    
+    void addBacktrace(String line) { backtrace.addElement(line); }
+    
+    public static class Wrapper extends RuntimeException {
+        public final JSExn e;
+        public Wrapper(JSExn e) { this.e = e; }
+        public JSExn refill() {
+            e.addBacktrace("[foreign code]");
+            e.fill(null);
+            return e;
+        }
+    }
+    
+    public static class IO extends JSExn {
+        public IO(java.io.IOException ioe) {
+            super("ibex.io: " + ioe.toString());
+            JS.warn(ioe);
+        }
     }
 } 
-
-/** should only be used for failed coercions */
-class JSRuntimeExn extends RuntimeException {
-    private Object js = null; 
-    public JSRuntimeExn(Object js) { this.js = js; } 
-    public String toString() { return "JSRuntimeExn: " + js; }
-    public String getMessage() { return toString(); }
-    public Object getObject() { return js; } 
-}
-